package thyyxxk.webserver.controller.medicalinsurance;

import com.alibaba.fastjson.JSONObject;
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.MzDepositFile;
import thyyxxk.webserver.entity.markmtfees.MzReceipt;
import thyyxxk.webserver.entity.markmtfees.OrderNo;
import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.RxIndex;
import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.request.*;
import thyyxxk.webserver.entity.medicalinsurance.manage.clinicinfo.RevokeRegRequest;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.SiMzDiag;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.FundDetail;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.MzPatientInfo;
import thyyxxk.webserver.entity.medicalinsurance.query.SiPatInfo;
import thyyxxk.webserver.entity.medicalinsurance.outpatient.SpcChrDiseAcct;
import thyyxxk.webserver.entity.outpatient.medicalinfo.MzBlRecord;
import thyyxxk.webserver.service.medicalinsurance.SiMzService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.List;
import java.util.Map;

/**
 * @description: 门诊医保入口
 * @author: DingJie
 * @create: 2021/7/2214:24
 */
@RestController
@RequestMapping("/siMz")
public class SiMzController {
    private final SiMzService service;

    @Autowired
    public SiMzController(SiMzService service) {
        this.service = service;
    }

    @PostMapping("/outpatientRegistration")
    public ResultVo<String> outpatientRegistration(@RequestBody MzPatientInfo p) {
        return service.outpatientRegistration(p);
    }

    @PassToken
    @PostMapping("/revokeOutpatientRegistration")
    public ResultVo<String> revokeOutpatientRegistration(@RequestBody MzPatientInfo p) {
        return service.revokeOutpatientRegistration(p);
    }

    @PostMapping("/executeOutpatientRegRevoke")
    public ResultVo<String> executeOutpatientRegRevoke(@RequestBody RevokeRegRequest request) {
        return service.executeOutpatientRegRevoke(request);
    }

    @PassToken
    @PostMapping("/getMzReceipts")
    public ResultVo<List<Map<String, Object>>> getMzReceipts(@RequestBody MzPatientInfo p) {
        return service.getMzReceipts(p);
    }

    @GetMapping("/getHistoryMzReceipts")
    public ResultVo<List<MzDepositFile>> getHistoryMzReceipts(@RequestParam("patNo") String patNo,
                                                              @RequestParam("start") String start,
                                                              @RequestParam("end") String end) {
        return service.getHistoryMzReceipts(patNo, start, end);
    }

    @PostMapping("/getHistoryReceiptDetail")
    public ResultVo<Map<String, Object>> getHistoryReceiptDetail(@RequestBody MzDepositFile depositFile) {
        return ResultVoUtil.success(service.getHistoryReceiptDetail(depositFile));
    }

