IndexApiController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.StringUtils;
  8. import com.dgtis.common.core.web.controller.BaseController;
  9. import com.dgtis.common.core.web.domain.AjaxResult;
  10. import org.apache.http.util.EntityUtils;
  11. import org.elasticsearch.client.Response;
  12. import org.elasticsearch.client.RestClient;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Controller;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. /**
  24. * @author koucx
  25. * @version 1.0
  26. * @descption: TODO
  27. * @company 神州数码通用软件(洛阳)有限公司
  28. * @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
  29. * @date 2021-01-21
  30. * @since JDK1.8
  31. */
  32. @Controller
  33. @RequestMapping("/index")
  34. public class IndexApiController extends BaseController {
  35. @Autowired
  36. private RestClient restClient;
  37. @GetMapping("/riskcode")
  38. @ResponseBody
  39. public AjaxResult riskcode() {
  40. // 获取索引的别名,字段,创建时间
  41. List xaxisData = new ArrayList();//存放险种
  42. List seriesData = new ArrayList();//客户数量和保单数量
  43. try {
  44. Map<String, String> paramMap = new HashMap<String, String>();
  45. paramMap.put("pretty", "true");
  46. Response response = restClient.performRequest("GET", "shanglifeecif.riskcode_statistics/_search?", paramMap);
  47. String jsonStr = EntityUtils.toString(response.getEntity());
  48. if(StringUtils.isNotEmpty(jsonStr)){
  49. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  50. jsonObject = jsonObject.getJSONObject("hits");
  51. if(jsonObject!=null){
  52. JSONArray hits = jsonObject.getJSONArray("hits");
  53. if(hits!=null && hits.size()>0){
  54. List khnum = new ArrayList();//客户数量
  55. List bdtotalPrem = new ArrayList();//保单数量
  56. for (Object o:hits) {
  57. JSONObject object = JSONUtil.parseObj(o);
  58. if(object!=null){
  59. JSONObject source = object.getJSONObject("_source");
  60. xaxisData.add(source.getStr("kindname"));
  61. String num = source.getStr("khnum");
  62. khnum.add(num);
  63. String tatolprem = source.getStr("tatolprem");
  64. if("0".equals(num)){
  65. bdtotalPrem.add("0");
  66. }else {
  67. bdtotalPrem.add(String.format("%.2f", Float.parseFloat(tatolprem)/Float.parseFloat(num)));
  68. }
  69. }
  70. }
  71. Map code1 = new HashMap();
  72. code1.put("name","客户数量");
  73. code1.put("data",khnum);
  74. seriesData.add(code1);
  75. Map code2 = new HashMap();
  76. code2.put("name","人均保额");
  77. code2.put("data",bdtotalPrem);
  78. seriesData.add(code2);
  79. }
  80. }
  81. }
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. return AjaxResult.error("获取数据失败");
  85. }
  86. Map data = new HashMap();
  87. data.put("xaxisData",xaxisData);
  88. data.put("seriesData",seriesData);
  89. return AjaxResult.success("险种数据返回成功!",data);
  90. }
  91. @GetMapping("/bdNumDis")
  92. @ResponseBody
  93. public AjaxResult bdNumDis() {
  94. // 获取索引的别名,字段,创建时间
  95. Map[] seriesData = new HashMap[8];
  96. try {
  97. Map<String, String> paramMap = new HashMap<String, String>();
  98. paramMap.put("pretty", "true");
  99. Response response = restClient.performRequest("GET", "shanglifeecif.bdnum_distribution/_search?", paramMap);
  100. String jsonStr = EntityUtils.toString(response.getEntity());
  101. if(StringUtils.isNotEmpty(jsonStr)){
  102. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  103. jsonObject = jsonObject.getJSONObject("hits");
  104. if(jsonObject!=null){
  105. JSONArray hits = jsonObject.getJSONArray("hits");
  106. if(hits!=null && hits.size()>0){
  107. for (Object o:hits) {
  108. JSONObject object = JSONUtil.parseObj(o);
  109. if(object!=null){
  110. JSONObject source = object.getJSONObject("_source");
  111. Map map = new HashMap();
  112. String labelname = source.getStr("labelname").trim();
  113. /* map.put("value",source.getStr("bdnum"));
  114. map.put("name",source.getStr("labelname").trim());
  115. seriesData.add(map);*/
  116. Map m = new HashMap();
  117. m.put("name",labelname);
  118. m.put("value",source.getStr("bdnum"));
  119. if("无保单".equals(labelname)){
  120. seriesData[0] = m;
  121. }else if("1件".equals(labelname)){
  122. seriesData[1] = m;
  123. }else if("2件".equals(labelname)){
  124. seriesData[2] = m;
  125. }else if("3-5件".equals(labelname)){
  126. seriesData[3] = m;
  127. }else if("6-10件".equals(labelname)){
  128. seriesData[4] = m;
  129. }else if("11-20件".equals(labelname)){
  130. seriesData[5] = m;
  131. }else if("21-50件".equals(labelname)){
  132. seriesData[6] = m;
  133. }else{
  134. seriesData[7] = m;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. } catch (Exception e) {
  142. e.printStackTrace();
  143. return AjaxResult.error("获取数据失败");
  144. }
  145. Map data = new HashMap();
  146. data.put("seriesData",seriesData);
  147. return AjaxResult.success("保单件数分布数据返回成功!",data);
  148. }
  149. @GetMapping("/ageSexDis")
  150. @ResponseBody
  151. public AjaxResult ageSexDis() {
  152. // 获取索引的别名,字段,创建时间
  153. List legendData = new ArrayList();//性别集合
  154. legendData.add("男");
  155. legendData.add("女");
  156. List xaxisData = new ArrayList();//年龄段
  157. xaxisData.add("儿童");
  158. xaxisData.add("少年");
  159. xaxisData.add("青年");
  160. xaxisData.add("中年");
  161. xaxisData.add("老年");
  162. List seriesData = new ArrayList();//性别,年龄段数据
  163. try {
  164. Map<String, String> paramMap = new HashMap<String, String>();
  165. paramMap.put("pretty", "true");
  166. Response response = restClient.performRequest("GET", "shanglifeecif.age_sex_distribution/_search?", paramMap);
  167. String jsonStr = EntityUtils.toString(response.getEntity());
  168. if(StringUtils.isNotEmpty(jsonStr)){
  169. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  170. jsonObject = jsonObject.getJSONObject("hits");
  171. if(jsonObject!=null){
  172. JSONArray hits = jsonObject.getJSONArray("hits");
  173. if(hits!=null && hits.size()>0){
  174. String[] manNum = new String[5];
  175. String[] womanNum = new String[5];
  176. for (Object o:hits) {
  177. JSONObject object = JSONUtil.parseObj(o);
  178. if(object!=null){
  179. JSONObject source = object.getJSONObject("_source");
  180. String gender = source.getStr("gender");
  181. String labelname = source.getStr("labelname").trim();
  182. String cusnum = source.getStr("cusnum");
  183. if("0".equals(gender)){
  184. if("儿童".equals(labelname)){
  185. manNum[0] = cusnum;
  186. }else if("少年".equals(labelname)){
  187. manNum[1] = cusnum;
  188. }else if("青年".equals(labelname)){
  189. manNum[2] = cusnum;
  190. }else if("中年".equals(labelname)){
  191. manNum[3] = cusnum;
  192. }else {
  193. manNum[4] = cusnum;
  194. }
  195. }else {
  196. if("儿童".equals(labelname)){
  197. womanNum[0] = cusnum;
  198. }else if("少年".equals(labelname)){
  199. womanNum[1] = cusnum;
  200. }else if("青年".equals(labelname)){
  201. womanNum[2] = cusnum;
  202. }else if("中年".equals(labelname)){
  203. womanNum[3] = cusnum;
  204. }else {
  205. womanNum[4] = cusnum;
  206. }
  207. }
  208. }
  209. }
  210. seriesData.add(manNum);
  211. seriesData.add(womanNum);
  212. }
  213. }
  214. }
  215. } catch (Exception e) {
  216. e.printStackTrace();
  217. return AjaxResult.error("获取数据失败");
  218. }
  219. Map data = new HashMap();
  220. data.put("legendData",legendData);
  221. data.put("xaxisData",xaxisData);
  222. data.put("seriesData",seriesData);
  223. return AjaxResult.success("客户年龄段性别分布数据返回成功!",data);
  224. }
  225. }