package com.dgtis.data.api;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.dgtis.common.core.utils.StringUtils;
import com.dgtis.common.core.web.controller.BaseController;
import com.dgtis.common.core.web.domain.AjaxResult;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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


/**
 * @author koucx
 * @version 1.0
 * @descption: TODO
 * @company 神州数码通用软件（洛阳）有限公司
 * @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
 * @date 2021-01-21
 * @since JDK1.8
 */
@Controller
@RequestMapping("/index")
public class IndexApiController extends BaseController {

    @Autowired
    private RestClient restClient;

    @GetMapping("/riskcode")
    @ResponseBody
    public AjaxResult riskcode() {



        // 获取索引的别名，字段，创建时间
        List xaxisData = new ArrayList();//存放险种
        List seriesData = new ArrayList();//客户数量和保单数量
        try {

            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("pretty", "true");
            Request scriptRequest = new Request("GET", "/shanglifeecif.riskcode_statistics/_search?pretty=true");

            Response response = restClient.performRequest(scriptRequest);

            /*Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("pretty", "true");*/
//            Response response = restClient.performRequest("GET", "shanglifeecif.riskcode_statistics/_search?", paramMap);
            String jsonStr = EntityUtils.toString(response.getEntity());
            if(StringUtils.isNotEmpty(jsonStr)){
                JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
                jsonObject = jsonObject.getJSONObject("hits");
                if(jsonObject!=null){
                    JSONArray hits = jsonObject.getJSONArray("hits");
                    if(hits!=null && hits.size()>0){
                        List khnum = new ArrayList();//客户数量
                        List bdtotalPrem = new ArrayList();//保单数量
                        for (Object o:hits) {
                            JSONObject object = JSONUtil.parseObj(o);
                            if(object!=null){
                                JSONObject source = object.getJSONObject("_source");
                                xaxisData.add(source.getStr("kindname"));
                                String num = source.getStr("khnum");
                                khnum.add(num);
                                String tatolprem = source.getStr("tatolprem");

                                if("0".equals(num)){
                                    bdtotalPrem.add("0");
                                }else {

                                    bdtotalPrem.add(String.format("%.2f", Float.parseFloat(tatolprem)/Float.parseFloat(num)));
                                }


                            }

                        }

                        Map code1 = new HashMap();
                        code1.put("name","客户数量");
                        code1.put("data",khnum);
                        seriesData.add(code1);
                        Map code2 = new HashMap();
                        code2.put("name","人均保额");
                        code2.put("data",bdtotalPrem);
                        seriesData.add(code2);

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取数据失败");
        }

        Map data = new HashMap();
        data.put("xaxisData",xaxisData);
        data.put("seriesData",seriesData);
        return AjaxResult.success("险种数据返回成功!",data);
    }

    @GetMapping("/bdNumDis")
    @ResponseBody
    public AjaxResult bdNumDis() {
        // 获取索引的别名，字段，创建时间
        Map[] seriesData = new HashMap[8];
        try {

            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("pretty", "true");
            Request scriptRequest = new Request("GET", "/shanglifeecif.bdnum_distribution/_search?pretty=true");

            Response response = restClient.performRequest(scriptRequest);
//            Response response = restClient.performRequest("GET", "shanglifeecif.bdnum_distribution/_search?", paramMap);
            String jsonStr = EntityUtils.toString(response.getEntity());
            if(StringUtils.isNotEmpty(jsonStr)){
                JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
                jsonObject = jsonObject.getJSONObject("hits");
                if(jsonObject!=null){
                    JSONArray hits = jsonObject.getJSONArray("hits");
                    if(hits!=null && hits.size()>0){
                        for (Object o:hits) {
                            JSONObject object = JSONUtil.parseObj(o);
                            if(object!=null){
                                JSONObject source = object.getJSONObject("_source");
                                Map map = new HashMap();
                                String labelname = source.getStr("labelname").trim();
                                /*map.put("value",source.getStr("bdnum"));
                                map.put("name",source.getStr("labelname").trim());
                                seriesData.add(map);*/
                                Map m = new HashMap();
                                m.put("name",labelname);
                                m.put("value",source.getStr("bdnum"));
                                if("无保单".equals(labelname)){
                                    seriesData[0] = m;
                                }else if("1件".equals(labelname)){
                                    seriesData[1] = m;
                                }else if("2件".equals(labelname)){
                                    seriesData[2] = m;
                                }else if("3-5件".equals(labelname)){
                                    seriesData[3] = m;
                                }else if("6-10件".equals(labelname)){
                                    seriesData[4] = m;
                                }else if("11-20件".equals(labelname)){
                                    seriesData[5] = m;
                                }else if("21-50件".equals(labelname)){
                                    seriesData[6] = m;
                                }else{
                                    seriesData[7] = m;
                                }
                            }

                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取数据失败");
        }

        Map data = new HashMap();
        data.put("seriesData",seriesData);
        return AjaxResult.success("保单件数分布数据返回成功!",data);
    }


    @GetMapping("/ageSexDis")
    @ResponseBody
    public AjaxResult ageSexDis() {
        // 获取索引的别名，字段，创建时间
        List legendData = new ArrayList();//性别集合
        legendData.add("男");
        legendData.add("女");
        List xaxisData = new ArrayList();//年龄段
        xaxisData.add("儿童");
        xaxisData.add("少年");
        xaxisData.add("青年");
        xaxisData.add("中年");
        xaxisData.add("老年");
        List seriesData = new ArrayList();//性别，年龄段数据

        try {

            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("pretty", "true");
            Request scriptRequest = new Request("GET", "/shanglifeecif.age_sex_distribution/_search?pretty=true");

            Response response = restClient.performRequest(scriptRequest);
//            Response response = restClient.performRequest("GET", "shanglifeecif.age_sex_distribution/_search?", paramMap);
            String jsonStr = EntityUtils.toString(response.getEntity());
            if(StringUtils.isNotEmpty(jsonStr)){
                JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
                jsonObject = jsonObject.getJSONObject("hits");
                if(jsonObject!=null){
                    JSONArray hits = jsonObject.getJSONArray("hits");
                    if(hits!=null && hits.size()>0){
                        String[] manNum = new String[5];
                        String[] womanNum = new String[5];
                        for (Object o:hits) {
                            JSONObject object = JSONUtil.parseObj(o);
                            if(object!=null){
                                JSONObject source = object.getJSONObject("_source");
                                String gender = source.getStr("gender");
                                String labelname = source.getStr("labelname").trim();
                                String cusnum = source.getStr("cusnum");
                                if("0".equals(gender)){
                                    if("儿童".equals(labelname)){
                                        manNum[0] = cusnum;
                                    }else if("少年".equals(labelname)){
                                        manNum[1] = cusnum;
                                    }else if("青年".equals(labelname)){
                                        manNum[2] = cusnum;
                                    }else if("中年".equals(labelname)){
                                        manNum[3] = cusnum;
                                    }else {
                                        manNum[4] = cusnum;
                                    }

                                }else {
                                    if("儿童".equals(labelname)){
                                        womanNum[0] = cusnum;
                                    }else if("少年".equals(labelname)){
                                        womanNum[1] = cusnum;
                                    }else if("青年".equals(labelname)){
                                        womanNum[2] = cusnum;
                                    }else if("中年".equals(labelname)){
                                        womanNum[3] = cusnum;
                                    }else {
                                        womanNum[4] = cusnum;
                                    }
                                }
                            }

                        }
                        seriesData.add(manNum);
                        seriesData.add(womanNum);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取数据失败");
        }

        Map data = new HashMap();
        data.put("legendData",legendData);
        data.put("xaxisData",xaxisData);
        data.put("seriesData",seriesData);
        return AjaxResult.success("客户年龄段性别分布数据返回成功!",data);
    }


}
