CustomerApiController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package com.dgtis.data.api;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.arronlong.httpclientutil.HttpClientUtil;
  6. import com.arronlong.httpclientutil.common.HttpConfig;
  7. import com.arronlong.httpclientutil.exception.HttpProcessException;
  8. import com.dgtis.common.core.utils.DateUtils;
  9. import com.dgtis.common.core.utils.StringUtils;
  10. import com.dgtis.common.core.utils.sql.EsJsonUtil;
  11. import com.dgtis.common.core.web.domain.AjaxResult;
  12. import com.dgtis.common.security.annotation.PreAuthorize;
  13. import org.apache.http.HttpEntity;
  14. import org.apache.http.entity.ContentType;
  15. import org.apache.http.nio.entity.NStringEntity;
  16. import org.apache.http.util.EntityUtils;
  17. import org.elasticsearch.client.Response;
  18. import org.elasticsearch.client.RestClient;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.beans.factory.annotation.Qualifier;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.annotation.Resource;
  27. import java.util.*;
  28. /**
  29. * @description:客户信息列表api
  30. * @author:qxm
  31. * @date:2021/1/22 10:24
  32. */
  33. @Controller
  34. @RequestMapping("/customer")
  35. public class CustomerApiController {
  36. private static Logger logger = LoggerFactory.getLogger(CustomerApiController.class);
  37. @Autowired
  38. private RestClient restClient;
  39. @GetMapping("/getCustomerList")
  40. @ResponseBody
  41. public AjaxResult getCustomerList(@RequestParam(defaultValue = "0") int pageNum,@RequestParam(defaultValue = "10") int pageSize,
  42. String education,//学历
  43. String policybelong,//业绩归属
  44. String payS,String payE, //年缴费区间
  45. String fadate,//投保年份
  46. String productname,//险种类别
  47. String insuredAmountS,String insuredAmountE,//保额区间
  48. String ageS,String ageE,//年龄区间
  49. String gender,//性别
  50. String sobirth,//省份
  51. String NOVPolicy,//保单件数
  52. String custtype,//客户类型 投保人
  53. String SCustID,// 核心客户号
  54. String custclass,//客户等级
  55. String participantsDateS,String participantsDateE,//参与方时间
  56. String PIncomeS,String PIncomeE,//年收入区间
  57. String Insured,//投保对象
  58. String lp,//理赔
  59. String yx,//有效
  60. String gzgw,//关注官微
  61. String zcgw//注册官微
  62. ) {
  63. // 获取索引的别名,字段,创建时间
  64. String beginBirthDay = "";
  65. String endBirthDay = "";
  66. if(StringUtils.isNotEmpty(ageS)){
  67. String year = DateUtils.getYYYY();
  68. int bYear = Integer.parseInt(year)-Integer.parseInt(ageS);
  69. endBirthDay = bYear+"-"+DateUtils.getMMDD();
  70. }
  71. if(StringUtils.isNotEmpty(ageE)){
  72. String year = DateUtils.getYYYY();
  73. int bYear = Integer.parseInt(year)-Integer.parseInt(ageE);
  74. beginBirthDay = bYear+"-"+DateUtils.getMMDD();
  75. }
  76. try {
  77. //拼装查询条件
  78. StringBuilder builder =new StringBuilder();
  79. builder.append("{");
  80. builder.append("\"query\": {");//query开始
  81. builder.append("\"bool\": {");//boot开始
  82. builder.append("\"must\": [");//must开始
  83. if(StringUtils.isNotEmpty(gender)){
  84. builder.append("{\"prefix\": { \"gender\": "+gender+"}},");
  85. }
  86. if(StringUtils.isNotEmpty(NOVPolicy)){
  87. builder.append("{\"prefix\": { \"label25\": \""+NOVPolicy+"\"}},");
  88. }
  89. if(StringUtils.isNotEmpty(custclass)){
  90. builder.append("{\"prefix\": { \"custclass\": \""+custclass+"\"}},");
  91. }
  92. if(StringUtils.isNotEmpty(sobirth)){
  93. builder.append("{\"wildcard\": { \"sobirth\": \"*"+sobirth+"*\"}},");//模糊匹配
  94. }
  95. if(StringUtils.isNotEmpty(SCustID)){
  96. builder.append("{\"wildcard\": { \"custid\": \"*"+SCustID+"*\"}},");//模糊匹配
  97. }
  98. if(StringUtils.isNotEmpty(beginBirthDay) && StringUtils.isNotEmpty(endBirthDay)){
  99. builder.append("{\"range\":{\"birthday\":{\"gt\":\""+beginBirthDay+"\",\"lt\":\""+endBirthDay+"\"}}},");
  100. }else{
  101. if(StringUtils.isNotEmpty(beginBirthDay)){
  102. builder.append("{\"range\":{\"birthday\":{\"gt\":\""+beginBirthDay+"\"}}},");
  103. }
  104. if(StringUtils.isNotEmpty(endBirthDay)){
  105. builder.append("{\"range\":{\"birthday\":{\"lt\":\""+endBirthDay+"\"}}},");
  106. }
  107. }
  108. if(StringUtils.isNotEmpty(gender) || StringUtils.isNotEmpty(NOVPolicy) ||
  109. StringUtils.isNotEmpty(custclass) || StringUtils.isNotEmpty(sobirth) || StringUtils.isNotEmpty(SCustID)){
  110. builder.delete(builder.length()-1,builder.length());
  111. }
  112. builder.append("],");//must结束
  113. builder.append("\"must_not\": [ ],");//must_not
  114. builder.append("\"should\": [ ]");//should
  115. builder.append("}");//boot结束
  116. builder.append("},");//query结束
  117. builder.append("\"from\": "+pageNum+",");//第几页
  118. builder.append("\"size\": "+pageSize+",");//查询几个
  119. builder.append(" \"sort\": [{\"created_time\":{\"order\":\"desc\"}}],");//排序
  120. builder.append(" \"aggs\": {}");//
  121. builder.append("}");
  122. HttpEntity entity = new NStringEntity(builder.toString(), ContentType.APPLICATION_JSON);
  123. Response response= restClient.performRequest("GET", "/shanglifeecif.individual/_search",Collections.<String, String>emptyMap(),entity);
  124. String result = EntityUtils.toString(response.getEntity());
  125. JSONObject jsonObject = JSON.parseObject(result);
  126. JSONObject hitsobject = (JSONObject) jsonObject.get("hits");
  127. JSONArray array = JSON.parseArray(hitsobject.get("hits").toString());
  128. List listMap = new ArrayList<>();
  129. Map map = new HashMap();
  130. for (int i = 0; i <array.size() ; i++) {
  131. JSONObject json = (JSONObject)array.get(i);
  132. Map mp = new HashMap();
  133. mp.put("id",json.getString("_id"));
  134. JSONObject jsonOb = JSON.parseObject(json.getString("_source"));
  135. mp.putAll(JSON.parseObject(json.getString("_source"),Map.class));
  136. if(mp.containsKey("birthday")){
  137. Date date = jsonOb.getDate("birthday");
  138. if(date!=null){
  139. mp.put("birthday", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", date));
  140. mp.put("age",DateUtils.getAge(date));
  141. }else{
  142. mp.put("birthday", "");
  143. mp.put("age","");
  144. }
  145. }else{
  146. mp.put("birthday", "");
  147. mp.put("age","");
  148. }
  149. listMap.add(mp);
  150. }
  151. long total = hitsobject.getLong("total");
  152. map.put("list",listMap);
  153. map.put("total",total);
  154. return AjaxResult.success(map);
  155. } catch (Exception e) {
  156. e.printStackTrace();
  157. return AjaxResult.error();
  158. }
  159. }
  160. @GetMapping("/getCustomerById")
  161. @ResponseBody
  162. public AjaxResult getCustomerById(String id) {
  163. // 获取索引的别名,字段,创建时间http://10.32.2.231:9200/shanglifeecif.individual/default_type_/1
  164. try {
  165. Map<String, String> paramMap = new HashMap<String, String>();
  166. paramMap.put("pretty", "true");
  167. Response response = restClient.performRequest("GET", "/shanglifeecif.individual/default_type_/"+id, paramMap);
  168. String result = EntityUtils.toString(response.getEntity());
  169. Map map = new HashMap();
  170. JSONObject jsonObject = JSON.parseObject(result);
  171. Map mpInfo = new HashMap();
  172. String indid = jsonObject.getString("_id");
  173. mpInfo.put("id",indid);
  174. JSONObject source = JSON.parseObject(jsonObject.getString("_source"));
  175. mpInfo.putAll(JSON.parseObject(jsonObject.getString("_source"),Map.class));
  176. //mpInfo.put("birthday",DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",JSON.parseObject(jsonObject.getString("_source")).getDate("birthday")));
  177. if(mpInfo.containsKey("birthday")){
  178. Date date = source.getDate("birthday");
  179. if(date!=null){
  180. mpInfo.put("birthday",DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",date));
  181. mpInfo.put("age",DateUtils.getAge(date));
  182. }
  183. }else{
  184. mpInfo.put("birthday","");
  185. mpInfo.put("age","");
  186. }
  187. map.put("custInfo",mpInfo);
  188. Map query =new HashMap();
  189. query.put("indid1",indid);
  190. HttpEntity entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
  191. response= restClient.performRequest("GET", "/shanglifeecif.indrelationship/_search",Collections.<String, String>emptyMap(),entity);
  192. result = EntityUtils.toString(response.getEntity());
  193. jsonObject = JSON.parseObject(result);
  194. JSONObject hitsobject = (JSONObject) jsonObject.get("hits");
  195. JSONArray array = JSON.parseArray(hitsobject.get("hits").toString());
  196. List relationship = new ArrayList<>();
  197. for (int i = 0; i <array.size() ; i++) {
  198. JSONObject json = (JSONObject)array.get(i);
  199. Map mp = new HashMap();
  200. mp.put("id",json.getString("_id"));
  201. mp.putAll(JSON.parseObject(json.getString("_source"),Map.class));
  202. relationship.add(mp);
  203. }
  204. map.put("relationship",relationship);
  205. query =new HashMap();
  206. query.put("applicantid",indid);
  207. entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
  208. response= restClient.performRequest("GET", "/shanglifeecif.insuranceclaimthread/_search",Collections.<String, String>emptyMap(),entity);
  209. result = EntityUtils.toString(response.getEntity());
  210. jsonObject = JSON.parseObject(result);
  211. hitsobject = (JSONObject) jsonObject.get("hits");
  212. array = JSON.parseArray(hitsobject.get("hits").toString());
  213. List insuranceclaimthread = new ArrayList<>();
  214. for (int i = 0; i <array.size() ; i++) {
  215. JSONObject json = (JSONObject)array.get(i);
  216. Map mp = new HashMap();
  217. mp.put("id",json.getString("_id"));
  218. mp.putAll(JSON.parseObject(json.getString("_source"),Map.class));
  219. insuranceclaimthread.add(mp);
  220. }
  221. map.put("insuranceclaimthread",insuranceclaimthread);
  222. map.put("custInfo3",null);
  223. map.put("custInfo4",null);
  224. map.put("custInfo5",null);
  225. map.put("custInfo6",null);
  226. map.put("custInfo7",null);
  227. map.put("custInfo8",null);
  228. return AjaxResult.success(map);
  229. } catch (Exception e) {
  230. e.printStackTrace();
  231. return AjaxResult.error();
  232. }
  233. }
  234. @GetMapping("/getCustomerOverViewById")
  235. @ResponseBody
  236. public AjaxResult getCustomerOverViewById(String id) {
  237. // 获取索引的别名,字段,创建时间http://10.32.2.231:9200/shanglifeecif.individual/default_type_/1
  238. try {
  239. Map<String, String> paramMap = new HashMap<String, String>();
  240. paramMap.put("pretty", "true");
  241. Response response = restClient.performRequest("GET", "shanglifeecif.individual/default_type_/"+id, paramMap);
  242. String result = EntityUtils.toString(response.getEntity());
  243. Map map = new HashMap();
  244. JSONObject jsonObject = JSON.parseObject(result);
  245. Map mpInfo = new HashMap();
  246. String indid = jsonObject.getString("_id");
  247. mpInfo.put("id",indid);
  248. JSONObject source = JSON.parseObject(jsonObject.getString("_source"));
  249. mpInfo.putAll(JSON.parseObject(jsonObject.getString("_source"),Map.class));
  250. //mpInfo.put("birthday",DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",JSON.parseObject(jsonObject.getString("_source")).getDate("birthday")));
  251. if(mpInfo.containsKey("birthday")){
  252. Date date = source.getDate("birthday");
  253. if(date!=null){
  254. mpInfo.put("birthday",DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",date));
  255. mpInfo.put("age",DateUtils.getAge(date));
  256. }
  257. }else{
  258. mpInfo.put("birthday","");
  259. mpInfo.put("age","");
  260. }
  261. Map query =new HashMap();
  262. query.put("applicantid",indid);
  263. HttpEntity entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
  264. response= restClient.performRequest("GET", "/shanglifeecif.insurancearrangement/_search",Collections.<String, String>emptyMap(),entity);
  265. result = EntityUtils.toString(response.getEntity());
  266. jsonObject = JSON.parseObject(result);
  267. JSONObject hitsobject = (JSONObject) jsonObject.get("hits");
  268. JSONArray array = JSON.parseArray(hitsobject.get("hits").toString());
  269. long prem = 0;//总保费
  270. Map expectedData = new HashMap();
  271. List edata = new ArrayList();
  272. expectedData.put("name","");
  273. Map actaulData = new HashMap();
  274. List adata = new ArrayList();
  275. actaulData.put("name","");
  276. List xaxisData = new ArrayList();
  277. long insureDay = 0;
  278. for (int i = 0; i <array.size() ; i++) {
  279. JSONObject json = (JSONObject)array.get(i);
  280. JSONObject _source = (JSONObject) json.get("_source");
  281. if(_source.containsKey("prem")){
  282. prem+=_source.getLong("prem");
  283. edata.add(_source.getLong("prem"));
  284. adata.add(_source.getLong("prem"));
  285. }
  286. if(_source.containsKey("padate")){
  287. Date padate = _source.getDate("padate");
  288. xaxisData.add(DateUtils.parseDateToStr("MM",padate));
  289. insureDay = DateUtils.getDateDifDay(new Date(),padate);
  290. }
  291. }
  292. expectedData.put("data",edata);
  293. actaulData.put("data",adata);
  294. Map lineBarChartData = new HashMap();
  295. lineBarChartData.put("expectedData",expectedData);
  296. lineBarChartData.put("actaulData",actaulData);
  297. lineBarChartData.put("xaxisData",xaxisData);
  298. map.put("lineBarChartData",lineBarChartData);
  299. query =new HashMap();
  300. query.put("applicantid",indid);
  301. entity = new NStringEntity(EsJsonUtil.QuerygetMust(query), ContentType.APPLICATION_JSON);
  302. response= restClient.performRequest("GET", "/shanglifeecif.insuranceclaimthread/_search",Collections.<String, String>emptyMap(),entity);
  303. result = EntityUtils.toString(response.getEntity());
  304. jsonObject = JSON.parseObject(result);
  305. hitsobject = (JSONObject) jsonObject.get("hits");
  306. array = JSON.parseArray(hitsobject.get("hits").toString());
  307. List insuranceclaimthread = new ArrayList<>();
  308. for (int i = 0; i <array.size() ; i++) {
  309. JSONObject json = (JSONObject)array.get(i);
  310. Map mp = new HashMap();
  311. mp.put("id",json.getString("_id"));
  312. mp.putAll(JSON.parseObject(json.getString("_source"),Map.class));
  313. insuranceclaimthread.add(mp);
  314. }
  315. map.put("insuranceclaimthread",insuranceclaimthread);
  316. mpInfo.put("prem",prem);
  317. mpInfo.put("insureDay",insureDay);
  318. map.put("custInfo",mpInfo);
  319. return AjaxResult.success(map);
  320. } catch (Exception e) {
  321. e.printStackTrace();
  322. return AjaxResult.error();
  323. }
  324. }
  325. }