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.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 { @Value("${elasticsearch.host}") private String esHost; @Value("${elasticsearch.port}") private int esPort; @GetMapping("/riskcode") @ResponseBody public AjaxResult riskcode() { // 获取索引的别名,字段,创建时间 HttpConfig config = HttpConfig.custom().url("http://"+esHost+":"+esPort+"/shanglifeecif.riskcode_statistics/_search?"); String jsonStr = null; List xaxisData = new ArrayList();//存放险种 List seriesData = new ArrayList();//客户数量和保单数量 try { jsonStr = HttpClientUtil.get(config); 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")); khnum.add(source.getStr("khnum")); bdtotalPrem.add(source.getStr("tatolprem")); } } 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() { // 获取索引的别名,字段,创建时间 HttpConfig config = HttpConfig.custom().url("http://"+esHost+":"+esPort+"/shanglifeecif.bdnum_distribution/_search?"); String jsonStr = null; Map[] seriesData = new HashMap[8]; //List seriesData = new ArrayList();//客户数量和保单数量 /* seriesData.add(0,"无保单"); seriesData.add(1,"1件保单"); seriesData.add(2,"2件保单"); seriesData.add(3,"3-5件保单"); seriesData.add(4,"6-10件保单"); seriesData.add(5,"11-20件保单"); seriesData.add(6,"21-50件保单"); seriesData.add(7,"50件以上保单");*/ try { jsonStr = HttpClientUtil.get(config); 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() { // 获取索引的别名,字段,创建时间 HttpConfig config = HttpConfig.custom().url("http://"+esHost+":"+esPort+"/shanglifeecif.age_sex_distribution/_search?"); String jsonStr = null; 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 { jsonStr = HttpClientUtil.get(config); 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); } }