package thyyxxk.webserver.controller.zygl;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.Constants;
import thyyxxk.webserver.constants.YesOrNo;
import thyyxxk.webserver.constants.ZyDepositFileStatusEnum;
import thyyxxk.webserver.dao.his.zygl.MzZyReqDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.inpatient.ZyActpatient;
import thyyxxk.webserver.entity.inpatient.charge.MisPosVo;
import thyyxxk.webserver.entity.inpatient.charge.PatientParam;
import thyyxxk.webserver.entity.inpatient.charge.ZyDepositFile;
import thyyxxk.webserver.entity.medicaladvice.medicamanage.MzZyReq;
import thyyxxk.webserver.service.inpatient.charge.TransactionService;
import thyyxxk.webserver.service.zygl.ZyActpatientService;
import thyyxxk.webserver.service.zygl.ZyDepositFileService;
import thyyxxk.webserver.utils.AssertUtil;
import thyyxxk.webserver.utils.IpAddressUtil;
import thyyxxk.webserver.utils.ResultVoUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 预交金处理
 * @Author:lihong
 * @Date: 2025/5/12
 */
@RequestMapping("/advancePaymentDeal")
@RestController
@Slf4j
public class AdvancePaymentDealController {
    @Resource
    private ZyActpatientService zyActpatientService;
    @Resource
    private ZyDepositFileService zyDepositFileService;
    @Resource
    private MzZyReqDao mzZyReqDao;
    @Resource
    private TransactionService transactionService;

    @PostMapping("/queryAdvancePayment")
    public ResultVo<ZyActpatient> queryAdvancePayment(@RequestBody PatientParam param){
        ZyActpatient zyActpatient = zyActpatientService.queryAdvancePayment(param);
      return ResultVoUtil.success(zyActpatient);
    }

    @PostMapping("/queryLastMzZyReq")
    public ResultVo<MzZyReq> queryLastMzZyReq(@RequestBody PatientParam param){
        MzZyReq zyReq = mzZyReqDao.selectLastMzZyReq(param.getInpatientNo());
        return ResultVoUtil.success(zyReq);
    }

    @PostMapping("/queryAdvancePaymentPatientList")
    public ResultVo<List<ZyActpatient>> queryAdvancePaymentPatientList(@RequestBody PatientParam param){
        List<ZyActpatient> zyActpatientList = zyActpatientService.queryAdvancePaymentPatientList(param);
        return ResultVoUtil.success(zyActpatientList);
    }

    @GetMapping("/printZyDepositFile")
    public ResultVo<String> printZyDepositFile(String receiptNo){
        AssertUtil.isnotBlank(receiptNo,"打印收据单号不能为空");
        zyDepositFileService.modifyPrinted(receiptNo);
        return ResultVoUtil.success("打印住院缴费单成功");
    }

    @PostMapping("/queryPrintAdvancePayment")
    public ResultVo<ZyDepositFile> queryPrintAdvancePayment(@RequestBody ZyDepositFile param){
        ZyDepositFile zyDepositFile = zyActpatientService.queryPrintAdvancePayment(param);
        return ResultVoUtil.success(zyDepositFile);
    }

    @PostMapping("/saveAdvancePayment")
    public ResultVo saveAdvancePayment(@RequestBody ZyDepositFile param, HttpServletRequest request){
       zyDepositFileService.saveZyDepositFile(param);
        Map<String,Object> res = new HashMap(10);
        String warMessage = "";
        res.put("inpatientNo", param.getInpatientNo());
        res.put("receiptNo", param.getReceiptNo());
       //pos退费 1.原路退费
       if(ZyDepositFileStatusEnum.STRAIGHT_BACK.code.equals(param.getStatus()) && YesOrNo.YES.getCode().equals(param.getRefundType())){
            ZyDepositFile zyDepositFile = zyDepositFileService.queryZyDepositFileByReceiptNo(param.getReceiptNo());
            if(StrUtil.isNotBlank(zyDepositFile.getPsordnum()) && Arrays.asList(Constants.YLK, Constants.JHZF).contains(zyDepositFile.getDepoType())){
                String ipAddress = IpAddressUtil.getIPAddress(request);
                MisPosVo misPosVo = new MisPosVo();
                misPosVo.setIp(ipAddress);
                misPosVo.setAmt(zyDepositFile.getDepoAmount().setScale(2, BigDecimal.ROUND_HALF_UP));
                misPosVo.setPsordnum(zyDepositFile.getPsordnum());
                misPosVo.setAgtordnum(zyDepositFile.getAgtordnum());
                misPosVo.setChequeType(zyDepositFile.getDepoType());
                misPosVo.setParChannel(zyDepositFile.getParChannel());
                misPosVo.setTransDate(zyDepositFile.getTransDate());
                try {
                    ResultVo resultVo = transactionService.posPost(misPosVo, "refund");
                    if(ExceptionEnum.SUCCESS_AND_EL_MESSAGE.getCode() != resultVo.getCode()){
                        warMessage = "住院预交金已退，但是患者智能POS退款失败,请去财务工具查询并退款,错误信息"+resultVo.getMessage();
                    }
                }catch (Exception e){
                    log.error("[saveAdvancePayment]调用pos报错:",e);
                    warMessage = "住院预交金已退，但是患者智能POS退款失败，请去财务工具查询并退款,错误信息:"+e.getMessage();
                }
            }
        }
        res.put("warMessage", warMessage);
        return ResultVoUtil.success(res);
    }



}
