package thyyxxk.webserver.controller.inpatient;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.constants.sidicts.MedType;
import thyyxxk.webserver.dao.his.inpatient.PatientDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.medicalinsurance.inpatient.ZyPatientInfo;
import thyyxxk.webserver.entity.inpatient.dismiss.*;
import thyyxxk.webserver.entity.inpatient.patient.Patient;
import thyyxxk.webserver.service.medicalinsurance.SiZyService;
import thyyxxk.webserver.service.inpatient.DismissService;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;
import thyyxxk.webserver.utils.TokenUtil;

import java.util.List;

/**
 * @author dj
 */
@Slf4j
@RestController
@RequestMapping("/dismiss")
public class DismissController {
    private final DismissService service;
    private final SiZyService zyService;
    private final PatientDao dao;

    public DismissController(DismissService service, SiZyService zyService, PatientDao dao) {
        this.service = service;
        this.zyService = zyService;
        this.dao = dao;
    }

    @PassToken
    @GetMapping("/getActOrders")
    public ResultVo<List<ActOrderGroup>> getActOrders(@RequestParam("inpatientNo") String inpatientNo,
                                                      @RequestParam("admissTimes") Integer admissTimes) {
        return service.getActOrders(inpatientNo, admissTimes);
    }

    @PostMapping("/dismiss")
    public ResultVo<Object> dismiss(@RequestBody Patient param) {
        param.setStaffId(TokenUtil.getTokenUserId());
        return service.dismiss(param);
    }

    @PostMapping("/dismissCalculate")
    public ResultVo<List<NegativeFee>> calculate(@RequestBody Patient param) {
        param.setStaffId(TokenUtil.getTokenUserId());
        return service.calculateForDismiss(param);
    }

    @PostMapping("/selfPayDismiss")
    public synchronized ResultVo<String> selfPayDismiss(@RequestBody MedinsSettleFee param) {
        param.setStaffId(TokenUtil.getTokenUserId());
        return service.executeDischarging(param);
    }

    @PassToken
    @PostMapping("/verification")
    public ResultVo<List<NegativeFee>> verification(@Validated @RequestBody HrgDismissParam param) {
        if (param.getDismissFlag() == 2 && null == param.getZjdzDatetime()) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "中间断账时，断账时间不能为空。");
        }
        String table = "zy_actpatient";
        Patient p = dao.getPatientInfoForHrg(param.getInpatientNo(), param.getAdmissTimes(), table);
        if (null == p) {
            table = "zy_inactpatient";
            p = dao.getPatientInfoForHrg(param.getInpatientNo(), param.getAdmissTimes(), table);
        }
        if (null == p) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未找到住院号【" + param.getInpatientNo() + "】的在院业务。");
        }
        p.setTable(table);
        p.setStaffId(param.getStaffId());
        p.setMidSetl(param.getDismissFlag() == 2);
        return service.calculateForDismiss(p);
    }

    @PassToken
    @PostMapping("/execute")
    public synchronized ResultVo<String> dismissForHrg(@Validated @RequestBody HrgDismissParam param) {
        if (param.getDismissFlag() == 2 && null == param.getZjdzDatetime()) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "中间断账时，断账时间不能为空。");
        }
        log.info("出院结算：{}", param);
        String table = "zy_actpatient";
        Patient p = dao.getPatientInfoForHrg(param.getInpatientNo(), param.getAdmissTimes(), table);
        if (null == p) {
            table = "zy_inactpatient";
            p = dao.getPatientInfoForHrg(param.getInpatientNo(), param.getAdmissTimes(), table);
        }
        if (null == p) {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未找到住院号【" + param.getInpatientNo() + "】的在院业务。");
        }
        p.setTable(table);
        if (StringUtil.isBlank(p.getMedType()) || p.getMedType().equals(MedType.SELF_PAY.getCode())) {
            MedinsSettleFee feePojo = new MedinsSettleFee();
            feePojo.setInpatientNo(p.getInpatientNo());
            feePojo.setAdmissTimes(p.getAdmissTimes());
            feePojo.setBedNo(p.getBedNo());
            feePojo.setMidSetl(param.getDismissFlag() == 2);
            feePojo.setZjdzDatetime(param.getZjdzDatetime());
            feePojo.setStaffId(param.getStaffId());
            feePojo.setWardCode(p.getAdmissWard());
            feePojo.setDeptCode(p.getAdmissDept());
            feePojo.setTable(table);
            return service.executeDischarging(feePojo);
        } else {
            ZyPatientInfo zyPatientInfo = new ZyPatientInfo();
            zyPatientInfo.setInpatientNo(param.getInpatientNo());
            zyPatientInfo.setAdmissTimes(param.getAdmissTimes());
            zyPatientInfo.setMidSetl(param.getDismissFlag() == 2);
            zyPatientInfo.setZjdzDatetime(param.getZjdzDatetime());
            zyPatientInfo.setStaffId(param.getStaffId());
            return zyService.dischargeProcessing(zyPatientInfo);
        }
    }
}
