AitableServiceImpl.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package com.malk.eastar.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.malk.eastar.model.AITableParam;
  5. import com.malk.eastar.model.AITableResult;
  6. import com.malk.eastar.service.MDTableClient;
  7. import com.malk.eastar.service.AitableService;
  8. import lombok.extern.slf4j.Slf4j;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.stereotype.Service;
  12. import java.util.*;
  13. /**
  14. * 专用于AI表格复杂逻辑处理的接口服务
  15. * add by Jason 20260409
  16. */
  17. @Slf4j
  18. @Service
  19. public class AitableServiceImpl implements AitableService {
  20. @Autowired
  21. private MDTableClient mdTableClient;
  22. @Override
  23. public List<Map> querySys1CustomerData() throws Exception {
  24. /*
  25. 参数定义
  26. */
  27. List<Map> customerData = new ArrayList<>(); // 客户数据
  28. List<Map> customerDataDeduplicate = new ArrayList<>(); // 客户数据(去重)
  29. String baseId; //AI表格文档ID
  30. String sheetIdOrName; //数据表ID或数据表名称
  31. Map<String,Object> param = new HashMap<>(); //HTTP请求参数
  32. AITableParam aiTableParam = new AITableParam(); //HTTP请求体
  33. boolean hasMore; //是否还有下一页
  34. int pageNo; //当前页码
  35. int pageSize; //每页获取的数据量(上限100)
  36. AITableResult aiTableResult; //请求返回内容
  37. JSONArray records; //每页记录数据
  38. JSONArray allRecords = new JSONArray(); //所有记录数据
  39. JSONObject fields; //字段数据
  40. String customerName; //客户名称
  41. JSONArray businessUserJSONArray; //业务【人事】
  42. String businessUserUnionId; //业务员unionId
  43. String businessUserName; //业务员姓名
  44. String kp; //KP
  45. boolean isCustomerNameEmpty; //客户名称是否为空
  46. boolean isBusinessUserEmpty; //业务员是否为空
  47. boolean isKpEmpty; //KP是否为空
  48. int customerEmptyCount = 0; //客户名称为空的记录数
  49. Map<String,String> customerRecord; //客户记录
  50. Map<String, Long> customerNameCountMap = new HashMap<>(); //客户名称与出现的次数映射
  51. /*
  52. 项目总表副本-20260409
  53. */
  54. baseId = "GZLxjv9VGqBAMDOoHYzrPZzE86EDybno";
  55. sheetIdOrName = "E6RcJi3";
  56. /*
  57. 初始化参数值
  58. */
  59. hasMore = true; //第一页默认有数据
  60. pageNo = 1;
  61. pageSize = 100;
  62. param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
  63. aiTableParam.setMaxResults(pageSize);
  64. /*
  65. 翻页获取数据
  66. */
  67. while(hasMore){
  68. aiTableResult = mdTableClient.queryMultiRecords(baseId,sheetIdOrName,param,aiTableParam);
  69. hasMore = aiTableResult.getHasMore();
  70. log.info("当前第"+pageNo+"页");
  71. log.info("记录数="+aiTableResult.getRecords().size());
  72. records = aiTableResult.getRecords();
  73. for(int i=0;i<records.size();i++){
  74. allRecords.add(records.getJSONObject(i));
  75. }
  76. log.info("是否有更多数据:"+hasMore);
  77. pageNo++;
  78. aiTableParam.setNextToken(aiTableResult.getNextToken());
  79. }
  80. /*
  81. 请求记录数据处理
  82. */
  83. log.info("所有记录数量="+allRecords.size());
  84. customerName = "";
  85. businessUserUnionId = "";
  86. businessUserName = "";
  87. kp = "";
  88. for(int i=0;i<allRecords.size();i++){
  89. /*
  90. 重置是否为空标识
  91. */
  92. isCustomerNameEmpty = false;
  93. isBusinessUserEmpty = false;
  94. isKpEmpty = false;
  95. //解析字段数据
  96. fields = allRecords.getJSONObject(i).getJSONObject("fields");
  97. /*
  98. 获取客户名称数据
  99. */
  100. if(fields.get("客户公司名【基础】") == null){
  101. isCustomerNameEmpty = true;
  102. }else{
  103. customerName = fields.getString("客户公司名【基础】");
  104. customerName = customerName.replace("\n",""); //去除换行符
  105. customerName = customerName.trim(); //去除空格
  106. if(StringUtils.isEmpty(customerName)){
  107. isCustomerNameEmpty = true;
  108. }
  109. }
  110. if(isCustomerNameEmpty){
  111. customerEmptyCount++;
  112. // throw new RuntimeException("客户公司名【基础】字段为空");
  113. continue;
  114. }else{
  115. // System.out.println("客户名称="+customerName);
  116. }
  117. /*
  118. 获取业务员数据
  119. */
  120. if(fields.get("业务【人事】") == null){
  121. isBusinessUserEmpty = true;
  122. }else{
  123. businessUserJSONArray = fields.getJSONArray("业务【人事】");
  124. if(businessUserJSONArray.isEmpty()){
  125. isBusinessUserEmpty = true;
  126. }else{
  127. if(businessUserJSONArray.size()>1){ //一个客户有多个业务员负责对接
  128. log.info("客户名称="+customerName+",业务员数量="+businessUserJSONArray.size());
  129. }
  130. businessUserUnionId = businessUserJSONArray.getJSONObject(0).getString("unionId");
  131. businessUserName = businessUserJSONArray.getJSONObject(0).getString("name");
  132. }
  133. }
  134. // System.out.println("业务员是否为空="+isBusinessUserEmpty);
  135. if(isBusinessUserEmpty){
  136. // throw new RuntimeException("业务【人事】字段为空");
  137. }else{
  138. // System.out.println("业务员="+businessUser);
  139. }
  140. /*
  141. 获取KP数据
  142. */
  143. /*
  144. if(fields.get("一把手KP") == null){
  145. isKpEmpty = true;
  146. }else{
  147. kp = fields.getString("一把手KP");
  148. }
  149. // System.out.println("KP是否为空="+isKpEmpty);
  150. if(isKpEmpty){
  151. // throw new RuntimeException("一把手KP字段为空");
  152. }else{
  153. // System.out.println("KP="+kp);
  154. }
  155. */
  156. /*
  157. 汇总客户数据
  158. */
  159. customerRecord = new HashMap<>();
  160. customerRecord.put("customerName",customerName);
  161. customerRecord.put("businessUserName",businessUserName);
  162. customerRecord.put("businessUserUnionId",businessUserUnionId);
  163. // customerRecord.put("kp",kp);
  164. // System.out.println(JSONObject.toJSONString(customerRecord));
  165. customerData.add(customerRecord);
  166. }
  167. log.info("客户名称为空的记录数="+customerEmptyCount);
  168. /*
  169. 排除有重复的客户
  170. */
  171. for (Map<String, String> record : customerData) {
  172. customerName = record.get("customerName");
  173. customerNameCountMap.merge(customerName, 1L, Long::sum);
  174. }
  175. for (Map.Entry<String, Long> entry : customerNameCountMap.entrySet()) {
  176. if (entry.getValue() > 1) {
  177. System.out.println("客户名称="+entry.getKey()+",重复次数="+ entry.getValue());
  178. }
  179. }
  180. for (Map<String, String> record : customerData) {
  181. customerName = record.get("customerName");
  182. if(customerNameCountMap.get(customerName)==1){
  183. customerDataDeduplicate.add(record);
  184. }
  185. }
  186. /*
  187. 返回客户数据(去重)
  188. */
  189. return customerDataDeduplicate;
  190. }
  191. }