package com.emr.service.emrCode;

import com.emr.dao.CommomMapper;
import com.emr.dao.Emr_DictionaryMapper;
import com.emr.dao.emrCode.T_DiagMapper;
import com.emr.entity.Emr_Dictionary;
import com.emr.entity.emrCode.T_Diag;
import com.emr.service.CommomService;
import com.emr.vo.commomSearch.CommomVo;
import com.lowagie.text.pdf.PdfStructureElement;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/6/12 16:32
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/6/12 16:32
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class DiagServiceImpl implements DiagService {
    @Autowired
    private T_DiagMapper diagMapper;
    @Autowired
    private Emr_DictionaryMapper dictionaryMapper;
    @Autowired
    private CommomMapper commomMapper;
    @Autowired
    private CommomService commomService;
    @Override
    public List<T_Diag> selectAllByPatientId(String patientId, Short diagNo) {
        return diagMapper.selectAllByPatientId(patientId, diagNo);
    }

    @Override
    public int insert(T_Diag record) {
        record.setCreateDate(new Date());
        //如果是第一次，要更新commomtable表 main_diag_name,main_diag_thing
        if(record.getDiagNo() == 1){
            updateCommomTableByDiag(record);
        }
        return diagMapper.insert(record);
    }

    @Override
    public void updateByPrimaryKeySelective(T_Diag record) {
        diagMapper.updateByPrimaryKeySelective(record);
        //同步commomtable表
        //如果是第一次，要更新commomtable表 main_diag_name,main_diag_thing
        if(record.getDiagNo() == 1){
            updateCommomTableByDiag(record);
        }else{
            //查询是否存在diagNo为1，不存在则同步commomtable的main_diag_name和main_thing_status置空
            List<T_Diag> t_diags = selectAllByPatientId(record.getPatientId(), Short.valueOf("1"));
            if(null == t_diags || t_diags.isEmpty()){
                CommomVo commomVo = commomMapper.selectByPrimaryKey(record.getPatientId());
                commomVo.setMainDisThing("");
                commomVo.setMainDiagName("");
                commomMapper.updateByPrimaryKeySelective(commomVo);
            }
        }
    }
    private void updateCommomTableByDiag(T_Diag record){
        CommomVo commom = new CommomVo();
        commom.setPatientId(record.getPatientId());
        commom.setMainDiagName(record.getDiagName());
        if(null != record.getDisThing()){
            //转归情况转换字典
            List<Emr_Dictionary> list = dictionaryMapper.selectDictionaryByTypeCode("dis_diag_status");
            if(null != list && !list.isEmpty()){
                for (Emr_Dictionary dictionary : list) {
                    if (StringUtils.isNoneBlank(dictionary.getCode()) && dictionary.getCode().equals(record.getDisThing())) {
                        commom.setMainDisThing(dictionary.getName());
                        break;
                    }
                }
            }

        }
        commomMapper.updateByPrimaryKeySelective(commom);
    }
    @Override
    public void deleteDiagByPatientIdAndDiagNo(String patientId, Short diagNo) {
        diagMapper.deleteDiagByPatientIdAndDiagNo(patientId, diagNo+"");
        //删除的是1主要诊断，即把commomTable两诊断字段置空
        if(diagNo == 1){
            CommomVo commomVo = commomMapper.selectByPrimaryKey(patientId);
            commomVo.setMainDisThing("");
            commomVo.setMainDiagName("");
            commomMapper.updateByPrimaryKeySelective(commomVo);
        }
    }

    @Override
    public void addDiagCode(String patientId, String diagNos, String diagNames, String diagCodes, String inhospstats) {
        String[] diagNamesSplit = diagNames.split(",");
        String[] diagCodesSplit = diagCodes.split(",");
        String[] inhospstatsSplit = inhospstats.split(",");
        String[] diagNosSplit = diagNos.split(",");
        //新增集合
        List<T_Diag> inserts = new ArrayList<>();
        //修改集合
        List<T_Diag> updates = new ArrayList<>();
        //按patientId查询诊断编码
        List<T_Diag> diags = diagMapper.selectAllByPatientId(patientId, null);
        if(null != diags && !diags.isEmpty()){
            //定义最大诊断序号
            int diagNoMax = 0;
            for (int i = 0; i < diagNamesSplit.length; i++) {
                //判断序号是否存在，存在则添加进批量更新，不存在添加进批量添加
                Boolean diagNoExist = false;
                for(T_Diag diag : diags){
                    if(StringUtils.isNotBlank(diagNosSplit[i])){
                        if(diagNosSplit[i].equals(diag.getDiagNo()+"")){
                            if(diag.getDiagNo() > diagNoMax){
                                diagNoMax = diag.getDiagNo();
                            }
                            diagNoExist = true;
                            diag.setPatientId(patientId);
                            diag.setDiagName(diagNamesSplit[i]);
                            diag.setDiagCode(diagCodesSplit[i]);
                            if(StringUtils.isNotBlank(inhospstatsSplit[i]) && !"null".equals(inhospstatsSplit[i])){
                                diag.setInhospstat(inhospstatsSplit[i]);
                            }else{
                                diag.setInhospstat(null);
                            }
                            updates.add(diag);
                            break;
                        }
                    }
                }
                //不存在
                if(!diagNoExist){
                    T_Diag diag = new T_Diag();
                    diag.setCreateDate(new Date());
                    diag.setPatientId(patientId);
                    diag.setDiagName(diagNamesSplit[i]);
                    diag.setDiagCode(diagCodesSplit[i]);
                    diag.setDiagNo(Short.valueOf(diagNoMax+1+""));
                    if(StringUtils.isNotBlank(inhospstatsSplit[i]) && !"null".equals(inhospstatsSplit[i])){
                        diag.setInhospstat(inhospstatsSplit[i]);
                    }
                    inserts.add(diag);
                }
            }
            //计算删除
            //删除集合
            StringBuilder deleteDiagNos = new StringBuilder();
            for(T_Diag diag : diags){
                boolean diagNoExist = false;
                for (String s : diagNosSplit) {
                    if (StringUtils.isNotBlank(s)) {
                        if (s.equals(diag.getDiagNo() + "")) {
                            diagNoExist = true;
                            break;
                        }
                    }
                }
                if(!diagNoExist){
                    deleteDiagNos.append(diag.getDiagNo()).append(",");
                }
            }
            if(StringUtils.isNotBlank(deleteDiagNos)){
                deleteDiagNos = new StringBuilder(deleteDiagNos.substring(0,deleteDiagNos.length()-1));
                diagMapper.deleteDiagByPatientIdAndDiagNo(patientId,deleteDiagNos.toString());
            }
        }else{
            //全部新增
            for (int i = 0; i < diagNamesSplit.length; i++) {
                if(StringUtils.isNotBlank(diagNamesSplit[i])){
                    T_Diag diag = new T_Diag();
                    diag.setCreateDate(new Date());
                    diag.setPatientId(patientId);
                    diag.setDiagName(diagNamesSplit[i]);
                    diag.setDiagCode(diagCodesSplit[i]);
                    diag.setDiagNo(Short.valueOf(i+1+""));
                    if(StringUtils.isNotBlank(inhospstatsSplit[i]) && !"null".equals(inhospstatsSplit[i])){
                        diag.setInhospstat(inhospstatsSplit[i]);
                    }
                    inserts.add(diag);
                }
            }
        }
        //批量添加
        if(!inserts.isEmpty()){
            commomService.simpleInsert("t_diag",diagMapper,inserts);
        }
        //批量更新
        if(!updates.isEmpty()){
            diagMapper.SimpleUpdate(updates);
        }
    }
}
