package com.emr.service.his;

import com.emr.annotation.DBSource;
import com.emr.dao.his.*;
import com.emr.entity.Emr_Dictionary;
import com.emr.entity.emrCode.T_Diag;
import com.emr.entity.emrCode.T_Operate;
import com.emr.entity.his.*;
import com.emr.entity.tBoby.TBaby;
import com.emr.entity.tTumour.TTumourChemotherapy;
import com.emr.vo.User;
import com.emr.vo.commomSearch.CommomVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2020/1/14 17:33
 * @UpdateUser: 曾文和
 * @UpdateDate: 2020/1/14 17:33
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class HisService{
    @Value("${icdVersion}")
    private String icdVersion;
    @Autowired
    private HISBA1Mapper hisba1Mapper;
    @Autowired
    private HISBA2Mapper hisba2Mapper;
    @Autowired
    private HISBA3Mapper hisba3Mapper;
    @Autowired
    private HISBA4Mapper hisba4Mapper;
    @Autowired
    private HISBA5Mapper hisba5Mapper;
    @Autowired
    private HISBA6Mapper hisba6Mapper;
    @Autowired
    private HISBA7Mapper hisba7Mapper;
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
    //插入HIS_BA1基本信息表
    @DBSource("salve")
    public void insertHisBa1(CommomVo commomVo,List<Emr_Dictionary> dictionarys,List<User> userList,List<TBaby> babies,int operCount) throws ParseException {
        //性别字典集合
        Map<String,String> sexMap = new HashMap<>();
        //国籍字典集合
        Map<String,String> countryMap = new HashMap<>();
        //民族字典集合
        Map<String,String> nationMap = new HashMap<>();
        //婚况字典集合
        Map<String,String> maritalMap = new HashMap<>();
        //科室字典集合
        Map<String,String> deptMap = new HashMap<>();
        //病案质量字典集合
        Map<String,String> qualityMap = new HashMap<>();
        //是否尸检字典集合
        Map<String,String> idDeadPsyMap = new HashMap<>();
        //血型字典集合
        Map<String,String> bloodTypeMap = new HashMap<>();
        //rh字典集合
        Map<String,String> rhMap = new HashMap<>();
        //病历分型字典集合
        Map<String,String> recordTypeMap = new HashMap<>();
        //职业字典集合
        Map<String,String> jobMap = new HashMap<>();
        //入院途径字典集合
        Map<String,String> admissTypeMap = new HashMap<>();
        //是否临床路径病例字典集合
        Map<String,String> isMedicalRecordMap = new HashMap<>();
        //是否药物过敏字典集合
        Map<String,String> isDrugAllergyMap = new HashMap<>();
        //离院方式字典集合
        Map<String,String> disTypeMap = new HashMap<>();
        //是否30天后再住院字典集合
        Map<String,String> isAdmissPlanMap = new HashMap<>();
        //付款方式字典集合
        Map<String,String> payTypeMap = new HashMap<>();
        if(null != dictionarys && !dictionarys.isEmpty()){
            for(Emr_Dictionary dictionary : dictionarys){
                //筛选性别字典
                selectDictionaryByTypeCode("sex_code",sexMap,dictionary);
                //筛选国籍字典
                selectDictionaryByTypeCode("country",countryMap,dictionary);
                //筛选民族字典
                selectDictionaryByTypeCode("nation_code",nationMap,dictionary);
                //筛选婚况字典
                selectDictionaryByTypeCode("marital_status",maritalMap,dictionary);
                //筛选科室字典
                selectDictionaryByTypeCode("dept_code",deptMap,dictionary);
                //病案质量字典集合
                selectDictionaryByTypeCode("quality_level",qualityMap,dictionary);
                //是否尸检字典集合
                selectDictionaryByTypeCode("idDeadPsy_code",idDeadPsyMap,dictionary);
                //血型字典集合
                selectDictionaryByTypeCode("blood_type",bloodTypeMap,dictionary);
                //rh字典集合
                selectDictionaryByTypeCode("rh_code",rhMap,dictionary);
                //病历分型字典集合
                selectDictionaryByTypeCode("recordType_code",recordTypeMap,dictionary);
                //职业字典集合
                selectDictionaryByTypeCode("job",jobMap,dictionary);
                //入院途径字典集合
                selectDictionaryByTypeCode("admiss_type",admissTypeMap,dictionary);
                //是否临床路径病例字典集合
                selectDictionaryByTypeCode("clinicalRecord_code",isMedicalRecordMap,dictionary);
                //是否药物过敏字典集合
                selectDictionaryByTypeCode("isDrugAllergy_code",isDrugAllergyMap,dictionary);
                //离院方式字典集合
                selectDictionaryByTypeCode("zd_dis_type",disTypeMap,dictionary);
                //是否30天后再住院字典集合
                selectDictionaryByTypeCode("isAdmissPlan_code",isAdmissPlanMap,dictionary);
                //付款方式字典集合
                selectDictionaryByTypeCode("pay_type",payTypeMap,dictionary);
            }
        }
        //病案号
        String inpatientNo = commomVo.getInpatientNo();
        //住院次数
        Short admissTimes = commomVo.getAdmissTimes();
        HISBA1 hisba1 = new HISBA1();

        //查询是否存在
        List<HISBA1> hisba1s = hisba1Mapper.selectAllByParam(inpatientNo, Integer.valueOf(admissTimes));

        if(null != hisba1s && !hisba1s.isEmpty()){
            hisba1 = hisba1s.get(0);
        }
        //病案号
        hisba1.setFprn(inpatientNo);
        //住院次数
        if(null != admissTimes){
            hisba1.setFtimes(Integer.valueOf(admissTimes));
        }
        //ICD版本
        hisba1.setFicdversion(Byte.valueOf(icdVersion));
        //住院流水号
        hisba1.setFzyid(commomVo.getAdmissId());
        //年龄
        hisba1.setFage(commomVo.getAge().toString());
        //病人姓名
        hisba1.setFname(commomVo.getName());
        //性别编号
        String sex = commomVo.getSex();
        if(StringUtils.isNotBlank(sex)){
            //转换字典
            String code = nameConvertCode(sexMap, sex);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFsexbh(code);
            }
        } else{
            hisba1.setFsexbh(null);
        }
        //性别
        hisba1.setFsex(sex);
        //出生日期
        if(StringUtils.isNotBlank(commomVo.getBirthday())){
            hisba1.setFbirthday(fmt.parse(commomVo.getBirthday()));
        }
        //出生地
        hisba1.setFbirthplace(commomVo.getBirthAddr());
        //身份证号
        hisba1.setFidcard(commomVo.getIdCard());
        //国籍编号
        String country = commomVo.getCountry();
        if(StringUtils.isNotBlank(country)){
            //转换字典
            String code = nameConvertCode(countryMap, country);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFcountrybh(code);
            }
        }else{
            hisba1.setFcountrybh(null);
        }
        //国籍
        hisba1.setFcountry(country);
        //民族编号
        String nation = commomVo.getNation();
        if(StringUtils.isNotBlank(nation)){
            //转换字典
            String code = nameConvertCode(nationMap, nation);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFnationalitybh(code);
            }
        }else{
            hisba1.setFnationalitybh(null);
        }
        //民族
        hisba1.setFnationality(nation);
        //职业
        hisba1.setFjob(commomVo.getJob());
        //婚姻状况编号
        String marriage = commomVo.getMarriage();
        if(StringUtils.isNotBlank(marriage)){
            //转换字典
            String code = nameConvertCode(maritalMap, marriage);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFstatusbh(code);
            }
        }else{
            hisba1.setFstatusbh(null);
        }
        //婚姻状况
        hisba1.setFstatus(marriage);
        //单位名称
        hisba1.setFdwname(commomVo.getWorkName());
        //单位地址
        hisba1.setFdwaddr(commomVo.getWorkAddr());
        //单位电话
        if(StringUtils.isNoneBlank(commomVo.getWorkTel()))
        hisba1.setFdwtele(commomVo.getWorkTel());
        //单位邮编
        hisba1.setFdwpost(commomVo.getWorkZip());
        //户口地址
        hisba1.setFhkaddr(commomVo.getHomeAddr());
        //户口邮编
        hisba1.setFhkpost(commomVo.getHomeZip());
        //联系人
        hisba1.setFlxname(commomVo.getLinkman());
        //与病人关系
        hisba1.setFrelate(commomVo.getRelation());
        //联系人地址
        hisba1.setFlxaddr(commomVo.getRelAddr());
        //联系人电话
        hisba1.setFlxtele(commomVo.getRelTel());
        //健康卡号
        hisba1.setFascard1(commomVo.getHealthyCard());
        //入院日期
        String admissDate = commomVo.getAdmissDate();
        if(StringUtils.isNotBlank(admissDate)){
            hisba1.setFrydate(fmt.parse(admissDate));
        }else{
            hisba1.setFrydate(null);
        }
        //入院时间
        if(StringUtils.isNotBlank(admissDate)){
            admissDate = fmt.format(fmt.parse(admissDate));
        }
        hisba1.setFrytime(admissDate);
        //入院统一科号
        String admissDept = commomVo.getAdmissDept();
        if(StringUtils.isNotBlank(admissDept)){
            //转换字典
            String code = nameConvertCode(deptMap, admissDept);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFrytykh(code);
            }
        } else{
            hisba1.setFrytykh(null);
        }
        //入院科别
        hisba1.setFrydept(admissDept);
        //入院病室
        hisba1.setFrybs(commomVo.getAdmissWard());
        //出院日期
        String disDate = commomVo.getDisDate();
        if(StringUtils.isNotBlank(disDate)){
            hisba1.setFcydate(fmt.parse(disDate));
        }else{
            hisba1.setFcydate(null);
        }
        //出院时间
        if(StringUtils.isNotBlank(disDate)){
            disDate = fmt.format(fmt.parse(disDate));
        }
        hisba1.setFcytime(disDate);
        //出院统一科号
        String disDept = commomVo.getDisDept();
        if(StringUtils.isNotBlank(disDept)){
            //转换字典
            String code = nameConvertCode(deptMap, disDept);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFcytykh(code);
            }
        } else{
            hisba1.setFcytykh(null);
        }
        //出院科别
        hisba1.setFcydept(disDept);
        //出院病室
        hisba1.setFcybs(commomVo.getDisWard());
        //实际住院天数
        hisba1.setFdays(commomVo.getAdmissDays());
        //门（急）诊诊断编码
        hisba1.setFmzzdbh(commomVo.getClinicDiag());
        //门（急）诊诊断疾病名
        hisba1.setFmzzd(commomVo.getClinicName());
        //门、急诊医生编号
        String clinicDoctor = commomVo.getClinicDoctor();
        String clinicDoctorUserName = "";
        //科主任编号
        String deptDirector = commomVo.getDeptDirector();
        String deptDirectorUserName = "";
        //主（副主）任医生编号
        String director = commomVo.getDirector();
        String directorUserName = "";
        //主治医生编号
        String attending = commomVo.getAttending();
        String attendingUserName = "";
        //住院医生编号
        String admissDoctor = commomVo.getAdmissDoctor();
        String admissDoctorUserName = "";
        //进修医师编号
        String refresher = commomVo.getRefresher();
        String refresherUserName = "";
        //实习医师编号
        String praxis = commomVo.getPraxis();
        String praxisUserName = "";
        //编码员编号
        String coding = commomVo.getCoding();
        String codingUserName = "";
        //质控医师编号
        String control = commomVo.getControl();
        String controlUserName = "";
        //质控护士编号
        String nurses = commomVo.getNurses();
        String nursesUserName = "";
        //输入员编号
        String inputName = commomVo.getInputName();
        String inputNameUserName = "";
        //责任护士编号
        String nurses1 = commomVo.getResponsibleNurse();
        String nurses1UserName = "";
        if(null != userList && !userList.isEmpty()){
            for (User user : userList) {
                if(StringUtils.isNotBlank(user.getName())) {
                    //门、急诊医生编号
                    if (StringUtils.isNotBlank(clinicDoctor) && user.getName().equals(clinicDoctor)) {
                        clinicDoctorUserName = user.getUserName();
                    }
                    //科主任编号
                    if (StringUtils.isNotBlank(deptDirector) && user.getName().equals(deptDirector)) {
                        deptDirectorUserName = user.getUserName();
                    }
                    //主（副主）任医生编号
                    if (StringUtils.isNotBlank(director) && user.getName().equals(director)) {
                        directorUserName = user.getUserName();
                    }
                    //主治医生编号
                    if (StringUtils.isNotBlank(attending) && user.getName().equals(attending)) {
                        attendingUserName = user.getUserName();
                    }
                    //住院医生编号
                    if (StringUtils.isNotBlank(admissDoctor) && user.getName().equals(admissDoctor)) {
                        admissDoctorUserName = user.getUserName();
                    }
                    //进修医师编号
                    if (StringUtils.isNotBlank(refresher) && user.getName().equals(refresher)) {
                        refresherUserName = user.getUserName();
                    }
                    //实习医师编号
                    if (StringUtils.isNotBlank(praxis) && user.getName().equals(praxis)) {
                        praxisUserName = user.getUserName();
                    }
                    //编码员编号
                    if (StringUtils.isNotBlank(coding) && user.getName().equals(coding)) {
                        codingUserName = user.getUserName();
                    }
                    //质控医师编号
                    if (StringUtils.isNotBlank(control) && user.getName().equals(control)) {
                        controlUserName = user.getUserName();
                    }
                    //质控护士编号
                    if (StringUtils.isNotBlank(nurses) && user.getName().equals(nurses)) {
                        nursesUserName = user.getUserName();
                    }
                    //输入员编号
                    if (StringUtils.isNotBlank(inputName) && user.getName().equals(inputName)) {
                        inputNameUserName = user.getUserName();
                    }
                    //责任护士编号
                    if (StringUtils.isNotBlank(nurses1) && user.getName().equals(nurses1)) {
                        nurses1UserName = user.getUserName();
                    }
                }
            }
        }
        //门、急诊医生编号
        hisba1.setFmzdoctbh(clinicDoctorUserName);
        //门、急诊医生
        hisba1.setFmzdoct(clinicDoctor);
        //病理诊断
        hisba1.setFphzd(commomVo.getPathologyName());
        //过敏药物
        hisba1.setFgmyw(commomVo.getMedicine());
        //抢救次数
        if(StringUtils.isNotBlank(commomVo.getSaveTimes())){
            hisba1.setFqjtimes(Integer.valueOf(commomVo.getSaveTimes()));
        }
        //抢救成功次数
        if(StringUtils.isNotBlank(commomVo.getSaveSuccessTimes())){
            hisba1.setFqjsuctimes(Integer.valueOf(commomVo.getSaveSuccessTimes()));
        }
        //科主任编号
        hisba1.setFkzrbh(deptDirectorUserName);
        //科主任
        hisba1.setFkzr(deptDirector);
        //主（副主）任医生编号
        hisba1.setFzrdoctbh(directorUserName);
        //主（副主）任医生
        hisba1.setFzrdoctor(director);
        //主治医生编号
        hisba1.setFzzdoctbh(attendingUserName);
        //主治医生
        hisba1.setFzzdoct(attending);
        //住院医生编号
        hisba1.setFzydoctbh(admissDoctorUserName);
        //住院医生
        hisba1.setFzydoct(admissDoctor);
        //进修医师编号
        hisba1.setFjxdoctbh(refresherUserName);
        //进修医师
        hisba1.setFjxdoct(refresher);
        //实习医师编号
        hisba1.setFsxdoctbh(praxisUserName);
        //实习医师
        hisba1.setFsxdoct(praxis);
        //编码员编号
        hisba1.setFbmybh(codingUserName);
        //编码员
        hisba1.setFbmy(coding);
        //病案质量编号
        String quality = commomVo.getQuality();
        if(StringUtils.isNotBlank(quality)){
            //转换字典
            String code = nameConvertCode(qualityMap, quality);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFqualitybh(code);
            }
        }else{
            hisba1.setFqualitybh(null);
        }
        //病案质量
        hisba1.setFquality(quality);
        //质控医师编号
        hisba1.setFzkdoctbh(controlUserName);
        //质控医师
        hisba1.setFzkdoct(control);
        //质控护士编号
        hisba1.setFzknursebh(nursesUserName);
        //质控护士
        hisba1.setFzknurse(nurses);
        //质控日期
        if(StringUtils.isNotBlank(commomVo.getQualityDate())){
            hisba1.setFzkrq(fmt.parse(commomVo.getQualityDate()));
        }else{
            hisba1.setFzkrq(null);
        }
        //总费用
        hisba1.setFsum1(commomVo.getTotalCharge());
        //西药费
        hisba1.setFxyf(commomVo.getCharge03());
        //中成药费
        hisba1.setFzchyf(commomVo.getCharge04());
        //中草药费
        hisba1.setFzcyf(commomVo.getCharge05());
        //其他费
        hisba1.setFqtf(commomVo.getOther());
        //是否尸检编号
        String emitPathology = commomVo.getEmitPathology();
        if(StringUtils.isNotBlank(emitPathology)){
            //转换字典
            String code = nameConvertCode(idDeadPsyMap, emitPathology);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFbodybh(code);
            }
        } else{
            hisba1.setFbodybh(null);
        }
        //是否尸检
        hisba1.setFbody(emitPathology);
        //血型编号
        String bloodType = commomVo.getBloodType();
        if(StringUtils.isNotBlank(bloodType)){
            //转换字典
            String code = nameConvertCode(bloodTypeMap, bloodType);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFbloodbh(code);
            }
        } else{
            hisba1.setFblood(null);
        }
        //血型
        hisba1.setFblood(bloodType);
        //RH编号
        String rh = commomVo.getRh();
        if(StringUtils.isNotBlank(rh)){
            //转换字典
            String code = nameConvertCode(rhMap, rh);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFrhbh(code);
            }
        } else{
            hisba1.setFrhbh(null);
        }
        //RH
        hisba1.setFrh(rh);
        //婴儿数
        if(null != babies && !babies.isEmpty()){
            hisba1.setFbabynum(babies.size());
        }else{
            hisba1.setFbabynum(0);
        }
        //首次转科统一科号
        String changeDept1 = commomVo.getChangeDept1();
        if(StringUtils.isNotBlank(changeDept1)){
            //转换字典
            String code = nameConvertCode(deptMap, changeDept1);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFzktykh(code);
            }
        } else{
            hisba1.setFzktykh(null);
        }
        //首次转科科别
        hisba1.setFzkdept(changeDept1);
        //首次转科日期
        if(StringUtils.isNotBlank(commomVo.getChangeDate1())){
            hisba1.setFzkdate(fmt.parse(commomVo.getChangeDate1()));
        }else{
            hisba1.setFzkdate(null);
        }
        //首次转科时间
        hisba1.setFzktime(commomVo.getChangeDate1());
        //输入员编号
        hisba1.setFsrybh(inputNameUserName);
        //输入员
        hisba1.setFsry(inputName);
        //输入日期
        if(StringUtils.isNotBlank(commomVo.getInputCreateTime())){
            hisba1.setFworkrq(fmt.parse(commomVo.getInputCreateTime()));
        }

        //疾病分型
        hisba1.setFjbfx(commomVo.getCaseType());
        String recordType = null;
        if(null != recordTypeMap && !recordTypeMap.isEmpty()){
            for(Map.Entry<String,String> keyMap : recordTypeMap.entrySet()){
                if(StringUtils.isNotBlank(commomVo.getCaseType()) && keyMap.getValue().equals(commomVo.getCaseType())){
                    recordType = keyMap.getKey();
                    //疾病分型编号
                    hisba1.setFjbfxbh(recordType);
                    break;
                }
            }
        }
        //是否手术
        if(operCount == 0){
            hisba1.setFifss(false);
        }else{
            hisba1.setFifss(true);
        }
        //是否输入妇婴卡
        if(babies.size() == 0){
            hisba1.setFiffyk(false);
        }else{
            hisba1.setFiffyk(true);
        }
        //籍贯
        hisba1.setFnative(commomVo.getNativePlace());
        //现住址
        hisba1.setFcurraddr(commomVo.getAddr());
        //现电话
        hisba1.setFcurrtele(commomVo.getTelphone());
        //现邮编
        hisba1.setFcurrpost(commomVo.getZip());
        //职业编号
        String job = commomVo.getJob();
        if(StringUtils.isNotBlank(job)){
            //转换字典
            String code = nameConvertCode(jobMap, job);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFjobbh(code);
            }
        } else{
            hisba1.setFjobbh(null);
        }
        //新生儿出生体重
        if(null != commomVo.getAvoirdupois())
            hisba1.setFcstz(commomVo.getAvoirdupois().doubleValue());
        //新生儿入院体重
        if(null != commomVo.getBirthbabyWeight())
            hisba1.setFrytz(commomVo.getBirthbabyWeight().doubleValue());
        //入院途径编号
        String admissType = commomVo.getAdmissType();
        if(StringUtils.isNotBlank(admissType)){
            //转换字典
            String code = nameConvertCode(admissTypeMap, admissType);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFrytjbh(code);
            }
        } else{
            hisba1.setFrytjbh(null);
        }
        //入院途径
        hisba1.setFrytj(admissType);
        //临床路径病例编号
        String isMedicalRecordPath = commomVo.getIsMedicalRecordPath();
        if(StringUtils.isNotBlank(isMedicalRecordPath)){
            //转换字典
            String code = nameConvertCode(isMedicalRecordMap, isMedicalRecordPath);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFycljbh(code);
            }
        } else{
            hisba1.setFycljbh(null);
        }
        //临床路径病例
        hisba1.setFyclj(isMedicalRecordPath);
        //病理疾病编码
        hisba1.setFphzdbh(commomVo.getPathologyCode());
        //病理号
        hisba1.setFphzdnum(commomVo.getPathologyNo());
        //是否药物过敏编号
        String isMedicine = commomVo.getIsMedicine();
        if(StringUtils.isNotBlank(isMedicine)){
            //转换字典
            String code = nameConvertCode(isDrugAllergyMap, isMedicine);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFifgmywbh(code);
            }
        } else{
            hisba1.setFifgmywbh(null);
        }
        //是否药物过敏
        hisba1.setFifgmyw(isMedicine);
        //责任护士编号
        hisba1.setFnursebh(nurses1UserName);
        //责任护士
        hisba1.setFnurse(nurses1);
        //离院方式编号
        String disType = commomVo.getDisType();
        if(StringUtils.isNotBlank(disType)){
            //转换字典
            String code = nameConvertCode(disTypeMap, disType);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFlyfsbh(code);
            }
        } else{
            hisba1.setFlyfsbh(null);
        }
        //离院方式
        hisba1.setFlyfs(disType);
        //离院方式为医嘱转院，拟接收医疗机构名称
        hisba1.setFyzouthostital(commomVo.getTransferHospitalName());
        //离院方式为转社区卫生服务器机构/乡镇卫生院，拟接收医疗机构名称
        hisba1.setFsqouthostital(commomVo.getTransferHealcenterName());
        //是否有出院31天内再住院计划编号
        String isHospitalization = commomVo.getIsHospitalization();
        if(StringUtils.isNotBlank(isHospitalization)){
            //转换字典
            String code = nameConvertCode(isAdmissPlanMap, isHospitalization);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFisagainrybh(code);
            }
        } else{
            hisba1.setFisagainrybh(null);
        }
        //是否有出院31天内再住院计划
        hisba1.setFisagainry(isHospitalization);
        //再住院目的
        hisba1.setFisagainrymd(commomVo.getHospitalizationObjective());
        //颅脑损伤患者昏迷时间：入院前 天
        hisba1.setFryqhmdays(commomVo.getRyqhmDays());
        //颅脑损伤患者昏迷时间：入院前 小时
        hisba1.setFryqhmhours(commomVo.getRyqhmHours());
        //颅脑损伤患者昏迷时间：入院前 分钟
        hisba1.setFryqhmmins(commomVo.getRyqhmMins());
        int ryqhmmins = 0;
        if(null != commomVo.getRyqhmDays()){
            ryqhmmins += commomVo.getRyqhmDays()*24*60;
        }
        if(null != commomVo.getRyqhmHours()){
            ryqhmmins += commomVo.getRyqhmHours()*60;
        }
        if(null != commomVo.getRyqhmMins()){
            ryqhmmins += commomVo.getRyqhmMins();
        }
        //入院前昏迷总分钟
        if(ryqhmmins != 0){
            hisba1.setFryqhmcounts(ryqhmmins);
        }else{
            hisba1.setFryqhmcounts(null);
        }
        //颅脑损伤患者昏迷时间：入院后 天
        hisba1.setFryhmdays(commomVo.getRyhmDays());
        //颅脑损伤患者昏迷时间：入院后 小时
        hisba1.setFryhmhours(commomVo.getRyhmHours());
        //颅脑损伤患者昏迷时间：入院后 分钟
        hisba1.setFryhmmins(commomVo.getRyhmMins());
        //入院后昏迷总分钟
        int ryhmmins = 0;
        if(null != commomVo.getRyhmDays()){
            ryhmmins += commomVo.getRyhmDays()*24*60;
        }
        if(null != commomVo.getRyhmHours()){
            ryhmmins += commomVo.getRyhmHours()*60;
        }
        if(null != commomVo.getRyhmMins()){
            ryhmmins += commomVo.getRyhmMins();
        }
        if(ryhmmins != 0){
            hisba1.setFryhmcounts(ryhmmins);
        }else{
            hisba1.setFryhmcounts(null);
        }
        //付款方式编号
        String payType = commomVo.getPayType();
        if(StringUtils.isNotBlank(payType)){
            //转换字典
            String code = nameConvertCode(payTypeMap, payType);
            if(StringUtils.isNotBlank(code)){
                hisba1.setFfbbhnew(code);
            }
        } else{
            hisba1.setFfbbhnew(null);
        }
        //付款方式
        hisba1.setFfbnew(payType);
        //住院总费用：自费金额
        hisba1.setFzfje(commomVo.getCharge22());
        //综合医疗服务类：（1）一般医疗服务费
        hisba1.setFzhfwlylf(commomVo.getCharge10());
        //综合医疗服务类：（2）一般治疗操作费FZHFWLCZF
        hisba1.setFzhfwlczf(commomVo.getCharge25());
        //综合医疗服务类：（3）护理费
        hisba1.setFzhfwlhlf(commomVo.getCharge02());
        //综合医疗服务类：（4）其他费用
        hisba1.setFzhfwlqtf(commomVo.getCharge20());
        //诊断类：(5) 病理诊断费
        hisba1.setFzdlblf(commomVo.getCharge29());
        //诊断类：(6) 实验室诊断费
        hisba1.setFzdlsssf(commomVo.getCharge30());
        //诊断类：(7) 影像学诊断费
        hisba1.setFzdlyxf(commomVo.getCharge31());
        //诊断类：(8) 临床诊断项目费
        hisba1.setFzdllcf(commomVo.getCharge32());
        //治疗类：(9) 非手术治疗项目费
        hisba1.setFzllffssf(commomVo.getCharge33());
        //治疗类：非手术治疗项目费 其中临床物理治疗费
        hisba1.setFzllfwlzwlf(commomVo.getCharge34());
        //治疗类：(10) 手术治疗费
        hisba1.setFzllfssf(commomVo.getCharge35());
        //治疗类：手术治疗费 其中麻醉费
        hisba1.setFzllfmzf(commomVo.getCharge14());
        //治疗类：手术治疗费 其中手术费
        hisba1.setFzllfsszlf(commomVo.getCharge19());
        //康复类：(11) 康复费
        hisba1.setFkflkff(commomVo.getCharge36());
        //中医类：中医治疗类
        hisba1.setFzylzf(commomVo.getCharge38());
        //西药类： 西药费 其中抗菌药物费用
        hisba1.setFxylgjf(commomVo.getCharge37());
        //血液和血液制品类： 血费
        hisba1.setFxylxf(commomVo.getCharge09());
        //血液和血液制品类： 白蛋白类制品费
        hisba1.setFxylbqbf(commomVo.getCharge39());
        //血液和血液制品类： 球蛋白制品费
        hisba1.setFxylqdbf(commomVo.getCharge40());
        //血液和血液制品类：凝血因子类制品费
        hisba1.setFxylyxyzf(commomVo.getCharge41());
        //血液和血液制品类： 细胞因子类费
        hisba1.setFxylxbyzf(commomVo.getCharge42());
        //耗材类：检查用一次性医用材料费
        hisba1.setFhclcjf(commomVo.getCharge43());
        //耗材类：治疗用一次性医用材料费
        hisba1.setFhclzlf(commomVo.getCharge44());
        //耗材类：手术用一次性医用材料费
        hisba1.setFhclssf(commomVo.getCharge45());
        if(null == hisba1.getFid()){
            hisba1.setFifinput(false);
            hisba1Mapper.insert(hisba1);
        }else{
            hisba1Mapper.updateByPrimaryKey(hisba1);
        }
    }

    //插入HIS_BA2 HIS_病人转科情况
    @DBSource("salve")
    public void insertHisBa2(CommomVo commomVo,List<Emr_Dictionary> dictionarys) throws Exception{
        if(StringUtils.isNotBlank(commomVo.getChangeDept1()) || StringUtils.isNotBlank(commomVo.getChangeDept2()) || StringUtils.isNotBlank(commomVo.getChangeDept3())){
            String inpatientNo = commomVo.getInpatientNo();
            Short admissTimes = commomVo.getAdmissTimes();
            HISBA2 obj1 = new HISBA2();
            HISBA2 obj2 = new HISBA2();
            HISBA2 obj3 = new HISBA2();
            String deptCode1 = "";
            String deptCode2 = "";
            String deptCode3 = "";
            //科室字典集合
            Map<String,String> deptMap = new HashMap<>();
            if(null != dictionarys && !dictionarys.isEmpty()){
                for(Emr_Dictionary dictionary : dictionarys){
                    //筛选科室字典
                    selectDictionaryByTypeCode("dept_code",deptMap,dictionary);
                }
            }
            String deptName1 = commomVo.getChangeDept1();
            String deptName2 = commomVo.getChangeDept2();
            String deptName3 = commomVo.getChangeDept3();
            if(null != deptMap && !deptMap.isEmpty()){
                for(Map.Entry<String,String> keyMap : deptMap.entrySet()){
                    if(StringUtils.isNotBlank(deptName1) && keyMap.getValue().equals(deptName1)){
                        deptCode1 = keyMap.getKey();
                    }
                    if(StringUtils.isNotBlank(deptName2) && keyMap.getValue().equals(deptName2)){
                        deptCode2 = keyMap.getKey();
                    }
                    if(StringUtils.isNotBlank(deptName3) && keyMap.getValue().equals(deptName3)){
                        deptCode3 = keyMap.getKey();
                    }
                }
            }
            List<HISBA2> insertList = new ArrayList<>();
            List<HISBA2> updateList = new ArrayList<>();
            List<HISBA2> deleteList = new ArrayList<>();
            List<HISBA2> hisba2s = hisba2Mapper.selectAllByParam(inpatientNo, Integer.valueOf(admissTimes));
            if(StringUtils.isNotBlank(deptName1)){
                obj1.setFprn(inpatientNo);
                obj1.setFtimes(Integer.valueOf(admissTimes));
                obj1.setFzktykh(deptCode1);
                obj1.setFzkdept(deptName1);
                String changeDate1 = commomVo.getChangeDate1();
                if(StringUtils.isNotBlank(changeDate1)){
                    obj1.setFzkdate(fmt.parse(changeDate1));
                    obj1.setFzktime(changeDate1);
                }
            }
            if(StringUtils.isNotBlank(deptName2)){
                obj2.setFprn(inpatientNo);
                if(null != admissTimes){
                    obj2.setFtimes(Integer.valueOf(admissTimes));
                }
                obj2.setFzktykh(deptCode2);
                obj2.setFzkdept(deptName2);
                String changeDate2 = commomVo.getChangeDate2();
                if(StringUtils.isNotBlank(changeDate2)){
                    obj2.setFzkdate(fmt.parse(changeDate2));
                    obj2.setFzktime(changeDate2);
                }
            }
            if(StringUtils.isNotBlank(deptName3)){
                obj3.setFprn(inpatientNo);
                if(null != admissTimes){
                    obj3.setFtimes(Integer.valueOf(admissTimes));
                }
                obj3.setFzktykh(deptCode3);
                obj3.setFzkdept(deptName3);
                String changeDate3 = commomVo.getChangeDate3();
                if(StringUtils.isNotBlank(changeDate3)){
                    obj3.setFzkdate(fmt.parse(changeDate3));
                    obj3.setFzktime(changeDate3);
                }
            }
            //定义是否存在
            Boolean flag1 = false;
            Boolean flag2 = false;
            Boolean flag3 = false;
            if(null != hisba2s && !hisba2s.isEmpty()){
                for(HISBA2 his2 : hisba2s){
                    //判断存在则更新，不存在则新增
                    if(StringUtils.isNotBlank(deptName1)){
                        if(his2.getFzkdept().equals(deptName1)){
                            flag1 = true;
                            obj1.setFid(his2.getFid());
                            BeanUtils.copyProperties(obj1,his2);
                            updateList.add(his2);
                        }
                    }
                    if(StringUtils.isNotBlank(deptName2)){
                        if(his2.getFzkdept().equals(deptName2)){
                            flag2 = true;
                            obj2.setFid(his2.getFid());
                            BeanUtils.copyProperties(obj2,his2);
                            updateList.add(his2);
                        }
                    }
                    if(StringUtils.isNotBlank(deptName3)){
                        if(his2.getFzkdept().equals(deptName3)){
                            flag3 = true;
                            obj3.setFid(his2.getFid());
                            BeanUtils.copyProperties(obj3,his2);
                            updateList.add(his2);
                        }
                    }
                    if(!flag1 && !flag2 && !flag3){
                        deleteList.add(his2);
                    }
                }
                //不存在，全部新增
                if(StringUtils.isNotBlank(obj1.getFzkdept()) && !flag1){
                    insertList.add(obj1);
                }
                if(StringUtils.isNotBlank(obj2.getFzkdept()) && !flag2) {
                    insertList.add(obj2);
                }
                if(StringUtils.isNotBlank(obj3.getFzkdept()) && !flag3) {
                    insertList.add(obj3);
                }
            }else{
                //不存在，全部新增
                if(null != obj1){
                    insertList.add(obj1);
                }
                if(null != obj2) {
                    insertList.add(obj2);
                }
                if(null != obj3) {
                    insertList.add(obj3);
                }
            }
            //批量添加
            if(null != insertList && !insertList.isEmpty()){
                hisba2Mapper.SimpleInsert(insertList);
            }
            //批量更新
            if(null != updateList && !updateList.isEmpty()){
                hisba2Mapper.SimpleUpdate(updateList);
            }
            //批量添加
            if(null != deleteList && !deleteList.isEmpty()){
                String ids = "";
                for (int i = 0; i < deleteList.size(); i++) {
                    if(null != deleteList.get(i).getFid()){
                        ids +=  deleteList.get(i).getFid() + ",";
                    }
                }
                if(StringUtils.isNotBlank(ids)){
                    ids = ids.substring(0,ids.length()-1);
                    hisba2Mapper.SimpleDelete(ids);
                }

            }
        }
    }
    //筛选字典
    private void selectDictionaryByTypeCode(String typeCode,Map<String,String> map,Emr_Dictionary dictionary){
        String typecode = dictionary.getTypecode();
        if(typeCode.equals(typecode)){
            map.put(dictionary.getCode(),dictionary.getName());
        }
    }

    //字典转换，由中文转编码
    private String nameConvertCode(Map<String,String> map,String name){
        if(null != map && !map.isEmpty()){
            for(Map.Entry<String,String> keyMap : map.entrySet()){
                if(keyMap.getValue().equals(name)){
                    return keyMap.getKey();
                }
            }
        }
        return "";
    }

    //姓名name转换工号userName
    private String nameConvertUserName(List<User> userList,String userName){
        if(StringUtils.isNotBlank(userName)){
            if(null != userList && !userList.isEmpty()){
                for (User user : userList) {
                    if(user.getName().equals(userName)){
                        return user.getName();
                    }
                }
            }
        }
        return null;
    }
    //插入HIS_BA3 病人诊断信息
    @DBSource("salve")
    public void insertHisBa3(CommomVo commomVo,List<Emr_Dictionary> dictionarys,List<T_Diag> diags) throws Exception{
        String inpatientNo = commomVo.getInpatientNo();
        Short admissTimes = commomVo.getAdmissTimes();
        //先删除后新增
        hisba3Mapper.deleteAllByParam(inpatientNo,Integer.valueOf(admissTimes));
        //批量新增
        List<HISBA3> insertList = new ArrayList<>();
        if(null != diags && !diags.isEmpty()){
            for (T_Diag diag : diags) {
                HISBA3 hisObj = new HISBA3();
                //病案号
                hisObj.setFprn(inpatientNo);
                //次数
                if(null != admissTimes){
                    hisObj.setFtimes(Integer.valueOf(admissTimes));
                }
                //诊断类型
                hisObj.setFzdlx(diag.getDiagType());
                //ICD版本
                hisObj.setFicdversion(Byte.valueOf(icdVersion));
                //ICD码
                hisObj.setFicdm(diag.getDiagCode());
                //疾病名称
                hisObj.setFjbname(diag.getDiagName());
                //入院病情编号
                String inhospstat = diag.getInhospstat();
                if(StringUtils.isNotBlank(inhospstat)){
                    hisObj.setFrybqbh(inhospstat);
                    //入院病情
                    //入院病情集合
                    Map<String,String> admissThingMap = new HashMap<>();
                    if(null != dictionarys && !dictionarys.isEmpty()){
                        for(Emr_Dictionary dictionary : dictionarys){
                            //筛选入院病情字典
                            selectDictionaryByTypeCode("admiss_thing",admissThingMap,dictionary);
                        }
                    }
                    if(null != admissThingMap && !admissThingMap.isEmpty()){
                        for(Map.Entry<String,String> keyMap : admissThingMap.entrySet()){
                            if(keyMap.getKey().equals(inhospstat)){
                                hisObj.setFrybq(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //逐个添加进集合
                insertList.add(hisObj);
            }
            hisba3Mapper.SimpleInsert(insertList);
        }

    }
    //插入HIS_BA4 病人手术信息
    @DBSource("salve")
    public void insertHisBa4(CommomVo commomVo,List<Emr_Dictionary> dictionarys,List<T_Operate> operates,List<User> userList) throws Exception{
        String inpatientNo = commomVo.getInpatientNo();
        Short admissTimes = commomVo.getAdmissTimes();
        //先删除后新增
        hisba4Mapper.deleteAllByParam(inpatientNo,Integer.valueOf(admissTimes));
        //批量新增
        List<HISBA4> insertList = new ArrayList<>();
        if(null != operates && !operates.isEmpty()){
            //切口集合
            Map<String,String> cutCodeMap = new HashMap<>();
            //愈合集合
            Map<String,String> healCodeMap = new HashMap<>();
            //科室集合
            Map<String,String> deptMap = new HashMap<>();
            //麻醉方式集合
            Map<String,String> anaesthesiaMap = new HashMap<>();
            //是否择期集合
            Map<String,String> whetherMap = new HashMap<>();
            //手术级别集合
            Map<String,String> operateClassMap = new HashMap<>();
            //切口/愈合等级集合
            Map<String,String> cutHealGradeMap = new HashMap<>();
            if(null != dictionarys && !dictionarys.isEmpty()){
                for(Emr_Dictionary dictionary : dictionarys){
                    //筛选切口字典
                    selectDictionaryByTypeCode("cut_code",cutCodeMap,dictionary);
                    //筛选愈合等级字典
                    selectDictionaryByTypeCode("heal_code",healCodeMap,dictionary);
                    //筛选科室字典
                    selectDictionaryByTypeCode("dept_code",deptMap,dictionary);
                    //筛选麻醉方式字典
                    selectDictionaryByTypeCode("anaesthesia",anaesthesiaMap,dictionary);
                    //筛选是否择期手术字典
                    selectDictionaryByTypeCode("whether",whetherMap,dictionary);
                    //筛选是否择期手术字典
                    selectDictionaryByTypeCode("operate_class",operateClassMap,dictionary);
                    //切口/愈合等级字典
                    selectDictionaryByTypeCode("cut_heal_grade",cutHealGradeMap,dictionary);
                }
            }
            for (T_Operate operate : operates) {
                HISBA4 hisObj = new HISBA4();
                //病案号
                hisObj.setFprn(inpatientNo);
                //次数
                if(null != admissTimes){
                    hisObj.setFtimes(Integer.valueOf(admissTimes));
                }
                //病人姓名
                hisObj.setFname(commomVo.getName());
                //手术次数
                if(null != operate.getOperateNo()){
                    hisObj.setFoptimes(Integer.valueOf(operate.getOperateNo()));
                }
                //手术码
                hisObj.setFopcode(operate.getOperateCode());
                //手术码对应名称
                hisObj.setFop(operate.getOperateName());
                //手术日期dateTime
                hisObj.setFopdate(operate.getOperateDate());
                //切口编号
                String cut = operate.getCut();
                //I/甲
                if(StringUtils.isNotBlank(cut)){
                    if (null != cutHealGradeMap && !cutHealGradeMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : cutHealGradeMap.entrySet()) {
                            if (keyMap.getKey().equals(cut)) {
                                cut = keyMap.getValue();
                                break;
                            }
                        }
                    }
                    if(cut.contains("/")) {
                        String[] cuts = cut.split("/");
                        if (StringUtils.isNotBlank(cuts[0])) {
                            //切口
                            hisObj.setFqiekou(cuts[0]);
                            if (null != cutCodeMap && !cutCodeMap.isEmpty()) {
                                for (Map.Entry<String, String> keyMap : cutCodeMap.entrySet()) {
                                    if (keyMap.getValue().equals(cuts[0])) {
                                        //切口编号
                                        hisObj.setFqiekoubh(keyMap.getKey());
                                        break;
                                    }
                                }
                            }
                        }
                        if (StringUtils.isNotBlank(cuts[1])) {
                            //愈合
                            hisObj.setFyuhe(cuts[1]);
                            //愈合编号
                            if (null != healCodeMap && !healCodeMap.isEmpty()) {
                                for (Map.Entry<String, String> keyMap : healCodeMap.entrySet()) {
                                    if (keyMap.getValue().equals(cuts[1])) {
                                        hisObj.setFyuhebh(keyMap.getKey());
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                //手术医生编号
                String operator = operate.getOperator();
                String operatorUserName = "";
                //手术医生所属科室编号
                String operatorDeptCode = "";
                //I助编号
                String assistant1 = operate.getAssistant1();
                String assistant1UserName = "";
                //II助编号
                String assistant2 = operate.getAssistant2();
                String assistant2UserName = "";
                //麻醉医生编号
                String anaesthesiaName = operate.getAnaesthesiaName();
                String anaesthesiaNameUserName = "";
                if(null != userList && !userList.isEmpty()){
                    for (User user : userList) {
                        if(StringUtils.isNotBlank(user.getName())) {
                            //手术医生编号
                            if (StringUtils.isNotBlank(operator) && user.getName().equals(operator)) {
                                operatorUserName = user.getUserName();
                                operatorDeptCode = user.getDeptCode();
                            }
                            //I助编号
                            if (StringUtils.isNotBlank(assistant1) && user.getName().equals(assistant1)) {
                                assistant1UserName = user.getUserName();
                            }
                            //II助编号
                            if (StringUtils.isNotBlank(assistant2) && user.getName().equals(assistant2)) {
                                assistant2UserName = user.getUserName();
                            }
                            //麻醉医生编号
                            if (StringUtils.isNotBlank(anaesthesiaName) && user.getName().equals(anaesthesiaName)) {
                                anaesthesiaNameUserName = user.getUserName();
                            }
                        }
                    }
                }
                //手术医生编号
                hisObj.setFdocbh(operatorUserName);
                //手术医生
                hisObj.setFdocname(operator);
                //麻醉方式编号
                String anaesthesiaType = operate.getAnaesthesiaType();
                if(StringUtils.isNotBlank(anaesthesiaType)){
                    hisObj.setFmazuibh(anaesthesiaType);
                    //麻醉方式
                    if (null != anaesthesiaMap && !anaesthesiaMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : anaesthesiaMap.entrySet()) {
                            if (keyMap.getKey().equals(anaesthesiaType)) {
                                hisObj.setFmazui(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //I助编号
                hisObj.setFopdoct1bh(assistant1UserName);
                //I助姓名
                hisObj.setFopdoct1(assistant1);
                //II助编号
                hisObj.setFopdoct2bh(assistant2UserName);
                //II助姓名
                hisObj.setFopdoct2(assistant1);
                //麻醉医生编号
                hisObj.setFmzdoctbh(anaesthesiaNameUserName);
                //麻醉医生
                hisObj.setFmzdoct(anaesthesiaName);
                //择期手术编号
                String chossurg = operate.getChossurg();
                if(StringUtils.isNotBlank(chossurg)){
                    hisObj.setFzqssbh(chossurg);
                    //择期手术
                    if (null != whetherMap && !whetherMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : whetherMap.entrySet()) {
                            if (keyMap.getKey().equals(chossurg)) {
                                hisObj.setFzqss(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //手术级别编号
                String operateClass = operate.getOperateClass();
                if(StringUtils.isNotBlank(operateClass)){
                    hisObj.setFssjbbh(operateClass);
                    //手术级别
                    if (null != operateClassMap && !operateClassMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : operateClassMap.entrySet()) {
                            if (keyMap.getKey().equals(operateClass)) {
                                hisObj.setFssjb(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                if(StringUtils.isNotBlank(operator)){
                    if(StringUtils.isNotBlank(operatorDeptCode)){
                        //手术医生所在科室名称
                        if (null != deptMap && !deptMap.isEmpty()) {
                            for (Map.Entry<String, String> keyMap : deptMap.entrySet()) {
                                if (keyMap.getKey().equals(operatorDeptCode)) {
                                    hisObj.setFopksname(keyMap.getValue());
                                    break;
                                }
                            }
                        }
                        //手术医生所在科室编号
                        hisObj.setFoptykh(operatorDeptCode);
                    }
                }
                //逐个添加进集合
                insertList.add(hisObj);
            }
            hisba4Mapper.SimpleInsert(insertList);
        }
    }
    //插入HIS_BA5 妇婴卡
    @DBSource("salve")
    public void insertHisBa5(CommomVo commomVo,List<Emr_Dictionary> dictionarys, List<TBaby> babies) throws Exception {
        String inpatientNo = commomVo.getInpatientNo();
        Short admissTimes = commomVo.getAdmissTimes();
        //先删除后新增
        hisba5Mapper.deleteAllByParam(inpatientNo, Integer.valueOf(admissTimes));
        //批量新增
        List<HISBA5> insertList = new ArrayList<>();
        if (null != babies && !babies.isEmpty()) {
            //婴儿性别集合
            Map<String, String> sexMap = new HashMap<>();
            //分娩结果集合
            Map<String, String> childbirthResultMap = new HashMap<>();
            //婴儿转归集合
            Map<String, String> childStatusMap = new HashMap<>();
            //呼吸集合
            Map<String, String> breathMap = new HashMap<>();
            if (null != dictionarys && !dictionarys.isEmpty()) {
                for (Emr_Dictionary dictionary : dictionarys) {
                    //筛选婴儿性别字典
                    selectDictionaryByTypeCode("sex_code", sexMap, dictionary);
                    //筛选分娩结果字典
                    selectDictionaryByTypeCode("childbirthResult_code", childbirthResultMap, dictionary);
                    //筛选婴儿转归字典
                    selectDictionaryByTypeCode("childStatus_code", childStatusMap, dictionary);
                    //筛选呼吸字典
                    selectDictionaryByTypeCode("breath_code", breathMap, dictionary);
                }
            }
            for (int i = 0; i < babies.size(); i++) {
                TBaby baby = babies.get(i);
                HISBA5 hisObj = new HISBA5();
                //病案号
                hisObj.setFprn(inpatientNo);
                //次数
                if(null != admissTimes){
                    hisObj.setFtimes(Integer.valueOf(admissTimes));
                }
                //婴儿序号
                hisObj.setFbabynum((i + 1));
                //病人姓名
                hisObj.setFname(commomVo.getName());
                //婴儿性别编号
                String sex = baby.getSex();
                if (StringUtils.isNotBlank(sex)) {
                    hisObj.setFbabysexbh(sex);
                    //婴儿性别
                    if (null != sexMap && !sexMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : sexMap.entrySet()) {
                            if (keyMap.getKey().equals(sex)) {
                                hisObj.setFbabysex(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //婴儿体重
                hisObj.setFtz(baby.getAvoirdupois().doubleValue());
                //分娩结果编号
                String childbirthMothed = baby.getChildbirthMothed();
                if (StringUtils.isNotBlank(childbirthMothed)) {
                    hisObj.setFresultbh(childbirthMothed);
                    //分娩结果
                    if (null != childbirthResultMap && !childbirthResultMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : childbirthResultMap.entrySet()) {
                            if (keyMap.getKey().equals(childbirthMothed)) {
                                hisObj.setFresult(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //转归编号
                String diagType = baby.getDiagType();
                if (StringUtils.isNotBlank(diagType)) {
                    hisObj.setFzgbh(diagType);
                    //转归
                    if (null != childStatusMap && !childStatusMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : childStatusMap.entrySet()) {
                            if (keyMap.getKey().equals(diagType)) {
                                hisObj.setFzg(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //婴儿抢救成功次数
                hisObj.setFbabysuc(Integer.valueOf(baby.getRescueTimes()));
                //呼吸编号
                String breathing = baby.getBreathing();
                if (StringUtils.isNotBlank(breathing)) {
                    hisObj.setFhxbh(breathing);
                    //呼吸
                    if (null != breathMap && !breathMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : breathMap.entrySet()) {
                            if (keyMap.getKey().equals(breathing)) {
                                hisObj.setFhx(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //逐个添加进集合
                insertList.add(hisObj);
            }
            hisba5Mapper.SimpleInsert(insertList);
        }
    }
    //插入HIS_BA6 肿瘤卡
    @DBSource("salve")
    public void insertHisBa6(CommomVo commomVo,List<Emr_Dictionary> dictionarys) throws Exception{
        String inpatientNo = commomVo.getInpatientNo();
        Short admissTimes = commomVo.getAdmissTimes();
        HISBA6 hisObj = new HISBA6();
        List<HISBA6> hisba6s = hisba6Mapper.selectAllByParam(inpatientNo, admissTimes);
        if(null != hisba6s && !hisba6s.isEmpty()){
            hisObj = hisba6s.get(0);
        }
        //放疗方式集合
        Map<String, String> radiotherapyTypeMap = new HashMap<>();
        //放疗程式集合
        Map<String, String> radiotherapyProgramMap = new HashMap<>();
        //放疗装置集合
        Map<String, String> radiotherapyDeviceMap = new HashMap<>();
        //化疗方式集合
        Map<String, String> chemotherapyTypeMap = new HashMap<>();
        //化疗方法集合
        Map<String, String> chemotherapyMethodMap = new HashMap<>();
        if (null != dictionarys && !dictionarys.isEmpty()) {
            for (Emr_Dictionary dictionary : dictionarys) {
                //筛选放疗方式字典
                selectDictionaryByTypeCode("radiotherapy_type_code", radiotherapyTypeMap, dictionary);
                //筛选放疗程式字典
                selectDictionaryByTypeCode("radiotherapy_program_code", radiotherapyProgramMap, dictionary);
                //筛选放疗装置字典
                selectDictionaryByTypeCode("radiotherapy_device_code", radiotherapyDeviceMap, dictionary);
                //筛选化疗方式字典
                selectDictionaryByTypeCode("chemotherapy_type_code", chemotherapyTypeMap, dictionary);
                //筛选化疗方法字典
                selectDictionaryByTypeCode("chemotherapy_method_code", chemotherapyMethodMap, dictionary);
            }
        }
        //病案号
        hisObj.setFprn(inpatientNo);
        //次数
        hisObj.setFtimes(Integer.valueOf(admissTimes));
        //放疗方式
        String radiotherapyType = commomVo.getRadiotherapyType();
        if(StringUtils.isNotBlank(radiotherapyType)){
            hisObj.setFflfs(radiotherapyType);
            //放疗方式编号
            if (null != radiotherapyTypeMap && !radiotherapyTypeMap.isEmpty()) {
                for (Map.Entry<String, String> keyMap : radiotherapyTypeMap.entrySet()) {
                    if (keyMap.getValue().equals(radiotherapyType)) {
                        hisObj.setFflfsbh(keyMap.getKey());
                        break;
                    }
                }
            }
        }
        String radiotherapyProgram = commomVo.getRadiotherapyProgram();
        if(StringUtils.isNotBlank(radiotherapyProgram)){
            //放疗程式
            hisObj.setFflcx(radiotherapyProgram);
            //放疗程式编号
            if (null != radiotherapyProgramMap && !radiotherapyProgramMap.isEmpty()) {
                for (Map.Entry<String, String> keyMap : radiotherapyProgramMap.entrySet()) {
                    if (keyMap.getValue().equals(radiotherapyProgram)) {
                        hisObj.setFflcxbh(keyMap.getKey());
                        break;
                    }
                }
            }
        }
        //放疗装置
        String radiotherapyDevice = commomVo.getRadiotherapyDevice();
        if(StringUtils.isNotBlank(radiotherapyDevice)){
            hisObj.setFflzz(radiotherapyDevice);
            //放疗装置编号
            if (null != radiotherapyDeviceMap && !radiotherapyDeviceMap.isEmpty()) {
                for (Map.Entry<String, String> keyMap : radiotherapyDeviceMap.entrySet()) {
                    if (keyMap.getValue().equals(radiotherapyDevice)) {
                        hisObj.setFflzzbh(keyMap.getKey());
                        break;
                    }
                }
            }
        }
        //肿瘤分期类型 T N M
        String tumorStagingType = commomVo.getTumorStagingType();
        if(StringUtils.isNotBlank(tumorStagingType)){
            //放疗服药天数
            Integer primaryTumorDay = commomVo.getPrimaryTumorDay();
            //放疗服药次数
            Integer primaryTumorTimes = commomVo.getPrimaryTumorTimes();
            //放疗服药剂量
            String primaryTumorDose = commomVo.getPrimaryTumorDose();
            //放疗服药起始时间
            String primaryTumorStarttime = commomVo.getPrimaryTumorStarttime();
            //放疗服药终止时间
            String primaryTumorEndtime = commomVo.getPrimaryTumorEndtime();

            if("T".equals(tumorStagingType)){
                //1.原发灶剂量
                if(StringUtils.isNotBlank(primaryTumorDose))
                hisObj.setFyjy(Integer.valueOf(primaryTumorDose));
                //原发灶次数
                hisObj.setFycs(primaryTumorTimes);
                //原发灶天数
                hisObj.setFyts(primaryTumorDay);
                //原发灶开始日期dateTime
                if(StringUtils.isNotBlank(primaryTumorStarttime)){
                    hisObj.setFyrq1(fmt.parse(primaryTumorStarttime));
                }
                //原发灶结束时间dateTime
                if(StringUtils.isNotBlank(primaryTumorEndtime)){
                    hisObj.setFyrq2(fmt.parse(primaryTumorEndtime));
                }
            }else if("N".equals(tumorStagingType)){
                //2.区域淋巴结剂量
                if(StringUtils.isNotBlank(primaryTumorDose))
                hisObj.setFqjy(Integer.valueOf(primaryTumorDose));
                //区域淋巴结次数
                hisObj.setFqcs(primaryTumorTimes);
                //区域淋巴结天数
                hisObj.setFqts(primaryTumorDay);
                //区域淋巴结开始时间dateTime
                if(StringUtils.isNotBlank(primaryTumorStarttime)){
                    hisObj.setFqrq1(fmt.parse(primaryTumorStarttime));
                }
                //区域淋巴结结束时间dateTime
                if(StringUtils.isNotBlank(primaryTumorEndtime)){
                    hisObj.setFqrq2(fmt.parse(primaryTumorEndtime));
                }
            }else if("M".equals(tumorStagingType)){
                //3.转移灶剂量
                if(StringUtils.isNotBlank(primaryTumorDose))
                hisObj.setFzjy(Integer.valueOf(primaryTumorDose));
                //转移灶次数
                hisObj.setFzcs(primaryTumorTimes);
                //转移灶天数
                hisObj.setFzts(primaryTumorDay);
                //转移灶开始时间dateTime
                if(StringUtils.isNotBlank(primaryTumorStarttime)){
                    hisObj.setFzrq1(fmt.parse(primaryTumorStarttime));
                }
                //转移灶结束时间dateTime
                if(StringUtils.isNotBlank(primaryTumorEndtime)){
                    hisObj.setFzrq2(fmt.parse(primaryTumorEndtime));
                }
            }
        }
        //化疗方式
        String chemotherapyType = commomVo.getChemotherapyType();
        if(StringUtils.isNotBlank(chemotherapyType)){
            hisObj.setFhlfs(chemotherapyType);
            //化疗方式编号
            if (null != chemotherapyTypeMap && !chemotherapyTypeMap.isEmpty()) {
                for (Map.Entry<String, String> keyMap : chemotherapyTypeMap.entrySet()) {
                    if (keyMap.getValue().equals(chemotherapyType)) {
                        hisObj.setFhlfsbh(keyMap.getKey());
                        break;
                    }
                }
            }
        }
        //化疗方法
        String chemotherapyMethod = commomVo.getChemotherapyMethod();
        if(StringUtils.isNotBlank(chemotherapyMethod)){
            hisObj.setFhlff(chemotherapyMethod);
            //化疗方法编号
            if (null != chemotherapyMethodMap && !chemotherapyMethodMap.isEmpty()) {
                for (Map.Entry<String, String> keyMap : chemotherapyMethodMap.entrySet()) {
                    if (keyMap.getValue().equals(chemotherapyMethod)) {
                        hisObj.setFhlffbh(keyMap.getKey());
                        break;
                    }
                }
            }
        }
        if(null == hisObj.getFid()){
            //新增
            hisba6Mapper.insert(hisObj);
        }else{
            //修改
            hisba6Mapper.updateByPrimaryKey(hisObj);
        }
    }
    //插入HIS_BA7 化疗卡
    @DBSource("salve")
    public void insertHisBa7(CommomVo commomVo,List<Emr_Dictionary> dictionarys,List<TTumourChemotherapy> tTumourChemotherapies) throws Exception{
        String inpatientNo = commomVo.getInpatientNo();
        Short admissTimes = commomVo.getAdmissTimes();
        //先删除后新增
        hisba7Mapper.deleteAllByParam(inpatientNo,Integer.valueOf(admissTimes));
        //批量新增
        List<HISBA7> insertList = new ArrayList<>();
        if(null != tTumourChemotherapies && !tTumourChemotherapies.isEmpty()){
            //化疗疗效集合
            Map<String, String> curativeEffectMap = new HashMap<>();
            if (null != dictionarys && !dictionarys.isEmpty()) {
                for (Emr_Dictionary dictionary : dictionarys) {
                    //筛选放疗方式字典
                    selectDictionaryByTypeCode("curative_effect_code", curativeEffectMap, dictionary);
                }
            }
            for (TTumourChemotherapy tumourChemotherapy : tTumourChemotherapies) {
                HISBA7 hisObj = new HISBA7();
                //病案号
                hisObj.setFprn(inpatientNo);
                //次数
                if(null != admissTimes)
                hisObj.setFtimes(Integer.valueOf(admissTimes));
                //化疗开始日期dateTime
                String startTime = tumourChemotherapy.getStartTime();
                if(StringUtils.isNotBlank(startTime)){
                    hisObj.setFhlrq1(fmt.parse(startTime));
                }
                //化疗结束日期dateTime
                String endTime = tumourChemotherapy.getEndTime();
                if(StringUtils.isNotBlank(endTime)){
                    hisObj.setFhlrq2(fmt.parse(endTime));
                }
                //化疗药物名称及剂量
                hisObj.setFhldrug(tumourChemotherapy.getDrugNameAndDose());
                //化疗疗程
                hisObj.setFhlproc(tumourChemotherapy.getTreatmentCourse());
                //疗效编号
                String curativeEffect = tumourChemotherapy.getCurativeEffect();
                if(StringUtils.isNotBlank(curativeEffect)){
                    hisObj.setFhllxbh(curativeEffect);
                    if (null != curativeEffectMap && !curativeEffectMap.isEmpty()) {
                        for (Map.Entry<String, String> keyMap : curativeEffectMap.entrySet()) {
                            if (keyMap.getKey().equals(curativeEffect)) {
                                //疗效
                                hisObj.setFhllx(keyMap.getValue());
                                break;
                            }
                        }
                    }
                }
                //逐个添加进集合
                insertList.add(hisObj);
            }
            hisba7Mapper.SimpleInsert(insertList);
        }
    }
}
