IndexApiController.java 11 KB

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