package cn.hnthyy.thmz.service.impl.his;

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.APatientMi;
import cn.hnthyy.thmz.entity.his.MzZyReq;
import cn.hnthyy.thmz.entity.his.ZyActpatient;
import cn.hnthyy.thmz.enums.BedStatusEnum;
import cn.hnthyy.thmz.enums.ResponceTypeEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.mapper.his.APatientMiMapper;
import cn.hnthyy.thmz.mapper.his.MzZyReqMapper;
import cn.hnthyy.thmz.mapper.his.PatientMiSfzMapper;
import cn.hnthyy.thmz.mapper.his.ZyActpatientMapper;
import cn.hnthyy.thmz.service.his.ZyActpatientService;
import cn.hnthyy.thmz.service.his.ZyConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
public class ZyActpatientServiceImpl implements ZyActpatientService {
    @SuppressWarnings("all")
    @Autowired
    private ZyActpatientMapper zyActpatientMapper;
    @SuppressWarnings("all")
    @Autowired
    private MzZyReqMapper mzZyReqMapper;
    @SuppressWarnings("all")
    @Autowired
    private APatientMiMapper aPatientMiMapper;
    @SuppressWarnings("all")
    @Autowired
    private PatientMiSfzMapper patientMiSfzMapper;
    @Autowired
    private ZyConfigService zyConfigService;

