package thyyxxk.webserver.service.outpatient.booking;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.service.TokenService;
import thyyxxk.webserver.service.externalhttp.ThmzSystem;
import thyyxxk.webserver.utils.ResultVoUtil;


@Service
public class OutpatientBookingService {
    private final ThmzSystem thmzHttp;
    private final TokenService tokenService;
    @Value("${thmz-api-url}")
    private String thmzApiUrl;


    public OutpatientBookingService(ThmzSystem thmzHttp, TokenService tokenService) {
        this.thmzHttp = thmzHttp;
        this.tokenService = tokenService;
    }

    /**
     * 获取上午下午的号段
     *
     * @param date     时间
     * @param deptCode 科室
     * @return
     */
    public ResultVo<JSONArray> getAmpmByDateAndDeptCode(String date, String deptCode) {
        JSONObject js = thmzHttp.getAmpmByDateAndDeptCode(tokenService.getThmzToken(), thmzApiUrl, date, deptCode);
        if (js.getInteger("code").equals(0)) {
            return ResultVoUtil.success(js.getJSONArray("data"));
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, js.getString("message"));
    }

    /**
     * 获取 号别
     *
     * @param unitCode
     * @param ampm
     * @param date
     * @param apTime
     * @return
     */
    public ResultVo<JSONArray> getChargeTypeByRequestDayAndDoctor(String unitCode, String ampm, String date, String apTime) {
        JSONObject js = thmzHttp.getChargeTypeByRequestDayAndDoctor(tokenService.getThmzToken(), thmzApiUrl, unitCode, ampm, date, apTime);
        if (js.getInteger("code").equals(0)) {
            return ResultVoUtil.success(js.getJSONArray("data"));
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, js.getString("message"));
    }

    /**
     * 获取医生
     *
     * @param unitCode
     * @param ampm
     * @param date
     * @param apTime
     * @param chargeType
     * @return
     */
    public ResultVo<JSONArray> getDoctorCodeByRequestDayAndDoctor(String unitCode, String ampm, String date, String apTime, String chargeType) {
        JSONObject js = thmzHttp.getDoctorCodeByRequestDayAndDoctor(tokenService.getThmzToken(), thmzApiUrl, unitCode, ampm, date, apTime, chargeType);
        if (js.getInteger("code").equals(0)) {
            return ResultVoUtil.success(js.getJSONArray("data"));
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, js.getString("message"));
    }

    /**
     * 获取费用接口
     *
     * @param mzyRequestId
     * @param birthDay
     * @param patientId
     * @return
     */
    public ResultVo<JSONObject> getMzChargeTypeByMzyRequestId(String mzyRequestId,
                                                              String birthDay,
                                                              String patientId) {
        JSONObject js = thmzHttp.getMzChargeTypeByMzyRequestId(tokenService.getThmzToken(), thmzApiUrl, mzyRequestId, birthDay, patientId);
        if (js.getInteger("code").equals(0)) {
            return ResultVoUtil.success(js.getJSONObject("data"));
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, js.getString("message"));
    }


    public ResultVo<String> saveMzyReqrec(JSONObject req) {
        JSONObject js = thmzHttp.saveMzyReqrec(tokenService.getThmzToken(), thmzApiUrl, req);
        if (js.getInteger("code").equals(0)) {
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "预约成功。");
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, js.getString("message"));
    }


}
