package thyyxxk.webserver.service.outpatient.booking;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.OutpatientBookingHttp;
import thyyxxk.webserver.utils.ResultVoUtil;


@Service
public class OutpatientBookingService {
    private final OutpatientBookingHttp http;
    private final TokenService tokenService;

    public OutpatientBookingService(OutpatientBookingHttp http, TokenService tokenService) {
        this.http = http;
        this.tokenService = tokenService;
    }

    /**
     * 获取上午下午的号段
     *
     * @param date     时间
     * @param deptCode 科室
     * @return
     */
    public ResultVo<JSONArray> getAmpmByDateAndDeptCode(String date, String deptCode) {
        JSONObject js = http.getAmpmByDateAndDeptCode(tokenService.getThmzToken(), 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 = http.getChargeTypeByRequestDayAndDoctor(tokenService.getThmzToken(), 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 = http.getDoctorCodeByRequestDayAndDoctor(tokenService.getThmzToken(), 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 = http.getMzChargeTypeByMzyRequestId(tokenService.getThmzToken(), 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 = http.saveMzyReqrec(tokenService.getThmzToken(), req);
        if (js.getInteger("code").equals(0)) {
            return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "预约成功。");
        }
        return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, js.getString("message"));
    }


}
