package com.yihu.wlyy.service.third.jw;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.wlyy.entity.dict.SystemDict;
import com.yihu.wlyy.service.system.SystemDictService;
import com.yihu.wlyy.service.third.ehr.EhrService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.util.HttpClientUtil;
import com.yihu.wlyy.util.SystemConf;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hzp on 2016/11/15.
 * 基卫健康档案服务
 */
@Service("JwArchivesService")
public class JwArchivesService {

    //基卫服务地址
    private String jwUrl = SystemConf.getInstance().getJwUrl();


    /**
     *  查询居民健康体检列表信息接口
     */
    public String getEhrSickMedicalList(String idcard)  throws Exception
    {
        String re = "";
        String url = jwUrl + "/third/archives/getEhrSickMedicalList";

        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("idcard", idcard));

        String response = HttpClientUtil.post(url, params, "UTF-8");

        if(!StringUtils.isEmpty(response))
        {
            JSONObject json = new JSONObject(response);
            if("1".equals(json.optString("CODE")))
            {
                re = json.getJSONArray("DATA").toString();
            }
            else{
                throw new Exception(json.optString("MESSAGE"));
            }
        }
        else{
            re = "[]";
        }

        return re;
    }

    /**
     *  查询居民健康体检详情接口
     */
    public String getEhrSickMedicalRecord(String medicalNo)  throws Exception
    {
        String re = "";
        String url = jwUrl + "/third/archives/getEhrSickMedicalRecord";

        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("medicalNo", medicalNo));

        String response = HttpClientUtil.post(url, params, "UTF-8");

        if(!StringUtils.isEmpty(response))
        {
            JSONObject json = new JSONObject(response);
            if("1".equals(json.optString("CODE")))
            {
                re = json.getJSONArray("DATA").toString();
            }
            else{
                throw new Exception(json.optString("MESSAGE"));
            }
        }
        else{
            re = "[]";
        }

        return re;
    }

    /**
     *  上传体征指标接口
     */
    public void uploadHealthIndex(String id)  throws Exception
    {
        String url = jwUrl + "/third/archives/uploadHealthIndex";

        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("id", id));

        String response = HttpClientUtil.post(url, params, "UTF-8");

        if(!StringUtils.isEmpty(response))
        {
            JSONObject json = new JSONObject(response);
            if (!"200".equals(json.optString("status"))) {
                throw new Exception(json.optString("msg"));
            }
        }
        else{
            throw new Exception("返回结果为空！");
        }
    }

    /**
     * 上传随访记录接口
     */
    public void uploadFollowup(String id) throws Exception
    {
        String url = jwUrl + "/third/archives/uploadFollowup";

        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("id", id));

        String response = HttpClientUtil.post(url, params, "UTF-8");

        if(!StringUtils.isEmpty(response))
        {
            JSONObject json = new JSONObject(response);
            if (!"200".equals(json.optString("status"))) {
                throw new Exception(json.optString("msg"));
            }
        }
        else{
            throw new Exception("返回结果为空！");
        }
    }
}
