|
|
@ -91,6 +91,7 @@ public class TbsCostApplyServiceImpl extends ServiceImpl<TbsCostApplyMapper,TbsC |
|
|
|
private TbsScheduleItemBudgetMapper tbsScheduleItemBudgetMapper; |
|
|
|
private final ErpDispatchDataMapper dispatchDataMapper; |
|
|
|
private BirActivityCenterGoodsService birActivityCenterGoodsService; |
|
|
|
private TbsBudgetMapper tbsBudgetMapper; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@ -423,60 +424,114 @@ public class TbsCostApplyServiceImpl extends ServiceImpl<TbsCostApplyMapper,TbsC |
|
|
|
subList.add(new TbsCostSubItem.Subject(subjectName)); |
|
|
|
} |
|
|
|
|
|
|
|
//目标费率和成本中心统计
|
|
|
|
List<TbsBudgetCostItem> tbsBudgetCostItems = result.getBudgetMatchList(); |
|
|
|
List<Long> scheduleItemBudgetIds = tbsBudgetCostItems.stream().map(a->a.getScheduleItemBudgetId()).distinct().collect(Collectors.toList()); |
|
|
|
List<TbsScheduleItemBudget> tbsScheduleItemBudgets = tbsScheduleItemBudgetMapper.selectBatchIds(scheduleItemBudgetIds); |
|
|
|
|
|
|
|
//查询发货金额,并按月份分割
|
|
|
|
int year = LocalDate.now().getYear(); |
|
|
|
int month = LocalDate.now().getMonthValue(); |
|
|
|
month = month>1?month:1; |
|
|
|
int startMonthNum = year*100 + 1; |
|
|
|
int endMonthNum = year*100 + month; |
|
|
|
tbsActivityCenters.stream().collect(Collectors.groupingBy(TbsActivityCenter::getCenterType)); |
|
|
|
BirRoiRateService roiRateService = SpringUtils.getBean(BirRoiRateService.class); |
|
|
|
BirRoiCostDTO costDTO = new BirRoiCostDTO(); |
|
|
|
costDTO.setStartMonthNum(startMonthNum); |
|
|
|
costDTO.setEndMonthNum(endMonthNum); |
|
|
|
/*成本中心维度*/ |
|
|
|
//发货单
|
|
|
|
Map<String,List<ErpDispatchSumVo>> supplierCodeMapWithCenterType = new HashMap<>(); |
|
|
|
Map<String,List<TbsActivityCenter>> centerMapList = tbsActivityCenters.stream().collect(Collectors.groupingBy(TbsActivityCenter::getCenterType)); |
|
|
|
for (String centerType : centerMapList.keySet()) { |
|
|
|
List<String> centerIds =centerMapList.get(centerType).stream().map(TbsActivityCenter::getCenterId).collect(Collectors.toList()); |
|
|
|
List<String> supplierCodeList = roiRateService.getSupplierCodesByCenter(costDTO,centerType,centerIds); |
|
|
|
//发货单
|
|
|
|
List<ErpDispatchSumVo> dispatchSumVos = null; |
|
|
|
if(supplierCodeList!=null&&supplierCodeList.size()>0){ |
|
|
|
dispatchSumVos = dispatchDataMapper.querySumCost(startMonthNum,endMonthNum,supplierCodeList); |
|
|
|
supplierCodeMapWithCenterType.put(centerType,dispatchSumVos); |
|
|
|
}else { |
|
|
|
//防止后面获取空指针
|
|
|
|
supplierCodeMapWithCenterType.put(centerType,new ArrayList<>()); |
|
|
|
|
|
|
|
//获取BIR表的数据,用于计算实际费用率
|
|
|
|
LambdaQueryWrapper<BirActivityCenterGoods> birLwq = new LambdaQueryWrapper<>(); |
|
|
|
birLwq.in(BirActivityCenterGoods::getCenterId,tbsActivityCenters.stream().map(a->a.getCenterId()).collect(Collectors.toList())); |
|
|
|
birLwq.ge(BirActivityCenterGoods::getKeyNum,startMonthNum); |
|
|
|
birLwq.le(BirActivityCenterGoods::getKeyNum,endMonthNum); |
|
|
|
List<BirActivityCenterGoods> birActivityCenterGoodsList = birActivityCenterGoodsService.list(birLwq); |
|
|
|
Map<String,List<BirActivityCenterGoods>> birCenterCostMap = birActivityCenterGoodsList.stream().collect(Collectors.groupingBy(a->a.getCenterType() + "-"+a.getCenterId())); |
|
|
|
//成本中心去重
|
|
|
|
Map<String,List<TbsActivityCenter>> centerMapList = tbsActivityCenters.stream().collect(Collectors.groupingBy(a->a.getCenterType()+"-"+a.getCenterId())); |
|
|
|
List<TbsActivityCenter> centerList = centerMapList.values().stream().map(a->a.get(0)).collect(Collectors.toList()); |
|
|
|
|
|
|
|
for(TbsActivityCenter center : centerList){ |
|
|
|
String centerType = center.getCenterType(); |
|
|
|
String centerId = center.getCenterId(); |
|
|
|
String centerKey = centerType + "-" + centerId; |
|
|
|
TbsCostSubItem.CostCenter costCenter = new TbsCostSubItem.CostCenter(center.getCenterName(),centerType+"-"+centerId); |
|
|
|
/* --------------- 全年目标费用率YTD 目标预计发货,目标预算 --------------------------- */ |
|
|
|
//通过成本中心TYPE和ID取预算
|
|
|
|
LambdaQueryWrapper<TbsBudget> budgetLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
budgetLambdaQueryWrapper.eq(TbsBudget::getCenterId,centerId); |
|
|
|
budgetLambdaQueryWrapper.eq(TbsBudget::getCenterType,centerType); |
|
|
|
List<TbsBudget> tbsBudgetList = tbsBudgetMapper.selectList(budgetLambdaQueryWrapper); |
|
|
|
//通过预算ID取预算的发货和金额
|
|
|
|
LambdaQueryWrapper<TbsScheduleItemBudget> scheduleItemBudgetLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
|
|
|
scheduleItemBudgetLambdaQueryWrapper.in(TbsScheduleItemBudget::getBudgetId,tbsBudgetList.stream().map(a->a.getId()).collect(Collectors.toList())); |
|
|
|
List<TbsScheduleItemBudget> scheduleItemBudgetList = tbsScheduleItemBudgetMapper.selectList(scheduleItemBudgetLambdaQueryWrapper); |
|
|
|
//合计目标发货和金定额
|
|
|
|
BigDecimal targetBudgetAmout = scheduleItemBudgetList.stream().map(a->a.getBudgetAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
BigDecimal targetDispatchAmout = scheduleItemBudgetList.stream().map(a->a.getPreDispatchAmount()==null?BigDecimal.ZERO:a.getPreDispatchAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
costCenter.setTargetSales(targetDispatchAmout); |
|
|
|
costCenter.setAreaBudget(targetBudgetAmout); |
|
|
|
BigDecimal targetExpenseRate = BigDecimal.ZERO; |
|
|
|
if(targetDispatchAmout.compareTo(BigDecimal.ZERO)!=0) { |
|
|
|
targetExpenseRate = targetBudgetAmout.divide(targetDispatchAmout, 2, BigDecimal.ROUND_HALF_DOWN); |
|
|
|
} |
|
|
|
} |
|
|
|
costCenter.setTargetExpenseRate(targetExpenseRate); |
|
|
|
costCenter.setYtdTargetBudget(targetBudgetAmout); |
|
|
|
costCenter.setYtdTargetSales(targetDispatchAmout); |
|
|
|
costCenter.setYtdTargetExpenseRate(targetExpenseRate); |
|
|
|
// costCenter.setQtdTargetBudget(BigDecimal.ZERO);
|
|
|
|
// costCenter.setQtdTargetSales(BigDecimal.ZERO);
|
|
|
|
// costCenter.setQtdTargetExpenseRate(BigDecimal.ZERO);
|
|
|
|
// costCenter.setMtdTargetBudget(BigDecimal.ZERO);
|
|
|
|
// costCenter.setMtdTargetSales(BigDecimal.ZERO);
|
|
|
|
// costCenter.setMtdTargetExpenseRate(BigDecimal.ZERO);
|
|
|
|
/* ------------------------------------------------------------------------- */ |
|
|
|
//实际发贷数
|
|
|
|
List<String> supplierCodeList = roiRateService.getSupplierCodesByCenter(costDTO,centerType,Arrays.asList(centerId)); |
|
|
|
List<ErpDispatchSumVo> dispatchSumVos = dispatchDataMapper.querySumCost(startMonthNum,endMonthNum,supplierCodeList); |
|
|
|
List<BirActivityCenterGoods> birCenterCost = birCenterCostMap.containsKey(centerKey)?birCenterCostMap.get(centerKey):new ArrayList<>(); |
|
|
|
|
|
|
|
/* --------------- 实际费用率YTD --------------------------------------------- */ |
|
|
|
BigDecimal ytdRealDipatch = dispatchSumVos.stream().map(a->a.getDispatchSumCost()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
BigDecimal ytdRealCost = birCenterCost.stream().map(a->a.getSplitUsedAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
costCenter.setYtdRealCost(ytdRealCost); |
|
|
|
costCenter.setYtdRealSales(ytdRealDipatch); |
|
|
|
BigDecimal ytdRealExpenseRate = BigDecimal.ZERO; |
|
|
|
if(ytdRealDipatch.compareTo(BigDecimal.ZERO)!=0) { |
|
|
|
ytdRealExpenseRate = ytdRealCost.divide(ytdRealDipatch, 2, BigDecimal.ROUND_HALF_DOWN); |
|
|
|
} |
|
|
|
costCenter.setYtdRealExpenseRate(ytdRealExpenseRate); |
|
|
|
/* ------------------------------------------------------------------------- */ |
|
|
|
|
|
|
|
/* --------------- 实际费用率QTD --------------------------------------------- */ |
|
|
|
List<Integer> yearQuarter = QuarterUtil.getQuarterNumbers(year, month); |
|
|
|
BigDecimal qtdRealDipatch = dispatchSumVos.stream().filter(a->yearQuarter.contains(a.getYearMonth())).map(a->a.getDispatchSumCost()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
BigDecimal qtdRealCost = birCenterCost.stream().filter(a->yearQuarter.contains(a.getKeyNum())).map(a->a.getSplitUsedAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
costCenter.setQtdRealCost(qtdRealCost); |
|
|
|
costCenter.setQtdRealSales(qtdRealDipatch); |
|
|
|
BigDecimal qtdRealExpenseRate = BigDecimal.ZERO; |
|
|
|
if(qtdRealDipatch.compareTo(BigDecimal.ZERO)!=0) { |
|
|
|
qtdRealExpenseRate = qtdRealCost.divide(qtdRealDipatch, 2, BigDecimal.ROUND_HALF_DOWN); |
|
|
|
} |
|
|
|
costCenter.setQtdRealExpenseRate(qtdRealExpenseRate); |
|
|
|
/* ------------------------------------------------------------------------- */ |
|
|
|
|
|
|
|
/* --------------- 实际费用率MTD --------------------------------------------- */ |
|
|
|
Integer yearMonth = year*100 + month; |
|
|
|
BigDecimal mtdRealDipatch = dispatchSumVos.stream().filter(a->yearMonth.equals(a.getYearMonth())).map(a->a.getDispatchSumCost()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
BigDecimal mtdRealCost = birCenterCost.stream().filter(a->yearMonth.equals(a.getKeyNum())).map(a->a.getSplitUsedAmount()).reduce(BigDecimal.ZERO,BigDecimal::add); |
|
|
|
costCenter.setMtdRealCost(mtdRealCost); |
|
|
|
costCenter.setMtdRealSales(mtdRealDipatch); |
|
|
|
BigDecimal mtdRealExpenseRate = BigDecimal.ZERO; |
|
|
|
if(mtdRealDipatch.compareTo(BigDecimal.ZERO)!=0) { |
|
|
|
mtdRealExpenseRate = mtdRealCost.divide(mtdRealDipatch, 2, BigDecimal.ROUND_HALF_DOWN); |
|
|
|
} |
|
|
|
costCenter.setMtdRealExpenseRate(mtdRealExpenseRate); |
|
|
|
/* ------------------------------------------------------------------------- */ |
|
|
|
|
|
|
|
|
|
|
|
//查询ROI底表,并拼接DTO
|
|
|
|
List<TbsCenterGoodBirDTO> centerGoodDTOS = new ArrayList<>(); |
|
|
|
// LambdaQueryWrapper<BirActivityCenterGoods> birLwq = new LambdaQueryWrapper<>();
|
|
|
|
// birLwq.in(BirActivityCenterGoods::getCenterId,tbsActivityCenters.stream().map(a->a.getCenterId()).collect(Collectors.toList()));
|
|
|
|
// List<BirActivityCenterGoods> birActivityCenterGoodsList = birActivityCenterGoodsService.list(birLwq);
|
|
|
|
List<BirActivityCenterGoods> birActivityCenterGoodsList = new ArrayList<>(); |
|
|
|
//按成本中心维度分组
|
|
|
|
Map<String,List<BirActivityCenterGoods>> birGroupByCenter = birActivityCenterGoodsList.stream() |
|
|
|
.collect(Collectors.groupingBy(a->a.getCenterType()+"_"+a.getCenterId())); |
|
|
|
|
|
|
|
for (TbsActivityCenter activityCenter : tbsActivityCenters) { |
|
|
|
String centerKey = activityCenter.getCenterType()+"_"+activityCenter.getCenterId(); |
|
|
|
//创建成本中心维度对象
|
|
|
|
TbsCostSubItem.CostCenter costCenter = this.buildCostCenterParam(result, tbsScheduleItemBudgets, |
|
|
|
year, month, supplierCodeMapWithCenterType, birGroupByCenter, activityCenter, centerKey); |
|
|
|
subList.add(costCenter); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 创建成本中心维度对象 |
|
|
|
* 创建成本中心维度对象(弃) |
|
|
|
* @param result |
|
|
|
* @param tbsScheduleItemBudgets |
|
|
|
* @param year |
|
|
@ -568,7 +623,7 @@ public class TbsCostApplyServiceImpl extends ServiceImpl<TbsCostApplyMapper,TbsC |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 创建 成本中心 DTO |
|
|
|
* 创建 成本中心 DTO(弃) |
|
|
|
* @param centerGoods |
|
|
|
* @param erpDispatchSumVoList |
|
|
|
* @return |
|
|
|