123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- 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<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);
- logger.info("险种类别请求响应时间:" + DateUtils.getTime());
- /*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) {
- 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<String, String>[] seriesData = new HashMap[0];
- try {
- logger.info("进保单件数时间:" + DateUtils.getTime());
- 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);
- 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<String, String> 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<Map<String, String>> list = new ArrayList(seriesData.length);
- for (Map m : seriesData) {
- if (m != null) {
- list.add(m);
- }
- }
- Map<String, String>[] 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<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);
- 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<String> 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 <xaxisData.size() ; i++) {
- list1.add(mapData.get(xaxisData.get(i)+"1"));
- list2.add(mapData.get(xaxisData.get(i)+"2"));
- list3.add(mapData.get(xaxisData.get(i)+"3"));
- list4.add(mapData.get(xaxisData.get(i)+"4"));
- }
- map1.put("name","钻石");
- map1.put("data",list1);
- map2.put("name","白金");
- map2.put("data",list2);
- map3.put("name","黄金");
- map3.put("data",list3);
- map4.put("name","普通");
- map4.put("data",list4);
- seriesData.add(map1);
- seriesData.add(map2);
- seriesData.add(map3);
- seriesData.add(map4);
- Map data = new HashMap();
- data.put("xaxisData", xaxisData);
- data.put("seriesData", seriesData);
- logger.info("出有效客户时间:" + DateUtils.getTime());
- return AjaxResult.success("有效客户数据返回成功!", data);
- }
- }
|