package thyyxxk.webserver.service.medicalinsurance;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.medicalinsurance.TSetlDeptConfDao;
import thyyxxk.webserver.entity.querydata.TSetlDeptConf;
import thyyxxk.webserver.service.outpatient.wxapi.SendWxInfoService;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 结算单上传科室配置表 服务实现类
 * </p>
 *
 * @author lihong
 * @since 2023-01-09
 */
@Service
public class TSetlDeptConfService extends ServiceImpl<TSetlDeptConfDao, TSetlDeptConf>   {
    @Autowired
    private SendWxInfoService sendWxInfoService;

    public List<TSetlDeptConf> querySetlDeptConfInfo(TSetlDeptConf query) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtil.notBlank(query.getCode()), "code", query.getCode());
        queryWrapper.eq(query.getTypeFlag() != null,"type_flag" ,query.getTypeFlag());
        return baseMapper.selectList(queryWrapper);
    }

    public void batchSaveData(List<TSetlDeptConf> list) {
        baseMapper.batchSaveData(list);

    }
    /**
     * @description:给编码员发送消息
     * @author: lihong
     * @date: 2023/1/10 9:33
     * @param: deptCode
     * @param: name
     * @param: patNo
     **/
    @Async
    public void asynSendAuditMessge(String deptCode,String deptName,String name,String patNo){
        QueryWrapper<TSetlDeptConf> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept_code",deptCode);
        queryWrapper.eq("type_flag",1);
        List<TSetlDeptConf> list = baseMapper.selectList(queryWrapper);
        List<String>  codeRs ;
        if(ListUtil.notBlank(list)){
            List<String> codes = list.stream().map(TSetlDeptConf::getCode).distinct().collect(Collectors.toList());
            if(CollUtil.isEmpty(codes)) return;
            codeRs =  baseMapper.getCodeRs(codes);
        }else {
        //    给所有的编码员发送消息
             codeRs = baseMapper.getBmyCodeRs();
        }
        if(CollUtil.isNotEmpty(codeRs)){
            codeRs.forEach(code->{
                sendWxInfoService.sendCorpWxMsg(code, String.format("医保结算单上传\n发起申请审核\n患者姓名：【%s】\n住院号：【%s】\n出院科室：【%s】", name,patNo,deptName));
            });
        }
    }
}
