|
|
@@ -0,0 +1,104 @@
|
|
|
+package com.storlead.trade.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.storlead.framework.common.constant.CommonConstant;
|
|
|
+import com.storlead.trade.constant.CustomerAnalysisConstants;
|
|
|
+import com.storlead.trade.entity.CustomerAnalysisResultEntity;
|
|
|
+import com.storlead.trade.entity.CustomerEntity;
|
|
|
+import com.storlead.trade.service.CustomerAnalysisDispatchService;
|
|
|
+import com.storlead.trade.service.CustomerAnalysisResultEntityService;
|
|
|
+import com.storlead.trade.service.CustomerEntityService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CustomerAnalysisDispatchServiceImpl implements CustomerAnalysisDispatchService {
|
|
|
+
|
|
|
+ private static final DateTimeFormatter BATCH_NO_FMT = DateTimeFormatter.BASIC_ISO_DATE;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CustomerEntityService customerEntityService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private CustomerAnalysisResultEntityService customerAnalysisResultEntityService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public int dispatchPendingInvalidCustomers() {
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ String batchNo = today.format(BATCH_NO_FMT);
|
|
|
+ Date todayDate = java.sql.Date.valueOf(today);
|
|
|
+ Date nextAnalysisDate = java.sql.Date.valueOf(today.plusDays(CustomerAnalysisConstants.NEXT_ANALYSIS_INTERVAL_DAYS));
|
|
|
+
|
|
|
+ List<CustomerEntity> candidates = customerEntityService.list(new LambdaQueryWrapper<CustomerEntity>()
|
|
|
+ .eq(CustomerEntity::getIsDelete, CommonConstant.DEL_FLAG_0)
|
|
|
+ .eq(CustomerEntity::getHasGarbageCustomer, CustomerAnalysisConstants.GARBAGE_CUSTOMER_YES)
|
|
|
+ .and(w -> w.isNull(CustomerEntity::getNextAnalysisDate)
|
|
|
+ .or()
|
|
|
+ .le(CustomerEntity::getNextAnalysisDate, todayDate)));
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(candidates)) {
|
|
|
+ log.info("无效客户分析投递:无待投递数据, batchNo={}", batchNo);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ Set<Long> alreadyDispatchedToday = loadDispatchedCustomerIds(batchNo);
|
|
|
+ List<CustomerAnalysisResultEntity> toInsert = new ArrayList<>();
|
|
|
+ List<CustomerEntity> toUpdate = new ArrayList<>();
|
|
|
+
|
|
|
+ for (CustomerEntity customer : candidates) {
|
|
|
+ if (customer.getId() == null || alreadyDispatchedToday.contains(customer.getId())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ CustomerAnalysisResultEntity result = new CustomerAnalysisResultEntity();
|
|
|
+ result.setCustomerId(customer.getId());
|
|
|
+ result.setAnalysisBatchNo(batchNo);
|
|
|
+ result.setAnalysisStatus(CustomerAnalysisConstants.ANALYSIS_STATUS_PENDING);
|
|
|
+ result.setIsDelete(CommonConstant.DEL_FLAG_0);
|
|
|
+ toInsert.add(result);
|
|
|
+
|
|
|
+ CustomerEntity update = new CustomerEntity();
|
|
|
+ update.setId(customer.getId());
|
|
|
+ update.setNextAnalysisDate(nextAnalysisDate);
|
|
|
+ toUpdate.add(update);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(toInsert)) {
|
|
|
+ log.info("无效客户分析投递:候选已全部在本批次投递过, batchNo={}, candidates={}", batchNo, candidates.size());
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ customerAnalysisResultEntityService.saveBatch(toInsert);
|
|
|
+ customerEntityService.updateBatchById(toUpdate);
|
|
|
+ log.info("无效客户分析投递完成: batchNo={}, count={}, nextAnalysisDate={}", batchNo, toInsert.size(), nextAnalysisDate);
|
|
|
+ return toInsert.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Set<Long> loadDispatchedCustomerIds(String batchNo) {
|
|
|
+ List<CustomerAnalysisResultEntity> exists = customerAnalysisResultEntityService.list(
|
|
|
+ new LambdaQueryWrapper<CustomerAnalysisResultEntity>()
|
|
|
+ .select(CustomerAnalysisResultEntity::getCustomerId)
|
|
|
+ .eq(CustomerAnalysisResultEntity::getAnalysisBatchNo, batchNo)
|
|
|
+ .eq(CustomerAnalysisResultEntity::getIsDelete, CommonConstant.DEL_FLAG_0));
|
|
|
+ if (CollectionUtils.isEmpty(exists)) {
|
|
|
+ return new HashSet<>();
|
|
|
+ }
|
|
|
+ return exists.stream()
|
|
|
+ .map(CustomerAnalysisResultEntity::getCustomerId)
|
|
|
+ .filter(id -> id != null)
|
|
|
+ .collect(Collectors.toCollection(HashSet::new));
|
|
|
+ }
|
|
|
+}
|