package thyyxxk.webserver.controller.jiekou;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.IsProd;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.service.jiekou.MedicalTechnologyIsPaidService;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

/**
 * <p>
 * 描述： 对外医技确费接口
 * </p>
 *
 * @author xc
 * @date 2021-07-13 08:04
 */
@RestController
@RequestMapping("/thyy/med")
@Slf4j
public class MedicalTechnologyIsPaidController {
    private final MedicalTechnologyIsPaidService service;

    @Autowired
    public MedicalTechnologyIsPaidController(MedicalTechnologyIsPaidService service) {
        this.service = service;
    }

    @GetMapping("/medConfirm")
    @PassToken
    @IsProd
    public ResultVo<String> medConfirm(@RequestParam("reqNo") Integer reqNo,
                                       @RequestParam("confirmId") String confirmId,
                                       @RequestParam("type") Integer type) {

        ResultVo<String> INVALID_PARAM = verify(reqNo, confirmId, type);

        if (INVALID_PARAM != null) return INVALID_PARAM;

        if (type == 1) {
            return service.mzProject(reqNo, confirmId, "1");
        } else if (type == 2) {
            return service.zyProject(reqNo, confirmId);
        }

        return ResultVoUtil.success(ExceptionEnum.SUCCESS);
    }

    @GetMapping("/medCancel")
    @PassToken
    @IsProd
    public ResultVo<String> medCancel(@RequestParam("reqNo") Integer reqNo,
                                      @RequestParam("confirmId") String confirmId,
                                      @RequestParam("type") Integer type) {
        ResultVo<String> INVALID_PARAM = verify(reqNo, confirmId, type);
        if (INVALID_PARAM != null) return INVALID_PARAM;
        if (type == 1) {
            return service.mzProject(reqNo, confirmId, "3");
        } else if (type == 2) {
            return service.zyProjectCancel(reqNo, confirmId);
        }
        return ResultVoUtil.success(ExceptionEnum.SUCCESS);
    }

    @PassToken
    @PostMapping("/medConfirmList")
    @IsProd
    public ResultVo<String> medConfirmList(@RequestBody MedicalTechnologyIsPaidService.ConfirmList param) {
        if (param == null) {
            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "参数为空");
        }
        if (ListUtil.notBlank(param.getReqNoAndPatNoList())) {
            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "申请唯一id不能为空。");
        }
        return service.medConfirmList(param.getReqNoAndPatNoList(), param.getConfirmId());
    }

    @Nullable
    private static ResultVo<String> verify(Integer reqNo, String confirmId, Integer type) {
        if (reqNo == null) {
            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "申请号为空");
        }

        if (StringUtil.isBlank(confirmId)) {
            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "确认人为空");
        }

        if (type == null) {
            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "类型为空");
        }
        return null;
    }


}
