package com.openhis.clinical.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.clinical.domain.ConditionDefinition;
import com.openhis.clinical.mapper.ConditionDefinitionMapper;
import com.openhis.clinical.service.IConditionDefinitionService;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.utils.HisQueryUtils;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 诊断定义管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Slf4j
@Service
@AllArgsConstructor
public class ConditionDefinitionServiceImpl extends ServiceImpl<ConditionDefinitionMapper, ConditionDefinition>
    implements IConditionDefinitionService {

    private final ConditionDefinitionMapper conditionDefinitionMapper;

    /**
     * 新增病种
     * 
     * @param conditionDefinition 病种目录实体
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addDisease(ConditionDefinition conditionDefinition) {
        // 根据病种编码判断病种是否存在
//        List<ConditionDefinition> conditionDefinitions =
//            conditionDefinitionMapper.selectList(new LambdaQueryWrapper<ConditionDefinition>()
//                .eq(ConditionDefinition::getConditionCode, conditionDefinition.getConditionCode()));
        // 根据医保编码判断病种是否存在
        List<ConditionDefinition> conditionDefinitions =
                conditionDefinitionMapper.selectList(new LambdaQueryWrapper<ConditionDefinition>()
                        .eq(ConditionDefinition::getYbNo,conditionDefinition.getYbNo()));
        if (conditionDefinitions.size() > 0) {
            return false;
        }
        // 新增病种
        int insert = conditionDefinitionMapper.insert(conditionDefinition);
        return insert == 1;
    }

    /**
     * 新增医保病种
     * 
     * @param conditionDefinition 病种目录实体
     * @return
     */
    @Override
    public boolean addYbDisease(ConditionDefinition conditionDefinition) {
        return false;
    }

    /**
     * 根据id查询诊断定义
     * 
     * @param collect id集合
     * @return 诊断定义集合
     */
    @Override
    public List<ConditionDefinition> getConditionDefinitionListByIds(List<Long> collect) {
        return baseMapper.selectList(new LambdaQueryWrapper<ConditionDefinition>()
            .in(ConditionDefinition::getId, collect).eq(ConditionDefinition::getDescription, DelFlag.NO.getCode()));
    }

    /**
     * 根据名称查询诊断定义
     *
     * @param searchKey 目标字符
     * @return 诊断定义集合
     */
    @Override
    public List<ConditionDefinition> getConditionDefinitionListBySearchKey(String searchKey,
        HttpServletRequest request) {
        // 构建查询条件
        QueryWrapper<ConditionDefinition> queryWrapper = HisQueryUtils.buildQueryWrapper(null, searchKey,
            new HashSet<>(Arrays.asList("condition_code", "name", "py_str", "wb_str")), request);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 诊断定义查询
     * 
     * @param conditionDefId 诊断定义id
     * @return 查询结果
     */
    @Override
    public ConditionDefinition getConditionDefinitionListById(Long conditionDefId) {
        return baseMapper
            .selectOne(new LambdaQueryWrapper<ConditionDefinition>().eq(ConditionDefinition::getId, conditionDefId));
    }
}