AitableServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. String aiTableId; //AI表格ID
  40. JSONObject fields; //字段数据
  41. String customerName; //客户名称
  42. JSONArray businessUserJSONArray; //业务【人事】
  43. String businessUserUnionId; //业务员unionId
  44. String businessUserName; //业务员姓名
  45. String kp; //KP
  46. boolean isCustomerNameEmpty; //客户名称是否为空
  47. boolean isBusinessUserEmpty; //业务员是否为空
  48. boolean isKpEmpty; //KP是否为空
  49. int customerEmptyCount = 0; //客户名称为空的记录数
  50. Map<String,String> customerRecord; //客户记录
  51. Map<String, Long> customerNameCountMap = new HashMap<>(); //客户名称与出现的次数映射
  52. String customerLevel; //客户分级
  53. String newOrOld; //新老客户
  54. /*
  55. 项目总表
  56. */
  57. baseId = "G53mjyd80pEr5grBfpjmMX6586zbX04v";
  58. sheetIdOrName = "E6RcJi3";
  59. /*
  60. 项目总表副本-20260409
  61. */
  62. // baseId = "GZLxjv9VGqBAMDOoHYzrPZzE86EDybno";
  63. // sheetIdOrName = "E6RcJi3";
  64. /*
  65. 初始化参数值
  66. */
  67. hasMore = true; //第一页默认有数据
  68. pageNo = 1;
  69. pageSize = 100;
  70. param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
  71. aiTableParam.setMaxResults(pageSize);
  72. /*
  73. 翻页获取数据
  74. */
  75. while(hasMore){
  76. aiTableResult = mdTableClient.queryMultiRecords(baseId,sheetIdOrName,param,aiTableParam);
  77. hasMore = aiTableResult.getHasMore();
  78. log.info("当前第"+pageNo+"页");
  79. log.info("记录数="+aiTableResult.getRecords().size());
  80. records = aiTableResult.getRecords();
  81. for(int i=0;i<records.size();i++){
  82. allRecords.add(records.getJSONObject(i));
  83. }
  84. log.info("是否有更多数据:"+hasMore);
  85. pageNo++;
  86. aiTableParam.setNextToken(aiTableResult.getNextToken());
  87. }
  88. /*
  89. 请求记录数据处理
  90. */
  91. log.info("所有记录数量="+allRecords.size());
  92. customerName = "";
  93. businessUserUnionId = "";
  94. businessUserName = "";
  95. kp = "";
  96. for(int i=0;i<allRecords.size();i++){
  97. /*
  98. 重置是否为空标识
  99. */
  100. isCustomerNameEmpty = false;
  101. isBusinessUserEmpty = false;
  102. isKpEmpty = false;
  103. //获取AI表格ID
  104. aiTableId = allRecords.getJSONObject(i).getString("id");
  105. //解析字段数据
  106. fields = allRecords.getJSONObject(i).getJSONObject("fields");
  107. /*
  108. 获取客户名称数据
  109. */
  110. if(fields.get("客户公司名【基础】") == null){
  111. isCustomerNameEmpty = true;
  112. }else{
  113. customerName = fields.getString("客户公司名【基础】");
  114. customerName = customerName.replace("\n",""); //去除换行符
  115. customerName = customerName.trim(); //去除空格
  116. if(StringUtils.isEmpty(customerName)){
  117. isCustomerNameEmpty = true;
  118. }
  119. }
  120. if(isCustomerNameEmpty){
  121. customerEmptyCount++;
  122. // throw new RuntimeException("客户公司名【基础】字段为空");
  123. continue;
  124. }else{
  125. // System.out.println("客户名称="+customerName);
  126. }
  127. /*
  128. 获取业务员数据
  129. */
  130. if(fields.get("业务【人事】") == null){
  131. isBusinessUserEmpty = true;
  132. }else{
  133. businessUserJSONArray = fields.getJSONArray("业务【人事】");
  134. if(businessUserJSONArray.isEmpty()){
  135. isBusinessUserEmpty = true;
  136. }else{
  137. if(businessUserJSONArray.size()>1){ //一个客户有多个业务员负责对接
  138. log.info("客户名称="+customerName+",业务员数量="+businessUserJSONArray.size());
  139. }
  140. businessUserUnionId = businessUserJSONArray.getJSONObject(0).getString("unionId");
  141. businessUserName = businessUserJSONArray.getJSONObject(0).getString("name");
  142. }
  143. }
  144. // System.out.println("业务员是否为空="+isBusinessUserEmpty);
  145. if(isBusinessUserEmpty){
  146. // throw new RuntimeException("业务【人事】字段为空");
  147. }else{
  148. // System.out.println("业务员="+businessUser);
  149. }
  150. /*
  151. 获取客户分级
  152. */
  153. customerLevel = "";
  154. if(fields.get("客户分级【客户】") != null){
  155. customerLevel = fields.getJSONObject("客户分级【客户】").getString("name");
  156. if(
  157. "A".equals(customerLevel)
  158. ||"潜A".equals(customerLevel)
  159. ||"B".equals(customerLevel)
  160. ||"潜B".equals(customerLevel)
  161. ||"C".equals(customerLevel)
  162. ||"潜C".equals(customerLevel)
  163. ||"D".equals(customerLevel)
  164. ||"潜D".equals(customerLevel)
  165. ){
  166. //不变
  167. }else if("E".equals(customerLevel)){
  168. customerLevel = "保稳E";
  169. }else{
  170. customerLevel = "";
  171. }
  172. }
  173. /*
  174. 获取新老客户
  175. */
  176. newOrOld = "";
  177. if(fields.get("新老客户?【客户】") != null){
  178. newOrOld = fields.getJSONObject("新老客户?【客户】").getString("name");
  179. if(newOrOld.contains("新")){
  180. newOrOld = "新";
  181. }
  182. if(newOrOld.contains("老")){
  183. newOrOld = "老";
  184. }
  185. if(newOrOld.contains("不合作")){
  186. newOrOld = "不合作";
  187. }
  188. }
  189. /*
  190. 获取KP数据
  191. */
  192. /*
  193. if(fields.get("一把手KP") == null){
  194. isKpEmpty = true;
  195. }else{
  196. kp = fields.getString("一把手KP");
  197. }
  198. // System.out.println("KP是否为空="+isKpEmpty);
  199. if(isKpEmpty){
  200. // throw new RuntimeException("一把手KP字段为空");
  201. }else{
  202. // System.out.println("KP="+kp);
  203. }
  204. */
  205. /*
  206. 汇总客户数据
  207. */
  208. customerRecord = new HashMap<>();
  209. customerRecord.put("aiTableId",aiTableId);
  210. customerRecord.put("customerName",customerName);
  211. customerRecord.put("businessUserName",businessUserName);
  212. customerRecord.put("businessUserUnionId",businessUserUnionId);
  213. // customerRecord.put("kp",kp);
  214. // System.out.println(JSONObject.toJSONString(customerRecord));
  215. customerRecord.put("customerLevel",customerLevel);
  216. customerRecord.put("newOrOld",newOrOld);
  217. // System.out.println(aiTableId+","+customerLevel+","+newOrOld);
  218. customerData.add(customerRecord);
  219. }
  220. log.info("客户名称为空的记录数="+customerEmptyCount);
  221. /*
  222. 排除有重复的客户
  223. */
  224. for (Map<String, String> record : customerData) {
  225. customerName = record.get("customerName");
  226. customerNameCountMap.merge(customerName, 1L, Long::sum);
  227. }
  228. for (Map.Entry<String, Long> entry : customerNameCountMap.entrySet()) {
  229. if (entry.getValue() > 1) {
  230. System.out.println("客户名称="+entry.getKey()+",重复次数="+ entry.getValue());
  231. }
  232. }
  233. for (Map<String, String> record : customerData) {
  234. customerName = record.get("customerName");
  235. if(customerNameCountMap.get(customerName)==1){
  236. customerDataDeduplicate.add(record);
  237. }
  238. }
  239. /*
  240. 返回客户数据(去重)
  241. */
  242. return customerDataDeduplicate;
  243. }
  244. @Override
  245. public String querySys1CustomerIdByName(String customerName) throws Exception {
  246. /*
  247. 参数定义
  248. */
  249. String baseId; //AI表格文档ID
  250. String sheetIdOrName; //数据表ID或数据表名称
  251. Map<String,Object> param = new HashMap<>(); //HTTP请求参数
  252. AITableParam aiTableParam = new AITableParam(); //HTTP请求体
  253. AITableResult aiTableResult; //请求返回内容
  254. JSONArray records; //记录数据
  255. String customerId = ""; //客户名册记录ID
  256. /*
  257. 项目总表
  258. */
  259. baseId = "G53mjyd80pEr5grBfpjmMX6586zbX04v";
  260. sheetIdOrName = "E6RcJi3";
  261. /*
  262. 项目总表副本-20260409
  263. */
  264. // baseId = "GZLxjv9VGqBAMDOoHYzrPZzE86EDybno";
  265. // sheetIdOrName = "E6RcJi3";
  266. /*
  267. 初始化参数值
  268. */
  269. param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
  270. JSONObject filter = new JSONObject();
  271. JSONArray conditions = new JSONArray();
  272. JSONObject condition = new JSONObject();
  273. condition.put("field","20rO7zS");
  274. condition.put("operator","equal");
  275. condition.put("value",new String[]{customerName});
  276. conditions.add( condition);
  277. filter.put("conditions",conditions);
  278. aiTableParam.setFilter(filter);
  279. /*
  280. 获取数据
  281. */
  282. log.info(JSONObject.toJSONString(aiTableParam));
  283. aiTableResult = mdTableClient.queryMultiRecords(baseId,sheetIdOrName,param,aiTableParam);
  284. log.info(JSONObject.toJSONString(aiTableResult));
  285. records = aiTableResult.getRecords();
  286. if(records!=null && !records.isEmpty()){
  287. customerId = records.getJSONObject(0).getString("id");
  288. }
  289. return customerId;
  290. }
  291. @Override
  292. public String addSys1CustomerData(Map<String, Object> fields) throws Exception {
  293. /*
  294. 参数定义
  295. */
  296. String baseId; //AI表格文档ID
  297. String sheetIdOrName; //数据表ID或数据表名称
  298. Map<String,Object> param = new HashMap<>(); //HTTP请求参数
  299. JSONObject insertData = new JSONObject(); //HTTP请求体
  300. JSONArray insertRecords = new JSONArray(); //新增记录集合
  301. JSONObject insertRecord = new JSONObject(); //新增记录行数据
  302. String result; //返回结果
  303. JSONObject resultJSONObject;
  304. JSONArray aitableIdJSONArray;
  305. String aitableId = ""; //AI表格记录ID
  306. /*
  307. 项目总表
  308. */
  309. baseId = "G53mjyd80pEr5grBfpjmMX6586zbX04v";
  310. sheetIdOrName = "E6RcJi3";
  311. /*
  312. 初始化参数值
  313. */
  314. param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
  315. insertRecord.put("fields",fields);
  316. insertRecords.add(insertRecord);
  317. insertData.put("records",insertRecords);
  318. /*
  319. 新增数据
  320. */
  321. log.info(JSONObject.toJSONString(insertData));
  322. result = mdTableClient.createMultiRecords(baseId,sheetIdOrName,param,insertData);
  323. log.info(result);
  324. resultJSONObject = JSONObject.parseObject(result);
  325. aitableIdJSONArray = resultJSONObject.getJSONArray("value");
  326. if(aitableIdJSONArray!=null || !aitableIdJSONArray.isEmpty()){
  327. aitableId = aitableIdJSONArray.getJSONObject(0).getString("id");
  328. }
  329. return aitableId;
  330. }
  331. @Override
  332. public String updateSys1CustomerData(String id,Map<String, Object> fields) throws Exception {
  333. /*
  334. 参数定义
  335. */
  336. String baseId; //AI表格文档ID
  337. String sheetIdOrName; //数据表ID或数据表名称
  338. Map<String,Object> param = new HashMap<>(); //HTTP请求参数
  339. JSONObject updateData = new JSONObject(); //HTTP请求体
  340. JSONArray updateRecords = new JSONArray(); //更新记录集合
  341. JSONObject updateRecord = new JSONObject(); //更新记录行数据
  342. String result; //返回结果
  343. /*
  344. 项目总表
  345. */
  346. baseId = "G53mjyd80pEr5grBfpjmMX6586zbX04v";
  347. sheetIdOrName = "E6RcJi3";
  348. /*
  349. 初始化参数值
  350. */
  351. param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
  352. updateRecord.put("id",id);
  353. updateRecord.put("fields",fields);
  354. updateRecords.add(updateRecord);
  355. updateData.put("records",updateRecords);
  356. /*
  357. 更新数据
  358. */
  359. log.info(JSONObject.toJSONString(updateData));
  360. result = mdTableClient.modifyMultiRecords(baseId,sheetIdOrName,param,updateData);
  361. log.info(result);
  362. return result;
  363. }
  364. @Override
  365. public String addSys1SaleData(Map<String,Object> fields) throws Exception {
  366. /*
  367. 参数定义
  368. */
  369. String baseId; //AI表格文档ID
  370. String sheetIdOrName; //数据表ID或数据表名称
  371. Map<String,Object> param = new HashMap<>(); //HTTP请求参数
  372. JSONObject insertData = new JSONObject(); //HTTP请求体
  373. JSONArray insertRecords = new JSONArray(); //新增记录集合
  374. JSONObject insertRecord = new JSONObject(); //新增记录行数据
  375. String result; //返回结果
  376. JSONObject resultJSONObject;
  377. JSONArray aitableIdJSONArray;
  378. String aitableId = ""; //AI表格记录ID
  379. /*
  380. 项目总表
  381. */
  382. baseId = "G53mjyd80pEr5grBfpjmMX6586zbX04v";
  383. sheetIdOrName = "C1mtX34";
  384. /*
  385. 项目总表副本-20260409
  386. */
  387. // baseId = "GZLxjv9VGqBAMDOoHYzrPZzE86EDybno";
  388. // sheetIdOrName = "C1mtX34"; //售前台账
  389. /*
  390. 初始化参数值
  391. */
  392. param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
  393. insertRecord.put("fields",fields);
  394. insertRecords.add(insertRecord);
  395. insertData.put("records",insertRecords);
  396. /*
  397. 新增数据
  398. */
  399. log.info(JSONObject.toJSONString(insertData));
  400. result = mdTableClient.createMultiRecords(baseId,sheetIdOrName,param,insertData);
  401. log.info(result);
  402. resultJSONObject = JSONObject.parseObject(result);
  403. aitableIdJSONArray = resultJSONObject.getJSONArray("value");
  404. if(aitableIdJSONArray!=null || !aitableIdJSONArray.isEmpty()){
  405. aitableId = aitableIdJSONArray.getJSONObject(0).getString("id");
  406. }
  407. return aitableId;
  408. }
  409. @Override
  410. public String updateSys1SaleData(String id,Map<String, Object> fields) throws Exception {
  411. /*
  412. 参数定义
  413. */
  414. String baseId; //AI表格文档ID
  415. String sheetIdOrName; //数据表ID或数据表名称
  416. Map<String,Object> param = new HashMap<>(); //HTTP请求参数
  417. JSONObject updateData = new JSONObject(); //HTTP请求体
  418. JSONArray updateRecords = new JSONArray(); //更新记录集合
  419. JSONObject updateRecord = new JSONObject(); //更新记录行数据
  420. String result; //返回结果
  421. /*
  422. 项目总表
  423. */
  424. baseId = "G53mjyd80pEr5grBfpjmMX6586zbX04v";
  425. sheetIdOrName = "C1mtX34";
  426. /*
  427. 初始化参数值
  428. */
  429. param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
  430. updateRecord.put("id",id);
  431. updateRecord.put("fields",fields);
  432. updateRecords.add(updateRecord);
  433. updateData.put("records",updateRecords);
  434. /*
  435. 更新数据
  436. */
  437. log.info(JSONObject.toJSONString(updateData));
  438. result = mdTableClient.modifyMultiRecords(baseId,sheetIdOrName,param,updateData);
  439. log.info(result);
  440. return result;
  441. }
  442. }