package thyyxxk.webserver.controller.jiekou;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.auth.PassToken;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.datamodify.MzChargeDetail;
import thyyxxk.webserver.entity.jiekou.GetPatientInformationParam;
import thyyxxk.webserver.entity.jiekou.PatientInformation;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.service.PublicServer;
import thyyxxk.webserver.service.jiekou.DuiWaiJieKouService;
import thyyxxk.webserver.service.zhuyuanyiji.MenZhengXiangMuServer;
import thyyxxk.webserver.utils.ListUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 描述： 对深圳博英接口文档
 * </p>
 *
 * @author xc
 * @date 2021-07-13 08:04
 */
@RestController
@RequestMapping("/thyy")
@Slf4j
public class DuiWaiJieKouController {
    private final DuiWaiJieKouService service;
    private final PublicServer publicServer;
    private final MenZhengXiangMuServer mzServer;

    @Autowired
    public DuiWaiJieKouController(DuiWaiJieKouService service, PublicServer publicServer, MenZhengXiangMuServer mzServer) {
        this.service = service;
        this.publicServer = publicServer;
        this.mzServer = mzServer;
    }


    @PassToken
    @PostMapping("/getPatientInformation")
    public ResultVo<List<PatientInformation>> getPatientInformation(@RequestBody GetPatientInformationParam param) {
        return service.getPatientInformation(param);
    }

    /**
     * 门诊项目确认
     *
     * @param mz reqNos 可多个申请号 confirmId 操作人 execDept  执行科室
     * @return 操作提示
     */
    @PassToken
    @PostMapping("/mzProjectConfirm")
    public ResultVo<String> mzProjectConfirm(@RequestBody MzChargeDetail mz) {
        log.info("门诊项目确认接口调用：{}", JSON.toJSONString(mz));
        if (ListUtil.isBlank(mz.getReqNos())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "申请号不能为空。");
        }
        if (StringUtil.isBlank(mz.getConfirmId())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "操作人不能为空");
        }
        UserInfo userInfo = publicServer.huoQuYuanGongXinXi(mz.getConfirmId());
        MzChargeDetail mzChargeDetail = new MzChargeDetail();
        mzChargeDetail.setSource("接口");
        if (userInfo != null) {
            if (mz.getExecDept() == null) {
                mzChargeDetail.setExecDept(userInfo.getDeptCode());
            } else {
                mzChargeDetail.setExecDept(mz.getExecDept());
            }
            mzChargeDetail.setConfirmId(userInfo.getCode());
        } else {
            return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到对应的操作人信息。");
        }
        mzChargeDetail.setXiangMuZhuangTai(0);
        for (Integer reqNo : mz.getReqNos()) {
            if (mzChargeDetail.getList() == null) {
                mzChargeDetail.setList(new ArrayList<>());
            }
            MzChargeDetail chargeDetail = new MzChargeDetail();
            chargeDetail.setReqNo(reqNo);
            chargeDetail.setConfirmFlag(1);
            mzChargeDetail.getList().add(chargeDetail);
        }
        ResultVo<String> fanHui = mzServer.menZhenXiangMuQueXiaoHuoQueRen(mzChargeDetail);
        if (fanHui.getCode() == 201) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, fanHui.getMessage());
    }

    @PassToken
    @PostMapping("/mzProjectCancel")
    public ResultVo<String> mzProjectCancel(@RequestBody MzChargeDetail mz) {
        log.info("门诊项目确认接口调用：{}", JSON.toJSONString(mz));
        if (ListUtil.isBlank(mz.getReqNos())) {
            return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "申请号不能为空。");
        }
        MzChargeDetail mzChargeDetail = new MzChargeDetail();
        mzChargeDetail.setSource("接口");
        for (Integer reqNo : mz.getReqNos()) {
            if (mzChargeDetail.getList() == null) {
                mzChargeDetail.setList(new ArrayList<>());
            }
            MzChargeDetail chargeDetail = new MzChargeDetail();
            chargeDetail.setReqNo(reqNo);
            chargeDetail.setConfirmFlag(3);
            mzChargeDetail.getList().add(chargeDetail);
        }
        ResultVo<String> fanHui = mzServer.menZhenXiangMuQueXiaoHuoQueRen(mzChargeDetail);
        if (fanHui.getCode() == 201) {
            return ResultVoUtil.success();
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, fanHui.getMessage());
    }

}
