|
|
@@ -1,6 +1,7 @@
|
|
|
package com.goafanti.report.service;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
@@ -15,6 +16,7 @@ import org.springframework.ui.ModelMap;
|
|
|
|
|
|
import com.goafanti.common.constant.AFTConstants;
|
|
|
import com.goafanti.common.dao.DailySalesReportMapper;
|
|
|
+import com.goafanti.common.error.BusinessException;
|
|
|
import com.goafanti.core.mybatis.BaseMybatisDao;
|
|
|
import com.goafanti.report.bo.DepartmentSalesReportBO;
|
|
|
import com.goafanti.report.bo.PersonalSalesReportBO;
|
|
|
@@ -136,18 +138,47 @@ public class SalesReportServiceImpl extends BaseMybatisDao<DailySalesReportMappe
|
|
|
|
|
|
for (DepartmentSalesReportBO dsr : list) {
|
|
|
if (StringUtils.isNotBlank(dsr.getSuperId()) && !StringUtils.equals(dsr.getId(), dsr.getSuperId())) {
|
|
|
- addToParent(map, dsr.getValues(), dsr.getSuperId(), 0);
|
|
|
+ dsr.setLevel(calcLevel(map, dsr.getSuperId(), 0));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ list.sort(new Comparator<DepartmentSalesReportBO>() {
|
|
|
+ @Override
|
|
|
+ public int compare(DepartmentSalesReportBO o1, DepartmentSalesReportBO o2) {
|
|
|
+ // 排序确保父节点在前,不重复计算父级数据
|
|
|
+ return o1.getLevel() - o2.getLevel();
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ for (DepartmentSalesReportBO dsr : list) {
|
|
|
+ if (StringUtils.isNotBlank(dsr.getSuperId()) && !StringUtils.equals(dsr.getId(), dsr.getSuperId())) {
|
|
|
+ addToParent(map, dsr.getValues(), dsr.getMemberCount(), dsr.getSuperId());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
+ private int calcLevel(Map<String, DepartmentSalesReportBO> map, String parentId, int level) {
|
|
|
+ DepartmentSalesReportBO dsr = map.get(parentId);
|
|
|
+ if (dsr != null) {
|
|
|
+ level++;
|
|
|
+ if (level > 5) {
|
|
|
+ // 业务只能有3层,代码定义强行5层跳出,超过5层可能出现上下级循环嵌套的错误数据,导致递归死循环
|
|
|
+ throw new BusinessException("部门数据层级错误,请检查数据: " + parentId);
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotBlank(dsr.getSuperId()) && !StringUtils.equals(dsr.getId(), dsr.getSuperId())) {
|
|
|
+ return calcLevel(map, dsr.getSuperId(), level);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return level;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * 递归给所有上级增加销售统计, 业务规定最多3级上级,递归设定超出5级递归直接退出
|
|
|
+ * 递归给所有上级增加销售统计
|
|
|
*/
|
|
|
- private void addToParent(Map<String, DepartmentSalesReportBO> map, Map<String, SalesValues> values, String parentId,
|
|
|
- int level) {
|
|
|
+ private void addToParent(Map<String, DepartmentSalesReportBO> map, Map<String, SalesValues> values, int memberCount,
|
|
|
+ String parentId) {
|
|
|
DepartmentSalesReportBO dsr = map.get(parentId);
|
|
|
if (dsr != null) {
|
|
|
Map<String, SalesValues> pValues = dsr.getValues();
|
|
|
@@ -157,9 +188,9 @@ public class SalesReportServiceImpl extends BaseMybatisDao<DailySalesReportMappe
|
|
|
psv.setOrderCount(psv.getOrderCount() + sv.getOrderCount());
|
|
|
psv.setOrderAmount(psv.getOrderAmount().add(sv.getOrderAmount()));
|
|
|
}
|
|
|
- if (StringUtils.isNotBlank(dsr.getSuperId()) && !StringUtils.equals(dsr.getId(), dsr.getSuperId())
|
|
|
- && level < 5) {
|
|
|
- addToParent(map, values, dsr.getSuperId(), ++level);
|
|
|
+ dsr.setMemberCount(dsr.getMemberCount() + memberCount);
|
|
|
+ if (StringUtils.isNotBlank(dsr.getSuperId()) && !StringUtils.equals(dsr.getId(), dsr.getSuperId())) {
|
|
|
+ addToParent(map, values, memberCount, dsr.getSuperId());
|
|
|
}
|
|
|
}
|
|
|
}
|