16 changed files with 606 additions and 234 deletions
@ -0,0 +1,11 @@ |
|||||
|
package com.qs.serve.modules.bir.service; |
||||
|
|
||||
|
import com.qs.serve.modules.tbs.entity.dto.TbsCostSubItem; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface BirCenterRateService { |
||||
|
TbsCostSubItem.CostCenterTranStr buildCostCenter(String centerType, String centerId, String centerName); |
||||
|
|
||||
|
List<TbsCostSubItem.CostCenterTranStr> findCostCenterDataByCostApplyId(Long costApplyId,Long policyId); |
||||
|
} |
@ -0,0 +1,203 @@ |
|||||
|
package com.qs.serve.modules.bir.service.impl; |
||||
|
|
||||
|
import cn.hutool.core.collection.CollUtil; |
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
import com.qs.serve.common.util.CopierUtil; |
||||
|
import com.qs.serve.common.util.SpringUtils; |
||||
|
import com.qs.serve.modules.bir.entity.BirActivityCenterGoods; |
||||
|
import com.qs.serve.modules.bir.entity.BirRoiRate; |
||||
|
import com.qs.serve.modules.bir.entity.dto.BirRoiCostDTO; |
||||
|
import com.qs.serve.modules.bir.entity.so.BirCostRoiSo; |
||||
|
import com.qs.serve.modules.bir.entity.vo.BirRoiCostItemVo; |
||||
|
import com.qs.serve.modules.bir.entity.vo.YtdQtdToOAVo; |
||||
|
import com.qs.serve.modules.bir.mapper.BirActivityCenterGoodsMapper; |
||||
|
import com.qs.serve.modules.bir.mapper.BirRoiRateMapper; |
||||
|
import com.qs.serve.modules.bir.service.BirActivityCenterGoodsService; |
||||
|
import com.qs.serve.modules.bir.service.BirCenterRateService; |
||||
|
import com.qs.serve.modules.bir.service.BirRoiRateService; |
||||
|
import com.qs.serve.modules.bms.entity.BmsCenterRegion; |
||||
|
import com.qs.serve.modules.bms.entity.BmsSupplier; |
||||
|
import com.qs.serve.modules.bms.mapper.BmsCenterRegionMapper; |
||||
|
import com.qs.serve.modules.bms.mapper.BmsSupplierMapper; |
||||
|
import com.qs.serve.modules.erp.entity.dto.ErpDispatchSumVo; |
||||
|
import com.qs.serve.modules.erp.mapper.ErpDispatchDataMapper; |
||||
|
import com.qs.serve.modules.tbs.common.util.QuarterUtil; |
||||
|
import com.qs.serve.modules.tbs.entity.*; |
||||
|
import com.qs.serve.modules.tbs.entity.dto.TbsCostSubItem; |
||||
|
import com.qs.serve.modules.tbs.mapper.TbsActivityCenterMapper; |
||||
|
import com.qs.serve.modules.tbs.mapper.TbsBudgetMapper; |
||||
|
import com.qs.serve.modules.tbs.mapper.TbsScheduleItemBudgetMapper; |
||||
|
import com.qs.serve.modules.tzc.entity.TzcPolicyItem; |
||||
|
import com.qs.serve.modules.tzc.mapper.TzcPolicyItemMapper; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.math.BigDecimal; |
||||
|
import java.math.RoundingMode; |
||||
|
import java.time.LocalDate; |
||||
|
import java.time.Month; |
||||
|
import java.util.*; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* 服务实现类 |
||||
|
* @author YenHex |
||||
|
* @since 2023-06-05 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
@AllArgsConstructor |
||||
|
public class BirCenterRateServiceImpl implements BirCenterRateService { |
||||
|
|
||||
|
private TbsScheduleItemBudgetMapper tbsScheduleItemBudgetMapper; |
||||
|
private TbsBudgetMapper tbsBudgetMapper; |
||||
|
private BirRoiRateService birRoiRateService; |
||||
|
private final ErpDispatchDataMapper dispatchDataMapper; |
||||
|
private BirActivityCenterGoodsMapper birActivityCenterGoodsMapper; |
||||
|
private TbsActivityCenterMapper tbsActivityCenterMapper; |
||||
|
private TzcPolicyItemMapper tzcPolicyItemMapper; |
||||
|
|
||||
|
|
||||
|
public List<TbsCostSubItem.CostCenterTranStr> findCostCenterDataByCostApplyId(Long costApplyId,Long policyId){ |
||||
|
List<TbsCostSubItem.CostCenterTranStr> centerDataList = new ArrayList<>(); |
||||
|
if(costApplyId!=null) { |
||||
|
LambdaQueryWrapper<TbsActivityCenter> lqw = new LambdaQueryWrapper<>(); |
||||
|
lqw.eq(TbsActivityCenter::getCostApplyId, costApplyId); |
||||
|
List<TbsActivityCenter> tbsActivityCenters = tbsActivityCenterMapper.selectList(lqw); |
||||
|
if (tbsActivityCenters.size() == 0) { |
||||
|
return centerDataList; |
||||
|
} |
||||
|
|
||||
|
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) { |
||||
|
TbsCostSubItem.CostCenterTranStr costCenter = this.buildCostCenter(center.getCenterType(), center.getCenterId(), center.getCenterName()); |
||||
|
costCenter.setCenterId(center.getCenterId()); |
||||
|
costCenter.setCentertype(center.getCenterType()); |
||||
|
costCenter.setCenterCode(center.getCenterCode()); |
||||
|
centerDataList.add(costCenter); |
||||
|
} |
||||
|
} |
||||
|
if(policyId!=null) { |
||||
|
LambdaQueryWrapper<TzcPolicyItem> lqw = new LambdaQueryWrapper<>(); |
||||
|
lqw.eq(TzcPolicyItem::getPolicyId,policyId); |
||||
|
List<TzcPolicyItem> policyItems = tzcPolicyItemMapper.selectList(lqw); |
||||
|
Map<String, List<TzcPolicyItem>> centerMapList = policyItems.stream().collect(Collectors.groupingBy(a -> a.getCenterType() + "-" + a.getCenterId())); |
||||
|
List<TzcPolicyItem> centerList = centerMapList.values().stream().map(a -> a.get(0)).collect(Collectors.toList()); |
||||
|
for(TzcPolicyItem center : centerList) { |
||||
|
TbsCostSubItem.CostCenterTranStr costCenter = this.buildCostCenter(center.getCenterType(), center.getCenterId(), center.getCenterName()); |
||||
|
costCenter.setCenterId(center.getCenterId()); |
||||
|
costCenter.setCentertype(center.getCenterType()); |
||||
|
costCenter.setCenterCode(center.getCenterCode()); |
||||
|
centerDataList.add(costCenter); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return centerDataList; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public TbsCostSubItem.CostCenterTranStr buildCostCenter(String centerType,String centerId,String centerName){ |
||||
|
|
||||
|
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; |
||||
|
BirRoiCostDTO costDTO = new BirRoiCostDTO(); |
||||
|
costDTO.setStartMonthNum(startMonthNum); |
||||
|
costDTO.setEndMonthNum(endMonthNum); |
||||
|
|
||||
|
|
||||
|
String centerKey = centerType + "-" + centerId; |
||||
|
TbsCostSubItem.CostCenterTranStr costCenter = new TbsCostSubItem.CostCenterTranStr(centerName,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.toString()); |
||||
|
costCenter.setAreaBudget(targetBudgetAmout.toString()); |
||||
|
BigDecimal targetExpenseRate = BigDecimal.ZERO; |
||||
|
if(targetDispatchAmout.compareTo(BigDecimal.ZERO)!=0) { |
||||
|
targetExpenseRate = targetBudgetAmout.divide(targetDispatchAmout, 2, BigDecimal.ROUND_HALF_DOWN); |
||||
|
costCenter.setTargetExpenseRate(targetExpenseRate.toString()); |
||||
|
costCenter.setYtdTargetExpenseRate(targetExpenseRate.toString()); |
||||
|
} |
||||
|
// costCenter.setTargetExpenseRate(targetExpenseRate);
|
||||
|
costCenter.setYtdTargetBudget(targetBudgetAmout.toString()); |
||||
|
costCenter.setYtdTargetSales(targetDispatchAmout.toString()); |
||||
|
// 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 = birRoiRateService.getSupplierCodesByCenter(costDTO,centerType, Arrays.asList(centerId)); |
||||
|
List<ErpDispatchSumVo> dispatchSumVos = dispatchDataMapper.querySumCost(startMonthNum,endMonthNum,supplierCodeList); |
||||
|
|
||||
|
//获取BIR表的数据,用于计算实际费用率
|
||||
|
LambdaQueryWrapper<BirActivityCenterGoods> birLwq = new LambdaQueryWrapper<>(); |
||||
|
birLwq.eq(BirActivityCenterGoods::getCenterId,centerId); |
||||
|
birLwq.eq(BirActivityCenterGoods::getCenterType,centerType); |
||||
|
birLwq.ge(BirActivityCenterGoods::getKeyNum,startMonthNum); |
||||
|
birLwq.le(BirActivityCenterGoods::getKeyNum,endMonthNum); |
||||
|
List<BirActivityCenterGoods> birCenterCost = birActivityCenterGoodsMapper.selectList(birLwq); |
||||
|
|
||||
|
/* --------------- 实际费用率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.toString()); |
||||
|
costCenter.setYtdRealSales(ytdRealDipatch.toString()); |
||||
|
BigDecimal ytdRealExpenseRate = BigDecimal.ZERO; |
||||
|
if(ytdRealDipatch.compareTo(BigDecimal.ZERO)!=0) { |
||||
|
ytdRealExpenseRate = ytdRealCost.divide(ytdRealDipatch, 2, BigDecimal.ROUND_HALF_DOWN); |
||||
|
costCenter.setYtdRealExpenseRate(ytdRealExpenseRate.toString()); |
||||
|
} |
||||
|
// 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.toString()); |
||||
|
costCenter.setQtdRealSales(qtdRealDipatch.toString()); |
||||
|
BigDecimal qtdRealExpenseRate = BigDecimal.ZERO; |
||||
|
if(qtdRealDipatch.compareTo(BigDecimal.ZERO)!=0) { |
||||
|
qtdRealExpenseRate = qtdRealCost.divide(qtdRealDipatch, 2, BigDecimal.ROUND_HALF_DOWN); |
||||
|
costCenter.setQtdRealExpenseRate(qtdRealExpenseRate.toString()); |
||||
|
} |
||||
|
// 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.toString()); |
||||
|
costCenter.setMtdRealSales(mtdRealDipatch.toString()); |
||||
|
BigDecimal mtdRealExpenseRate = BigDecimal.ZERO; |
||||
|
if(mtdRealDipatch.compareTo(BigDecimal.ZERO)!=0) { |
||||
|
mtdRealExpenseRate = mtdRealCost.divide(mtdRealDipatch, 2, BigDecimal.ROUND_HALF_DOWN); |
||||
|
costCenter.setMtdRealExpenseRate(mtdRealExpenseRate.toString()); |
||||
|
} |
||||
|
return costCenter; |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
@ -0,0 +1,33 @@ |
|||||
|
package com.qs.serve.task; |
||||
|
|
||||
|
import com.qs.serve.modules.bir.mapper.BirReportAccountBookMapper; |
||||
|
import com.qs.serve.modules.bir.service.BirActivityCenterGoodsService; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
import org.springframework.scheduling.annotation.Scheduled; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.time.LocalDate; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/7/14 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Component |
||||
|
@AllArgsConstructor |
||||
|
@ConditionalOnProperty(value = "project.task", havingValue = "true") |
||||
|
public class ErpDipatchTask { |
||||
|
|
||||
|
private final BirReportAccountBookMapper birReportAccountBookMapper; |
||||
|
|
||||
|
@Scheduled(cron="0 0 1 * * ?") |
||||
|
public void buildTempTable(){ |
||||
|
birReportAccountBookMapper.buildAllDispatch(); |
||||
|
birReportAccountBookMapper.buildRegionDispatch(); |
||||
|
birReportAccountBookMapper.buildBizRegionDispatch(); |
||||
|
birReportAccountBookMapper.buildCustomerDispatch(); |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue