package thyyxxk.webserver.controller.medicalinsurance;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.injury.InjuryCareQualification;
import thyyxxk.webserver.entity.medicalinsurance.injury.InjuryEmployeeInquiry;
import thyyxxk.webserver.entity.inpatient.dismiss.MedinsSettleFee;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.entity.medicalinsurance.query.QryPsnBsInfo;
import thyyxxk.webserver.entity.medicalinsurance.setlinfo.FundDetail;
import thyyxxk.webserver.service.externalhttp.SiInjuryFeeUpld;
import thyyxxk.webserver.service.externalhttp.SiInjurySystem;
import thyyxxk.webserver.service.inpatient.DismissService;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/siInjury")
public class SiInjuryController {
    private final DismissService dismissService;
    private final SiInjurySystem injurySystem;
    private final SiInjuryFeeUpld injuryFeeUpld;
    @Value("${si-injury-fee-url}")
    private String siInjuryFeeUrl;
    @Value("${si-injury-systm-url}")
    private String siInjurySystmUrl;

    @Autowired
    public SiInjuryController(DismissService dismissService, SiInjurySystem injurySystem, SiInjuryFeeUpld injuryFeeUpld) {
        this.dismissService = dismissService;
        this.injurySystem = injurySystem;
        this.injuryFeeUpld = injuryFeeUpld;
    }

    @PostMapping("/employeeInfoInquiry")
    public ResultVo<List<InjuryCareQualification>> getPersonBaseinfo(@RequestBody Patient p) {
        InjuryEmployeeInquiry inquiry = new InjuryEmployeeInquiry();
        inquiry.setAac002(p.getSocialNo());
        inquiry.setPatNo(p.getInpatientNo());
        inquiry.setTimes(p.getAdmissTimes());
        inquiry.setLedgerSn(p.getLedgerSn());
        inquiry.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.getPersonBaseinfo(siInjurySystmUrl, inquiry);
    }

    @PostMapping("/employeeInfoInquiry2")
    public ResultVo<List<InjuryCareQualification>> employeeInfoInquiry2(@RequestBody QryPsnBsInfo p) {
        InjuryEmployeeInquiry inquiry = new InjuryEmployeeInquiry();
        inquiry.setPatNo(p.getPatNo());
        inquiry.setTimes(p.getTimes());
        inquiry.setLedgerSn(p.getLedgerSn());
        inquiry.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.getPersonBaseinfo(siInjurySystmUrl, inquiry);
    }

    @PostMapping("/inpatientRegister")
    public ResultVo<String> admissRegistration(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.admissRegistration(siInjurySystmUrl, qualification);
    }

    @PostMapping("/inpatientCostUpload")
    public ResultVo<String> uploadFees(@RequestBody Patient p) {
        InjuryCareQualification qualification = new InjuryCareQualification();
        qualification.setPatNo(p.getInpatientNo());
        qualification.setTimes(p.getAdmissTimes());
        qualification.setLedgerSn(p.getLedgerSn());
        qualification.setSid(p.getSid());
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injuryFeeUpld.uploadFees(siInjuryFeeUrl, qualification);
    }

    @PostMapping("/revokeInpatientRegister")
    public ResultVo<String> cancelRegister(@RequestBody Patient p) {
        InjuryCareQualification qualification = new InjuryCareQualification();
        qualification.setPatNo(p.getInpatientNo());
        qualification.setTimes(p.getAdmissTimes());
        qualification.setLedgerSn(p.getLedgerSn());
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.cancelRegister(siInjurySystmUrl, qualification);
    }

    @PostMapping("/revokeInpatientCost")
    public ResultVo<String> cancelUploadFee(@RequestBody Patient p) {
        InjuryCareQualification qualification = new InjuryCareQualification();
        qualification.setPatNo(p.getInpatientNo());
        qualification.setTimes(p.getAdmissTimes());
        qualification.setLedgerSn(p.getLedgerSn());
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.cancelUploadFee(siInjurySystmUrl, qualification);
    }

