IndexApiController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package com.dgtis.data.api;
  2. import cn.hutool.json.JSONArray;
  3. import cn.hutool.json.JSONObject;
  4. import cn.hutool.json.JSONUtil;
  5. import com.arronlong.httpclientutil.HttpClientUtil;
  6. import com.arronlong.httpclientutil.common.HttpConfig;
  7. import com.dgtis.common.core.utils.DateUtils;
  8. import com.dgtis.common.core.utils.StringUtils;
  9. import com.dgtis.common.core.web.controller.BaseController;
  10. import com.dgtis.common.core.web.domain.AjaxResult;
  11. import org.apache.http.util.EntityUtils;
  12. import org.elasticsearch.client.Request;
  13. import org.elasticsearch.client.Response;
  14. import org.elasticsearch.client.RestClient;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Controller;
  18. import org.springframework.web.bind.annotation.GetMapping;
  19. import org.springframework.web.bind.annotation.RequestMapping;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * @author koucx
  27. * @version 1.0
  28. * @descption: TODO
  29. * @company 神州数码通用软件(洛阳)有限公司
  30. * @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
  31. * @date 2021-01-21
  32. * @since JDK1.8
  33. */
  34. @Controller
  35. @RequestMapping("/index")
  36. public class IndexApiController extends BaseController {
  37. @Autowired
  38. private RestClient restClient;
  39. @GetMapping("/riskcode")
  40. @ResponseBody
  41. public AjaxResult riskcode() {
  42. // 获取索引的别名,字段,创建时间
  43. List xaxisData = new ArrayList();//存放险种
  44. List seriesData = new ArrayList();//客户数量和保单数量
  45. try {
  46. logger.info("进险种类别时间:" + DateUtils.getTime());
  47. Map<String, String> paramMap = new HashMap<String, String>();
  48. paramMap.put("pretty", "true");
  49. Request scriptRequest = new Request("GET", "/shanglifeecif.riskcode_statistics/_search?pretty=true");
  50. Response response = restClient.performRequest(scriptRequest);
  51. logger.info("险种类别请求响应时间:" + DateUtils.getTime());
  52. /*Map<String, String> paramMap = new HashMap<String, String>();
  53. paramMap.put("pretty", "true");*/
  54. // Response response = restClient.performRequest("GET", "shanglifeecif.riskcode_statistics/_search?", paramMap);
  55. String jsonStr = EntityUtils.toString(response.getEntity());
  56. if (StringUtils.isNotEmpty(jsonStr)) {
  57. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  58. jsonObject = jsonObject.getJSONObject("hits");
  59. if (jsonObject != null) {
  60. JSONArray hits = jsonObject.getJSONArray("hits");
  61. if (hits != null && hits.size() > 0) {
  62. List khnum = new ArrayList();//客户数量
  63. List bdtotalPrem = new ArrayList();//保单数量
  64. for (Object o : hits) {
  65. JSONObject object = JSONUtil.parseObj(o);
  66. if (object != null) {
  67. JSONObject source = object.getJSONObject("_source");
  68. xaxisData.add(source.getStr("kindname"));
  69. String num = source.getStr("khnum");
  70. khnum.add(num);
  71. String tatolprem = source.getStr("tatolprem");
  72. if ("0".equals(num)) {
  73. bdtotalPrem.add("0");
  74. } else {
  75. bdtotalPrem.add(String.format("%.2f", Float.parseFloat(tatolprem) / Float.parseFloat(num)));
  76. }
  77. }
  78. }
  79. Map code1 = new HashMap();
  80. code1.put("name", "存量客户数量");
  81. code1.put("data", khnum);
  82. seriesData.add(code1);
  83. Map code2 = new HashMap();
  84. code2.put("name", "被保人人均保额");
  85. code2.put("data", bdtotalPrem);
  86. seriesData.add(code2);
  87. }
  88. }
  89. }
  90. } catch (Exception e) {
  91. logger.info("险种类别报错时间:" + DateUtils.getTime());
  92. e.printStackTrace();
  93. return AjaxResult.error("获取数据失败");
  94. }
  95. Map data = new HashMap();
  96. data.put("xaxisData", xaxisData);
  97. data.put("seriesData", seriesData);
  98. logger.info("出险种类别时间:" + DateUtils.getTime());
  99. return AjaxResult.success("险种数据返回成功!", data);
  100. }
  101. @GetMapping("/bdNumDis")
  102. @ResponseBody
  103. public AjaxResult bdNumDis() {
  104. // 获取索引的别名,字段,创建时间
  105. Map<String, String>[] seriesData = new HashMap[0];
  106. try {
  107. logger.info("进保单件数时间:" + DateUtils.getTime());
  108. Map<String, String> paramMap = new HashMap<String, String>();
  109. paramMap.put("pretty", "true");
  110. Request scriptRequest = new Request("GET", "/shanglifeecif.bdnum_distribution/_search?pretty=true");
  111. Response response = restClient.performRequest(scriptRequest);
  112. logger.info("保单件数请求响应时间:" + DateUtils.getTime());
  113. String jsonStr = EntityUtils.toString(response.getEntity());
  114. if (StringUtils.isNotEmpty(jsonStr)) {
  115. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  116. jsonObject = jsonObject.getJSONObject("hits");
  117. if (jsonObject != null) {
  118. JSONArray hits = jsonObject.getJSONArray("hits");
  119. if (hits != null && hits.size() > 0) {
  120. seriesData = new HashMap[hits.size()];
  121. for (Object o : hits) {
  122. try {
  123. JSONObject object = JSONUtil.parseObj(o);
  124. if (object != null) {
  125. JSONObject source = object.getJSONObject("_source");
  126. String id = object.getStr("_id");
  127. String labelname = source.getStr("labelname").trim();
  128. String bdnum = source.getStr("bdnum").trim();
  129. Map<String, String> m = new HashMap();
  130. m.put("name", labelname);
  131. m.put("value", bdnum);
  132. // 根据id值按顺序存入数据,保证返回给前端的数据是按id排序的
  133. seriesData[Integer.valueOf(id) - 1] = m;
  134. }
  135. } catch (NullPointerException | NumberFormatException e) {
  136. logger.warn("保单件数计算异常,该条记录不返回,记录信息: "+ o, e);
  137. }
  138. }
  139. // 如果上面for循环中有异常数组中就会有null元素,下面把null元素删除掉只保留有内容的元素
  140. List<Map<String, String>> list = new ArrayList(seriesData.length);
  141. for (Map m : seriesData) {
  142. if (m != null) {
  143. list.add(m);
  144. }
  145. }
  146. Map<String, String>[] newArray = new HashMap[0];
  147. seriesData = list.toArray(newArray);
  148. }
  149. }
  150. }
  151. } catch (Exception e) {
  152. logger.error("保单件数出错时间:" + DateUtils.getTime());
  153. e.printStackTrace();
  154. return AjaxResult.error("获取数据失败");
  155. }
  156. Map data = new HashMap();
  157. data.put("seriesData", seriesData);
  158. logger.info("出保单件数时间:" + DateUtils.getTime());
  159. return AjaxResult.success("保单件数分布数据返回成功!", data);
  160. }
  161. @GetMapping("/ageSexDis")
  162. @ResponseBody
  163. public AjaxResult ageSexDis() {
  164. // 获取索引的别名,字段,创建时间
  165. List legendData = new ArrayList();//性别集合
  166. legendData.add("男");
  167. legendData.add("女");
  168. List xaxisData = new ArrayList();//年龄段
  169. xaxisData.add("儿童");
  170. xaxisData.add("少年");
  171. xaxisData.add("青年");
  172. xaxisData.add("中年");
  173. xaxisData.add("老年");
  174. List seriesData = new ArrayList();//性别,年龄段数据
  175. try {
  176. logger.info("进客户年龄段时间:" + DateUtils.getTime());
  177. Map<String, String> paramMap = new HashMap<String, String>();
  178. paramMap.put("pretty", "true");
  179. Request scriptRequest = new Request("GET", "/shanglifeecif.age_sex_distribution/_search?pretty=true");
  180. Response response = restClient.performRequest(scriptRequest);
  181. logger.info("客户年龄段请求响应时间:" + DateUtils.getTime());
  182. // Response response = restClient.performRequest("GET", "shanglifeecif.age_sex_distribution/_search?", paramMap);
  183. String jsonStr = EntityUtils.toString(response.getEntity());
  184. if (StringUtils.isNotEmpty(jsonStr)) {
  185. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  186. jsonObject = jsonObject.getJSONObject("hits");
  187. if (jsonObject != null) {
  188. JSONArray hits = jsonObject.getJSONArray("hits");
  189. if (hits != null && hits.size() > 0) {
  190. String[] manNum = new String[5];
  191. String[] womanNum = new String[5];
  192. for (Object o : hits) {
  193. JSONObject object = JSONUtil.parseObj(o);
  194. if (object != null) {
  195. JSONObject source = object.getJSONObject("_source");
  196. String gender = source.getStr("gender");
  197. String labelname = source.getStr("labelname").trim();
  198. String cusnum = source.getStr("cusnum");
  199. if ("0".equals(gender)) {
  200. if ("儿童".equals(labelname)) {
  201. manNum[0] = cusnum;
  202. } else if ("少年".equals(labelname)) {
  203. manNum[1] = cusnum;
  204. } else if ("青年".equals(labelname)) {
  205. manNum[2] = cusnum;
  206. } else if ("中年".equals(labelname)) {
  207. manNum[3] = cusnum;
  208. } else {
  209. manNum[4] = cusnum;
  210. }
  211. } else {
  212. if ("儿童".equals(labelname)) {
  213. womanNum[0] = cusnum;
  214. } else if ("少年".equals(labelname)) {
  215. womanNum[1] = cusnum;
  216. } else if ("青年".equals(labelname)) {
  217. womanNum[2] = cusnum;
  218. } else if ("中年".equals(labelname)) {
  219. womanNum[3] = cusnum;
  220. } else {
  221. womanNum[4] = cusnum;
  222. }
  223. }
  224. }
  225. }
  226. seriesData.add(manNum);
  227. seriesData.add(womanNum);
  228. }
  229. }
  230. }
  231. } catch (Exception e) {
  232. logger.info("客户年龄段报错时间:" + DateUtils.getTime());
  233. e.printStackTrace();
  234. return AjaxResult.error("获取数据失败");
  235. }
  236. Map data = new HashMap();
  237. data.put("legendData", legendData);
  238. data.put("xaxisData", xaxisData);
  239. data.put("seriesData", seriesData);
  240. logger.info("出客户年龄段时间:" + DateUtils.getTime());
  241. return AjaxResult.success("客户年龄段性别分布数据返回成功!", data);
  242. }
  243. /**
  244. * 有效客户
  245. *
  246. * @return
  247. */
  248. @GetMapping("/effectiveCustomer")
  249. @ResponseBody
  250. public AjaxResult effectiveCustomer() {
  251. // 获取索引的别名,字段,创建时间
  252. List<String> xaxisData = new ArrayList();//存放投保年份
  253. List seriesData = new ArrayList();//存放级别数据
  254. Map map1 = new HashMap();//客户等级为1 钻石
  255. Map map2 = new HashMap();//客户等级为2 白金
  256. Map map3 = new HashMap();//客户等级为3 黄金
  257. Map map4 = new HashMap();//客户等级为4 普通
  258. List list1 = new ArrayList();//存放级别数据
  259. List list2 = new ArrayList();//存放级别数据
  260. List list3 = new ArrayList();//存放级别数据
  261. List list4 = new ArrayList();//存放级别数据
  262. Map map = new HashMap();
  263. Map mapData = new HashMap();
  264. try {
  265. logger.info("进有效客户时间:" + DateUtils.getTime());
  266. Request scriptRequest = new Request("GET", "/shanglifeecif.effectivecustomer/_search?sort=fadatey:asc&size=100&pretty=true");
  267. Response response = restClient.performRequest(scriptRequest);
  268. logger.info("有效客户请求响应时间:" + DateUtils.getTime());
  269. String jsonStr = EntityUtils.toString(response.getEntity());
  270. if (StringUtils.isNotEmpty(jsonStr)) {
  271. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  272. jsonObject = jsonObject.getJSONObject("hits");
  273. if (jsonObject != null) {
  274. JSONArray hits = jsonObject.getJSONArray("hits");
  275. if (hits != null && hits.size() > 0) {
  276. for (Object o : hits) {
  277. JSONObject object = JSONUtil.parseObj(o);
  278. if (object != null) {
  279. JSONObject source = object.getJSONObject("_source");
  280. String fadatey = source.getStr("fadatey").trim();//投保年份
  281. String custclass = source.getStr("custclass").trim();//客户等级
  282. String cnum = source.getStr("cnum").trim();//客户数量
  283. if(!map.containsKey(fadatey)){
  284. xaxisData.add(fadatey);
  285. map.put(fadatey,fadatey);
  286. }
  287. mapData.put(fadatey+custclass,cnum);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. } catch (Exception e) {
  294. logger.info("有效客户报错时间:" + DateUtils.getTime());
  295. e.printStackTrace();
  296. return AjaxResult.error("获取数据失败");
  297. }
  298. for (int i = 0; i <xaxisData.size() ; i++) {
  299. list1.add(mapData.get(xaxisData.get(i)+"1"));
  300. list2.add(mapData.get(xaxisData.get(i)+"2"));
  301. list3.add(mapData.get(xaxisData.get(i)+"3"));
  302. list4.add(mapData.get(xaxisData.get(i)+"4"));
  303. }
  304. map1.put("name","钻石");
  305. map1.put("data",list1);
  306. map2.put("name","白金");
  307. map2.put("data",list2);
  308. map3.put("name","黄金");
  309. map3.put("data",list3);
  310. map4.put("name","普通");
  311. map4.put("data",list4);
  312. seriesData.add(map1);
  313. seriesData.add(map2);
  314. seriesData.add(map3);
  315. seriesData.add(map4);
  316. Map data = new HashMap();
  317. data.put("xaxisData", xaxisData);
  318. data.put("seriesData", seriesData);
  319. logger.info("出有效客户时间:" + DateUtils.getTime());
  320. return AjaxResult.success("有效客户数据返回成功!", data);
  321. }
  322. }