| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- package com.malk.eastar.schedule;
- import cn.hutool.core.date.DateUtil;
- 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.EastarTbService;
- import com.malk.eastar.service.YidaService;
- import com.malk.server.aliwork.YDConf;
- import com.malk.server.aliwork.YDParam;
- import com.malk.server.aliwork.YDSearch;
- import com.malk.service.aliwork.YDClient;
- import com.malk.service.aliwork.YDService;
- import com.malk.utils.PublicUtil;
- import com.malk.utils.UtilDateTime;
- import com.malk.utils.UtilMap;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.scheduling.annotation.Scheduled;
- import java.time.LocalDate;
- import java.time.LocalTime;
- import java.util.*;
- /**
- * @EnableScheduling 开启定时任务 [配置参考McScheduleTask]
- */
- @Slf4j
- @Configuration
- @EnableScheduling
- @ConditionalOnProperty(name = {"enable.scheduling"})
- public class ScheduleTask {
- @Autowired
- private YDClient ydClient;
- @Autowired
- private EastarTbService eastarTbService;
- @Autowired
- private YDService ydService;
- //add by Jason 20260313 start
- @Autowired
- private MDTableClient mdTableClient;
- //add by Jason 20260313 end
- //add by Jason 20260522 start
- @Autowired
- private YidaService yidaService;
- //add by Jason 20260522 end
- /**
- * 私海客户流出
- */
- @Scheduled(cron = "0 0 5 * * ?")
- public void releasePrivatePoolCustomers() {
- log.info("私海客户流出定时任务");
- try {
- //查询所有已建联客户跟进记录
- List<Map> establishContactData = yidaService.queryLtcCustomerEstablishContactData();
- log.info("已建联跟进记录数:"+ establishContactData.size());
- //查询超过3天未跟进的私海新客户记录
- long endTime = UtilDateTime.getLocalDateTimeTimeStamp(LocalDate.now().minusDays(3).atTime(LocalTime.MIN));
- List ydFilters = Arrays.asList(
- // new YDSearch("radioField_mnpdkss9","否","测试使用", YDSearch.Type.RADIO_FIELD,YDSearch.Operator.EQ),
- new YDSearch("radioField_md6q7oxa","私","公海or私海", YDSearch.Type.RADIO_FIELD,YDSearch.Operator.EQ),
- new YDSearch("radioField_md6q7ox8","新","新老客户", YDSearch.Type.RADIO_FIELD,YDSearch.Operator.EQ),
- new YDSearch("dateField_l3tw3kfn",endTime,"最后跟进时间",YDSearch.Type.DATE_FIELD,YDSearch.Operator.LT)
- );
- List<Map> ltcCustomerData = yidaService.queryLtcNotFollowedCustomerData(ydFilters);
- log.info("超过3天未跟进的私海新客户记录数:"+ ltcCustomerData.size());
-
- //排除已建联
- Set<String> contactedCustomerCodes = new HashSet<>();
- for(Map contactRecord : establishContactData){
- String customerCode = UtilMap.getString(contactRecord, "customerCode");
- if(StringUtils.isNotEmpty(customerCode)){
- contactedCustomerCodes.add(customerCode);
- }
- }
- List<Map> filteredCustomerData = new ArrayList<>();
- for(Map customerRecord : ltcCustomerData){
- String customerCode = UtilMap.getString(customerRecord, "customerCode");
- if(!contactedCustomerCodes.contains(customerCode)){
- filteredCustomerData.add(customerRecord);
- }
- }
- log.info("排除已建联后的记录数:"+ filteredCustomerData.size());
- Map alertDetailRecord;
- for (Map customerRecord : filteredCustomerData) {
- alertDetailRecord = new HashMap();
- alertDetailRecord.put("businessUserId",UtilMap.getString(customerRecord, "userId"));
- alertDetailRecord.put("formInstId",UtilMap.getString(customerRecord, "formInstId"));
- alertDetailRecord.put("customerName",UtilMap.getString(customerRecord, "customerName"));
- alertDetailRecord.put("customerCode",UtilMap.getString(customerRecord, "customerCode"));
- alertDetailRecord.put("today", UtilDateTime.getLocalDateTimeTimeStamp(LocalDate.now().atStartOfDay()));
- // alertDetailRecord.put("tomorrow", UtilDateTime.getLocalDateTimeTimeStamp(LocalDate.now().plusDays(1).atStartOfDay()));
- alertDetailRecord.put("remark","私海,新客户,未建联,超过3天未跟进");
- yidaService.addAlertDetail(alertDetailRecord);
- }
- //按业务员分组,统计客户数量
- Map<String, List<Map>> customerGroupBySales = new HashMap<>();
- for (Map customerRecord : filteredCustomerData) {
- String salesPerson = UtilMap.getString(customerRecord, "userId");
- if (StringUtils.isEmpty(salesPerson)) {
- salesPerson = "未知业务员";
- }
- customerGroupBySales.computeIfAbsent(salesPerson, k -> new ArrayList<>()).add(customerRecord);
- }
- Map alertLogRecord;
- for (Map.Entry<String, List<Map>> entry : customerGroupBySales.entrySet()) {
- log.info("业务员: {}, 可能会流出客户数量: {}", entry.getKey(), entry.getValue().size());
- //新增私海客户流出告警日志
- alertLogRecord = new HashMap();
- alertLogRecord.put("businessUserId",entry.getKey());
- alertLogRecord.put("customerNum",entry.getValue().size());
- alertLogRecord.put("today", UtilDateTime.getLocalDateTimeTimeStamp(LocalDate.now().atStartOfDay()));
- yidaService.addAlertLog(alertLogRecord);
- }
- //查询超过30天未跟进的私海新客户记录
- // long endTime= UtilDateTime.getLocalDateTimeTimeStamp(LocalDate.now().minusDays(30).atTime(LocalTime.MIN));
- // List<Map> list= (List<Map>) ydClient.queryData(YDParam.builder().formUuid("FORM-9261FABA687B4D1D917617F53B4A5DB7UN16").searchCondition(JSONObject.toJSONString(
- // Arrays.asList(
- // new YDSearch("radioField_md6q7oxa","私","公私海", YDSearch.Type.TEXT_FIELD,YDSearch.Operator.EQ),
- // new YDSearch("radioField_md6q7ox8","新","新老客户", YDSearch.Type.TEXT_FIELD,YDSearch.Operator.EQ),
- // new YDSearch("dateField_l3tw3kfn",endTime,"截至跟进时间",YDSearch.Type.DATE_FIELD,YDSearch.Operator.LT)
- // )
- // )).build(), YDConf.FORM_QUERY.retrieve_list).getData();
- // for(Map map:list){
- // log.info("客户:{}释放为公海", UtilMap.getMap(map,"formData").get("textField_lqanqe6j"));
- // ydClient.operateData(YDParam.builder().formInstId(UtilMap.getString(map,"formInstanceId")).updateFormDataJson(
- // JSONObject.toJSONString(UtilMap.map("radioField_md6q7oxa, employeeField_lqanqe6n, employeeField_lqx3act6","公","[]","[]"))
- // ).build(), YDConf.FORM_OPERATION.update);
- // }
- }catch (Exception e){
- log.error("私海客户流出定时任务异常",e);
- }
- log.info("私海客户流出定时任务执行完成");
- }
- /**
- */
- // @Scheduled(cron = "0 30 6 * * ?")
- public void syncTaskCount() {
- log.info("同步任务状态数量");
- try {
- List<Map> list= (List<Map>) ydClient.queryData(YDParam.builder().formUuid("FORM-D8DFF1F8C07A4869BCC850090F3A3B4EE0AV").searchCondition(JSONObject.toJSONString(
- Arrays.asList(
- new YDSearch("selectField_me2hylde",Arrays.asList("立项阶段","样品阶段","生产阶段","包装阶段","出货阶段","收尾阶段"),"项目状态", YDSearch.Type.TEXT_FIELD,YDSearch.Operator.CONTAINS)
- )
- )).build(), YDConf.FORM_QUERY.retrieve_list).getData();
- for(Map map:list){
- Map formData =UtilMap.getMap(map,"formData");
- log.info("任务数量同步:项目:{} ", formData.get("textField_me87x1nz"));
- String pid=UtilMap.getString(formData,"textField_me2hyldc");
- String tbPid=UtilMap.getString(formData,"textField_mecmytpo");
- if(PublicUtil.isNull(tbPid)){
- continue;
- }
- String projectState=UtilMap.getString(formData,"selectField_me2hylde");
- String fid=UtilMap.getString(map,"formInstanceId");
- List<UtilMap.assDetail> assDetails=UtilMap.getAssFieldValue(formData,"associationFormField_me9n24kk_id");
- if(assDetails!=null&&assDetails.size()>0){
- String tastTypeName= assDetails.get(0).title; // 项目任务类型
- eastarTbService.taskCount(pid,tbPid,fid,tastTypeName,projectState);
- }
- }
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- // @Scheduled(cron = "0 30 1 1 1 ?")
- public void syncCust(){
- log.info("同步客户上一年等级");
- List<Map> list= ydService.queryAllFormData(YDParam.builder().formUuid("FORM-9261FABA687B4D1D917617F53B4A5DB7UN16").build());
- for(Map map:list){
- Map formData =UtilMap.getMap(map,"formData");
- log.info("任务数量同步:客户:{},等级:{} ", formData.get("textField_lqanqe6j"),formData.get("selectField_mcsoft5i"));
- Map updateMap=new HashMap();
- updateMap.put("selectField_mfkshib3",formData.get("selectField_mcsoft5i"));
- updateMap.put("numberField_mfkshib2",0);
- updateMap.put("selectField_mcsoft5i","E");
- String fid=UtilMap.getString(map,"formInstanceId");
- ydClient.operateData(YDParam.builder().formInstId(fid).updateFormDataJson(JSONObject.toJSONString(updateMap)).useLatestFormSchemaVersion(true).build(), YDConf.FORM_OPERATION.update);
- Map recod=new HashMap();
- recod.put("textField_lqanqe6j",formData.get("textField_lqanqe6j"));
- recod.put("textField_mfkrv5w6",formData.get("serialNumberField_lqanqe6i"));
- recod.put("selectField_mcsoft5i",formData.get("selectField_mcsoft5i"));
- recod.put("dateField_mfkrv5w8",UtilDateTime.getLocalDateTimeTimeStamp((LocalDate.of(LocalDate.now().getYear()-1,1,1).atStartOfDay())));
- recod.put("associationFormField_mfkrv5w7",Arrays.asList(UtilMap.map("formType, formUuid, instanceId, title, appType","receipt","FORM-9261FABA687B4D1D917617F53B4A5DB7UN16",fid,formData.get("textField_lqanqe6j"),"APP_GM7D1NW6XCD0ZJBPVH51")));
- ydClient.operateData(YDParam.builder().formDataJson(JSONObject.toJSONString(recod)).formUuid("FORM-51F53E33609C4E20A918D5C55E237E95Z2GS").build(), YDConf.FORM_OPERATION.create);
- }
- }
- /**
- * 项目总表表2未填写完成统计
- * 每日9点55
- */
- @Scheduled(cron = "0 55 9 * * ?")
- public void table2IncompleteStatistics() {
- log.info("定时更新项目台账填写情况表");
- /*
- 查询【表2】项目台账所有项目状态为Pending、Done、Closed的记录
- */
- JSONArray table2RecordsFilter = new JSONArray();
- String baseId = "G53mjyd80pEr5grBfpjmMX6586zbX04v";
- String sheetIdOrName = "pl6sz00";
- Map<String,Object> param = new HashMap<>();
- param.put("operatorId","aj1wcWqKLXITiPDwbMIjUbAiEiE"); //操作人(Jason)的unionId
- AITableParam aiTableParam = new AITableParam();
- aiTableParam.setMaxResults(100);
- JSONObject filter = new JSONObject();
- filter.put("combination","and");
- JSONArray conditions = new JSONArray();
- JSONObject condition = new JSONObject();
- condition.put("field","项目状态");
- condition.put("operator","notContain");
- condition.put("value",new String[]{"Pending","Done","Closed"});
- conditions.add(condition);
- filter.put("conditions",conditions);
- aiTableParam.setFilter(filter);
- AITableResult aiTableResult;
- boolean hasMore = true;
- int pageNo = 1;
- JSONArray records;
- log.info("查询【表2】项目台账所有项目状态为Pending、Done、Closed的记录");
- 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++){
- table2RecordsFilter.add(records.getJSONObject(i));
- }
- log.info("是否有更多数据:"+hasMore);
- pageNo++;
- aiTableParam.setNextToken(aiTableResult.getNextToken());
- }
- /*
- 统计计划开始时间和计划结束时间未填的
- */
- List<String> checkEmptyRecordList = new ArrayList<>();
- String[] checkEmptyFieldArr = new String[]{
- "计划:开始白样【交付进度】",
- "计划:结束白样【交付进度】",
- "计划:开始制版【交付进度】",
- "计划:结束制版【交付进度】",
- "计划:开始产前样【交付进度】",
- "计划:结束产前样【交付进度】",
- "计划:开始大货【交付进度】",
- "计划:结束大货【交付进度】",
- "计划:开始包装【交付进度】",
- "计划:结束包装【交付进度】",
- "计划:开始出货【交付进度】",
- "计划:结束出货时间【交付进度】"
- };
- JSONObject table2Record;
- JSONObject fields;
- String unionIdPM;
- String checkFailureStr;
- JSONArray pmJSONArray;
- // System.out.println("表2PM:");
- for(int i=0;i<table2RecordsFilter.size();i++){
- table2Record = table2RecordsFilter.getJSONObject(i);
- fields = table2Record.getJSONObject("fields");
- pmJSONArray = fields.getJSONArray("PM(Owner)【人事】");
- if(pmJSONArray == null){
- continue;
- }
- unionIdPM = pmJSONArray.getJSONObject(0).getString("unionId");
- for(String checkEmptyField : checkEmptyFieldArr){
- if(StringUtils.isEmpty(fields.getString(checkEmptyField))){
- checkFailureStr = unionIdPM + "_" + checkEmptyField+"未填写";
- // System.out.println(checkFailureStr);
- checkEmptyRecordList.add(checkFailureStr);
- }
- }
- }
- // System.out.println("checkEmptyRecordList:");
- // System.out.println(JSON.toJSONString(checkEmptyRecordList));
- /*
- 查询【参数表-人事】部门为销售部、客服部的记录
- */
- JSONArray empRecordsFilter = new JSONArray();
- sheetIdOrName = "rtOAveM";
- aiTableParam = new AITableParam();
- aiTableParam.setMaxResults(100);
- filter = new JSONObject();
- filter.put("combination","and");
- conditions = new JSONArray();
- condition = new JSONObject();
- condition.put("field","部门");
- condition.put("operator","contain");
- condition.put("value",new String[]{"销售部","客服部"});
- conditions.add(condition);
- filter.put("conditions",conditions);
- aiTableParam.setFilter(filter);
- log.info("查询【参数表-人事】部门为销售部、客服部的记录");
- aiTableResult = mdTableClient.queryMultiRecords(baseId,sheetIdOrName,param,aiTableParam);
- hasMore = aiTableResult.getHasMore();
- log.info("是否有更多数据:"+hasMore);
- log.info("销售和PM人员记录数="+aiTableResult.getRecords().size());
- records = aiTableResult.getRecords();
- for(int i=0;i<records.size();i++){
- empRecordsFilter.add(records.getJSONObject(i));
- }
- JSONObject empRecord;
- String deptId;
- String unionIdPMLeader;
- // System.out.println("人员列表:");
- Map<String,Object> unionIdMap = new HashMap<>();
- for(int i=0; i<empRecordsFilter.size(); i++){
- fields = empRecordsFilter.getJSONObject(i).getJSONObject("fields");
- unionIdPM = fields.getJSONArray("人员").getJSONObject(0).getString("unionId");
- deptId = fields.getJSONObject("部门").getString("id");
- if(fields.getJSONArray("销售组长") != null){
- unionIdPMLeader = fields.getJSONArray("销售组长").getJSONObject(0).getString("unionId");
- }else{
- unionIdPMLeader = "U2puyufsTtwvfTXmWQoG0wiEiE";
- }
- // System.out.println("unionIdPM="+unionIdPM+",deptId="+deptId+",unionIdPMLeader="+unionIdPMLeader);
- empRecord = new JSONObject();
- empRecord.put("deptId",deptId);
- empRecord.put("unionIdPMLeader",unionIdPMLeader);
- unionIdMap.put(unionIdPM,empRecord);
- }
- /*
- 新增项目台账填写情况表记录
- */
- JSONObject insertData = new JSONObject();
- JSONArray insertRecords = new JSONArray();
- JSONObject insertRecord;
- JSONArray pmLeaderJSONArray;
- JSONObject unionIdJSONObject;
- for(String str : checkEmptyRecordList){
- unionIdPM = str.split("_")[0];
- empRecord = (JSONObject) unionIdMap.get(unionIdPM);
- fields = new JSONObject();
- fields.put("日期", DateUtil.today());
- unionIdJSONObject = new JSONObject();
- unionIdJSONObject.put("unionId",unionIdPM);
- pmJSONArray = new JSONArray();
- pmJSONArray.add(unionIdJSONObject);
- fields.put("负责人",pmJSONArray);
- unionIdJSONObject = new JSONObject();
- unionIdJSONObject.put("unionId",empRecord.getString("unionIdPMLeader"));
- pmLeaderJSONArray = new JSONArray();
- pmLeaderJSONArray.add(unionIdJSONObject);
- fields.put("组长",pmLeaderJSONArray);
- fields.put("填写情况", str.split("_")[1]);
- insertRecord = new JSONObject();
- insertRecord.put("fields",fields);
- // log.info(JSON.toJSONString(insertRecord));
- insertRecords.add(insertRecord);
- }
- String result;
- JSONArray newInsertRecords;
- sheetIdOrName = "OTgxt4G";
- if(insertRecords.size() > 100){
- pageNo = 1;
- while(insertRecords.size() > 100){
- newInsertRecords = new JSONArray();
- records = new JSONArray();
- for (int i = 0; i < insertRecords.size(); i++) {
- if(i<100){
- newInsertRecords.add(insertRecords.getJSONObject(i));
- }else{
- records.add(insertRecords.getJSONObject(i));
- }
- }
- insertData.put("records",newInsertRecords);
- // log.info(JSON.toJSONString(insertData));
- result = mdTableClient.createMultiRecords(baseId,sheetIdOrName,param,insertData);
- log.info("新增项目台账填写情况表记录结果,第"+pageNo+"页:");
- log.info(result);
- insertRecords = records;
- pageNo++;
- }
- }
- if(insertRecords.size() > 0){
- insertData.put("records",insertRecords);
- // log.info(JSON.toJSONString(insertData));
- sheetIdOrName = "OTgxt4G";
- result = mdTableClient.createMultiRecords(baseId,sheetIdOrName,param,insertData);
- log.info("新增项目台账填写情况表记录结果,第"+pageNo+"页:");
- log.info(result);
- }
- }
- }
|