package cn.hnthyy.thmz.controller.mz;

import cn.hnthyy.thmz.Utils.NumberToCN;
import cn.hnthyy.thmz.Utils.TokenUtil;
import cn.hnthyy.thmz.comment.UserLoginToken;
import cn.hnthyy.thmz.common.Constants;
import cn.hnthyy.thmz.entity.MzException;
import cn.hnthyy.thmz.entity.his.mz.*;
import cn.hnthyy.thmz.entity.his.zd.ZdChequeType;
import cn.hnthyy.thmz.entity.thmz.User;
import cn.hnthyy.thmz.enums.YesNoEnum;
import cn.hnthyy.thmz.pageDto.MzChargeDetailPageDto;
import cn.hnthyy.thmz.service.his.mz.*;
import cn.hnthyy.thmz.service.his.zd.ZdChequeTypeService;
import cn.hnthyy.thmz.service.thmz.UserService;
import cn.hnthyy.thmz.vo.MzReceiptSerialVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class MzReceiptSerialController {
    @Autowired
    private MzReceiptSerialService mzReceiptSerialService;
    @Autowired
    private MzChargeDetailService mzChargeDetailService;
    @Autowired
    private MzDrugWinService mzDrugWinService;
    @Autowired
    private UserService userService;
    @Autowired
    private MzDepositFileService mzDepositFileService;
    @Autowired
    private MzPatientMiService mzPatientMiService;
    @Autowired
    private ZdChequeTypeService zdChequeTypeService;

    /**
     * 查询病人的记账总额
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getTallyTotalCharge", method = {RequestMethod.GET})
    public Map<String, Object> getTallyTotalCharge(@RequestParam("patientId") String patientId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人编号参数不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "病人的记账总额查询成功");
            resultMap.put("data", mzReceiptSerialService.queryTallyTotalCharge(patientId));
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "病人的记账总额查询失败，系统出错，请联系管理员");
            log.error("病人的记账总额查询失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }


    /**
     * 查询病人的记账信息
     *
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getTallyReceiptSerial", method = {RequestMethod.GET})
    public Map<String, Object> getTallyReceiptSerial(@RequestParam("patientId") String patientId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人编号参数不能为空");
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "病人的记账信息查询成功");
            List<MzReceiptSerial> mzReceiptSerials = mzReceiptSerialService.queryTallyReceiptSerial(patientId);
            if (mzReceiptSerials != null && mzReceiptSerials.size() > 0) {
                MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
                mzReceiptSerials.forEach(m -> {
                    if (Constants.JZ_IC_CARD.equals(mzPatientMi.getIcCardNo())) {
                        String name = mzChargeDetailService.queryJZName(patientId, m.getTimes());
                        if (StringUtils.isNotBlank(name)) {
                            m.setName(name);
                        }
                    } else {
                        m.setName(mzPatientMi.getName());
                    }
                });
            }
            resultMap.put("data", mzReceiptSerials);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "病人的记账信息查询失败，系统出错，请联系管理员");
            log.error("病人的记账信息查询失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }


    /**
     * 根据病人编号和就诊次数查询其当次缴费对应的所有发票
     *
     * @param patientId
     * @param times
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllSerialForThisTime", method = {RequestMethod.GET})
    public Map<String, Object> getAllSerialForThisTime(@RequestParam("patientId") String patientId, @RequestParam("times") Integer times, @RequestParam("receiptNo") Integer receiptNo, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人编号参数不能为空");
                return resultMap;
            }
            if (times == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人就诊不能为空");
                return resultMap;
            }
            if (receiptNo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人缴费次数不能为空");
                return resultMap;
            }
            MzDepositFile mz = new MzDepositFile(patientId, times);
            mz.setReceiptNo(receiptNo);
            List<MzDepositFile> mzDepositFiles = mzDepositFileService.queryMzDepositFile(mz);
            //非本院记账 和非医保记账金额
            BigDecimal totalPay = BigDecimal.ZERO;
            //所有付款方式总金额
            BigDecimal fullTotalPay = BigDecimal.ZERO;
            if (mzDepositFiles != null && mzDepositFiles.size() > 0) {
                for (MzDepositFile mzDepositFile : mzDepositFiles) {
                    if (!Constants.BYJZ.equals(mzDepositFile.getChequeType()) && !Constants.YBJZ.equals(mzDepositFile.getChequeType())) {
                        totalPay = totalPay.add(mzDepositFile.getAmount());
                    }
                    fullTotalPay=fullTotalPay.add(mzDepositFile.getAmount());
                }
            }
            if (BigDecimal.ZERO.compareTo(totalPay) == 0) {
                resultMap.put("code", -1);
                resultMap.put("message", "当前病人没有实际缴费，暂时不打印发票");
                log.info("当前病人没有实际缴费，暂时不打印发票，patientId={},time={},receiptNo={}", patientId, times, receiptNo);
                return resultMap;
            }
            User tokenUser = TokenUtil.getUser(httpServletRequest);
            resultMap.put("code", 0);
            resultMap.put("message", "根据病人编号和就诊次数查询其当次缴费对应的所有发票成功");
            //将打印标志改为已经打印
            //mzChargeDetailService.modifyPrintFlag(patientId,timesList);
            MzReceiptSerial mzReceiptSerial = mzReceiptSerialService.queryAllSerialForThisTime(patientId, times, receiptNo);
            if(mzReceiptSerial == null){
                mzReceiptSerialService.repairDataFromBack(patientId, times, receiptNo);
                mzReceiptSerial = mzReceiptSerialService.queryAllSerialForThisTime(patientId, times, receiptNo);
            }
            if (StringUtils.isBlank(mzReceiptSerial.getChequeType())) {
                resultMap.put("code", -1);
                resultMap.put("message", "当前病人发票表中的收费类型为空，无法打印发票，请及时联系管理员");
                log.info("当前病人发票表中的收费类型为空，无法打印发票，请及时联系管理员，patientId={},time={},receiptNo={}", patientId, times,receiptNo);
                return resultMap;
            }
            String receipt = mzReceiptSerialService.printReceiptSerial(mzReceiptSerial, tokenUser);
            List<Integer> timesList = new ArrayList<>();
            List<MzReceiptSerialVo> mzReceiptSerialVos = getMzReceiptSerialVos(mzReceiptSerial, timesList, receipt);
            resultMap.put("data", mzReceiptSerialVos);
            MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
            if (Constants.JZ_IC_CARD.equals(mzPatientMi.getIcCardNo())) {
                String name = mzChargeDetailService.queryJZName(patientId, times);
                if (StringUtils.isNotBlank(name)) {
                    mzPatientMi.setName(name);
                }
            }
            resultMap.put("mzPatientMi", mzPatientMi);
            log.info("病人id=" + patientId + "，times=" + times + "的发票已经预打印了");
            return resultMap;
        } catch (MzException e) {
            resultMap.put("code", -1);
            resultMap.put("message", e.getMessage());
            log.error("打印发票失败，系统异常，错误信息{}", e);
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "根据病人编号和就诊次数查询其当次缴费对应的所有发票失败，系统出错，请联系管理员");
            log.error("根据病人编号和就诊次数查询其当次缴费对应的所有发票失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }

    /**
     * 将发票列表转换成发票视图列表
     *
     * @param mzReceiptSerial
     * @param timesList
     * @return
     */
    private List<MzReceiptSerialVo> getMzReceiptSerialVos(MzReceiptSerial mzReceiptSerial, List<Integer> timesList, String receipt) {
        List<MzReceiptSerialVo> mzReceiptSerialVos = new ArrayList<>();
        MzReceiptSerialVo mzReceiptSerialVo = new MzReceiptSerialVo();
        mzReceiptSerialVo.setMzReceiptSerial(mzReceiptSerial);
        List<String> feeList = new ArrayList<>();
        if (mzReceiptSerial.getCharge1() != null && mzReceiptSerial.getCharge1().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("挂号:" + mzReceiptSerial.getCharge1().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge2() != null && mzReceiptSerial.getCharge2().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("床位:" + mzReceiptSerial.getCharge2().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge3() != null && mzReceiptSerial.getCharge3().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("诊察:" + mzReceiptSerial.getCharge3().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge4() != null && mzReceiptSerial.getCharge4().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("检查:" + mzReceiptSerial.getCharge4().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge5() != null && mzReceiptSerial.getCharge5().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("治疗:" + mzReceiptSerial.getCharge5().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge6() != null && mzReceiptSerial.getCharge6().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("护理:" + mzReceiptSerial.getCharge6().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge7() != null && mzReceiptSerial.getCharge7().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("手术:" + mzReceiptSerial.getCharge7().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge8() != null && mzReceiptSerial.getCharge8().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("化验:" + mzReceiptSerial.getCharge8().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge9() != null && mzReceiptSerial.getCharge9().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("其他:" + mzReceiptSerial.getCharge9().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge10() != null && mzReceiptSerial.getCharge10().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("西药:" + mzReceiptSerial.getCharge10().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge11() != null && mzReceiptSerial.getCharge11().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("成药:" + mzReceiptSerial.getCharge11().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge12() != null && mzReceiptSerial.getCharge12().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("草药:" + mzReceiptSerial.getCharge12().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge13() != null && mzReceiptSerial.getCharge13().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("特材:" + mzReceiptSerial.getCharge13().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge14() != null && mzReceiptSerial.getCharge14().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("输血:" + mzReceiptSerial.getCharge14().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge15() != null && mzReceiptSerial.getCharge15().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("鉴定:" + mzReceiptSerial.getCharge15().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge16() != null && mzReceiptSerial.getCharge16().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("消毒:" + mzReceiptSerial.getCharge16().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge17() != null && mzReceiptSerial.getCharge17().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("器械:" + mzReceiptSerial.getCharge17().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge18() != null && mzReceiptSerial.getCharge18().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("材料:" + mzReceiptSerial.getCharge18().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge19() != null && mzReceiptSerial.getCharge19().compareTo(BigDecimal.ZERO) == 1) {
            feeList.add("麻醉:" + mzReceiptSerial.getCharge19().setScale(2, BigDecimal.ROUND_FLOOR));
        }
        if (mzReceiptSerial.getCharge20() != null && mzReceiptSerial.getCharge20().compareTo(BigDecimal.ZERO) != 0) {
            feeList.add("舍入:" + mzReceiptSerial.getCharge20().setScale(2, BigDecimal.ROUND_FLOOR));
        }
//        if (totalPay.compareTo(BigDecimal.ZERO) == 1) {
//            mzReceiptSerial.setTotalCharge(totalPay);
//        }
        if (mzReceiptSerial.getTotalCharge() != null) {
            mzReceiptSerial.setTotalCharge(mzReceiptSerial.getTotalCharge().setScale(1, BigDecimal.ROUND_FLOOR));
            mzReceiptSerialVo.setTotalChargeStr(NumberToCN.number2CNMontrayUnit(mzReceiptSerial.getTotalCharge()));
        }
        mzReceiptSerialVo.setFeeList(feeList);
        List<MzChargeDetail> mzChargeDetailList = mzChargeDetailService.queryFyJl(mzReceiptSerial.getPatientId(), mzReceiptSerial.getTimes());
        if (mzChargeDetailList != null && mzChargeDetailList.size() > 0) {
            List<String> qyxxs = new ArrayList<>();
            StringBuffer sbf = new StringBuffer();
            for (MzChargeDetail mzChargeDetail : mzChargeDetailList) {
                if (mzChargeDetail != null) {
                    if (StringUtils.isNotBlank(mzChargeDetail.getGroupNo()) && StringUtils.isNotBlank(mzChargeDetail.getWindowsNoYf())) {
                        MzDrugWin mzDrugWin = mzDrugWinService.queryMzDrugWinByGroupNoAndWinNo(mzChargeDetail.getGroupNo(), mzChargeDetail.getWindowsNoYf());
                        if (mzDrugWin != null) {
                            sbf.append(mzDrugWin.getWinName());
                            sbf.append(",");
                            sbf.append(mzChargeDetail.getBackSerial());
                            qyxxs.add(sbf.toString());
                            sbf.setLength(0);
                        }
                    }
                }
            }
            mzReceiptSerialVo.setQyxxs(qyxxs);
        }
        setChargeDate(mzReceiptSerial, mzReceiptSerialVo);
        if (StringUtils.isNotBlank(mzReceiptSerial.getOperatorId())) {
            User user = userService.queryUserByUserIdCode(mzReceiptSerial.getOperatorId());
            if (user != null) {
                mzReceiptSerial.setOperatorId(user.getUserName());
            }
            if (Constants.BRZZJF_CODE.equals(mzReceiptSerial.getPayId())) {
                mzReceiptSerial.setOperatorId(mzReceiptSerial.getOperatorId() + "(" + Constants.BRZZJF_NAME + ")");
            }
        }
        //退费重收的发票号码都是大于10的拼接字符串
        if (StringUtils.isNotBlank(mzReceiptSerial.getReceiptBill()) && mzReceiptSerial.getReceiptBill().length() > 8 && Constants.BYJZ.equals(mzReceiptSerial.getChequeType())) {
            mzReceiptSerial.setReceiptBill(receipt + "(退费重收)");
        } else {
            mzReceiptSerial.setReceiptBill(receipt);
        }
        mzReceiptSerialVos.add(mzReceiptSerialVo);
        timesList.add(mzReceiptSerial.getTimes());
        return mzReceiptSerialVos;
    }

    /**
     * 设置发票的年月日
     *
     * @param mzReceiptSerial
     * @param mzReceiptSerialVo
     */
    private void setChargeDate(MzReceiptSerial mzReceiptSerial, MzReceiptSerialVo mzReceiptSerialVo) {
        Date chargeDate = mzReceiptSerial.getChargeDate();
        if (chargeDate == null) {
            chargeDate = new Date();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(chargeDate);
        mzReceiptSerialVo.setYear(calendar.get(Calendar.YEAR));
        Integer month = calendar.get(Calendar.MONTH) + 1;
        if (month < 10) {
            mzReceiptSerialVo.setMonth("0" + month);
        } else {
            mzReceiptSerialVo.setMonth(month.toString());
        }
        Integer day = calendar.get(Calendar.DAY_OF_MONTH);
        if (day < 10) {
            mzReceiptSerialVo.setDay("0" + day);
        } else {
            mzReceiptSerialVo.setDay(day.toString());
        }
    }

    /**
     * 查询当前发票号码
     *
     * @param patientId
     * @param times
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getReceiptForThisTime", method = {RequestMethod.GET})
    public Map<String, Object> getReceiptForThisTime(@RequestParam("patientId") String patientId, @RequestParam("times") Integer times, @RequestParam("receiptNo") Integer receiptNo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人编号参数不能为空");
                return resultMap;
            }
            if (times == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人就诊次数不能为空");
                return resultMap;
            }
            if (receiptNo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人缴费次数不能为空");
                return resultMap;
            }
            List<MzReceiptSerial> mzReceiptSerials = mzReceiptSerialService.queryPrintedSerialForThisTime(patientId, times, null);
            if (mzReceiptSerials != null && mzReceiptSerials.size() > 0) {
                List<String> receiptBills = new ArrayList<>();
                for (MzReceiptSerial mzReceiptSerial : mzReceiptSerials) {
                    if (StringUtils.isNotBlank(mzReceiptSerial.getReceiptBill()) && receiptNo.equals(mzReceiptSerial.getReceiptNo())) {
                        receiptBills.add(mzReceiptSerial.getReceiptBill());
                    }
                }
                resultMap.put("code", 0);
                resultMap.put("message", "查询当前发票号码成功");
                resultMap.put("data", mzReceiptSerials);
                return resultMap;
            }
            resultMap.put("code", -1);
            resultMap.put("message", "查询当前发票号码失败，系统出错，请联系管理员");
            log.error("查询当前发票号码失败");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "查询当前发票号码失败，系统出错，请联系管理员");
            log.error("查询当前发票号码失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }


    /**
     * 重新打印当前发票 或者作废当前发票重打
     *
     * @param patientId
     * @param times
     * @param obsolete  Yes 作废重打  no 重新打印
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/repPrintForThisTime", method = {RequestMethod.GET})
    public Map<String, Object> repPrintForThisTime(@RequestParam("patientId") String patientId, @RequestParam("times") Integer times, @RequestParam("obsolete") String obsolete, @RequestParam("receiptBill") String receiptBill, HttpServletRequest httpServletRequest) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人编号参数不能为空");
                return resultMap;
            }
            if (times == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人编号参数不能为空");
                return resultMap;
            }
            if (StringUtils.isBlank(receiptBill)) {
                resultMap.put("code", -1);
                resultMap.put("message", "需要重打的发票号码不能为空");
                return resultMap;
            }
            String token = TokenUtil.getToken(httpServletRequest);
            if (StringUtils.isBlank(token)) {
                resultMap.put("code", -1);
                resultMap.put("message", "缴费失败,用户Token不存在");
                return resultMap;
            }
            List<MzDepositFile> mzDepositFiles = mzDepositFileService.queryMzDepositFile(new MzDepositFile(patientId, times));
            //非本院记账金额
            BigDecimal totalPay = BigDecimal.ZERO;
            if (mzDepositFiles != null && mzDepositFiles.size() > 0) {
                for (MzDepositFile mzDepositFile : mzDepositFiles) {
                    if (!Constants.BYJZ.equals(mzDepositFile.getChequeType())) {
                        totalPay = totalPay.add(mzDepositFile.getAmount());
                    }
                }
            }
            if (BigDecimal.ZERO.compareTo(totalPay) == 0) {
                resultMap.put("code", -1);
                resultMap.put("message", "当前病人没有实际缴费，暂时不打印发票");
                log.info("当前病人没有实际缴费，暂时不打印发票，patientId={},time={}", patientId, times);
                return resultMap;
            }
            resultMap.put("code", 0);
            resultMap.put("message", "重新打印当前发票成功");
            List<MzReceiptSerial> mzReceiptSerials = mzReceiptSerialService.queryPrintedSerialForThisTime(patientId, times, receiptBill);
            MzReceiptSerial mzReceiptSerial = mzReceiptSerials.get(0);
            String receipt = mzReceiptSerial.getReceiptBill();
            if (YesNoEnum.YES.code.toString().equals(obsolete)) {
                User tokenUser = TokenUtil.getUser(httpServletRequest);
                receipt = mzReceiptSerialService.obsoleteAndRepPrint(mzReceiptSerial, tokenUser);
                resultMap.put("message", "作废重新打印发票成功");
            }
            List<Integer> timesList = new ArrayList<>();
            List<MzReceiptSerialVo> mzReceiptSerialVos = getMzReceiptSerialVos(mzReceiptSerial, timesList, receipt);
            resultMap.put("data", mzReceiptSerialVos);

            MzPatientMi mzPatientMi = mzPatientMiService.queryByPatientId(patientId);
            if (Constants.JZ_IC_CARD.equals(mzPatientMi.getIcCardNo())) {
                String name = mzChargeDetailService.queryJZName(patientId, times);
                if (StringUtils.isNotBlank(name)) {
                    mzPatientMi.setName(name);
                }
            }
            resultMap.put("mzPatientMi", mzPatientMi);
            log.info("病人id=" + patientId + "，times=" + times + "的发票已经重新打印了");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "重新打印当前发票失败，系统出错，请联系管理员");
            log.error("重新打印当前发票失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }




    /**
     * 根据病人编号和就诊次数查询其当次缴费对应的所有支付方式列表
     *
     * @param patientId
     * @param times
     * @return
     */
    @UserLoginToken
    @RequestMapping(value = "/getAllDepositFileForThisTime", method = {RequestMethod.GET})
    public Map<String, Object> getAllDepositFileForThisTime(@RequestParam("patientId") String patientId, @RequestParam("times") Integer times, @RequestParam("receiptNo") Integer receiptNo) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if (StringUtils.isBlank(patientId)) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人编号参数不能为空");
                return resultMap;
            }
            if (times == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人就诊不能为空");
                return resultMap;
            }
            if (receiptNo == null) {
                resultMap.put("code", -1);
                resultMap.put("message", "病人缴费次数不能为空");
                return resultMap;
            }
            MzDepositFile mz = new MzDepositFile(patientId, times);
            mz.setReceiptNo(receiptNo);
            List<MzDepositFile> mzDepositFiles = mzDepositFileService.queryMzDepositFile(mz);
            List<ZdChequeType> chequeTypes = zdChequeTypeService.queryAllZdChequeTypeForMz();
            Map<String, String> chequeTypeMap = null;
            if (chequeTypes != null) {
                chequeTypeMap = chequeTypes.stream().collect(Collectors.toMap(ZdChequeType::getCode, ZdChequeType::getName));
            }
            if(chequeTypes!=null && chequeTypes.size()>0){
                for(MzDepositFile zd:mzDepositFiles){
                    if (chequeTypeMap != null) {
                        String paymode = chequeTypeMap.get(zd.getChequeType());
                        zd.setChequeTypeName(paymode == null ? "" : paymode);
                    }
                }
            }
            resultMap.put("code", 0);
            resultMap.put("data", mzDepositFiles);
            resultMap.put("message", "根据病人编号和就诊次数查询其当次缴费对应的所有支付方式列表成功");
            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("code", -1);
            resultMap.put("message", "根据病人编号和就诊次数查询其当次缴费对应的所有支付方式列表失败，系统出错，请联系管理员");
            log.error("根据病人编号和就诊次数查询其当次缴费对应的所有支付方式列表失败，系统异常，错误信息{}", e.getMessage());
            return resultMap;
        }
    }

}
