package com.emr.service.emrCode;

import com.emr.dao.emrCode.T_OperateMapper;
import com.emr.entity.emrCode.T_Diag;
import com.emr.entity.emrCode.T_Operate;
import com.emr.service.CommomService;
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/11 16:20
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/6/11 16:20
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class OperateServiceImpl implements OperateService{
    @Autowired
    private T_OperateMapper operateMapper;
    @Autowired
    private CommomService commomService;
    @Override
    public List<T_Operate> selectAllByPatientId(String patientId,Short currentOperateNo) {
        return operateMapper.selectAllByPatientId(patientId,currentOperateNo);
    }

    @Override
    public int insert(T_Operate record) {
        return operateMapper.insert(record);
    }

    @Override
    public void updateByPrimaryKeySelective(T_Operate record) {
        operateMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public void deleteOperCodeByPatientIdAndOperNo(String patientId, Short operateNo) {
        operateMapper.deleteOperCodeByPatientIdAndOperNo(patientId,operateNo+"");
    }

    @Override
    public void addOperCode(String patientId, String operNos, String operDates, String operCodes, String operNames, String operateClasses, String operators, String assistant1s, String assistant2s, String cuts, String chossurgs, String anaesthesiaTypes, String anaesthesiaNames) throws Exception{
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String[] operNamesSplit = operNames.split(",");
        String[] operCodesSplit = operCodes.split(",");
        String[] operDatesSplit = operDates.split(",");
        String[] operNosSplit = operNos.split(",");
        String[] operateClassesSplit = operateClasses.split(",");
        String[] operatorsSplit = operators.split(",");
        String[] assistant1sSplit = assistant1s.split(",");
        String[] assistant2sSplit = assistant2s.split(",");
        String[] cutsSplit = cuts.split(",");
        String[] chossurgsSplit = chossurgs.split(",");
        String[] anaesthesiaTypesSplit = anaesthesiaTypes.split(",");
        String[] anaesthesiaNamesSplit = anaesthesiaNames.split(",");
        //新增集合
        List<T_Operate> inserts = new ArrayList<>();
        //修改集合
        List<T_Operate> updates = new ArrayList<>();
        //按patientId查询手术编码
        List<T_Operate> operates = operateMapper.selectAllByPatientId(patientId, null);
        if(null != operates && !operates.isEmpty()){
            //定义最大手术序号
            int operNoMax = 0;
            for (int i = 0; i < operNamesSplit.length; i++) {
                //判断序号是否存在，存在则添加进批量更新，不存在添加进批量添加
                boolean operNoExist = false;
                for(T_Operate operate : operates){
                    if(StringUtils.isNotBlank(operNosSplit[i])){
                        if(operNosSplit[i].equals(operate.getOperateNo()+"")){
                            if(operate.getOperateNo() > operNoMax){
                                operNoMax = operate.getOperateNo();
                            }
                            operNoExist = true;
                            operate.setPatientId(patientId);
                            operate.setOperateNo(Short.valueOf(i+1+""));
                            operate.setOperateCode(operCodesSplit[i]);
                            operate.setOperateName(operNamesSplit[i]);
                            operate.setOperateDate(fmt.parse(operDatesSplit[i]));
                            if(StringUtils.isNotBlank(operateClassesSplit[i]) && !"null".equals(operateClassesSplit[i])){
                                operate.setOperateClass(operateClassesSplit[i]);
                            }
                            if(StringUtils.isNotBlank(operatorsSplit[i]) && !"null".equals(operatorsSplit[i])){
                                operate.setOperator(operatorsSplit[i]);
                            }
                            if(StringUtils.isNotBlank(assistant1sSplit[i]) && !"null".equals(assistant1sSplit[i])){
                                operate.setAssistant1(assistant1sSplit[i]);
                            }
                            if(StringUtils.isNotBlank(assistant2sSplit[i]) && !"null".equals(assistant2sSplit[i])){
                                operate.setAssistant2(assistant2sSplit[i]);
                            }
                            if(StringUtils.isNotBlank(cutsSplit[i]) && !"null".equals(cutsSplit[i])){
                                operate.setCut(cutsSplit[i]);
                            }
                            if(StringUtils.isNotBlank(chossurgsSplit[i]) && !"null".equals(chossurgsSplit[i])){
                                operate.setChossurg(chossurgsSplit[i]);
                            }
                            if(StringUtils.isNotBlank(anaesthesiaTypesSplit[i]) && !"null".equals(anaesthesiaTypesSplit[i])){
                                operate.setAnaesthesiaType(anaesthesiaTypesSplit[i]);
                            }
                            if(StringUtils.isNotBlank(anaesthesiaNamesSplit[i]) && !"null".equals(anaesthesiaNamesSplit[i])){
                                operate.setAnaesthesiaName(anaesthesiaNamesSplit[i]);
                            }
                            updates.add(operate);
                            break;
                        }
                    }
                }
                //不存在
                if(!operNoExist){
                    T_Operate operate = new T_Operate();
                    operate.setPatientId(patientId);
                    operate.setOperateNo(Short.valueOf(i+1+""));
                    operate.setOperateCode(operCodesSplit[i]);
                    operate.setOperateName(operNamesSplit[i]);
                    operate.setOperateDate(fmt.parse(operDatesSplit[i]));
                    if(StringUtils.isNotBlank(operateClassesSplit[i]) && !"null".equals(operateClassesSplit[i])){
                        operate.setOperateClass(operateClassesSplit[i]);
                    }
                    if(StringUtils.isNotBlank(operatorsSplit[i]) && !"null".equals(operatorsSplit[i])){
                        operate.setOperator(operatorsSplit[i]);
                    }
                    if(StringUtils.isNotBlank(assistant1sSplit[i]) && !"null".equals(assistant1sSplit[i])){
                        operate.setAssistant1(assistant1sSplit[i]);
                    }
                    if(StringUtils.isNotBlank(assistant2sSplit[i]) && !"null".equals(assistant2sSplit[i])){
                        operate.setAssistant2(assistant2sSplit[i]);
                    }
                    if(StringUtils.isNotBlank(cutsSplit[i]) && !"null".equals(cutsSplit[i])){
                        operate.setCut(cutsSplit[i]);
                    }
                    if(StringUtils.isNotBlank(chossurgsSplit[i]) && !"null".equals(chossurgsSplit[i])){
                        operate.setChossurg(chossurgsSplit[i]);
                    }
                    if(StringUtils.isNotBlank(anaesthesiaTypesSplit[i]) && !"null".equals(anaesthesiaTypesSplit[i])){
                        operate.setAnaesthesiaType(anaesthesiaTypesSplit[i]);
                    }
                    if(StringUtils.isNotBlank(anaesthesiaNamesSplit[i]) && !"null".equals(anaesthesiaNamesSplit[i])){
                        operate.setAnaesthesiaName(anaesthesiaNamesSplit[i]);
                    }
                    inserts.add(operate);
                }
            }
            //计算删除
            //删除集合
            StringBuilder deleteOperateNos = new StringBuilder();
            for(T_Operate operate : operates){
                boolean operateNoExist = false;
                for (String s : operNosSplit) {
                    if (StringUtils.isNotBlank(s)) {
                        if (s.equals(operate.getOperateNo() + "")) {
                            operateNoExist = true;
                            break;
                        }
                    }
                }
                if(!operateNoExist){
                    deleteOperateNos.append(operate.getOperateNo()).append(",");
                }
            }
            if(StringUtils.isNotBlank(deleteOperateNos)){
                deleteOperateNos = new StringBuilder(deleteOperateNos.substring(0,deleteOperateNos.length()-1));
                operateMapper.deleteOperCodeByPatientIdAndOperNo(patientId,deleteOperateNos.toString());
            }
        }else{
            //全部新增
            for (int i = 0; i < operNamesSplit.length; i++) {
                if(StringUtils.isNotBlank(operNamesSplit[i])){
                    T_Operate operate = new T_Operate();
                    operate.setPatientId(patientId);
                    operate.setOperateNo(Short.valueOf(i+1+""));
                    operate.setOperateCode(operCodesSplit[i]);
                    operate.setOperateName(operNamesSplit[i]);
                    operate.setOperateDate(fmt.parse(operDatesSplit[i]));
                    if(StringUtils.isNotBlank(operateClassesSplit[i]) && !"null".equals(operateClassesSplit[i])){
                        operate.setOperateClass(operateClassesSplit[i]);
                    }
                    if(StringUtils.isNotBlank(operatorsSplit[i]) && !"null".equals(operatorsSplit[i])){
                        operate.setOperator(operatorsSplit[i]);
                    }
                    if(StringUtils.isNotBlank(assistant1sSplit[i]) && !"null".equals(assistant1sSplit[i])){
                        operate.setAssistant1(assistant1sSplit[i]);
                    }
                    if(StringUtils.isNotBlank(assistant2sSplit[i]) && !"null".equals(assistant2sSplit[i])){
                        operate.setAssistant2(assistant2sSplit[i]);
                    }
                    if(StringUtils.isNotBlank(cutsSplit[i]) && !"null".equals(cutsSplit[i])){
                        operate.setCut(cutsSplit[i]);
                    }
                    if(StringUtils.isNotBlank(chossurgsSplit[i]) && !"null".equals(chossurgsSplit[i])){
                        operate.setChossurg(chossurgsSplit[i]);
                    }
                    if(StringUtils.isNotBlank(anaesthesiaTypesSplit[i]) && !"null".equals(anaesthesiaTypesSplit[i])){
                        operate.setAnaesthesiaType(anaesthesiaTypesSplit[i]);
                    }
                    if(StringUtils.isNotBlank(anaesthesiaNamesSplit[i]) && !"null".equals(anaesthesiaNamesSplit[i])){
                        operate.setAnaesthesiaName(anaesthesiaNamesSplit[i]);
                    }
                    inserts.add(operate);
                }
            }
        }
        //批量添加
        commomService.simpleInsert("t_operate",operateMapper,inserts);
        //批量更新
        if(!updates.isEmpty()){
            operateMapper.SimpleUpdate(updates);
        }
    }
}
