package cn.hnthyy.thmz.controller.api;


import cn.hnthyy.thmz.Utils.DateUtil;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.haici.HaiciCharge;
import cn.hnthyy.thmz.entity.haici.Haicipat;
import cn.hnthyy.thmz.entity.his.mz.*;
import cn.hnthyy.thmz.entity.his.zd.ZdEmpTitle;
import cn.hnthyy.thmz.entity.his.zd.ZdMzClass;
import cn.hnthyy.thmz.entity.his.zd.ZdUnitCode;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.ConfirmFlagEnum;
import cn.hnthyy.thmz.enums.PayMarkEnum;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.pageDto.MzChargeDetailPageDto;
import cn.hnthyy.thmz.pageDto.MzyReqrecPageDto;
import cn.hnthyy.thmz.pageDto.ZdUnitCodePageDto;
import cn.hnthyy.thmz.service.his.mz.*;
import cn.hnthyy.thmz.service.his.zd.ZdEmpTitleService;
import cn.hnthyy.thmz.service.his.zd.ZdMzClassService;
import cn.hnthyy.thmz.service.his.zd.ZdUnitCodeService;
import cn.hnthyy.thmz.service.his.zy.APatientMiService;
import cn.hnthyy.thmz.service.thmz.UserService;
import cn.hnthyy.thmz.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/v1")
@Slf4j
public class MedicalViewApiController {
    @Autowired
    private MzPatientMiService mzPatientMiService;
    @Autowired
    private MzChargeDetailService mzChargeDetailService;
    @Autowired
    private MzDepositFileService mzDepositFileService;
    @Autowired
    private MzVisitTableService mzVisitTableService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private MzZdSupplyTypeService zdSupplyTypeService;
    @Autowired
    private ZdUnitCodeService zdUnitCodeService;
    @Autowired
    private MzBillItemService mzBillItemService;
    @Autowired
    private APatientMiService aPatientMiService;
    @Autowired
    private ZdMzClassService zdMzClassService;
    @Autowired
    private MzyRequestService mzyRequestService;
    @Autowired
    private ZdEmpTitleService zdEmpTitleService;
    @Autowired
    private MzyZdChargeTypeService mzyZdChargeTypeService;
    @Autowired
    private MzZdWorkTimeService mzZdWorkTimeService;
    @Autowired
    private MzyReqrecService mzyReqrecService;
    @Autowired
    private UserService userService;

    //海慈身份证类型
    private static final String ID_CARD_TYPE = "11";
    //诊疗卡类型
    private static final String MEDICAL_CARD_TYPE = "99";
    //海慈参数男性
    private static final String MAN = "M";
    //海慈参数女性
    private static final String WONMAN = "F";
    //海慈参数成年人
    private static final String PERSON = "1";
    //海慈参数儿童
    private static final String CHILD = "2";
    //微信支付
    private static final String WX = "WX";
    //支付宝
    private static final String ZFB = "ZFB";

