package com.kcim.dao.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcim.common.constants.NumberConstant;
import com.kcim.common.util.UserContext;
import com.kcim.dao.mapper.DiseaseTypeDiagMapMapper;
import com.kcim.dao.model.DiseaseTypeDiagMap;
import com.kcim.dao.model.Item;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: CostAccount
 * @description: 病种与诊断对照字典
 * @author: Wang.YS
 * @create: 2023-10-19 19:26
 **/
@Repository
public class DiseaseTypeDiagMapRepository extends ServiceImpl<DiseaseTypeDiagMapMapper, DiseaseTypeDiagMap> {
    public List<DiseaseTypeDiagMap> getList() {
        LambdaQueryWrapper<DiseaseTypeDiagMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DiseaseTypeDiagMap::getHospId, UserContext.getHospId());
        return this.list(queryWrapper);
    }

    public  Map<String,String> getDiagDiseaseMap() {

        LambdaQueryWrapper<DiseaseTypeDiagMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DiseaseTypeDiagMap::getHospId, UserContext.getHospId());
        List<DiseaseTypeDiagMap> list = this.list(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            //判断诊断与病种是否已对照
            List<DiseaseTypeDiagMap> collect = list.stream().filter(f -> !StringUtils.isEmpty(f.getDiseaseTypeCode())).collect(Collectors.toList());
            if(!CollectionUtils.isEmpty(collect)){
                //2023.10.30 医院诊断编码与诊断对照表 icdCode 对应
              return   collect.stream().collect(Collectors.toMap(DiseaseTypeDiagMap::getIcdCode, DiseaseTypeDiagMap::getDiseaseTypeCode, (a, b) -> b));

            }

        }


        return new HashMap<>();
    }

    public Page<DiseaseTypeDiagMap> getPage(Integer current, Integer pageSize, String name, Integer type) {
        Page<DiseaseTypeDiagMap> page = new Page<>(current,pageSize);
        LambdaQueryWrapper<DiseaseTypeDiagMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DiseaseTypeDiagMap::getHospId, UserContext.getHospId());
        if(!StringUtils.isEmpty(name)){
//            queryWrapper.like(DiseaseTypeDiagMap::getDiagName,name);
            queryWrapper.and(q->q.like(DiseaseTypeDiagMap::getDiagName,name).or().like(DiseaseTypeDiagMap::getIcdCode,name));

        }
        if(type.equals(NumberConstant.ONE)){
            queryWrapper.isNull(DiseaseTypeDiagMap::getDiseaseTypeCode);
        }
        return this.page(page,queryWrapper);
    }

    public void deleteDiseaseTypeMap(Integer id) {
        DiseaseTypeDiagMap diagMap = this.getById(id);
        if(Objects.nonNull(diagMap)){
            diagMap.setDeleteTime(new Date());
            diagMap.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
            this.updateById(diagMap);
            this.removeById(id);
        }
    }

    public void removeList(List<DiseaseTypeDiagMap> list) {
        for (DiseaseTypeDiagMap diagMap : list) {
            diagMap.setDeleteTime(new Date());
            diagMap.setDeleteUser(String.valueOf(UserContext.getCurrentUser().getId()));
        }
        this.updateBatchById(list);
        List<Integer> collect = list.stream().map(DiseaseTypeDiagMap::getId).collect(Collectors.toList());
        this.removeBatchByIds(collect);
    }

    public List<DiseaseTypeDiagMap> getByDiagCode(String diagCode) {
        LambdaQueryWrapper<DiseaseTypeDiagMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DiseaseTypeDiagMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(DiseaseTypeDiagMap::getDiagCode,diagCode);
        return this.list(queryWrapper);
    }

    public DiseaseTypeDiagMap getOneByDiagCode(String diagCode) {
        LambdaQueryWrapper<DiseaseTypeDiagMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DiseaseTypeDiagMap::getHospId, UserContext.getHospId());
        queryWrapper.eq(DiseaseTypeDiagMap::getDiagCode,diagCode);
        return this.getOne(queryWrapper);
    }
}
