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.DateUtils; 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 { logger.info("进险种类别时间:" + DateUtils.getTime()); Map paramMap = new HashMap(); paramMap.put("pretty", "true"); Request scriptRequest = new Request("GET", "/shanglifeecif.riskcode_statistics/_search?pretty=true"); Response response = restClient.performRequest(scriptRequest); logger.info("险种类别请求响应时间:" + DateUtils.getTime()); /*Map paramMap = new HashMap(); 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) { logger.info("险种类别报错时间:" + DateUtils.getTime()); e.printStackTrace(); return AjaxResult.error("获取数据失败"); } Map data = new HashMap(); data.put("xaxisData", xaxisData); data.put("seriesData", seriesData); logger.info("出险种类别时间:" + DateUtils.getTime()); return AjaxResult.success("险种数据返回成功!", data); } @GetMapping("/bdNumDis") @ResponseBody public AjaxResult bdNumDis() { // 获取索引的别名,字段,创建时间 Map[] seriesData = new HashMap[0]; try { logger.info("进保单件数时间:" + DateUtils.getTime()); Map paramMap = new HashMap(); paramMap.put("pretty", "true"); Request scriptRequest = new Request("GET", "/shanglifeecif.bdnum_distribution/_search?pretty=true"); Response response = restClient.performRequest(scriptRequest); logger.info("保单件数请求响应时间:" + DateUtils.getTime()); 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) { seriesData = new HashMap[hits.size()]; for (Object o : hits) { try { JSONObject object = JSONUtil.parseObj(o); if (object != null) { JSONObject source = object.getJSONObject("_source"); String id = object.getStr("_id"); String labelname = source.getStr("labelname").trim(); String bdnum = source.getStr("bdnum").trim(); Map m = new HashMap(); m.put("name", labelname); m.put("value", bdnum); // 根据id值按顺序存入数据,保证返回给前端的数据是按id排序的 seriesData[Integer.valueOf(id) - 1] = m; } } catch (NullPointerException | NumberFormatException e) { logger.warn("保单件数计算异常,该条记录不返回,记录信息: "+ o, e); } } // 如果上面for循环中有异常数组中就会有null元素,下面把null元素删除掉只保留有内容的元素 List> list = new ArrayList(seriesData.length); for (Map m : seriesData) { if (m != null) { list.add(m); } } Map[] newArray = new HashMap[0]; seriesData = list.toArray(newArray); } } } } catch (Exception e) { logger.error("保单件数出错时间:" + DateUtils.getTime()); e.printStackTrace(); return AjaxResult.error("获取数据失败"); } Map data = new HashMap(); data.put("seriesData", seriesData); logger.info("出保单件数时间:" + DateUtils.getTime()); 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 { logger.info("进客户年龄段时间:" + DateUtils.getTime()); Map paramMap = new HashMap(); paramMap.put("pretty", "true"); Request scriptRequest = new Request("GET", "/shanglifeecif.age_sex_distribution/_search?pretty=true"); Response response = restClient.performRequest(scriptRequest); logger.info("客户年龄段请求响应时间:" + DateUtils.getTime()); // 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) { logger.info("客户年龄段报错时间:" + DateUtils.getTime()); e.printStackTrace(); return AjaxResult.error("获取数据失败"); } Map data = new HashMap(); data.put("legendData", legendData); data.put("xaxisData", xaxisData); data.put("seriesData", seriesData); logger.info("出客户年龄段时间:" + DateUtils.getTime()); return AjaxResult.success("客户年龄段性别分布数据返回成功!", data); } /** * 有效客户 * * @return */ @GetMapping("/effectiveCustomer") @ResponseBody public AjaxResult effectiveCustomer() { // 获取索引的别名,字段,创建时间 List xaxisData = new ArrayList();//存放投保年份 List seriesData = new ArrayList();//存放级别数据 Map map1 = new HashMap();//客户等级为1 钻石 Map map2 = new HashMap();//客户等级为2 白金 Map map3 = new HashMap();//客户等级为3 黄金 Map map4 = new HashMap();//客户等级为4 普通 List list1 = new ArrayList();//存放级别数据 List list2 = new ArrayList();//存放级别数据 List list3 = new ArrayList();//存放级别数据 List list4 = new ArrayList();//存放级别数据 Map map = new HashMap(); Map mapData = new HashMap(); try { logger.info("进有效客户时间:" + DateUtils.getTime()); Request scriptRequest = new Request("GET", "/shanglifeecif.effectivecustomer/_search?sort=fadatey:asc&size=100&pretty=true"); Response response = restClient.performRequest(scriptRequest); logger.info("有效客户请求响应时间:" + DateUtils.getTime()); 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"); String fadatey = source.getStr("fadatey").trim();//投保年份 String custclass = source.getStr("custclass").trim();//客户等级 String cnum = source.getStr("cnum").trim();//客户数量 if(!map.containsKey(fadatey)){ xaxisData.add(fadatey); map.put(fadatey,fadatey); } mapData.put(fadatey+custclass,cnum); } } } } } } catch (Exception e) { logger.info("有效客户报错时间:" + DateUtils.getTime()); e.printStackTrace(); return AjaxResult.error("获取数据失败"); } for (int i = 0; i