package thyyxxk.webserver.service.jianyanjiekou;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.Forest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.examinations.inspections.consts.PatientNumType;
import thyyxxk.webserver.entity.examinations.inspections.consts.ResponseMode;
import thyyxxk.webserver.entity.jianyanjiekou.QueryNormal;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

@Service
@Slf4j
public class JianYanJieKouService {

    @Data
    public static class JianYanRes {
        private Boolean code;
        private String message;
        private Integer returnNumber;
        private String data;
    }

    @Data
    public static class ResItems {
        private String paperSize;
        private String imgData;
    }

    private JianYanRes postHttp(String url, JSONObject data) {
        try {
            String API_IP = "http://172.16.32.178/apis";
            JianYanRes res = Forest.post(API_IP + url)
                    .addHeader("Content-Type", "application/json")
                    .addBody(data)
                    .setRetryEnabled(false)
                    .setConnectTimeout(2000)
                    .setReadTimeout(2000)
                    .execute(JianYanRes.class);
            if (res == null) {
                throw new BizException(ExceptionEnum.NETWORK_ERROR, "接口调用失败。");
            }
            return res;
        } catch (Exception e) {
            throw new BizException(ExceptionEnum.NETWORK_ERROR, e.getMessage());
        }
    }

    public ResultVo<JSONArray> getNormal(QueryNormal param) {
        if (param.getPatientNumType() == null) {
            param.setPatientNumType(PatientNumType.InPatient);
        }
        JSONObject js = new JSONObject();
        js.put("departId", "");
        js.put("patientNum", param.getPatientNum());
        js.put("patientNumType", param.getPatientNumType().toString());

        if (StringUtil.isBlank(param.getStartDate())) {
            return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, "请先选择时间。");
        }

        js.put("startDate", param.getStartDate());
        js.put("endDate", param.getEndDate());

        JianYanRes res = postHttp("/third/report/query/normal", js);
        if (res.getCode()) {
            JSONObject data = JSONObject.parseObject(res.getData());
            return ResultVoUtil.success(data.getJSONArray("items"));
        }
        return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, res.getMessage());
    }

    public ResultVo<JSONObject> getDetail(String reportId) {
        JSONObject js = new JSONObject();
        js.put("reportId", reportId);
        js.put("responseMode", ResponseMode.Json);
        JianYanRes res = postHttp("/third/report/query/detail", js);
        if (res.getCode()) {
            return ResultVoUtil.success(JSONObject.parseObject(res.getData()));
        }
        return ResultVoUtil.fail(ExceptionEnum.INVALID_PARAM, res.getMessage());
    }

    public JSONObject getImage(String reportId) {
        JSONObject js = new JSONObject();
        js.put("reportId", reportId);
        js.put("responseMode", ResponseMode.Json);
        JianYanRes res = postHttp("/third/report/query/pic", js);
        if (res.getCode()) {
            return JSONObject.parseObject(res.getData());
        }
        return null;
    }

}