    /**
     * 患者信息查询
     *
     * @param haicipat
     * @return
     */
    @RequestMapping(value = "/queryPatientInfo", method = {RequestMethod.POST})
    public Map<String, Object> queryPatientInfo(@RequestBody Haicipat haicipat) {
        Map<String, Object> results = new HashMap<>();
        if (StringUtils.isBlank(haicipat.getPatIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "顾客证件类型不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatIdNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "顾客证件号码不能为空");
            return results;
        }
        if (!Arrays.asList(ID_CARD_TYPE, MEDICAL_CARD_TYPE).contains(haicipat.getPatIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "顾客证件类型非身份证或者诊疗卡，暂不支持");
            return results;
        }
        List<MzPatientMi> mzPatientMis = null;
        if (ID_CARD_TYPE.equals(haicipat.getPatIdType())) {
            mzPatientMis = mzPatientMiService.queryBySocialNo(haicipat.getPatIdNo(), null);
        } else if (MEDICAL_CARD_TYPE.equals(haicipat.getPatIdType())) {
            MzPatientMi mzPatientMi = mzPatientMiService.queryByIcCardNo(haicipat.getPatIdNo());
            if (mzPatientMi != null) {
                mzPatientMis = Arrays.asList(mzPatientMi);
            }
        }
        if (mzPatientMis == null || mzPatientMis.size() == 0) {
            results.put("resultCode", -1);
            results.put("resultMessage", "没有查询到病人信息");
            return results;
        }
        results.put("resultCode", 0);
        MzPatientMi mzPatientMi = mzPatientMis.get(0);
        results.put("patName", mzPatientMi.getName());
        results.put("patSex", YesNoEnum.YES.code.equals(mzPatientMi.getSex()) ? MAN : WONMAN);
        results.put("patAge", mzPatientMi.getAge());
        results.put("patBirth", mzPatientMi.getBirthDayStr());
        results.put("patAddress", mzPatientMi.getAddress());
        results.put("patMobile", mzPatientMi.getPhoneNo());
        results.put("patIdType", YesNoEnum.YES.code);
        results.put("patIdNo", mzPatientMi.getSocialNo());
        results.put("patCardType", YesNoEnum.YES.code);
        results.put("patCardNo", mzPatientMi.getIcCardNo());
        results.put("patHisNo", mzPatientMi.getPatientId());
        List<String> inPatientNos = aPatientMiService.queryInPatientNoByMzNo(mzPatientMi.getPatientId());
        if (inPatientNos != null && inPatientNos.size() > 0) {
            results.put("inPatientNo", inPatientNos.get(0));
        } else {
            results.put("inPatientNo", "");
        }
        return results;
    }

    /**
     * 首诊患者建档
     *
     * @param haicipat
     * @return
     */
    @RequestMapping(value = "/savePatientInfo", method = {RequestMethod.POST})
    public Map<String, Object> savePatientInfo(@RequestBody Haicipat haicipat) {
        Map<String, Object> results = new HashMap<>();
        if (haicipat == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "建档参数为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者类型不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatName())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者姓名不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatSex())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者性别不能为空");
            return results;
        }
        if (haicipat.getPatAge() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者年龄不能为空");
            return results;
        }
        if (haicipat.getPatBirth() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者出生日期不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatAddress())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者地址不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatMobile())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者电话不能为空");
            return results;
        }
        if (PERSON.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getPatIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者证件类型不能为空");
            return results;
        }
        if (PERSON.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getPatIdNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者证件号码不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardName())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人姓名不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人证件类型不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardIdNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人证件号码不能为空");
            return results;
        }
        List<MzPatientMi> mzPatientMis = mzPatientMiService.queryBySocialNo(haicipat.getPatIdNo(), null);
        if (mzPatientMis != null && mzPatientMis.size() > 0) {
            results.put("resultCode", -1);
            results.put("resultMessage", "保存病人信息失败,已经存在相同的身份证号码记录");
            return results;
        }
        MzPatientMi mzPatientMi = new MzPatientMi();
        mzPatientMi.setName(haicipat.getPatName());
        mzPatientMi.setSex(MAN.equals(haicipat.getPatSex()) ? YesNoEnum.YES.code : 2);
        mzPatientMi.setAge(haicipat.getPatAge());
        mzPatientMi.setBirthDay(haicipat.getPatBirth());
        mzPatientMi.setAddress(haicipat.getPatAddress());
        mzPatientMi.setPhoneNo(haicipat.getPatMobile());
        mzPatientMi.setSocialNo(haicipat.getPatIdNo());
        mzPatientMi.setContractHz(haicipat.getGuardIdNo());
        mzPatientMi.setResponseType("01");
        try {
            int num = mzPatientMiService.saveMzPatientMi(mzPatientMi);
            if (num == 1) {
                results.put("resultCode", 0);
                results.put("resultMessage", "保存病人信息成功");
                results.put("patCardType", 1);
                results.put("data", mzPatientMi.getPatientId());
                return results;
            }
            results.put("resultCode", -1);
            results.put("resultMessage", "保存病人信息失败");
            return results;
        } catch (Exception e) {
            if (e instanceof DataIntegrityViolationException) {
                if (e.getMessage().contains("将截断字符串或二进制数据")) {
                    results.put("resultCode", -1);
                    results.put("resultMessage", "病人地址信息超长");
                    log.error("系统异常，错误信息{病人地址信息超长}");
                    return results;
                }
            }
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", StringUtils.isBlank(e.getMessage()) ? "系统出错，请联系管理员" : e.getMessage());
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 绑定通知
     *
     * @param haicipat
     * @return
     */
    @RequestMapping(value = "/boundPatCardNoInfo", method = {RequestMethod.POST})
    public Map<String, Object> boundPatCardNoInfo(@RequestBody Haicipat haicipat) {
        Map<String, Object> results = new HashMap<>();
        if (haicipat == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "绑定通知参数为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者类型不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatName())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者姓名不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatSex())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者性别不能为空");
            return results;
        }
        if (haicipat.getPatAge() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者年龄不能为空");
            return results;
        }
        if (haicipat.getPatBirth() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者出生日期不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatAddress())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者地址不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatMobile())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者电话不能为空");
            return results;
        }
        if (PERSON.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getPatIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者证件类型不能为空");
            return results;
        }
        if (PERSON.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getPatIdNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者证件号码不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardName())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人姓名不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人证件类型不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardIdNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人证件号码不能为空");
            return results;
        }

        MzPatientMi mzPatientMi = new MzPatientMi();
        mzPatientMi.setPatientId(haicipat.getPatCardNo());
        mzPatientMi.setIcCardNo(haicipat.getPatCardNo());
        try {
            //int num=mzPatientMiService.modifyMzPatientMi(mzPatientMi);
            int num = 1;
            if (num == 1) {
                results.put("resultCode", 0);
                results.put("resultMessage", "绑定病人信息成功");
                return results;
            }
            results.put("resultCode", -1);
            results.put("resultMessage", "绑定病人信息失败");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "绑定失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 取消绑定通知
     *
     * @param haicipat
     * @return
     */
    @RequestMapping(value = "/cancelBoundPatCardNoInfo", method = {RequestMethod.POST})
    public Map<String, Object> cancelBoundPatCardNoInfo(@RequestBody Haicipat haicipat) {
        Map<String, Object> results = new HashMap<>();
        if (haicipat == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "取消绑定通知参数为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatCardNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者诊疗卡不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者类型不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatName())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者姓名不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatSex())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者性别不能为空");
            return results;
        }
        if (haicipat.getPatAge() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者年龄不能为空");
            return results;
        }
        if (haicipat.getPatBirth() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者出生日期不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatAddress())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者地址不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatMobile())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者电话不能为空");
            return results;
        }
        if (PERSON.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getPatIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者证件类型不能为空");
            return results;
        }
        if (PERSON.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getPatIdNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者证件号码不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardName())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人姓名不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardIdType())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人证件类型不能为空");
            return results;
        }
        if (CHILD.equals(haicipat.getPatType()) && StringUtils.isBlank(haicipat.getGuardIdNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者监护人证件号码不能为空");
            return results;
        }
        try {
            //int num=mzPatientMiService.clearIcCardNo(haicipat.getPatCardNo());
            int num = 1;
            if (num == 1) {
                results.put("resultCode", 0);
                results.put("resultMessage", "取消绑定成功");
                return results;
            }
            results.put("resultCode", -1);
            results.put("resultMessage", "取消绑定失败");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "取消绑定失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊待缴费记录查询
     *
     * @param haicipat
     * @return
     */
    @RequestMapping(value = "/getMzChargeDetailForUnPaid", method = {RequestMethod.POST})
    public Map<String, Object> getMzChargeDetailForUnPaid(@RequestBody Haicipat haicipat) {
        Map<String, Object> results = new HashMap<>();
        if (haicipat == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊待缴费记录查询参数为空");
            return results;
        }
        if (haicipat.getPatCardType() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者诊疗卡类型不能为空");
            return results;
        }
        if (StringUtils.isBlank(haicipat.getPatCardNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者诊疗卡号不能为空");
            return results;
        }
        try {
            MzPatientMi mzPatientMi = mzPatientMiService.queryByIcCardNo(haicipat.getPatCardNo());
            if (mzPatientMi == null) {
                results.put("resultCode", -1);
                log.warn("海慈传入的病人卡号查询不到对应的患者信息，卡号信息：" + haicipat.getPatCardNo());
                results.put("resultMessage", "患者诊疗卡号查询不到对应的患者信息");
                return results;
            }
            List<MzChargeDetail> mzChargeDetailList = mzChargeDetailService.unPaidToFullChargeDetail(mzPatientMi.getPatientId(), mzPatientMi.getName());
            if (mzChargeDetailList == null || mzChargeDetailList.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "未查询到待缴费记录");
                return results;
            }
            mzChargeDetailService.formatMzChargeDetail(mzChargeDetailList);
            List<Map<String, Object>> returnList = new ArrayList<>();
            for (MzChargeDetail mzChargeDetail : mzChargeDetailList) {
                Map<String, Object> map = new HashMap<>();
                map.put("hisOrdNum", mzChargeDetail.getPatientId() + "_" + mzChargeDetail.getTimes() + "_" + mzChargeDetail.getReceiptNo());
                map.put("patName", mzChargeDetail.getName());
                map.put("payName", "");
                map.put("deptName", mzChargeDetail.getWarnDept());
                map.put("doctorName", mzChargeDetail.getDoctorCode());
                BigDecimal totalAmount = mzChargeDetail.getAmount().multiply(BigDecimal.valueOf(100));
                map.put("totalAmt", totalAmount);
                map.put("medInsAmt", BigDecimal.ZERO);
                map.put("selfAmt", totalAmount);
                map.put("priceTime", DateUtil.fomart(mzChargeDetail.getPriceTime(), "yyyy-MM-dd HH:mm:ss"));
                returnList.add(map);
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "待缴费记录查询成功");
            results.put("data", returnList);
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊待缴费记录查询失败，请联系管理员，错误信息{" + e.getMessage() + "}");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊待缴费记录明细查询
     *
     * @param haiciCharge
     * @return
     */
    @RequestMapping(value = "/getChargeDetailByHisOrdNum", method = {RequestMethod.POST})
    public Map<String, Object> getChargeDetailByHisOrdNum(@RequestBody HaiciCharge haiciCharge) {
        Map<String, Object> results = new HashMap<>();
        if (haiciCharge == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊待缴费记录明细查询参数为空");
            return results;
        }
        if (haiciCharge.getPatCardType() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者诊疗卡类型不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPatCardNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者诊疗卡号不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getHisOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "医院订单号不能为空");
            return results;
        }
        try {
            String[] arr = haiciCharge.getHisOrdNum().split("_");
            MzChargeDetail mzChargeDetail = new MzChargeDetail(arr[0], Integer.valueOf(arr[1]));
            mzChargeDetail.setBillItemCode("100");
            List<MzChargeDetail> mzChargeDetailList = mzChargeDetailService.getTempMzChargeDetailListForUnPaid(mzChargeDetail);
            if (mzChargeDetailList == null || mzChargeDetailList.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "未查询到待缴费明细记录");
                return results;
            }
            mzChargeDetailList = mzChargeDetailService.getMzChargeDetails(mzChargeDetailList);
            List<Map<String, Object>> returnList = new ArrayList<>();
            Map<String, ZdUnitCode> unitCodeMap = new HashMap<>();
            List<BillItem> mzBillItems = mzBillItemService.queryMzBillItem();
            Map<String, String> mzBillItemMap = null;
            if (mzBillItems != null) {
                mzBillItemMap = mzBillItems.stream().collect(Collectors.toMap(BillItem::getCode, BillItem::getName));
            }
            for (MzChargeDetail mz : mzChargeDetailList) {
                if (Arrays.asList(Constants.TC, Constants.SSWRF).contains(mz.getBillItemCode())) {
                    continue;
                }
                Map<String, Object> map = new HashMap<>();
                map.put("itemName", mz.getTcName());
                map.put("itemType", "");
                map.put("itemUnit", "");
                BigDecimal unitPrice = mz.getUnitPrice().setScale(2, BigDecimal.ROUND_HALF_UP);
                if (BigDecimal.ZERO.equals(unitPrice)) {
                    unitPrice = BigDecimal.valueOf(0.01D);
                }
                unitPrice = unitPrice.multiply(BigDecimal.valueOf(100));
                map.put("itemPrice", unitPrice);
                map.put("itemSpec", "");
                BigDecimal quantity = mz.getQuantity() == null ? BigDecimal.ONE : BigDecimal.valueOf(mz.getQuantity());
                BigDecimal drugWin = mz.getDrugWin() == null ? BigDecimal.ONE : BigDecimal.valueOf(mz.getDrugWin());
                map.put("itemNumber", quantity.multiply(drugWin));
                map.put("itemTotalFee", unitPrice.multiply(quantity).multiply(drugWin));
                ZdUnitCode unitCode = unitCodeMap.get(mz.getExecDept());
                if (unitCode == null && StringUtils.isNotBlank(mz.getExecDept())) {
                    unitCode = zdUnitCodeService.queryByCode(mz.getExecDept());
                    unitCodeMap.put(mz.getExecDept(), unitCode);
                }
                if (mzBillItemMap != null) {
                    map.put("itemType", mzBillItemMap.get(mz.getBillItemCode()));
                }
                map.put("execDeptName", unitCode == null ? "" : unitCode.getName());
                map.put("execDeptAddress", (unitCode == null || unitCode.getOfficePos() == null) ? "" : unitCode.getOfficePos());
                returnList.add(map);
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "待缴费记录明细查询成功");
            results.put("data", returnList);
            return results;
        } catch (Exception e) {
            log.error(haiciCharge.toString());
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊待缴费记录明细查询失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊缴费订单支付
     *
     * @param haiciCharge
     * @return
     */
    @RequestMapping(value = "/payChargeDetailFormHaiCi", method = {RequestMethod.POST})
    public Map<String, Object> payChargeDetailFormHaiCi(@RequestBody HaiciCharge haiciCharge) {
        Map<String, Object> results = new HashMap<>();
        if (haiciCharge == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊缴费订单支付参数为空");
            return results;
        }
        if (haiciCharge.getPatCardType() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者诊疗卡类型不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPatCardNo())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者诊疗卡号不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getHisOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "医院订单号不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPsOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "平台订单号不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPayMode())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付方式不能为空");
            return results;
        }
        if (!Arrays.asList(WX, ZFB).contains(haiciCharge.getPayMode())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付方式有误，请联系管理员");
            return results;
        }
        if (haiciCharge.getPayAmt() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付金额不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getAgtOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付机构流水号不能为空");
            return results;
        }
        if (haiciCharge.getPayTime() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付时间不能为空");
            return results;
        }
        try {
            MzDepositFile mzDepositFileDb = mzDepositFileService.queryMzDepositFileByAgtordnum(haiciCharge.getPsOrdNum(), haiciCharge.getAgtOrdNum(), null);
            if (mzDepositFileDb != null && PayMarkEnum.CHARGED.code.equals(mzDepositFileDb.getPayMark())) {
                results.put("resultCode", 0);
                results.put("resultMessage", "缴费成功");
                results.put("guideListInfo", "");
                return results;
            }
            String[] arr = haiciCharge.getHisOrdNum().split("_");
            MzDepositFileVo mzDepositFileVo = new MzDepositFileVo();
            mzDepositFileVo.setPatientId(arr[0]);
            mzDepositFileVo.setTimes(Integer.valueOf(arr[1]));
            mzDepositFileVo.setReceiptNo(Integer.valueOf(arr[2]));
            MzDepositFile mzDepositFile = new MzDepositFile();
            if (WX.equals(haiciCharge.getPayMode())) {
                mzDepositFile.setChequeType(Constants.ZZWX);
            } else {
                mzDepositFile.setChequeType(Constants.ZZZFB);
            }
            mzDepositFile.setPsordnum(haiciCharge.getPsOrdNum());
            mzDepositFile.setAgtordnum(haiciCharge.getAgtOrdNum());
            mzDepositFile.setAmount(haiciCharge.getPayAmt().divide(BigDecimal.valueOf(100)));
            mzDepositFileVo.setMzDepositFiles(Arrays.asList(mzDepositFile));
            mzChargeDetailService.chargeFee(Constants.BRZZJF_CODE, mzDepositFileVo);
            results.put("resultCode", 0);
            results.put("resultMessage", "缴费成功");
            results.put("guideListInfo", "");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊缴费订单支付失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊缴费订单支付状态查询
     *
     * @param haiciCharge
     * @return
     */
    @RequestMapping(value = "/getPayStatus", method = {RequestMethod.POST})
    public Map<String, Object> getPayStatus(@RequestBody HaiciCharge haiciCharge) {
        Map<String, Object> results = new HashMap<>();
        if (haiciCharge == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊缴费订单支付状态查询参数为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getHisOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "医院订单号不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPsOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "平台订单号不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPayMode())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付方式不能为空");
            return results;
        }
        if (!Arrays.asList(WX, ZFB).contains(haiciCharge.getPayMode())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付方式有误，请联系管理员");
            return results;
        }
        if (haiciCharge.getPayAmt() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付金额不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getAgtOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付机构流水号不能为空");
            return results;
        }
        if (haiciCharge.getPayTime() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付时间不能为空");
            return results;
        }
        try {
            MzDepositFile mzDepositFileDb = mzDepositFileService.queryMzDepositFileByAgtordnum(haiciCharge.getPsOrdNum(), haiciCharge.getAgtOrdNum(), null);
            if (mzDepositFileDb != null && PayMarkEnum.CHARGED.code.equals(mzDepositFileDb.getPayMark())) {
                results.put("resultCode", 0);
                results.put("resultMessage", "门诊缴费订单支付状态查询成功");
                results.put("payStatus", "1");
                results.put("guideListInfo", "");
                results.put("receiptNum", "");
                return results;
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "门诊缴费订单支付状态查询失败");
            results.put("payStatus", "2");
            results.put("guideListInfo", "");
            results.put("receiptNum", "");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊缴费订单支付状态查询失败，请联系管理员");
            results.put("payStatus", "2");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊缴费订单退费
     *
     * @param haiciCharge
     * @return
     */
    @RequestMapping(value = "/saveRefundFeeFormHaiCi", method = {RequestMethod.POST})
    public Map<String, Object> saveRefundFeeFormHaiCi(@RequestBody HaiciCharge haiciCharge) {
        Map<String, Object> results = new HashMap<>();
        if (haiciCharge == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊缴费订单退费参数为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getHisOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "医院订单号不能为空");
            return results;
        }
        try {
            String[] arr = haiciCharge.getHisOrdNum().split("_");
            MzChargeDetail mzChargeDetail = new MzChargeDetail(arr[0], Integer.valueOf(arr[1]));
            mzChargeDetail.setReceiptNo(Integer.valueOf(arr[2]));
            List<MzChargeDetail> mzChargeDetailList = mzChargeDetailService.queryMzChargeDetailByBillItemCode(mzChargeDetail.getPatientId(), mzChargeDetail.getTimes(), mzChargeDetail.getReceiptNo(), PayMarkEnum.CHARGED.code, "2", Arrays.asList("TC"));
            if (mzChargeDetailList == null || mzChargeDetailList.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "未查询到已缴费明细记录");
                return results;
            }
            String chargeItemCodes = null;
            for (MzChargeDetail md : mzChargeDetailList) {
                if (md == null || Constants.TC.equals(md.getBillItemCode())) {
                    continue;
                }
                if (ConfirmFlagEnum.CONFIRM.code.equals(md.getConfirmFlag())) {
                    results.put("resultCode", -1);
                    results.put("resultMessage", "无法退费，当前处方已经被确认，请您联系该科室进行处理");
                    return results;
                }
                if (chargeItemCodes == null) {
                    chargeItemCodes = md.getOrderNo() + "_" + md.getItemNo() + "_" + md.getChargeItemCode();
                } else {
                    chargeItemCodes += "," + md.getOrderNo() + "_" + md.getItemNo() + "_" + md.getChargeItemCode();
                }
            }
            MzDepositFileVo mzDepositFileVo = new MzDepositFileVo();
            mzDepositFileVo.setPatientId(arr[0]);
            mzDepositFileVo.setTimes(Integer.valueOf(arr[1]));
            mzDepositFileVo.setReceiptNo(Integer.valueOf(arr[2]));
            mzDepositFileVo.setChargeItemCodes(chargeItemCodes);
            User tokenUser = userService.queryUserByUserIdCode(Constants.BRZZJF_CODE);
            mzChargeDetailService.refundFee(tokenUser.getUserIdCode(), mzDepositFileVo);
            results.put("resultCode", 0);
            results.put("resultMessage", "退费成功");
            results.put("guideListInfo", "");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊订单退费失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊已缴费记录查询
     *
     * @param mzDepositFile
     * @return
     */
    @RequestMapping(value = "/getMzChargeDetailForPaid", method = {RequestMethod.POST})
    public Map<String, Object> getMzChargeDetailForPaid(@RequestBody MzDepositFile mzDepositFile) {
        Map<String, Object> results = new HashMap<>();
        if (mzDepositFile == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊待已费记录查询参数为空");
            return results;
        }
        if (mzDepositFile.getPatientId() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者ID不能为空");
            return results;
        }
        try {
            List<Map<String, Object>> mzChargeDetailList = mzDepositFileService.queryMzDepositFileForZZ(mzDepositFile);
            if (mzChargeDetailList == null || mzChargeDetailList.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "未查询到已缴费记录");
                return results;
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "已缴费记录查询成功");
            results.put("data", mzChargeDetailList);
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊已缴费记录查询失败，请联系管理员，错误信息{" + e.getMessage() + "}");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊挂号记录查询
     *
     * @param mzyReqrec
     * @return
     */
    @RequestMapping(value = "/getRegistrationForPaid", method = {RequestMethod.POST})
    public Map<String, Object> getRegistrationForPaid(@RequestBody MzyReqrec mzyReqrec) {
        Map<String, Object> results = new HashMap<>();
        if (mzyReqrec == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊挂号记录查询参数为空");
            return results;
        }
        if (mzyReqrec.getPatientId() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者ID不能为空");
            return results;
        }
        try {
            mzyReqrec.setOpId(Constants.BRZZJF_CODE);
            MzyReqrecPageDto mzyReqrecPageDto = new MzyReqrecPageDto();
            mzyReqrecPageDto.setPageSize(1000);
            mzyReqrecPageDto.setMzyReqrec(mzyReqrec);
            mzyReqrecPageDto.setPatientIds(Arrays.asList(mzyReqrec.getPatientId()));
            mzyReqrecPageDto.setTableName("mzy_reqrec");
            List<MzyReqrec> mzyReqrecList = mzyReqrecService.queryMzyReqrecWithPage(mzyReqrecPageDto);
            List<Map<String, Object>> list = new ArrayList<>();
            Map<String, String> employeeMap = null;
            Set<String> set = new HashSet<>();
            employeeMap = getStringStringMap(mzyReqrecList, list, employeeMap, set);
            mzyReqrecPageDto.setTableName("mzy_reqrec_b");
            mzyReqrecList = mzyReqrecService.queryMzyReqrecWithPage(mzyReqrecPageDto);
            getStringStringMap(mzyReqrecList, list, employeeMap, set);
            if (list == null || list.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "未查询到挂号记录");
                return results;
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "挂号记录查询成功");
            results.put("data", list);
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊挂号记录查询失败，请联系管理员，错误信息{" + e.getMessage() + "}");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }

    private Map<String, String> getStringStringMap(List<MzyReqrec> mzyReqrecList, List<Map<String, Object>> list, Map<String, String> employeeMap, Set<String> set) {
        if (mzyReqrecList != null && mzyReqrecList.size() > 0) {
            if (employeeMap == null) {
                List<Employee> employees = employeeService.queryAll();
                if (employees != null) {
                    employeeMap = employees.stream().collect(Collectors.toMap(Employee::getEmployeeCode, Employee::getEmployeeName));
                }
            }
            for (MzyReqrec mr : mzyReqrecList) {
                if (set.contains(mr.getPsordnum())) {
                    continue;
                }
                Map<String, Object> map = new HashMap<>();
                map.put("patientId", mr.getPatientId());
                map.put("doctorName", employeeMap.get(mr.getDoctorCode()));
                map.put("unitName", zdUnitCodeService.queryDeptNameByIdInCache(mr.getUnitCode()));
                map.put("requestDay", DateUtil.fomart(mr.getRequestDay(), "yyyy-MM-dd HH:mm:ss"));
                map.put("opDay", DateUtil.fomart(mr.getOpDay(), "yyyy-MM-dd HH:mm:ss"));
                if (StringUtils.isNotBlank(mr.getChargeType())) {
                    MzyZdChargeType mzyZdChargeType = mzyZdChargeTypeService.queryByCode(mr.getChargeType());
                    if (mzyZdChargeType != null) {
                        map.put("chargeType", mzyZdChargeType.getName());
                    } else {
                        map.put("chargeType", "");
                    }
                }
                BigDecimal fee = BigDecimal.ZERO;
                if (mr.getReqFee() == null) {
                    mr.setReqFee(BigDecimal.ZERO);
                }
                if (mr.getClinicFee() == null) {
                    mr.setClinicFee(BigDecimal.ZERO);
                }
                if (mr.getOthFee() == null) {
                    mr.setOthFee(BigDecimal.ZERO);
                }
                fee = fee.add(mr.getReqFee()).add(mr.getClinicFee()).add(mr.getOthFee());
                map.put("fee", fee);
                list.add(map);
                set.add(mr.getPatientId());
            }
        }
        return employeeMap;
    }


    /**
     * 保存处方信息
     *
     * @param mzPrescriptionVo
     * @return
     */
    @RequestMapping(value = "/savePrescription", method = {RequestMethod.POST})
    public Map<String, Object> savePrescription(@RequestBody MzPrescriptionVo mzPrescriptionVo) {
        Map<String, Object> results = new HashMap<>();
        try {
            int num = mzChargeDetailService.savePrescription(mzPrescriptionVo);
            if (num == 1) {
                results.put("resultCode", 0);
                results.put("resultMessage", "门诊处方保存成功");
                return results;
            }
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊处方保存失败");
            return results;
        } catch (MzException e) {
            results.put("resultCode", -1);
            results.put("resultMessage", e.getMessage());
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊处方保存失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 查询挂号科室分类
     *
     * @return
     */
    @RequestMapping(value = "/getMzClass", method = {RequestMethod.GET})
    public Map<String, Object> getMzClass() {
        Map<String, Object> results = new HashMap<>();
        List<ZdMzClass> zdMzClassList = zdMzClassService.queryAllZdMzClass();
        if (zdMzClassList == null || zdMzClassList.size() == 0) {
            results.put("resultCode", -1);
            results.put("resultMessage", "没有查询挂号科室分类");
            return results;
        }
        results.put("resultCode", 0);
        results.put("data", zdMzClassList);
        return results;
    }


    /**
     * 新冠拦截手机挂号
     *
     * @return
     */
    @RequestMapping(value = "/covidManageForHaici", method = {RequestMethod.GET})
    public Map<String, Object> covidManageForHaici(@RequestParam("patientId") String patientId, @RequestParam("unitCode") String unitCode) {
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isBlank(patientId)) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询当前病人流调失败，病人id为空");
            return resultMap;
        }
        if (StringUtils.isBlank(unitCode)) {
            resultMap.put("code", -1);
            resultMap.put("message", "查询当前病人流调失败，科室编码为空");
            return resultMap;
        }
        try {
            mzyReqrecService.covidManageForHaici(patientId, unitCode);
            resultMap.put("code", 0);
            resultMap.put("message", "当前病人已经通过流调");
            return resultMap;
        } catch (MzException e) {
            e.printStackTrace();
            log.error("系统异常，请联系管理员");
            resultMap.put("code", e.getValue());
            resultMap.put("message", e.getMessage());
            return resultMap;
        }
    }


    /**
     * 根据门诊科室分类码查询科室列表
     *
     * @return
     */
    @RequestMapping(value = "/getUnitCodeByMzClass", method = {RequestMethod.POST})
    public Map<String, Object> getUnitCodeByMzClass(@RequestBody ZdUnitCode zdUnitCode) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (zdUnitCode == null) {
                resultMap.put("resultCode", -1);
                resultMap.put("resultMessage", "查询科室列表失败，参数为空");
                return resultMap;
            }
            if (StringUtils.isBlank(zdUnitCode.getMzClass())) {
                resultMap.put("resultCode", -1);
                resultMap.put("resultMessage", "查询科室列表失败，门诊分类码为空");
                return resultMap;
            }
            ZdUnitCodePageDto zdUnitCodePageDto = new ZdUnitCodePageDto();
            zdUnitCode.setDelFlag(YesNoEnum.NO.code);
            zdUnitCode.setMzFlag(YesNoEnum.YES.code);
            zdUnitCode.setClassCode(YesNoEnum.YES.code);
            zdUnitCodePageDto.setOrderByCase("sort_code");
            zdUnitCodePageDto.setZdUnitCode(zdUnitCode);
            List<ZdUnitCode> zdUnitCodeList = zdUnitCodeService.queryZdUnitCode(zdUnitCodePageDto);
            if (zdUnitCodeList == null || zdUnitCodeList.size() == 0) {
                resultMap.put("code", -1);
                resultMap.put("message", "没有查询到符合条件的科室列表");
                return resultMap;
            }
            resultMap.put("resultCode", 0);
            resultMap.put("data", zdUnitCodeList);
            return resultMap;
        } catch (Exception e) {
            log.error("系统异常，请联系管理员");
            resultMap.put("code", -1);
            resultMap.put("message", "没有查询到符合条件的科室列表");
            return resultMap;
        }
    }


    /**
     * 预约号源统计信息查询
     *
     * @return
     */
    @RequestMapping(value = "/getNumByDateAndDept", method = {RequestMethod.GET})
    public Map<String, Object> getNumByDateAndDept(@RequestParam("beginDate") String beginDate, @RequestParam("endDate") String endDate, @RequestParam("unitCode") String unitCode) {
        Map<String, Object> results = new HashMap<>();
        try {
            if (StringUtils.isBlank(beginDate)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "开始时间不能为空");
                return results;
            }
            if (StringUtils.isBlank(endDate)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "结束时间不能为空");
                return results;
            }
            if (StringUtils.isBlank(unitCode)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "科室编码不能为空");
                return results;
            }
            Date beginDateD = DateUtil.pase(beginDate, "yyyy-MM-dd");
            Date endDateD = DateUtil.pase(endDate, "yyyy-MM-dd");
            List<Map<String, Object>> list = mzyRequestService.queryNumByDateAndDept(beginDateD, endDateD, unitCode);
            if (list == null || list.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "预约号源统计信息查询失败");
                return results;
            }
            Map<String, Map<String, Object>> tempMap = new HashMap<>();
            for (Map map : list) {
                tempMap.put((String) map.get("scheduleDate"), map);
            }
            //获取时间区间内的日期集合
            List<Date> days = DateUtil.getBetweenDates(beginDateD, endDateD);
            List<Map<String, Object>> resultMap = new ArrayList<>();
            for (Date date : days) {
                String key = DateUtil.fomart(date, "yyyy-MM-dd");
                Map<String, Object> item = tempMap.get(key);
                if (item == null) {
                    item = new HashMap<>();
                    item.put("scheduleDate", key);
                    item.put("status", 2);
                } else {
                    Integer leftNum = (Integer) item.get("leftNum");
                    if (leftNum == null || leftNum <= 0) {
                        item.put("status", 2);
                        item.remove("leftNum");
                        item.remove("totalNum");
                    } else {
                        item.put("status", 1);
                    }
                }
                resultMap.add(item);
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "预约号源统计信息查询成功");
            results.put("data", resultMap);
            return results;
        } catch (Exception e) {
            results.put("resultCode", -1);
            results.put("resultMessage", "预约号源统计信息查询失败");
            return results;
        }

    }


    /**
     * 查询所有门诊医生信息
     *
     * @return
     */
    @RequestMapping(value = "/getAllMzDoctor", method = {RequestMethod.GET})
    public Map<String, Object> getAllMzDoctor() {
        Map<String, Object> results = new HashMap<>();
        try {
            List<ZdUnitCode> list = zdUnitCodeService.queryAllMzDeptFromDb();
            List<String> codes = list.stream().map(ZdUnitCode::getCode).collect(Collectors.toList());
            List<Employee> employees = employeeService.queryEmployeeByDepts(codes);
            if (employees == null || employees.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "没有医生信息");
                return results;
            }
            List<ZdEmpTitle> zdEmpTitles = zdEmpTitleService.queryZdEmpTitle();
            Map<String, String> titleMap = null;
            Map<String, Integer> titGradeMap = null;
            Map<String, String> zdUnitCodeMap = list.stream().collect(Collectors.toMap(ZdUnitCode::getCode, ZdUnitCode::getName));
            if (zdEmpTitles != null && zdEmpTitles.size() > 0) {
                titleMap = zdEmpTitles.stream().collect(Collectors.toMap(ZdEmpTitle::getCode, ZdEmpTitle::getName));
                titGradeMap = zdEmpTitles.stream().collect(Collectors.toMap(ZdEmpTitle::getCode, ZdEmpTitle::getTitGrade));
            }
            List<Employee> notDoctors = new ArrayList<>();
            for (Employee employee : employees) {
                if (titGradeMap != null) {
                    Integer titGrade = titGradeMap.get(employee.getEmpTitCode());
                    if (!Constants.DOCTOR_TIT_GRADE.equals(titGrade)) {
                        notDoctors.add(employee);
                        continue;
                    }
                }
                if (titleMap != null) {
                    employee.setDoctorTitle(titleMap.get(employee.getEmpTitCode()));
                }
                employee.setDeptName(zdUnitCodeMap.get(employee.getDeptCode()));
            }
            employees.removeAll(notDoctors);
            results.put("resultCode", 0);
            results.put("resultMessage", "查询所有门诊医生信息成功");
            results.put("data", employees);
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "查询所有门诊医生信息失败");
            return results;
        }
    }


    /**
     * 查询可以挂号的科室内医生信息
     *
     * @return
     */
    @RequestMapping(value = "/getDoctorByDateAndDept", method = {RequestMethod.GET})
    public Map<String, Object> getDoctorByDateAndDept(@RequestParam("requestDay") String requestDay, @RequestParam("unitCode") String unitCode) {
        Map<String, Object> results = new HashMap<>();
        try {
            if (StringUtils.isBlank(requestDay)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "预约挂号时间不能为空");
                return results;
            }
            if (StringUtils.isBlank(unitCode)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "科室编码不能为空");
                return results;
            }
            List<String> ampms = null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String now = simpleDateFormat.format(new Date());
            if (requestDay.equals(now)) {
                int flg = DateUtil.IsAfternoon(java.util.Calendar.getInstance().getTime().getHours());
                if (flg >= 0) {
                    ampms = Arrays.asList(Constants.AM);
                }
                MzZdWorkTime mzZdWorkTime = mzZdWorkTimeService.queryMzZdWorkTimeByCode(Constants.PM);
                simpleDateFormat = new SimpleDateFormat("HH:mm");
                Date dateEndTime = simpleDateFormat.parse(mzZdWorkTime.getEndTime());
                long tsEndTime = dateEndTime.getTime();
                String realNowStr = simpleDateFormat.format(new Date());
                Date realHour = simpleDateFormat.parse(realNowStr);
                long realHourTs = realHour.getTime();
                if (realHourTs > tsEndTime) {
                    ampms = new ArrayList<>(ampms);
                    ampms.add(Constants.PM);
                }
            }
            Date requestDayD = DateUtil.pase(requestDay, "yyyy-MM-dd");
            Date toDay = DateUtil.pase(new Date(), "yyyy-MM-dd");
            if (requestDayD.before(toDay)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "预约挂号时间已过，无法挂号，请重新选择!");
                return results;
            }
            List<Map<String, Object>> list = mzyRequestService.queryDoctorByDateAndDept(requestDayD, ampms, unitCode);
            if (list == null || list.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "没有医生排班信息");
                return results;
            }
            List<Map<String, Object>> removeList = new ArrayList<>();
            for (Map map : list) {
                BigDecimal fee = BigDecimal.ZERO;
                String chargeType = (String) map.get("chargeType");
                if (StringUtils.isNotBlank(chargeType)) {
                    MzyZdChargeType mzyZdChargeType = mzyZdChargeTypeService.queryByCode(chargeType);
                    if (mzyZdChargeType != null) {
                        map.put("chargeType", mzyZdChargeType.getName());
                        if (mzyZdChargeType.getReqFee() == null) {
                            mzyZdChargeType.setReqFee(BigDecimal.ZERO);
                        }
                        if (mzyZdChargeType.getClinicFee() == null) {
                            mzyZdChargeType.setClinicFee(BigDecimal.ZERO);
                        }
                        if (mzyZdChargeType.getOthFee() == null) {
                            mzyZdChargeType.setOthFee(BigDecimal.ZERO);
                        }
                        fee = fee.add(mzyZdChargeType.getReqFee()).add(mzyZdChargeType.getClinicFee()).add(mzyZdChargeType.getOthFee());
                    }
                }
                BigDecimal checkFee = (BigDecimal) map.get("checkFee");
                if (checkFee == null) {
                    checkFee = BigDecimal.ZERO;
                }
                fee = fee.add(checkFee);
//                //周末挂号费为0，不收挂号费
//                if(DateUtil.isWeekend(requestDayD)){
//                    fee=BigDecimal.ZERO;
//                }
                map.put("fee", fee);
                map.remove("checkFee");
                String doctorCode = (String) map.get("doctorCode");
                if (StringUtils.isBlank(doctorCode)) {
                    removeList.add(map);
                    continue;
                }
                Employee employee = employeeService.queryByUserCode(doctorCode);
                if (employee == null) {
                    continue;
                }
                map.put("doctorName", employee.getEmployeeName());
                if (StringUtils.isNotBlank(employee.getEmpTitCode())) {
                    ZdEmpTitle zdEmpTitle = zdEmpTitleService.queryByCode(employee.getEmpTitCode());
                    if (zdEmpTitle == null) {
                        continue;
                    }
                    map.put("doctorTitle", zdEmpTitle.getName());
                }
            }
            list.removeAll(removeList);
            results.put("resultCode", 0);
            results.put("resultMessage", "查询可以挂号的科室内医生信息成功");
            results.put("data", list);
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "查询可以挂号的科室内医生信息失败");
            return results;
        }
    }


    /**
     * 查询可以挂号的医生号源信息
     *
     * @return
     */
    @RequestMapping(value = "/getRequestByDateAndDeptAndDoctor", method = {RequestMethod.GET})
    public Map<String, Object> getRequestByDateAndDeptAndDoctor(@RequestParam("requestDay") String requestDay, @RequestParam("unitCode") String unitCode, @RequestParam("doctorCode") String doctorCode) {
        Map<String, Object> results = new HashMap<>();
        try {
            if (StringUtils.isBlank(requestDay)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "预约挂号时间不能为空");
                return results;
            }
            if (StringUtils.isBlank(unitCode)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "科室编码不能为空");
                return results;
            }
            if (StringUtils.isBlank(doctorCode)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "医生编码不能为空");
                return results;
            }
            List<String> ampms = null;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String now = simpleDateFormat.format(new Date());
            if (requestDay.equals(now)) {
                int flg = DateUtil.IsAfternoon(java.util.Calendar.getInstance().getTime().getHours());
                if (flg >= 0) {
                    ampms = Arrays.asList(Constants.AM);
                }
                MzZdWorkTime mzZdWorkTime = mzZdWorkTimeService.queryMzZdWorkTimeByCode(Constants.PM);
                simpleDateFormat = new SimpleDateFormat("HH:mm");
                Date dateEndTime = simpleDateFormat.parse(mzZdWorkTime.getEndTime());
                long tsEndTime = dateEndTime.getTime();
                String realNowStr = simpleDateFormat.format(new Date());
                Date realHour = simpleDateFormat.parse(realNowStr);
                long realHourTs = realHour.getTime();
                if (realHourTs > tsEndTime) {
                    ampms = new ArrayList<>(ampms);
                    ampms.add(Constants.PM);
                }
            }
            Date requestDayD = DateUtil.pase(requestDay, "yyyy-MM-dd");
            Date toDay = DateUtil.pase(new Date(), "yyyy-MM-dd");
            if (requestDayD.before(toDay)) {
                results.put("resultCode", -1);
                results.put("resultMessage", "预约挂号时间已过，无法挂号，请重新选择!");
                return results;
            }
            List<Map<String, Object>> list = mzyRequestService.queryRequestByDateAndDeptAndDoctor(requestDayD, ampms, unitCode, doctorCode);
            if (list == null || list.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "没有医生排班信息");
                return results;
            }
            for (Map map : list) {
                BigDecimal fee = BigDecimal.ZERO;
                String chargeType = (String) map.get("chargeType");
                if (StringUtils.isNotBlank(chargeType)) {
                    MzyZdChargeType mzyZdChargeType = mzyZdChargeTypeService.queryByCode(chargeType);
                    if (mzyZdChargeType != null) {
                        map.put("chargeType", mzyZdChargeType.getName());
                        if (mzyZdChargeType.getReqFee() == null) {
                            mzyZdChargeType.setReqFee(BigDecimal.ZERO);
                        }
                        if (mzyZdChargeType.getClinicFee() == null) {
                            mzyZdChargeType.setClinicFee(BigDecimal.ZERO);
                        }
                        if (mzyZdChargeType.getOthFee() == null) {
                            mzyZdChargeType.setOthFee(BigDecimal.ZERO);
                        }
                        fee = fee.add(mzyZdChargeType.getReqFee()).add(mzyZdChargeType.getClinicFee()).add(mzyZdChargeType.getOthFee());
                    }
                }
                String ampm = (String) map.get("ampm");
                BigDecimal checkFee = (BigDecimal) map.get("checkFee");
                if (checkFee == null) {
                    checkFee = BigDecimal.ZERO;
                }
                fee = fee.add(checkFee);
                if (mzyReqrecService.notPayGhFee(unitCode, requestDayD, ampm)) {
                    fee = BigDecimal.ZERO;
                }
                map.put("fee", fee);
                map.remove("checkFee");
                Employee employee = employeeService.queryByUserCode(doctorCode);
                if (employee != null) {
                    map.put("doctorName", employee.getEmployeeName());
                    if (StringUtils.isNotBlank(employee.getEmpTitCode())) {
                        ZdEmpTitle zdEmpTitle = zdEmpTitleService.queryByCode(employee.getEmpTitCode());
                        if (zdEmpTitle != null) {
                            map.put("doctorTitle", zdEmpTitle.getName());
                        }
                    }
                }
                if (Constants.AM.equals(ampm)) {
                    ampm = "上午";
                } else if (Constants.PM.equals(ampm)) {
                    ampm = "下午";
                } else if (Constants.DAY.equals(ampm)) {
                    ampm = "全天";
                }
                map.put("ampm", ampm);
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "查询可以挂号的医生号源信息成功");
            results.put("data", list);
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "查询可以挂号的医生号源信息失败");
            return results;
        }
    }


    /**
     * 根据排班主键和生日查询挂号费用  （费用确认低于7岁儿童增加挂号费 上调50%）
     *
     * @return
     */
    @RequestMapping(value = "/getMzChargeTypeByRequestIdForHaiCi", method = {RequestMethod.GET})
    public Map<String, Object> getMzChargeTypeByRequestIdForHaiCi(@RequestParam("mzyRequestId") Long mzyRequestId, @RequestParam("patientId") String patientId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (mzyRequestId == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "排班主键不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人门诊ID不能为空");
                return resultMap;
            }
            MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
            if (mzPatientMi == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人不存在");
                return resultMap;
            }
            Date birthDayD = mzPatientMi.getBirthDay();
            MzyZdChargeType mzyZdChargeType = mzyRequestService.calculateAddition(mzyRequestId, birthDayD);
            BigDecimal fee = BigDecimal.ZERO;
            if (mzyZdChargeType.getReqFee() != null) {
                fee = fee.add(mzyZdChargeType.getReqFee());
            }
            if (mzyZdChargeType.getClinicFee() != null) {
                fee = fee.add(mzyZdChargeType.getClinicFee());
            }
            if (mzyZdChargeType.getOthFee() != null) {
                fee = fee.add(mzyZdChargeType.getOthFee());
            }
            resultMap.put("code", 0);
            resultMap.put("message", "根据排班主键和生日查询挂号费用功");
            resultMap.put("data", fee);
            resultMap.put("message", mzyZdChargeType.getMessage());
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("根据排班主键和生日查询挂号费用失败，错误信息{}", e.getMessage());
            resultMap.put("code", -1);
            resultMap.put("message", "根据排班主键和生日查询挂号费用失败");
            return resultMap;
        }
    }


    /**
     * 门诊挂号支付
     *
     * @return
     */
    @RequestMapping(value = "/payRegistrationFormHaiCi", method = {RequestMethod.POST})
    public Map<String, Object> payRegistrationFormHaiCi(@RequestBody MzyReqrecPageDto mzyReqrecPageDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (mzyReqrecPageDto == null || mzyReqrecPageDto.getMzyReqrec() == null) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "挂号参数不能为空");
                return resultMap;
            }
            MzyReqrec mzyReqrec = mzyReqrecPageDto.getMzyReqrec();
            if (StringUtils.isBlank(mzyReqrec.getPatientId())) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "病人id不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzyReqrec.getPaymode())) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "付款方式不能为空");
                return resultMap;
            }
            if (!Arrays.asList(WX, ZFB).contains(mzyReqrec.getPaymode())) {
                resultMap.put("resultCode", -1);
                resultMap.put("resultMessage", "支付方式有误，请联系管理员");
                return resultMap;
            }