    @PostMapping("/mergeReceipts")
    public ResultVo<MzDepositFile> mergeReceipts(@RequestBody List<MzDepositFile> list) {
        if (list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "请选择需要合并的处方。");
        }
        return ResultVoUtil.success(service.mergeReceipts(list));
    }

    @PassToken
    @PostMapping("/insertSiMzFees")
    public ResultVo<String> insertSiMzFees(@RequestBody List<MzReceipt> receipts) {
        return service.insertSiMzFees(receipts);
    }

    @PassToken
    @PostMapping("/insertSiMzFeesForSupplement")
    public ResultVo<String> insertSiMzFeesForSupplement(@RequestBody List<MzReceipt> receipts) {
        return service.insertSiMzFeesForSupplement(receipts);
    }

    @PostMapping("/deleteMzReceipt")
    public ResultVo<String> deleteMtFees(@RequestBody OrderNo param) {
        return service.deleteMzReceipt(param);
    }

    @PostMapping("/deleteAllMzReceipts")
    public ResultVo<String> deleteAllMzReceipts(@RequestBody MzPatientInfo p) {
        return service.deleteAllMzReceipts(p);
    }

    @PostMapping("/uploadOutpatientFeeDetails")
    public ResultVo<SiPatInfo> uploadOutpatientFeeDetails(@RequestBody SpcChrDiseAcct p) {
        return service.uploadOutpatientFeeDetails(p);
    }

    @PostMapping("/revokeOutpatientFeeDetails")
    public ResultVo<String> revokeOutpatientFeeDetails(@RequestBody MzPatientInfo p) {
        return service.revokeOutpatientFeeDetails(p);
    }

    @PostMapping("/outpatientPreSettlement")
    public ResultVo<FundDetail> outpatientPreSettlement(@RequestBody MzPatientInfo p) {
        return service.outpatientPreSettlement(p);
    }

    @PostMapping("/outpatientSettlement")
    public ResultVo<FundDetail> outpatientSettlement(@RequestBody MzPatientInfo p) {
        return service.outpatientSettlement(p);
    }

    @PostMapping("/timeoutSetl")
    public ResultVo<FundDetail> timeoutSetl(@RequestBody MzPatientInfo p) {
        return service.timeoutSetl(p);
    }

    @PostMapping("/revokeOutpatientSettlement")
    public ResultVo<FundDetail> revokeOutpatientSettlement(@RequestBody MzPatientInfo p) {
        if (service.receiptAlreadyPrinted(p)) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
                    "患者已打印发票，无法撤销医保结算。如需撤销结算，请到收费窗口做退费处理。");
        }
        return service.revokeOutpatientSettlement(p);
    }

    @PostMapping("/saveSiMzDiags")
    public ResultVo<String> saveSiMzDiags(@RequestBody List<SiMzDiag> diags) {
        return service.saveSiMzDiags(diags);
    }

    @GetMapping("/getMzBlRecord")
    public ResultVo<MzBlRecord> getMzBlRecord(@RequestParam("patientId") String patientId,
                                              @RequestParam("times") Integer times) {
        MzBlRecord mzBlRecord = service.getMzBlRecord(patientId, times);
        if (null == mzBlRecord) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有找到患者本次就诊的病历。");
        }
        return ResultVoUtil.success(mzBlRecord);
    }

    @PostMapping("/getRxIndex")
    public ResultVo<List<RxIndex>> getRxIndex(@RequestBody IndexInquiry inquiry) {
        return ResultVoUtil.success(service.getRxIndex(inquiry));
    }

    @GetMapping("/getRxDetail")
    public ResultVo<String> getRxDetail(@RequestParam("hospRxno") String hospRxno) {
        return ResultVoUtil.success(service.getRxDetail(hospRxno));
    }

    @PassToken
    @PostMapping("/rxPreCheck")
    public ResultVo<JSONObject> rxPreCheck(@RequestBody RxPreCheckRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.rxPreCheck(request);
    }

    @PassToken
    @PostMapping("/rxSign")
    public ResultVo<JSONObject> rxSign(@RequestBody RxSignRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.rxSign(request);
    }

    @PassToken
    @PostMapping("/rxUpload")
    public ResultVo<JSONObject> rxUpload(@RequestBody RxUploadRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.rxUpload(request);
    }

    @PassToken
    @PostMapping("/rxRevoke")
    public ResultVo<JSONObject> rxRevoke(@RequestBody RxRevokeRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.rxRevoke(request);
    }

    @PassToken
    @PostMapping("/rxInfoQuery")
    public ResultVo<JSONObject> rxInfoQuery(@RequestBody RxInfoQueryRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.rxInfoQuery(request);
    }

    @PassToken
    @PostMapping("/rxAuditingQuery")
    public ResultVo<JSONObject> rxAuditingQuery(@RequestBody RxAuditingQueryRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.rxAuditingQuery(request);
    }

    @PassToken
    @PostMapping("/rxSetlQuery")
    public ResultVo<JSONObject> rxSetlQuery(@RequestBody RxSetlQueryRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.rxSetlQuery(request);
    }

    @PassToken
    @PostMapping("/regRevoke")
    public ResultVo<String> regRevoke(@RequestBody RxRevokeRequest request) {
        if (StringUtil.isBlank(request.getStaffId())) {
            request.setStaffId(TokenUtil.getInstance().getTokenUserId());
        }
        return service.regRevoke(request);
    }

    @PassToken
    @PostMapping("/medicineInquiry")
    public ResultVo<JSONObject> medicineInquiry(@RequestBody MedicineInquiry inquiry) throws Exception {
        return service.medicineInquiry(inquiry);
    }
}
