| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- package com.malk.eastar.service.impl;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.malk.eastar.model.AITableParam;
- import com.malk.eastar.model.AITableResult;
- import com.malk.eastar.service.MDTableClient;
- import com.malk.eastar.service.AitableService;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.*;
- /**
- * 专用于AI表格复杂逻辑处理的接口服务
- * add by Jason 20260409
- */
- @Slf4j
- @Service
- public class AitableServiceImpl implements AitableService {
- @Autowired
- private MDTableClient mdTableClient;
- @Override
- public List<Map> querySys1CustomerData() throws Exception {
- /*
- 参数定义
- */
- List<Map> customerData = new ArrayList<>(); // 客户数据
- List<Map> customerDataDeduplicate = new ArrayList<>(); // 客户数据(去重)
- String baseId; //AI表格文档ID
- String sheetIdOrName; //数据表ID或数据表名称
- Map<String,Object> param = new HashMap<>(); //HTTP请求参数
- AITableParam aiTableParam = new AITableParam(); //HTTP请求体
- boolean hasMore; //是否还有下一页
- int pageNo; //当前页码
- int pageSize; //每页获取的数据量(上限100)
- AITableResult aiTableResult; //请求返回内容
- JSONArray records; //每页记录数据
- JSONArray allRecords = new JSONArray(); //所有记录数据
- JSONObject fields; //字段数据
- String customerName; //客户名称
- JSONArray businessUserJSONArray; //业务【人事】
- String businessUserUnionId; //业务员unionId
- String businessUserName; //业务员姓名
- String kp; //KP
- boolean isCustomerNameEmpty; //客户名称是否为空
- boolean isBusinessUserEmpty; //业务员是否为空
- boolean isKpEmpty; //KP是否为空
- int customerEmptyCount = 0; //客户名称为空的记录数
- Map<String,String> customerRecord; //客户记录
- Map<String, Long> customerNameCountMap = new HashMap<>(); //客户名称与出现的次数映射
- /*
- 项目总表副本-20260409
- */
- baseId = "GZLxjv9VGqBAMDOoHYzrPZzE86EDybno";
- sheetIdOrName = "E6RcJi3";
- /*
- 初始化参数值
- */
- hasMore = true; //第一页默认有数据
- pageNo = 1;
- pageSize = 100;
- param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
- aiTableParam.setMaxResults(pageSize);
- /*
- 翻页获取数据
- */
- while(hasMore){
- aiTableResult = mdTableClient.queryMultiRecords(baseId,sheetIdOrName,param,aiTableParam);
- hasMore = aiTableResult.getHasMore();
- log.info("当前第"+pageNo+"页");
- log.info("记录数="+aiTableResult.getRecords().size());
- records = aiTableResult.getRecords();
- for(int i=0;i<records.size();i++){
- allRecords.add(records.getJSONObject(i));
- }
- log.info("是否有更多数据:"+hasMore);
- pageNo++;
- aiTableParam.setNextToken(aiTableResult.getNextToken());
- }
- /*
- 请求记录数据处理
- */
- log.info("所有记录数量="+allRecords.size());
- customerName = "";
- businessUserUnionId = "";
- businessUserName = "";
- kp = "";
- for(int i=0;i<allRecords.size();i++){
- /*
- 重置是否为空标识
- */
- isCustomerNameEmpty = false;
- isBusinessUserEmpty = false;
- isKpEmpty = false;
- //解析字段数据
- fields = allRecords.getJSONObject(i).getJSONObject("fields");
- /*
- 获取客户名称数据
- */
- if(fields.get("客户公司名【基础】") == null){
- isCustomerNameEmpty = true;
- }else{
- customerName = fields.getString("客户公司名【基础】");
- customerName = customerName.replace("\n",""); //去除换行符
- customerName = customerName.trim(); //去除空格
- if(StringUtils.isEmpty(customerName)){
- isCustomerNameEmpty = true;
- }
- }
- if(isCustomerNameEmpty){
- customerEmptyCount++;
- // throw new RuntimeException("客户公司名【基础】字段为空");
- continue;
- }else{
- // System.out.println("客户名称="+customerName);
- }
- /*
- 获取业务员数据
- */
- if(fields.get("业务【人事】") == null){
- isBusinessUserEmpty = true;
- }else{
- businessUserJSONArray = fields.getJSONArray("业务【人事】");
- if(businessUserJSONArray.isEmpty()){
- isBusinessUserEmpty = true;
- }else{
- if(businessUserJSONArray.size()>1){ //一个客户有多个业务员负责对接
- log.info("客户名称="+customerName+",业务员数量="+businessUserJSONArray.size());
- }
- businessUserUnionId = businessUserJSONArray.getJSONObject(0).getString("unionId");
- businessUserName = businessUserJSONArray.getJSONObject(0).getString("name");
- }
- }
- // System.out.println("业务员是否为空="+isBusinessUserEmpty);
- if(isBusinessUserEmpty){
- // throw new RuntimeException("业务【人事】字段为空");
- }else{
- // System.out.println("业务员="+businessUser);
- }
- /*
- 获取KP数据
- */
- /*
- if(fields.get("一把手KP") == null){
- isKpEmpty = true;
- }else{
- kp = fields.getString("一把手KP");
- }
- // System.out.println("KP是否为空="+isKpEmpty);
- if(isKpEmpty){
- // throw new RuntimeException("一把手KP字段为空");
- }else{
- // System.out.println("KP="+kp);
- }
- */
- /*
- 汇总客户数据
- */
- customerRecord = new HashMap<>();
- customerRecord.put("customerName",customerName);
- customerRecord.put("businessUserName",businessUserName);
- customerRecord.put("businessUserUnionId",businessUserUnionId);
- // customerRecord.put("kp",kp);
- // System.out.println(JSONObject.toJSONString(customerRecord));
- customerData.add(customerRecord);
- }
- log.info("客户名称为空的记录数="+customerEmptyCount);
- /*
- 排除有重复的客户
- */
- for (Map<String, String> record : customerData) {
- customerName = record.get("customerName");
- customerNameCountMap.merge(customerName, 1L, Long::sum);
- }
- for (Map.Entry<String, Long> entry : customerNameCountMap.entrySet()) {
- if (entry.getValue() > 1) {
- System.out.println("客户名称="+entry.getKey()+",重复次数="+ entry.getValue());
- }
- }
- for (Map<String, String> record : customerData) {
- customerName = record.get("customerName");
- if(customerNameCountMap.get(customerName)==1){
- customerDataDeduplicate.add(record);
- }
- }
- /*
- 返回客户数据(去重)
- */
- return customerDataDeduplicate;
- }
- }
|