IndexApiController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. Map[] seriesData = new HashMap[8];
  89. //List seriesData = new ArrayList();//客户数量和保单数量
  90. /* seriesData.add(0,"无保单");
  91. seriesData.add(1,"1件保单");
  92. seriesData.add(2,"2件保单");
  93. seriesData.add(3,"3-5件保单");
  94. seriesData.add(4,"6-10件保单");
  95. seriesData.add(5,"11-20件保单");
  96. seriesData.add(6,"21-50件保单");
  97. seriesData.add(7,"50件以上保单");*/
  98. try {
  99. jsonStr = HttpClientUtil.get(config);
  100. if(StringUtils.isNotEmpty(jsonStr)){
  101. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  102. jsonObject = jsonObject.getJSONObject("hits");
  103. if(jsonObject!=null){
  104. JSONArray hits = jsonObject.getJSONArray("hits");
  105. if(hits!=null && hits.size()>0){
  106. for (Object o:hits) {
  107. JSONObject object = JSONUtil.parseObj(o);
  108. if(object!=null){
  109. JSONObject source = object.getJSONObject("_source");
  110. Map map = new HashMap();
  111. String labelname = source.getStr("labelname").trim();
  112. /* map.put("value",source.getStr("bdnum"));
  113. map.put("name",source.getStr("labelname").trim());
  114. seriesData.add(map);*/
  115. Map m = new HashMap();
  116. m.put("name",labelname);
  117. m.put("value",source.getStr("bdnum"));
  118. if("无保单".equals(labelname)){
  119. seriesData[0] = m;
  120. }else if("1件".equals(labelname)){
  121. seriesData[1] = m;
  122. }else if("2件".equals(labelname)){
  123. seriesData[2] = m;
  124. }else if("3-5件".equals(labelname)){
  125. seriesData[3] = m;
  126. }else if("6-10件".equals(labelname)){
  127. seriesData[4] = m;
  128. }else if("11-20件".equals(labelname)){
  129. seriesData[5] = m;
  130. }else if("21-50件".equals(labelname)){
  131. seriesData[6] = m;
  132. }else{
  133. seriesData[7] = m;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. return AjaxResult.error("获取数据失败");
  143. }
  144. Map data = new HashMap();
  145. data.put("seriesData",seriesData);
  146. return AjaxResult.success("保单件数分布数据返回成功!",data);
  147. }
  148. @GetMapping("/ageSexDis")
  149. @ResponseBody
  150. public AjaxResult ageSexDis() {
  151. // 获取索引的别名,字段,创建时间
  152. HttpConfig config = HttpConfig.custom().url("http://"+esHost+":"+esPort+"/shanglifeecif.age_sex_distribution/_search?");
  153. String jsonStr = null;
  154. List legendData = new ArrayList();//性别集合
  155. legendData.add("男");
  156. legendData.add("女");
  157. List xaxisData = new ArrayList();//年龄段
  158. xaxisData.add("儿童");
  159. xaxisData.add("少年");
  160. xaxisData.add("青年");
  161. xaxisData.add("中年");
  162. xaxisData.add("老年");
  163. List seriesData = new ArrayList();//性别,年龄段数据
  164. try {
  165. jsonStr = HttpClientUtil.get(config);
  166. if(StringUtils.isNotEmpty(jsonStr)){
  167. JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
  168. jsonObject = jsonObject.getJSONObject("hits");
  169. if(jsonObject!=null){
  170. JSONArray hits = jsonObject.getJSONArray("hits");
  171. if(hits!=null && hits.size()>0){
  172. String[] manNum = new String[5];
  173. String[] womanNum = new String[5];
  174. for (Object o:hits) {
  175. JSONObject object = JSONUtil.parseObj(o);
  176. if(object!=null){
  177. JSONObject source = object.getJSONObject("_source");
  178. String gender = source.getStr("gender");
  179. String labelname = source.getStr("labelname").trim();
  180. String cusnum = source.getStr("cusnum");
  181. if("0".equals(gender)){
  182. if("儿童".equals(labelname)){
  183. manNum[0] = cusnum;
  184. }else if("少年".equals(labelname)){
  185. manNum[1] = cusnum;
  186. }else if("青年".equals(labelname)){
  187. manNum[2] = cusnum;
  188. }else if("中年".equals(labelname)){
  189. manNum[3] = cusnum;
  190. }else {
  191. manNum[4] = cusnum;
  192. }
  193. }else {
  194. if("儿童".equals(labelname)){
  195. womanNum[0] = cusnum;
  196. }else if("少年".equals(labelname)){
  197. womanNum[1] = cusnum;
  198. }else if("青年".equals(labelname)){
  199. womanNum[2] = cusnum;
  200. }else if("中年".equals(labelname)){
  201. womanNum[3] = cusnum;
  202. }else {
  203. womanNum[4] = cusnum;
  204. }
  205. }
  206. }
  207. }
  208. seriesData.add(manNum);
  209. seriesData.add(womanNum);
  210. }
  211. }
  212. }
  213. } catch (Exception e) {
  214. e.printStackTrace();
  215. return AjaxResult.error("获取数据失败");
  216. }
  217. Map data = new HashMap();
  218. data.put("legendData",legendData);
  219. data.put("xaxisData",xaxisData);
  220. data.put("seriesData",seriesData);
  221. return AjaxResult.success("客户年龄段性别分布数据返回成功!",data);
  222. }
  223. }