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

import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.mz.MzZyReq;
import cn.hnthyy.thmz.entity.his.zy.*;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.BedStatusEnum;
import cn.hnthyy.thmz.enums.ResponceTypeEnum;
import cn.hnthyy.thmz.enums.SettleTypeEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.mapper.his.mz.MzZyReqMapper;
import cn.hnthyy.thmz.mapper.his.zy.PatientMiSfzMapper;
import cn.hnthyy.thmz.mapper.his.yz.YzActOrderMapper;
import cn.hnthyy.thmz.mapper.his.zy.*;
import cn.hnthyy.thmz.service.his.zy.ZyActpatientService;
import cn.hnthyy.thmz.service.his.zy.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.Date;
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;
    @SuppressWarnings("all")
    @Autowired
    private ZyAdtMapper zyAdtMapper;
    @SuppressWarnings("all")
    @Autowired
    private ZyBedMiMapper zyBedMiMapper;
    @SuppressWarnings("all")
    @Autowired
    private ZyLedgerFileMapper zyLedgerFileMapper;
    @SuppressWarnings("all")
    @Autowired
    private ZyWorkLogMapper zyWorkLogMapper;
    @SuppressWarnings("all")
    @Autowired
    private YzActOrderMapper yzActOrderMapper;
    @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 (zyActpatientMapper.selectCountByInpatientNo(aPatientMiDb.getInpatientNo()) > 0) {
            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.setInputDate(new Date());
        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);
            //病人流转入库
            ZyAdt zyAdt = new ZyAdt();
            zyAdt.setZySerialNo(zyActpatient.getZySerialNo());
            zyAdt.setInpatientNo(zyActpatient.getInpatientNo());
            zyAdt.setAdmissTimes(zyActpatient.getAdmissTimes());
            zyAdt.setTransTimes(0);
            zyAdt.setAdmissDate(zyActpatient.getAdmissDate());
            zyAdt.setCurrWard(zyActpatient.getWard());
            zyAdt.setCurrDept(zyActpatient.getDept());
            zyAdt.setCurrRoom(null);
            zyAdt.setCurrBed(zyActpatient.getBedNo());
            zyAdt.setMark("1");
            zyAdtMapper.deleteZyAdt(zyAdt.getInpatientNo(), zyAdt.getAdmissTimes());
            zyAdtMapper.inserZyAdt(zyAdt);
            //更新床号记录
            ZyBedMi zyBedMi = new ZyBedMi();
            zyBedMi.setBedStatus(ZyBedMi.BED_STATUS_FREE);
            zyBedMi.setInpatientNo(zyActpatient.getInpatientNo());
            zyBedMi.setAdmissTimes(zyActpatient.getAdmissTimes());
            zyBedMi.setDeptCode(zyActpatient.getDept());
            zyBedMi.setWardCode(zyActpatient.getDept());
            zyBedMi.setBedNo(zyActpatient.getBedNo());
            zyBedMiMapper.updateZyBedMi(zyBedMi);
            //入库账页信息
            ZyLedgerFile zyLedgerFile = new ZyLedgerFile();
            zyLedgerFile.setInpatientNo(zyActpatient.getInpatientNo());
            zyLedgerFile.setAdmissTimes(zyActpatient.getAdmissTimes());
            zyLedgerFile.setLedgerSn(1);
            zyLedgerFile.setOpIdCode(zyActpatient.getOpIdCode());
            zyLedgerFile.setDeposit(BigDecimal.ZERO);
            zyLedgerFile.setBalance(BigDecimal.ZERO);
            zyLedgerFile.setTotalCharge(BigDecimal.ZERO);
            zyLedgerFile.setCharge1(BigDecimal.ZERO);
            zyLedgerFile.setCharge2(BigDecimal.ZERO);
            zyLedgerFile.setCharge3(BigDecimal.ZERO);
            zyLedgerFile.setCharge4(BigDecimal.ZERO);
            zyLedgerFile.setCharge5(BigDecimal.ZERO);
            zyLedgerFile.setCharge6(BigDecimal.ZERO);
            zyLedgerFile.setCharge7(BigDecimal.ZERO);
            zyLedgerFile.setCharge8(BigDecimal.ZERO);
            zyLedgerFile.setCharge9(BigDecimal.ZERO);
            zyLedgerFile.setCharge10(BigDecimal.ZERO);
            zyLedgerFile.setCharge11(BigDecimal.ZERO);
            zyLedgerFile.setCharge12(BigDecimal.ZERO);
            zyLedgerFile.setCharge13(BigDecimal.ZERO);
            zyLedgerFile.setCharge14(BigDecimal.ZERO);
            zyLedgerFile.setCharge15(BigDecimal.ZERO);
            zyLedgerFile.setCharge16(BigDecimal.ZERO);
            zyLedgerFile.setCharge17(BigDecimal.ZERO);
            zyLedgerFile.setCharge18(BigDecimal.ZERO);
            zyLedgerFile.setCharge19(BigDecimal.ZERO);
            zyLedgerFile.setCharge20(BigDecimal.ZERO);
            zyLedgerFile.setCharge21(BigDecimal.ZERO);
            zyLedgerFile.setCharge22(BigDecimal.ZERO);
            zyLedgerFile.setCharge23(BigDecimal.ZERO);
            zyLedgerFile.setCharge24(BigDecimal.ZERO);
            zyLedgerFile.setCharge25(BigDecimal.ZERO);
            zyLedgerFile.setCharge26(BigDecimal.ZERO);
            zyLedgerFile.setCharge27(BigDecimal.ZERO);
            zyLedgerFile.setCharge28(BigDecimal.ZERO);
            zyLedgerFile.setCharge29(BigDecimal.ZERO);
            zyLedgerFile.setCharge30(BigDecimal.ZERO);
            zyLedgerFile.setSettle(BigDecimal.ZERO);
            zyLedgerFile.setLastBalance(BigDecimal.ZERO);
            zyLedgerFile.setSettleType("0");
            zyLedgerFile.setDeptCode(zyActpatient.getDept());
            zyLedgerFile.setWardCode(zyActpatient.getWard());
            zyLedgerFile.setDeptX("");
            zyLedgerFile.setWardX("");
            zyLedgerFile.setAccountDate(null);
            zyLedgerFile.setCashDate(null);
            zyLedgerFile.setCashId("");
            zyLedgerFile.setDcountNo("");
            zyLedgerFile.setDcountDate(null);
            zyLedgerFile.setZySerialNo(zyActpatient.getZySerialNo());
            zyLedgerFileMapper.insetZyLedgerFile(zyLedgerFile);
        }
        return num;
    }

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

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

    @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);
    }

    @Override
    public Integer queryCountByInpatientNoOrMzNO(ZyActpatient zyActpatient) {
        return zyActpatientMapper.selectCountByInpatientNoOrMzNO(zyActpatient);
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int leaveHospital(String inpatientNo, Integer admissTimes, User tokenUser) throws MzException {
        if (StringUtils.isBlank(inpatientNo)) {
            throw new MzException("病人住院号为空！");
        }
        if (admissTimes == null) {
            throw new MzException("病人住院次数为空！");
        }
        //出院
        String sourceTableName = "zy_actpatient";
        String targetTableName = "zy_inactpatient";
        String inpatientNoBegin = inpatientNo + "$1";
        String inpatientNoEnd = inpatientNo + "$6";
        Date disDate = new Date();
        ZyActpatient sourceZyActpatient = zyActpatientMapper.selectByInpatientNo(sourceTableName, inpatientNo, admissTimes);
        zyActpatientMapper.updateZyActpatientForTransfer(sourceTableName, disDate, sourceZyActpatient.getBedNo(),sourceZyActpatient.getWard(), sourceZyActpatient.getDept(), tokenUser.getUserIdCode(), BedStatusEnum.LEAVE_HOSPITAL.code, inpatientNo, admissTimes);
        zyActpatientMapper.updateZyActpatientForTransferBaby(sourceTableName, disDate,sourceZyActpatient.getBedNo(), sourceZyActpatient.getWard(), sourceZyActpatient.getDept(), tokenUser.getUserIdCode(), BedStatusEnum.LEAVE_HOSPITAL.code, inpatientNoBegin, inpatientNoEnd, admissTimes);
        int num = zyActpatientMapper.transferZyActpatient(sourceTableName, targetTableName, inpatientNo, admissTimes);
        if (num == 0) {
            throw new MzException("数据转移失败！");
        }
        zyActpatientMapper.transferZyActpatientBaby(sourceTableName, targetTableName, inpatientNoBegin, inpatientNoEnd, admissTimes);
        zyAdtMapper.updateZyAdtForDis(disDate, sourceZyActpatient.getZkWard(), sourceZyActpatient.getDept(), sourceZyActpatient.getBedNo(), inpatientNo, admissTimes);
        zyBedMiMapper.clearZyBedMi(inpatientNo, admissTimes);
        ZyWorkLog zyWorkLog = new ZyWorkLog(inpatientNo, Long.valueOf(admissTimes));
        ZyLedgerFile zyLedgerFile = zyLedgerFileMapper.selectLastLedgerSn(inpatientNo, admissTimes);
        if (SettleTypeEnum.UNBALANCED_ACCOUNT.code.equals(zyLedgerFile.getSettleType())) {
            zyWorkLog.setLedgerSn(0L);
        } else {
            zyWorkLog.setLedgerSn(Long.valueOf(zyLedgerFile.getLedgerSn()));
        }
        zyWorkLog.setOpId(tokenUser.getUserIdCode());
        zyWorkLog.setOpDate(disDate);
        zyWorkLog.setLogType("6");
        zyWorkLog.setDeptCode(sourceZyActpatient.getDept());
        zyWorkLog.setWardCode(sourceZyActpatient.getWard());
        zyWorkLog.setName(tokenUser.getUserName());
        zyWorkLogMapper.insertZyWorkLog(zyWorkLog);
        zyActpatientMapper.deleteZyActpatientBaby(sourceTableName, inpatientNoBegin, inpatientNoEnd, admissTimes);
        return zyActpatientMapper.deleteZyActpatient(sourceTableName, inpatientNo, admissTimes);
    }



    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public int recall(String inpatientNo, Integer admissTimes, User tokenUser) throws MzException {
        if (StringUtils.isBlank(inpatientNo)) {
            throw new MzException("病人住院号为空！");
        }
        if (admissTimes == null) {
            throw new MzException("病人住院次数为空！");
        }
        //召回
        String sourceTableName = "zy_inactpatient";
        String targetTableName = "zy_actpatient";
        String yzActOrderSourceTableName = "yz_inact_order";
        String yzActOrderTargetTableName = "yz_act_order";
        String inpatientNoBegin = inpatientNo + "$1";
        String inpatientNoEnd = inpatientNo + "$6";
        ZyActpatient sourceZyActpatient = zyActpatientMapper.selectByInpatientNo(sourceTableName, inpatientNo, admissTimes);
        ZyBedMi zyBedMi=zyBedMiMapper.selectZyBedMi(sourceZyActpatient.getDept(),sourceZyActpatient.getWard(),sourceZyActpatient.getBedNo());
        String bedNo = zyBedMi.getBedNo();
        if(zyBedMi==null || !BedStatusEnum.ADMISSION.code.equals(zyBedMi.getBedStatus())){
            bedNo = zyBedMiMapper.selectMaxFreeBedNo(sourceZyActpatient.getDept(),sourceZyActpatient.getWard());
            if(StringUtils.isBlank(bedNo)){
                throw new MzException("病人需要召回的病房已无空闲床位，无法进行召回操作！");
            }
        }
        zyActpatientMapper.updateZyActpatientForTransfer(sourceTableName, sourceZyActpatient.getDisDate(), bedNo,null, null, sourceZyActpatient.getJobNurse(), BedStatusEnum.RECALL.code, inpatientNo, admissTimes);
        zyActpatientMapper.updateZyActpatientForTransferBaby(sourceTableName, sourceZyActpatient.getDisDate(),bedNo, null, null, sourceZyActpatient.getJobNurse(), BedStatusEnum.RECALL.code, inpatientNoBegin, inpatientNoEnd, admissTimes);
        int num = zyActpatientMapper.transferZyActpatient(sourceTableName, targetTableName, inpatientNo, admissTimes);
        if (num == 0) {
            throw new MzException("数据转移失败！");
        }
        zyActpatientMapper.transferZyActpatientBaby(sourceTableName, targetTableName, inpatientNoBegin, inpatientNoEnd, admissTimes);
        ZyBedMi newZyBedMi=new ZyBedMi(sourceZyActpatient.getWard(),sourceZyActpatient.getDept(),sourceZyActpatient.getBedNo());
        newZyBedMi.setInpatientNo(inpatientNo);
        newZyBedMi.setAdmissTimes(admissTimes);
        newZyBedMi.setBedStatus(BedStatusEnum.ARRANGE.code);
        zyBedMiMapper.updateZyBedMi(newZyBedMi);
        zyAdtMapper.updateForRecall(inpatientNo,admissTimes);
        yzActOrderMapper.transferYzActOrder(yzActOrderSourceTableName,yzActOrderTargetTableName,inpatientNo,admissTimes);
        yzActOrderMapper.transferYzActOrderBaby(yzActOrderSourceTableName,yzActOrderTargetTableName,inpatientNoBegin,inpatientNoEnd,admissTimes);
        yzActOrderMapper.deleteYzActOrder(yzActOrderSourceTableName,inpatientNo,admissTimes);
        yzActOrderMapper.deleteYzActOrderBaby(yzActOrderSourceTableName,inpatientNoBegin,inpatientNoEnd,admissTimes);
        zyActpatientMapper.deleteZyActpatientBaby(sourceTableName, inpatientNoBegin, inpatientNoEnd, admissTimes);
        ZyWorkLog zyWorkLog = new ZyWorkLog(inpatientNo, Long.valueOf(admissTimes));
        ZyLedgerFile zyLedgerFile = zyLedgerFileMapper.selectLastLedgerSn(inpatientNo, admissTimes);
        if (SettleTypeEnum.UNBALANCED_ACCOUNT.code.equals(zyLedgerFile.getSettleType())) {
            zyWorkLog.setLedgerSn(0L);
        } else {
            zyWorkLog.setLedgerSn(Long.valueOf(zyLedgerFile.getLedgerSn()));
        }
        zyWorkLog.setOpId(tokenUser.getUserIdCode());
        zyWorkLog.setOpDate(new Date());
        zyWorkLog.setLogType("5");
        zyWorkLog.setDeptCode(sourceZyActpatient.getDept());
        zyWorkLog.setWardCode(sourceZyActpatient.getWard());
        zyWorkLog.setName(tokenUser.getUserName());
        zyWorkLogMapper.insertZyWorkLog(zyWorkLog);
        return zyActpatientMapper.deleteZyActpatient(sourceTableName, inpatientNo, admissTimes);
    }
}
