package thyyxxk.webserver.controller.medicalinsurance;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.markmtfees.ModifyFundAmt;
import thyyxxk.webserver.entity.markmtfees.SiMzBusinessParams;
import thyyxxk.webserver.entity.medicalinsurance.onlinepayment.EcToken;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.MzPatientInfo;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.SpcChrDiseAcct;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.FundDetail;
import thyyxxk.webserver.service.medicalinsurance.SiMzService;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/markMtFees")
public class MarkMtFeesController {
    private final SiMzService mzService;

    @Autowired
    public MarkMtFeesController(SiMzService mzService) {
        this.mzService = mzService;
    }

    /**
     * HRG查询门特备案
     * */
    @PassToken
    @GetMapping("/fetchSpcSlwinfo")
    public ResultVo<List<SpcChrDiseAcct>> fetchSpcSlwinfo(@RequestParam("socialNo") String socialNo) {
        return mzService.fetchSpcSlwinfo(socialNo);
    }

    /**
     * HRG调用试算
     * */
    @PassToken
    @PostMapping("/uploadFees")
    public Map<String, Object> uploadFees(@RequestBody SiMzBusinessParams param) {
        MzPatientInfo mz = new MzPatientInfo();
        mz.setPatNo(param.getPatientId());
        mz.setTimes(param.getTimes());
        mz.setStaffId(param.getStaffId());
        ResultVo<FundDetail> result = mzService.outpatientPreSettlement(mz);
        return getStringObjectMap(param, result, "收费窗口获取门特报销");
    }

    /**
     * HRG调用结算
     * */
    @PassToken
    @PostMapping("/calculateCost")
    public Map<String, Object> settlement(@RequestBody SiMzBusinessParams param) {
        MzPatientInfo mz = new MzPatientInfo();
        mz.setPatNo(param.getPatientId());
        mz.setTimes(param.getTimes());
        mz.setStaffId(param.getStaffId());
        ResultVo<FundDetail> result = mzService.outpatientSettlement(mz);
        return getStringObjectMap(param, result, "收费窗口门特结算");
    }

    /**
     * HRG调用取消结算
     * */
    @PassToken
    @PostMapping("/retractFees")
    public Map<String, Object> revokeSettlement(@RequestBody SiMzBusinessParams param) {
        MzPatientInfo mz = new MzPatientInfo();
        mz.setPatNo(param.getPatientId());
        mz.setTimes(param.getTimes());
        mz.setStaffId(param.getStaffId());
        mz.setNeedRevokeRegistration(1);
        ResultVo<FundDetail> result = mzService.revokeOutpatientSettlement(mz);
        mzService.insertSiPatInfo(mz);
        return getStringObjectMap(param, result, "收费窗口撤销门特结算");
    }

    private Map<String, Object> getStringObjectMap(SiMzBusinessParams param, ResultVo<FundDetail> result, String logText) {
        Map<String, Object> map = new HashMap<>();
        if (null == result) {
            map.put("code", -2);
            map.put("msg", "医保中心网络异常！");
            return map;
        }
        if (result.getCode() != ExceptionEnum.SUCCESS.getCode()) {
            map.put("code", -2);
            if (null == result.getMessage()) {
                map.put("msg", "医保中心网络异常！");
            } else {
                map.put("msg", result.getMessage());
            }
            return map;
        }
        FundDetail fund = result.getData();
        Map<String, Object> payInfo = new HashMap<>();
        payInfo.put("totalCost", fund.getTotalCost());
        payInfo.put("fundPay", fund.getFundPay());
        payInfo.put("cashPay", fund.getCashPay());
        payInfo.put("acctPay", fund.getAcctPay());
        payInfo.put("selfPay", fund.getSelfPay());
        payInfo.put("cardType", fund.getCardType());
        map.put("code", 0);
        map.put("msg", "成功。");
        map.put("payInfo", payInfo);
        log.info("{}：参数：{}，结果：{}", logText, param, map);
        return map;
    }

    @PassToken
    @PostMapping("/queryInsuinfo")
    public Map<String, Object> queryInsuinfo(@RequestBody SiMzBusinessParams param) {
        return mzService.queryInsuinfo(param);
    }

    @PassToken
    @PostMapping("/directRegistration")
    public Map<String, Object> directRegistration(@RequestBody SiMzBusinessParams param) {
        log.info("门诊共济：{}", param);
        ResultVo<FundDetail> result = mzService.directRegistration(param);
        return getStringObjectMap(param, result, "医生保存处方试算");
    }

    @PassToken
    @GetMapping("/isPatientDuringSiSettle")
    public Map<String, Object> isPatientDuringSiSettle(@RequestParam("patientId") String patientId) {
        ResultVo<Integer> resvo = mzService.isPatientDuringSiSettle(patientId);
        Map<String, Object> map = new HashMap<>();
        if (resvo.getData() == 1) {
            map.put("code", -1);
            map.put("msg", "此患者正处于医保统筹进程中，请稍候。");
        } else {
            map.put("code", 0);
            map.put("msg", "OK");
        }
        return map;
    }

    @PassToken
    @PostMapping("/modifyFundPayAmount")
    public Map<String, Object> modifyFundPayAmount(@RequestBody ModifyFundAmt modifyFundAmt) {
        return mzService.modifyFundPayAmount(modifyFundAmt);
    }

    @PassToken
    @PostMapping("/payOnline")
    public ResultVo<String> payOnline(@RequestBody EcToken token) {
        return ResultVoUtil.success(token.getEcToken());
    }

}