//            if (StringUtils.isBlank(mzyReqrec.getAgtordnum())) {
//                resultMap.put("resultCode", -1);
//                resultMap.put("message", "流水号不能为空");
//                return resultMap;
//            }
            if (StringUtils.isBlank(mzyReqrec.getPsordnum())) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "订单号不能为空");
                return resultMap;
            }
            if (mzyReqrecPageDto.getMzyRequestId() == null) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "门诊号表主键不能为空");
                return resultMap;
            }
            if (mzyReqrecPageDto.getTotalFee() == null) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "实缴费用不能为空");
                return resultMap;
            }
            mzyReqrecPageDto.setResponceType("01");
            MzyRequest mzyRequest = mzyRequestService.queryById(mzyReqrecPageDto.getMzyRequestId());
            if (mzyRequest == null) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "挂号信息对应的号表不存在");
                return resultMap;
            }
            mzyReqrec.setAmpm(mzyRequest.getAmpm());
            mzyReqrec.setUnitCode(mzyRequest.getUnitCode());
            mzyReqrec.setChargeType(mzyRequest.getChargeType());
            mzyReqrec.setDoctorCode(mzyRequest.getDoctorCode());
            mzyReqrec.setRequestDay(mzyRequest.getRequestDay());
            MzZdWorkTime mzZdWorkTime = mzZdWorkTimeService.queryMzZdWorkTimeByCode(mzyReqrec.getAmpm());
            if (mzZdWorkTime == null) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "号段不存在");
                return resultMap;
            }
            mzyReqrec.setOpId(Constants.BRZZJF_CODE);
            int num = mzyReqrecService.saveMzyReqrec(mzyReqrecPageDto);
            if (num == 0) {
                resultMap.put("resultCode", -1);
                resultMap.put("message", "挂号失败，请稍后重试！");
                return resultMap;
            }
            resultMap.put("resultCode", 0);
            resultMap.put("message", "保存挂号信息成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("resultCode", -1);
            resultMap.put("message", StringUtils.isBlank(e.getMessage()) ? "挂号失败，系统出错，请联系管理员" : e.getMessage());
            log.error("挂号失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }


    /**
     * 门诊挂号订单支付状态查询
     *
     * @param haiciCharge
     * @return
     */
    @RequestMapping(value = "/getPayStatusForRegistration", method = {RequestMethod.POST})
    public Map<String, Object> getPayStatusForRegistration(@RequestBody HaiciCharge haiciCharge) {
        Map<String, Object> results = new HashMap<>();
        if (haiciCharge == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊挂号订单支付状态查询参数为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPsOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "平台订单号不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getPayMode())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付方式不能为空");
            return results;
        }
        if (!Arrays.asList(WX, ZFB).contains(haiciCharge.getPayMode())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付方式有误，请联系管理员");
            return results;
        }
        if (haiciCharge.getPayAmt() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付金额不能为空");
            return results;
        }
        if (StringUtils.isBlank(haiciCharge.getAgtOrdNum())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "支付机构流水号不能为空");
            return results;
        }
//        if (haiciCharge.getPayTime() == null) {
//            results.put("resultCode", -1);
//            results.put("resultMessage", "支付时间不能为空");
//            return results;
//        }
        try {
            MzyReqrec mzyReqrec = mzyReqrecService.queryMzyReqrecByAgtordnum(haiciCharge.getPsOrdNum(), haiciCharge.getAgtOrdNum(), null);
            if (mzyReqrec != null) {
                results.put("resultCode", 0);
                results.put("resultMessage", "门诊挂号订单支付状态查询成功");
                results.put("payStatus", "1");
                results.put("guideListInfo", "");
                results.put("receiptNum", "");
                return results;
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "门诊挂号订单支付状态查询失败");
            results.put("payStatus", "2");
            results.put("guideListInfo", "");
            results.put("receiptNum", "");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊挂号订单支付状态查询失败，请联系管理员");
            results.put("payStatus", "2");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 查询门诊记录表 医患通已收费记录查询
     *
     * @return
     */
    @RequestMapping(value = "/listMzChargeDetail", method = {RequestMethod.POST})
    public Map<String, Object> listMzChargeDetail(@RequestBody MzChargeDetailPageDto mzChargeDetailPageDto) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (mzChargeDetailPageDto == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询门诊记录表参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzChargeDetailPageDto.getBeginTimeStr()) && mzChargeDetailPageDto.getBeginTime() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询门诊记录表开始时间参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(mzChargeDetailPageDto.getEndTimeStr()) && mzChargeDetailPageDto.getEndTime() == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "查询门诊记录表结束时间参数不能为空");
                return resultMap;
            }
            //日期参数为了兼容不同的接口，设计了两个不同的字段
            if (StringUtils.isNotBlank(mzChargeDetailPageDto.getBeginTimeStr())) {
                mzChargeDetailPageDto.setBeginTime(DateUtil.pase(mzChargeDetailPageDto.getBeginTimeStr(), "yyyy-MM-dd HH:mm:ss"));
            }
            if (StringUtils.isNotBlank(mzChargeDetailPageDto.getEndTimeStr())) {
                mzChargeDetailPageDto.setEndTime(DateUtil.pase(mzChargeDetailPageDto.getEndTimeStr(), "yyyy-MM-dd HH:mm:ss"));
            }
            PageViewVo pageViewVo = new PageViewVo();
            MzChargeDetail mzChargeDetail = mzChargeDetailPageDto.getMzChargeDetail();
            if (mzChargeDetail == null) {
                mzChargeDetail = new MzChargeDetail();
                mzChargeDetailPageDto.setMzChargeDetail(mzChargeDetail);
            }
            if (StringUtils.isBlank(mzChargeDetail.getPayMark())) {
                mzChargeDetail.setPayMark(PayMarkEnum.CHARGED.code);
            } else {
                if (!Arrays.asList(PayMarkEnum.CHARGED.code, PayMarkEnum.RETURN_PREMIUM.code).contains(mzChargeDetail.getPayMark())) {
                    resultMap.put("code", -1);
                    resultMap.put("message", "订单状态参数错误");
                    return resultMap;
                }
            }
            pageViewVo.setPageIndex(mzChargeDetailPageDto.getPageIndex());
            pageViewVo.setTotal(mzChargeDetailService.countMzChargeDetail(mzChargeDetailPageDto));
            mzChargeDetailPageDto.setPageSize(pageViewVo.getTotal());
            List<MzChargeDetail> mzChargeDetails = mzChargeDetailService.queryMzChargeDetailWithPage(mzChargeDetailPageDto);
            List<SfjkMzbrxxVo> resultList = new ArrayList<>();
            if (mzChargeDetails != null && mzChargeDetails.size() > 0) {
                List<String> doctorCodes = mzChargeDetails.stream().filter(u -> StringUtils.isNotBlank(u.getDoctorCode())).map(u -> u.getDoctorCode()).collect(Collectors.toList());
                Map<String, String> employeeMap = null;
                List<Employee> employees = employeeService.queryByCodes(doctorCodes);
                if (employees != null) {
                    employeeMap = employees.stream().collect(Collectors.toMap(Employee::getEmployeeCode, Employee::getEmployeeName));
                }
                for (MzChargeDetail mz : mzChargeDetails) {
                    if (mz != null) {
                        MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(mz.getPatientId());
                        SfjkMzbrxxVo sfjkMzbrxxVo = new SfjkMzbrxxVo();
                        sfjkMzbrxxVo.setBrbh(mz.getPatientId());
                        sfjkMzbrxxVo.setDjh(mz.getPatientId() + "_" + mz.getTimes() + "_" + mz.getReceiptNo());
                        sfjkMzbrxxVo.setMzbh(mz.getRealNo().toString());
                        sfjkMzbrxxVo.setBrxm(mz.getName());
                        if (mzPatientMi != null) {
                            sfjkMzbrxxVo.setBrxb(mzPatientMi.getSex());
                            sfjkMzbrxxVo.setBrxm(mzPatientMi.getName());
                            sfjkMzbrxxVo.setCsrq(mzPatientMi.getBirthDay());
                            sfjkMzbrxxVo.setSfzh(mzPatientMi.getSocialNo());
                            sfjkMzbrxxVo.setXzdz(mzPatientMi.getAddress());
                            sfjkMzbrxxVo.setSjhm(mzPatientMi.getPhoneNo());
                            sfjkMzbrxxVo.setLxdh(mzPatientMi.getPhoneNo());
                        }
                        sfjkMzbrxxVo.setMzrq(mz.getChargeDate());
                        sfjkMzbrxxVo.setMzks(mz.getWarnDept());
                        sfjkMzbrxxVo.setYsbh(mz.getDoctorCode());
                        if (employeeMap != null) {
                            String doctorCode = employeeMap.get(mz.getDoctorCode());
                            sfjkMzbrxxVo.setMzys(doctorCode == null ? "" : doctorCode);
                        }
                        if (mz.getAmount() != null) {
                            sfjkMzbrxxVo.setZfy(mz.getAmount().setScale(1, BigDecimal.ROUND_HALF_UP));
                        }
                        sfjkMzbrxxVo.setYblx("01");
                        sfjkMzbrxxVo.setYbfy(BigDecimal.ZERO);
                        MzVisitTable mzVisitTable = mzVisitTableService.queryByPatientIdAndTimes(mz.getPatientId(), mz.getTimes());
                        if (mzVisitTable != null) {
                            sfjkMzbrxxVo.setIcd(mzVisitTable.getIcdCode());
                            sfjkMzbrxxVo.setMzzd(mzVisitTable.getIcdText());
                            sfjkMzbrxxVo.setBrzz(mzVisitTable.getIcdText());
                        }
                        resultList.add(sfjkMzbrxxVo);
                    }
                }
            }
            resultMap.put("code", 0);
            resultMap.put("message", "查询门诊记录表成功");
            resultMap.put("pageViewVo", resultList);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询门诊记录表失败，系统出错，请联系管理员");
            log.error("查询门诊记录表失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }


    /**
     * 门诊已缴费记录明细查询  医患通已收费记录查询
     *
     * @param djh 门诊号
     * @return
     */
    @RequestMapping(value = "/getSfjkMzbrmx", method = {RequestMethod.GET})
    public Map<String, Object> getSfjkMzbrmx(@RequestParam("djh") String djh) {
        Map<String, Object> results = new HashMap<>();
        if (StringUtils.isBlank(djh)) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊已缴费记录明细查询参数为空");
            return results;
        }
        try {
            String[] arr = djh.split("_");
            MzChargeDetail mzChargeDetail = new MzChargeDetail(arr[0], Integer.valueOf(arr[1]));
            mzChargeDetail.setReceiptNo(Integer.valueOf(arr[2]));
            List<MzChargeDetail> mzChargeDetailList = mzChargeDetailService.queryMzChargeDetailByBillItemCode(mzChargeDetail.getPatientId(), mzChargeDetail.getTimes(), mzChargeDetail.getReceiptNo(), PayMarkEnum.CHARGED.code, "2", Arrays.asList("TC"));
            if (mzChargeDetailList == null || mzChargeDetailList.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "未查询到已缴费明细记录");
                return results;
            }
            mzChargeDetailList = mzChargeDetailService.getMzChargeDetails(mzChargeDetailList);
            List<SfjkMzbrmxVo> returnList = new ArrayList<>();
            for (MzChargeDetail mz : mzChargeDetailList) {
                SfjkMzbrmxVo sfjkMzbrmxVo = new SfjkMzbrmxVo();
                sfjkMzbrmxVo.setMzbh(djh);
                sfjkMzbrmxVo.setXmmc(mz.getTcName());
                if (StringUtils.isNotBlank(mz.getSupplyCode())) {
                    MzZdSupplyType mzZdSupplyType = zdSupplyTypeService.selectMzZdSupplyTypeByCode(mz.getSupplyCode());
                    if (mzZdSupplyType != null) {
                        sfjkMzbrmxVo.setYpyf(mzZdSupplyType.getSupplyName());
                    }
                }
                sfjkMzbrmxVo.setJcjg("");
                BigDecimal zsl = BigDecimal.ZERO;
                if (mz.getDrugWin() != null) {
                    zsl = BigDecimal.valueOf(mz.getDrugWin());
                }
                if (mz.getQuantity() == null) {
                    zsl = BigDecimal.ZERO;
                } else {
                    zsl = zsl.multiply(BigDecimal.valueOf(mz.getQuantity()));
                }
                sfjkMzbrmxVo.setZsl(zsl.toString());
                if (mz.getUnitPrice() == null) {
                    sfjkMzbrmxVo.setZfy(BigDecimal.ZERO);
                } else {
                    sfjkMzbrmxVo.setZfy(zsl.multiply(mz.getUnitPrice()));
                }
                returnList.add(sfjkMzbrmxVo);
            }
            results.put("resultCode", 0);
            results.put("resultMessage", "已缴费记录明细查询成功");
            results.put("data", returnList);
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊已缴费记录明细查询失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊缴费订单支付(单病种)
     *
     * @param mzDepositFileVo
     * @return
     */
    @RequestMapping(value = "/payChargeDetailFormDbzsf", method = {RequestMethod.POST})
    public Map<String, Object> payChargeDetailFormDbzsf(@RequestBody MzDepositFileVo mzDepositFileVo) {
        Map<String, Object> results = new HashMap<>();
        if (mzDepositFileVo == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊缴费订单支付参数为空");
            return results;
        }
        if (StringUtils.isBlank(mzDepositFileVo.getPatientId())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "病人编码不能为空");
            return results;
        }
        if (mzDepositFileVo.getTimes() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "病人就诊次数不能为空");
            return results;
        }
        if (mzDepositFileVo.getReceiptNo() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "病人结算次数不能为空");
            return results;
        }
        try {
            MzDepositFile mzDepositFile = new MzDepositFile();
            mzDepositFile.setChequeType(Constants.YBJZ);
            mzDepositFileVo.setMzDepositFiles(Arrays.asList(mzDepositFile));
            mzChargeDetailService.chargeFee(Constants.DBZJF_CODE, mzDepositFileVo);
            results.put("resultCode", 0);
            results.put("resultMessage", "单病种缴费成功");
            results.put("guideListInfo", "");
            return results;
        } catch (Exception e) {
            e.printStackTrace();
            results.put("resultCode", -1);
            results.put("resultMessage", "单病种门诊缴费订单支付失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }


    /**
     * 门诊待缴费记录明细查询（原始数据，给特门查询使用）
     *
     * @param mzChargeDetail
     * @return
     */
    @RequestMapping(value = "/unPaidToFullChargeDetail", method = {RequestMethod.POST})
    public Map<String, Object> unPaidToFullChargeDetail(@RequestBody MzChargeDetail mzChargeDetail) {
        Map<String, Object> results = new HashMap<>();
        if (mzChargeDetail == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊待缴费记录明细查询参数为空");
            return results;
        }
        if (StringUtils.isBlank(mzChargeDetail.getPatientId())) {
            results.put("resultCode", -1);
            results.put("resultMessage", "患者ID不能为空");
            return results;
        }
        if (mzChargeDetail.getTimes() == null) {
            results.put("resultCode", -1);
            results.put("resultMessage", "病人就诊次数不能为空");
            return results;
        }
        try {
            mzChargeDetail.setBillItemCode("100");
            List<MzChargeDetail> mzChargeDetailList = mzChargeDetailService.getTempMzChargeDetailListForUnPaid(mzChargeDetail);
            if (mzChargeDetailList == null || mzChargeDetailList.size() == 0) {
                results.put("resultCode", -1);
                results.put("resultMessage", "未查询到待缴费明细记录");
                return results;
            }
            mzChargeDetailService.getMzChargeDetails(mzChargeDetailList);
            results.put("resultCode", 0);
            results.put("resultMessage", "待缴费记录明细查询成功");
            results.put("data", mzChargeDetailList);
            return results;
        } catch (Exception e) {
            results.put("resultCode", -1);
            results.put("resultMessage", "门诊待缴费记录明细查询失败，请联系管理员");
            log.error("系统异常，错误信息{}", e.getMessage());
            return results;
        }
    }
}
