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 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 ltcCustomerData = yidaService.queryLtcNotFollowedCustomerData(ydFilters); log.info("超过3天未跟进的私海新客户记录数:"+ ltcCustomerData.size()); //排除已建联 Set contactedCustomerCodes = new HashSet<>(); for(Map contactRecord : establishContactData){ String customerCode = UtilMap.getString(contactRecord, "customerCode"); if(StringUtils.isNotEmpty(customerCode)){ contactedCustomerCodes.add(customerCode); } } List 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> 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> 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 list= (List) 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 list= (List) 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 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 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 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 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 unionIdMap = new HashMap<>(); for(int i=0; i 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); } } }