修改买家统计的逻
This commit is contained in:
@@ -562,11 +562,6 @@ public class LbBuyerShoppingServiceImpl
|
||||
result.put("message", "请求体不能为空");
|
||||
return result;
|
||||
}
|
||||
if (request.getBuyerId() == null) {
|
||||
result.put("success", false);
|
||||
result.put("message", "buyerId不能为空");
|
||||
return result;
|
||||
}
|
||||
String statType = request.getStatType() != null ? request.getStatType().trim() : "";
|
||||
if (statType.isEmpty()) {
|
||||
result.put("success", false);
|
||||
@@ -579,54 +574,84 @@ public class LbBuyerShoppingServiceImpl
|
||||
return result;
|
||||
}
|
||||
|
||||
Long buyerId = request.getBuyerId();
|
||||
List<LbBuyerShopping> details = listDetailRowsByBuyerId(buyerId);
|
||||
if (details.isEmpty()) {
|
||||
List<LbBuyerShopping> allDetails = listAllDetailRows();
|
||||
if (allDetails.isEmpty()) {
|
||||
result.put("success", true);
|
||||
result.put("message", "该买家无明细订单");
|
||||
result.put("message", "全表无明细订单");
|
||||
result.put("generated", 0);
|
||||
result.put("sourceCount", 0);
|
||||
result.put("buyerCount", 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
List<LbBuyerShopping> statRows;
|
||||
Map<Long, List<LbBuyerShopping>> byBuyer = new LinkedHashMap<>();
|
||||
int skippedNoBuyerId = 0;
|
||||
for (LbBuyerShopping row : allDetails) {
|
||||
if (row.getBuyerId() == null) {
|
||||
skippedNoBuyerId++;
|
||||
continue;
|
||||
}
|
||||
byBuyer.computeIfAbsent(row.getBuyerId(), k -> new ArrayList<>()).add(row);
|
||||
}
|
||||
if (byBuyer.isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "明细均无 buyer_id,无法按买家汇总");
|
||||
result.put("sourceCount", allDetails.size());
|
||||
result.put("skippedNoBuyerId", skippedNoBuyerId);
|
||||
return result;
|
||||
}
|
||||
|
||||
List<LbBuyerShopping> statRows = new ArrayList<>();
|
||||
int skippedNoBuyTime = 0;
|
||||
int skippedBuyersNoBuyTime = 0;
|
||||
if (DATA_TYPE_DAY_STAT.equals(statType)) {
|
||||
Map<LocalDate, List<LbBuyerShopping>> byDay = new LinkedHashMap<>();
|
||||
for (LbBuyerShopping row : details) {
|
||||
LocalDate day = resolveBuyDate(row.getBuyTime());
|
||||
if (day == null) {
|
||||
skippedNoBuyTime++;
|
||||
for (Map.Entry<Long, List<LbBuyerShopping>> buyerEntry : byBuyer.entrySet()) {
|
||||
Long buyerId = buyerEntry.getKey();
|
||||
Map<LocalDate, List<LbBuyerShopping>> byDay = new LinkedHashMap<>();
|
||||
for (LbBuyerShopping row : buyerEntry.getValue()) {
|
||||
LocalDate day = resolveBuyDate(row.getBuyTime());
|
||||
if (day == null) {
|
||||
skippedNoBuyTime++;
|
||||
continue;
|
||||
}
|
||||
byDay.computeIfAbsent(day, k -> new ArrayList<>()).add(row);
|
||||
}
|
||||
if (byDay.isEmpty()) {
|
||||
skippedBuyersNoBuyTime++;
|
||||
continue;
|
||||
}
|
||||
byDay.computeIfAbsent(day, k -> new ArrayList<>()).add(row);
|
||||
for (Map.Entry<LocalDate, List<LbBuyerShopping>> dayEntry : byDay.entrySet()) {
|
||||
statRows.add(buildDayStatRow(buyerId, dayEntry.getKey(), dayEntry.getValue()));
|
||||
}
|
||||
}
|
||||
if (byDay.isEmpty()) {
|
||||
if (statRows.isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "明细 buy_time 均无法解析为日期,无法按天汇总");
|
||||
result.put("skippedNoBuyTime", skippedNoBuyTime);
|
||||
result.put("skippedBuyersNoBuyTime", skippedBuyersNoBuyTime);
|
||||
return result;
|
||||
}
|
||||
statRows = new ArrayList<>(byDay.size());
|
||||
for (Map.Entry<LocalDate, List<LbBuyerShopping>> entry : byDay.entrySet()) {
|
||||
statRows.add(buildDayStatRow(buyerId, entry.getKey(), entry.getValue()));
|
||||
}
|
||||
} else {
|
||||
statRows = List.of(buildSumStatRow(buyerId, details));
|
||||
for (Map.Entry<Long, List<LbBuyerShopping>> buyerEntry : byBuyer.entrySet()) {
|
||||
statRows.add(buildSumStatRow(buyerEntry.getKey(), buyerEntry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
int upserted = upsertStatRows(statRows);
|
||||
result.put("success", upserted >= 0);
|
||||
result.put("message", upserted >= 0 ? "统计完成" : "保存失败");
|
||||
result.put("generated", Math.max(upserted, 0));
|
||||
result.put("sourceCount", details.size());
|
||||
result.put("sourceCount", allDetails.size());
|
||||
result.put("buyerCount", byBuyer.size());
|
||||
result.put("skippedNoBuyerId", skippedNoBuyerId);
|
||||
result.put("skippedNoBuyTime", skippedNoBuyTime);
|
||||
result.put("skippedBuyersNoBuyTime", skippedBuyersNoBuyTime);
|
||||
if (upserted >= 0) {
|
||||
result.put("data", statRows);
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error("买方购物统计异常 buyerId={}", request != null ? request.getBuyerId() : null, e);
|
||||
log.error("买方购物统计异常 statType={}", request != null ? request.getStatType() : null, e);
|
||||
result.put("success", false);
|
||||
result.put("message", "统计异常:" + e.getMessage());
|
||||
result.put("generated", 0);
|
||||
@@ -634,10 +659,10 @@ public class LbBuyerShoppingServiceImpl
|
||||
}
|
||||
}
|
||||
|
||||
private List<LbBuyerShopping> listDetailRowsByBuyerId(Long buyerId) {
|
||||
private List<LbBuyerShopping> listAllDetailRows() {
|
||||
LambdaQueryWrapper<LbBuyerShopping> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(LbBuyerShopping::getBuyerId, buyerId)
|
||||
.eq(LbBuyerShopping::getDataType, DATA_TYPE_DETAIL)
|
||||
queryWrapper.eq(LbBuyerShopping::getDataType, DATA_TYPE_DETAIL)
|
||||
.orderByAsc(LbBuyerShopping::getBuyerId)
|
||||
.orderByAsc(LbBuyerShopping::getBuyTime)
|
||||
.orderByAsc(LbBuyerShopping::getId);
|
||||
return this.list(queryWrapper);
|
||||
|
||||
Reference in New Issue
Block a user