    @Override
    public int saveZyActpatient(ZyActpatient zyActpatient) {
        if(StringUtils.isBlank(zyActpatient.getTableName())){
            zyActpatient.setTableName("zy_actpatient");
        }
        return zyActpatientMapper.insertZyActpatient(zyActpatient);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int saveZyActpatientFull(ZyActpatient zyActpatient) throws MzException {
        APatientMi aPatientMiDb=aPatientMiMapper.selectAPatientByMzNo(zyActpatient.getMzNo());
        if(aPatientMiDb==null){
            throw new MzException("当前病人还未建档成功！");
        }
        if(aPatientMiDb.getLastadmissTimes()==null){
            aPatientMiDb.setLastadmissTimes(0);
        }
        aPatientMiDb.setLastadmissTimes(aPatientMiDb.getLastadmissTimes()+1);
        aPatientMiMapper.updateMzPatientMiByMzno(aPatientMiDb);
        zyActpatient.setChargeType(YesNoEnum.YES.code.toString());
        zyActpatient.setInpatientNo(aPatientMiDb.getInpatientNo());
        zyActpatient.setAdmissTimes(aPatientMiDb.getLastadmissTimes());
        zyActpatient.setName(aPatientMiDb.getName());
        zyActpatient.setOutpatientNo(aPatientMiDb.getOutpatientNo());
        zyActpatient.setVisitType(ResponceTypeEnum.SELF_PAYING.code);
        zyActpatient.setTimesBilled(1);
        zyActpatient.setBedStatus(BedStatusEnum.ADMISSION.code);
        zyActpatient.setZySerialNo(zyConfigService.getZySerialNo());
        zyActpatient.setResponceType(ResponceTypeEnum.SELF_PAYING.code);
        zyActpatient.setBalance(BigDecimal.ZERO);
        zyActpatient.setTotalCharge(BigDecimal.ZERO);
        zyActpatient.setCharge1(BigDecimal.ZERO);
        zyActpatient.setCharge2(BigDecimal.ZERO);
        zyActpatient.setCharge3(BigDecimal.ZERO);
        zyActpatient.setCharge4(BigDecimal.ZERO);
        zyActpatient.setCharge5(BigDecimal.ZERO);
        zyActpatient.setCharge6(BigDecimal.ZERO);
        zyActpatient.setCharge7(BigDecimal.ZERO);
        zyActpatient.setCharge8(BigDecimal.ZERO);
        zyActpatient.setCharge9(BigDecimal.ZERO);
        zyActpatient.setCharge10(BigDecimal.ZERO);
        zyActpatient.setCharge11(BigDecimal.ZERO);
        zyActpatient.setCharge12(BigDecimal.ZERO);
        zyActpatient.setCharge13(BigDecimal.ZERO);
        zyActpatient.setCharge14(BigDecimal.ZERO);
        zyActpatient.setCharge15(BigDecimal.ZERO);
        zyActpatient.setCharge16(BigDecimal.ZERO);
        zyActpatient.setCharge17(BigDecimal.ZERO);
        zyActpatient.setCharge18(BigDecimal.ZERO);
        zyActpatient.setCharge19(BigDecimal.ZERO);
        zyActpatient.setCharge20(BigDecimal.ZERO);
        zyActpatient.setCharge21(BigDecimal.ZERO);
        zyActpatient.setCharge22(BigDecimal.ZERO);
        zyActpatient.setCharge23(BigDecimal.ZERO);
        zyActpatient.setCharge24(BigDecimal.ZERO);
        zyActpatient.setCharge25(BigDecimal.ZERO);
        zyActpatient.setCharge26(BigDecimal.ZERO);
        zyActpatient.setCharge27(BigDecimal.ZERO);
        zyActpatient.setCharge28(BigDecimal.ZERO);
        zyActpatient.setCharge29(BigDecimal.ZERO);
        zyActpatient.setCharge30(BigDecimal.ZERO);
        zyActpatient.setDept(zyActpatient.getAdmissDept());
        zyActpatient.setWard(zyActpatient.getAdmissWard());
        zyActpatient.setSex(aPatientMiDb.getSex());
        zyActpatient.setTableName("zy_actpatient");
        int num= zyActpatientMapper.insertZyActpatient(zyActpatient);
        if(num>0){
            MzZyReq mzZyReq = new MzZyReq();
            mzZyReq.setPatientId(zyActpatient.getMzNo());
            mzZyReq.setConfirmId(zyActpatient.getOpIdCode());
            mzZyReq.setConfirmDate(zyActpatient.getAdmissDate());
            mzZyReq.setZySerialNo(zyActpatient.getZySerialNo());
            mzZyReq.setReqStatus(YesNoEnum.YES.code.toString());
            mzZyReqMapper.updateMzZyReq(mzZyReq);
        }
        return num;
    }

    @Override
    public int queryCountByInpatientNo(String inpatientNo) {
        return zyActpatientMapper.selectCountByInpatientNo(inpatientNo);
    }

    @Override
    public ZyActpatient queryByInpatientNo(String tableName,String inpatientNo) {
        return zyActpatientMapper.selectByInpatientNo(tableName,inpatientNo);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int modifyInpatientNo(APatientMi aPatientMi) {
        //根据老住院号查询的老住院病人信息
        APatientMi oldAPatientMi = aPatientMiMapper.selectPatientMiByInPatientNo(aPatientMi.getOldInpatientNo());
        //根据老住院号查询的老住院病人信息
        Map<String,Object> newAPatientMiMap = aPatientMiMapper.selectMaxLastadmissTimes(aPatientMi.getInpatientNo());
        Short lastTime =(Short) newAPatientMiMap.get("lastadmiss_times");
        if(lastTime==null){
            lastTime=1;
        }else {
            lastTime++;
        }
        APatientMi updateAPatientMi= new APatientMi();
        updateAPatientMi.setInpatientNo(aPatientMi.getInpatientNo());
        updateAPatientMi.setOutpatientNo(aPatientMi.getInpatientNo());
        updateAPatientMi.setName(aPatientMi.getName());
        updateAPatientMi.setLastadmissTimes(Integer.valueOf(lastTime));
        updateAPatientMi.setMzNo(oldAPatientMi.getMzNo());
        updateAPatientMi.setOldInpatientNo(aPatientMi.getOldInpatientNo());
        aPatientMiMapper.deleteByInpatientNo(aPatientMi.getInpatientNo());
        aPatientMiMapper.insertFromOtherItem(updateAPatientMi);
        if(oldAPatientMi.getLastadmissTimes()==1){
            aPatientMiMapper.deleteByInpatientNo(oldAPatientMi.getInpatientNo());
        }else if(oldAPatientMi.getLastadmissTimes()>1){
            APatientMi tempAPatientMi= new APatientMi();
            tempAPatientMi.setOldInpatientNo(aPatientMi.getOldInpatientNo());
            tempAPatientMi.setLastadmissTimes(oldAPatientMi.getLastadmissTimes()-1);
            tempAPatientMi.setMzNo("");
            aPatientMiMapper.updateMzPatientMiByInpatientNo(tempAPatientMi);
        }
        ZyActpatient zyActpatient = new ZyActpatient();
        zyActpatient.setInpatientNo(aPatientMi.getInpatientNo());
        zyActpatient.setAdmissTimes(Integer.valueOf(lastTime));
        zyActpatient.setName(aPatientMi.getName());
        zyActpatient.setOldInpatientNo(aPatientMi.getOldInpatientNo());
        zyActpatient.setOldAdmissTimes(oldAPatientMi.getLastadmissTimes());
        zyActpatient.setTableName("zy_actpatient");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_inactpatient");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setName(null);
        zyActpatient.setTableName("zy_bed_mi");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_adt");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_ledger_file");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_receipt");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_deposit_file");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_drug");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_detail_charge");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_detail_charge_yb");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_owe_file");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("zy_owe_change");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_act_order");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_erase_order");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_inact_order");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_yp_zy_order");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_zy_patient_fee");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_act_occ");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_print_rec");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yz_temperature");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("yp_zy_patient");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("op_record");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("ysh_yj_req");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("ysh_bl_record");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("ysh_hz_record");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("tjsb_ry_record");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatient.setTableName("tjsb_cy_record");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        zyActpatientMapper.updateYjJcPatient(aPatientMi.getInpatientNo(),aPatientMi.getOldInpatientNo());
        zyActpatient.setTableName("yb_zy_review_record");
        zyActpatientMapper.updateZyActpatient(zyActpatient);
        patientMiSfzMapper.deletePatientMiSfzByPatientId(oldAPatientMi.getMzNo());
        return 1;
    }

    @Override
    public List<ZyActpatient> queryZyActpatientByInpatientNoOrMzNO(ZyActpatient zyActpatient) {
        return zyActpatientMapper.selectZyActpatientByInpatientNoOrMzNO(zyActpatient);
    }
}