    @PostMapping("/inpatientSettlement")
    public ResultVo<String> preCalculateCost(@RequestBody Patient p) {
        InjuryCareQualification qualification = new InjuryCareQualification();
        qualification.setPatNo(p.getInpatientNo());
        qualification.setTimes(p.getAdmissTimes());
        qualification.setLedgerSn(p.getLedgerSn());
        qualification.setCalType("0");
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.preCalculateCost(siInjurySystmUrl, qualification);
    }

    @PostMapping("/inpatientDischargeProcess")
    public synchronized String calculateCost(@RequestBody Patient p) {
        InjuryCareQualification qualification = new InjuryCareQualification();
        qualification.setPatNo(p.getInpatientNo());
        qualification.setTimes(p.getAdmissTimes());
        qualification.setLedgerSn(p.getLedgerSn());
        qualification.setCalType("1");
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        ResultVo<MedinsSettleFee> resultVo = injurySystem.calculateCost(siInjurySystmUrl, qualification);
        if (null == resultVo) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "获取医保结算费用失败。");
        }
        if (!Objects.equals(resultVo.getCode(), ExceptionEnum.SUCCESS.getCode())) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, resultVo.getMessage());
        }
        MedinsSettleFee settleFee = resultVo.getData();
        if (p.getMidSetl()) {
            settleFee.setZjdzDatetime(p.getZjdzDatetime());
        }
        settleFee.setMidSetl(p.getMidSetl());
        settleFee.setStaffId(TokenUtil.getInstance().getTokenUserId());
        settleFee.setInpatientNo(p.getInpatientNo());
        settleFee.setAdmissTimes(p.getAdmissTimes());
        settleFee.setBedNo(p.getBedNo());
        settleFee.setWardCode(p.getAdmissWard());
        settleFee.setDeptCode(p.getAdmissDept());
        return dismissService.executeDischarging(settleFee);
    }

    @PostMapping("/revokeInpatientSettlement")
    public ResultVo<String> cancelCalculate(@RequestBody Patient p) {
        InjuryCareQualification qualification = new InjuryCareQualification();
        qualification.setPatNo(p.getInpatientNo());
        qualification.setTimes(p.getAdmissTimes());
        qualification.setLedgerSn(p.getLedgerSn());
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.cancelCalculate(siInjurySystmUrl, qualification);
    }

    @PostMapping("/outpatientRegister")
    public ResultVo<String> outpatientRegister(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.outpatientRegister(siInjurySystmUrl, qualification);
    }

    @PostMapping("/outpatientFeeUpload")
    public ResultVo<String> outpatientFeeUpload(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.outpatientFeeUpload(siInjurySystmUrl, qualification);
    }

    @PostMapping("/outpatientSettlement")
    public ResultVo<FundDetail> outpatientSettlement(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.outpatientSettlement(siInjurySystmUrl, qualification);
    }

    @PostMapping("/revokeOutpatientFee")
    public ResultVo<String> revokeOutpatientFee(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.revokeOutpatientFee(siInjurySystmUrl, qualification);
    }

    @PostMapping("/outpatientFeeInquiry")
    public ResultVo<JSONObject> outpatientFeeInquiry(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.outpatientFeeInquiry(siInjurySystmUrl, qualification);
    }

    @PostMapping("/revokeOutpatientSettlement")
    public ResultVo<String> revokeOutpatientSettlement(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.revokeOutpatientSettlement(siInjurySystmUrl, qualification);
    }

    @PostMapping("/revokeOutpatientRegistration")
    public ResultVo<String> revokeOutpatientRegistration(@RequestBody InjuryCareQualification qualification) {
        qualification.setStaffId(TokenUtil.getInstance().getTokenUserId());
        return injurySystem.revokeOutpatientRegistration(siInjurySystmUrl, qualification);
    }

}
