package thyyxxk.webserver.factory.pathology;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.client.RestTemplate;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.examinations.inspections.request.ReportIndexInquiry;
import thyyxxk.webserver.factory.pathology.model.PathologyIndex;

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

public class PathologyService {
    private static final String URL = "http://172.16.32.160:9205/thyy/api/public/bl_report?beginTime={}&endTime={}&patientId={}";

    public List<PathologyIndex> queryOutpatientPathologyIndex(ReportIndexInquiry request) {
        String url = StrUtil.format(URL, request.getReqStartTime(),
                request.getReqEndTime(), request.getSocialNo());
        List<PathologyIndex> pathologyIndexList = new ArrayList<>();
        JSONObject response = new RestTemplate().getForObject(url, JSONObject.class);
        if (null == response) {
            throw new BizException(ExceptionEnum.NETWORK_ERROR);
        }
        int code = response.getIntValue("code");
        if (code != 1) {
            throw new BizException(response.getString("message"));
        }
        JSONArray rows = response.getJSONArray("data");
        if (null != rows && !rows.isEmpty()) {
            for (int i = 0; i < rows.size(); i++) {
                JSONObject row = rows.getJSONObject(i);
                PathologyIndex pathologyIndex = JSONObject.toJavaObject(row, PathologyIndex.class);
                pathologyIndexList.add(pathologyIndex);
            }
        }
        return pathologyIndexList;
    }

}
