调度补齐卖家姓名和电话
This commit is contained in:
@@ -5,10 +5,14 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.rj.dto.hxr.HxrOrderRow;
|
||||
import com.rj.dto.hxr.HxrOrderSelectResponse;
|
||||
import com.rj.entity.LbDepartmentUser;
|
||||
import com.rj.entity.LbOrderRow;
|
||||
import com.rj.mapper.LbOrderRowMapper;
|
||||
import com.rj.service.HxrAdminOrderSelectService;
|
||||
import com.rj.service.ILbDepartmentUserService;
|
||||
import com.rj.service.ILbOrderRowService;
|
||||
import com.rj.tenant.TenantContextHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -20,11 +24,14 @@ import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrderRow> implements ILbOrderRowService {
|
||||
|
||||
@@ -47,6 +54,9 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
@Autowired
|
||||
private HxrAdminOrderSelectService hxrAdminOrderSelectService;
|
||||
|
||||
@Autowired
|
||||
private ILbDepartmentUserService lbDepartmentUserService;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> add(LbOrderRow entity) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
@@ -224,6 +234,13 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Map<String, Object> syncFromHxrAdmin(
|
||||
String buyTimeStart, String buyTimeEnd, String tenantId, Integer isResell) {
|
||||
return syncFromHxrAdmin(buyTimeStart, buyTimeEnd, tenantId, isResell, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Map<String, Object> syncFromHxrAdmin(
|
||||
String buyTimeStart, String buyTimeEnd, String tenantId, Integer isResell, Integer hxrOrderStatus) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (tenantId == null || tenantId.trim().isEmpty()) {
|
||||
@@ -234,13 +251,15 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
|
||||
|
||||
String tid = tenantId.trim();
|
||||
String previousTenantId = TenantContextHolder.getTenantId();
|
||||
TenantContextHolder.setTenantId(tid);
|
||||
try {
|
||||
HxrOrderSelectResponse firstBody = null;
|
||||
int page = 1;
|
||||
int totalSynced = 0;
|
||||
while (true) {
|
||||
Optional<HxrOrderSelectResponse> opt =
|
||||
hxrAdminOrderSelectService.fetchOrderSelect(
|
||||
buyTimeStart, buyTimeEnd, isResell, page);
|
||||
fetchHxrOrderPage(buyTimeStart, buyTimeEnd, isResell, hxrOrderStatus, page);
|
||||
if (opt.isEmpty()) {
|
||||
if (page == 1) {
|
||||
result.put("success", false);
|
||||
@@ -273,9 +292,9 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
for (HxrOrderRow row : rows) {
|
||||
entities.add(toLbOrderRow(row, tid));
|
||||
}
|
||||
|
||||
boolean ok = this.saveOrUpdateBatch(entities);
|
||||
if (!ok) {
|
||||
fillSellerInfoFromDepartmentUsers(entities, tid);
|
||||
int upserted = upsertBatch(entities);
|
||||
if (upserted < 0) {
|
||||
result.put("success", false);
|
||||
result.put("message", "第 " + page + " 页保存失败,已成功同步前 "
|
||||
+ (page - 1) + " 页共 " + totalSynced + " 条");
|
||||
@@ -285,7 +304,7 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
result.put("pages", page - 1);
|
||||
return result;
|
||||
}
|
||||
totalSynced += entities.size();
|
||||
totalSynced += upserted;
|
||||
|
||||
if (rows.size() < HXR_ORDER_PAGE_SIZE ) {
|
||||
break;
|
||||
@@ -309,14 +328,40 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
result.put("allMoney", firstBody.allMoney());
|
||||
result.put("pages", page);
|
||||
return result;
|
||||
} finally {
|
||||
if (previousTenantId != null) {
|
||||
TenantContextHolder.setTenantId(previousTenantId);
|
||||
} else {
|
||||
TenantContextHolder.clear();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "同步异常:" + e.getMessage());
|
||||
result.put("synced", 0);
|
||||
log.error("syncFromHxrAdmin failed", e);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
private Optional<HxrOrderSelectResponse> fetchHxrOrderPage(
|
||||
String buyTimeStart,
|
||||
String buyTimeEnd,
|
||||
Integer isResell,
|
||||
Integer hxrOrderStatus,
|
||||
int page)
|
||||
throws Exception {
|
||||
if (hxrOrderStatus != null) {
|
||||
if (hxrOrderStatus == 0) {
|
||||
return hxrAdminOrderSelectService.fetchUnpaidOrderSelect(buyTimeStart, buyTimeEnd, page);
|
||||
}
|
||||
if (hxrOrderStatus == 1) {
|
||||
return hxrAdminOrderSelectService.fetchPaidOrderSelect(buyTimeStart, buyTimeEnd, page);
|
||||
}
|
||||
}
|
||||
return hxrAdminOrderSelectService.fetchOrderSelect(buyTimeStart, buyTimeEnd, isResell, page);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Map<String, Object> generateDailySumData(
|
||||
@@ -373,10 +418,11 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
sumRows.add(buildDailySumRow(tid, entry.getKey(), entry.getValue(), nowStr));
|
||||
}
|
||||
|
||||
boolean ok = this.saveOrUpdateBatch(sumRows);
|
||||
int upserted = upsertBatch(sumRows);
|
||||
boolean ok = upserted >= 0;
|
||||
result.put("success", ok);
|
||||
result.put("message", ok ? "按日汇总完成" : "保存失败");
|
||||
result.put("generated", ok ? sumRows.size() : 0);
|
||||
result.put("generated", ok ? upserted : 0);
|
||||
result.put("sourceCount", details.size());
|
||||
result.put("skippedNoBuyTime", skippedNoBuyTime);
|
||||
if (ok) {
|
||||
@@ -481,6 +527,78 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按外部订单 id(主键)插入或更新;同批重复 id 保留最后一条。
|
||||
*
|
||||
* @return 实际 upsert 条数;无有效 id 时返回 0;失败返回 -1
|
||||
*/
|
||||
private int upsertBatch(List<LbOrderRow> entities) {
|
||||
if (entities == null || entities.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
Map<Long, LbOrderRow> deduped = new LinkedHashMap<>();
|
||||
for (LbOrderRow entity : entities) {
|
||||
if (entity.getId() != null) {
|
||||
deduped.put(entity.getId(), entity);
|
||||
}
|
||||
}
|
||||
if (deduped.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
List<LbOrderRow> rows = new ArrayList<>(deduped.values());
|
||||
try {
|
||||
baseMapper.upsertBatch(rows);
|
||||
return rows.size();
|
||||
} catch (Exception e) {
|
||||
log.error("lb_order_row upsertBatch failed, size={}", rows.size(), e);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 seller_id 关联 lb_department_user.user_id,用部门用户的 name、phone 填充卖方姓名与电话。
|
||||
*/
|
||||
private void fillSellerInfoFromDepartmentUsers(List<LbOrderRow> entities, String tenantId) {
|
||||
if (entities == null || entities.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Set<Long> sellerIds = new HashSet<>();
|
||||
for (LbOrderRow entity : entities) {
|
||||
if (entity.getSellerId() != null) {
|
||||
sellerIds.add(entity.getSellerId());
|
||||
}
|
||||
}
|
||||
if (sellerIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<String> userIdStrs = new ArrayList<>(sellerIds.size());
|
||||
for (Long sellerId : sellerIds) {
|
||||
userIdStrs.add(String.valueOf(sellerId));
|
||||
}
|
||||
LambdaQueryWrapper<LbDepartmentUser> w = new LambdaQueryWrapper<>();
|
||||
w.eq(LbDepartmentUser::getTenantId, tenantId)
|
||||
.in(LbDepartmentUser::getUserId, userIdStrs)
|
||||
.orderByDesc(LbDepartmentUser::getUpdateTime)
|
||||
.orderByDesc(LbDepartmentUser::getCreateTime);
|
||||
List<LbDepartmentUser> users = lbDepartmentUserService.list(w);
|
||||
Map<String, LbDepartmentUser> byUserId = new LinkedHashMap<>();
|
||||
for (LbDepartmentUser user : users) {
|
||||
if (user.getUserId() != null && !byUserId.containsKey(user.getUserId())) {
|
||||
byUserId.put(user.getUserId(), user);
|
||||
}
|
||||
}
|
||||
for (LbOrderRow entity : entities) {
|
||||
if (entity.getSellerId() == null) {
|
||||
continue;
|
||||
}
|
||||
LbDepartmentUser user = byUserId.get(String.valueOf(entity.getSellerId()));
|
||||
if (user != null) {
|
||||
entity.setSellerName(user.getName());
|
||||
entity.setSellerPhone(user.getPhone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static LbOrderRow toLbOrderRow(HxrOrderRow row, String tenantId) {
|
||||
LbOrderRow e = new LbOrderRow();
|
||||
e.setId(row.id());
|
||||
@@ -491,6 +609,8 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
|
||||
e.setTodayOrderCount(0);
|
||||
e.setTodayUnresellCount(0);
|
||||
e.setSellerId(row.sellerId());
|
||||
e.setSellerName(row.sellerName());
|
||||
e.setSellerPhone(row.sellerPhone());
|
||||
e.setBuyerId(row.buyerId());
|
||||
e.setOrderSn(row.orderSn());
|
||||
e.setTotalMoney(parseMoney(row.totalMoney()));
|
||||
|
||||
@@ -600,7 +600,7 @@ public class LbPurchaseApplyServiceImpl
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
LocalDateTime vipTime = HxrAdminUserService.computeVipExpireTime(row.getApplyDate(), 3);
|
||||
LocalDateTime vipTime = HxrAdminUserService.computeVipExpireTime(row.getApplyDate(), 2);
|
||||
String vipTimeStr = HxrAdminUserService.formatVipTime(vipTime);
|
||||
one.put("viptime", vipTimeStr);
|
||||
|
||||
@@ -940,7 +940,6 @@ public class LbPurchaseApplyServiceImpl
|
||||
.orderByAsc(LbPurchaseApply::getApplyDate)
|
||||
.orderByAsc(LbPurchaseApply::getId);
|
||||
List<LbPurchaseApply> rows = this.list(queryWrapper);
|
||||
Map<String, Integer> maxOrderByColleaguePhone = loadColleagueMaxOrderCache(rows);
|
||||
|
||||
LinkedHashMap<LocalDate, List<LbPurchaseApply>> grouped = new LinkedHashMap<>();
|
||||
for (LbPurchaseApply row : rows) {
|
||||
@@ -983,7 +982,7 @@ public class LbPurchaseApplyServiceImpl
|
||||
createExportCell(row, 2, nullToEmpty(item.getApplyUser()), dataStyle);
|
||||
createExportCell(row, 3, nullToEmpty(item.getApplyPhone()), dataStyle);
|
||||
createExportCell(row, 4, formatPrivilegeRange(item), dataStyle);
|
||||
createExportCell(row, 5, formatRecommender(item, maxOrderByColleaguePhone), dataStyle);
|
||||
createExportCell(row, 5, formatRecommender(item), dataStyle);
|
||||
createExportCell(row, 6, nullToEmpty(item.getColleaguePhone()), dataStyle);
|
||||
createExportCell(row, 7, nullToEmpty(item.getTeamLeader()), dataStyle);
|
||||
createExportCell(row, 8, formatActivationDate(item.getApplyDate()), dataStyle);
|
||||
@@ -1026,25 +1025,6 @@ public class LbPurchaseApplyServiceImpl
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Integer> loadColleagueMaxOrderCache(List<LbPurchaseApply> rows) {
|
||||
Map<String, Integer> cache = new HashMap<>();
|
||||
Set<String> phones = new HashSet<>();
|
||||
for (LbPurchaseApply row : rows) {
|
||||
if (row.getColleaguePhone() != null && !row.getColleaguePhone().trim().isEmpty()) {
|
||||
phones.add(row.getColleaguePhone().trim());
|
||||
}
|
||||
}
|
||||
for (String phone : phones) {
|
||||
try {
|
||||
Optional<HxrUserRow> userOpt = hxrAdminUserService.fetchFirstUserByMobile(phone);
|
||||
userOpt.map(HxrUserRow::maxOrder).ifPresent(maxOrder -> cache.put(phone, maxOrder));
|
||||
} catch (Exception ex) {
|
||||
log.debug("导出时查询推荐人 max_order 失败,phone={}", phone, ex);
|
||||
}
|
||||
}
|
||||
return cache;
|
||||
}
|
||||
|
||||
private static String formatDatePeopleLabel(LocalDate applyDate, int count) {
|
||||
if (applyDate == null || applyDate.equals(LocalDate.MIN)) {
|
||||
return "未知日期招商" + count + "人";
|
||||
@@ -1062,20 +1042,12 @@ public class LbPurchaseApplyServiceImpl
|
||||
return start.format(PRIVILEGE_RANGE_FMT) + " 到 " + endDate.format(PRIVILEGE_RANGE_FMT);
|
||||
}
|
||||
|
||||
private static String formatRecommender(LbPurchaseApply item, Map<String, Integer> maxOrderByPhone) {
|
||||
private static String formatRecommender(LbPurchaseApply item) {
|
||||
String name = item.getColleagueName();
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
String trimmedName = name.trim();
|
||||
String phone = item.getColleaguePhone() == null ? "" : item.getColleaguePhone().trim();
|
||||
if (!phone.isEmpty()) {
|
||||
Integer maxOrder = maxOrderByPhone.get(phone);
|
||||
if (maxOrder != null) {
|
||||
return trimmedName + " " + maxOrder;
|
||||
}
|
||||
}
|
||||
return trimmedName;
|
||||
return name.trim();
|
||||
}
|
||||
|
||||
private static String formatActivationDate(LocalDate applyDate) {
|
||||
|
||||
Reference in New Issue
Block a user