9 changed files with 253 additions and 205 deletions
@ -0,0 +1,227 @@ |
|||||
|
package com.qs.serve.modules.bms.service.impl; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
import com.qs.serve.common.model.consts.BudgetLogOptFlag; |
||||
|
import com.qs.serve.common.util.CollectionUtil; |
||||
|
import com.qs.serve.common.util.CopierUtil; |
||||
|
import com.qs.serve.modules.tbs.common.TbsCenterType; |
||||
|
import com.qs.serve.modules.tbs.common.TbsGoodsType; |
||||
|
import com.qs.serve.modules.tbs.entity.*; |
||||
|
import com.qs.serve.modules.tbs.mapper.TbsActivityMapper; |
||||
|
import com.qs.serve.modules.tbs.mapper.TbsBudgetConditionMapper; |
||||
|
import com.qs.serve.modules.tbs.mapper.TbsBudgetMapper; |
||||
|
import com.qs.serve.modules.tbs.mapper.TbsScheduleItemBudgetMapper; |
||||
|
import com.qs.serve.modules.tbs.service.TbsBudgetLogService; |
||||
|
import com.qs.serve.modules.tbs.service.TbsCostUnItemService; |
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
import java.util.stream.Collectors; |
||||
|
|
||||
|
/** |
||||
|
* @author YenHex |
||||
|
* @since 2023/2/8 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
@AllArgsConstructor |
||||
|
public class BmsSupplierApplicationService { |
||||
|
|
||||
|
private TbsBudgetLogService budgetLogService; |
||||
|
private TbsCostUnItemService costUnItemService; |
||||
|
private TbsBudgetMapper budgetMapper; |
||||
|
private TbsBudgetConditionMapper budgetConditionMapper; |
||||
|
private TbsScheduleItemBudgetMapper scheduleItemBudgetMapper; |
||||
|
private TbsActivityMapper activityMapper; |
||||
|
|
||||
|
public void migrateRegion(String supplierId, String regionType, String orgRegionId, String newRegionId) { |
||||
|
//todo 待测
|
||||
|
LocalDateTime nowTime = LocalDateTime.now(); |
||||
|
LambdaQueryWrapper<TbsBudgetLog> lqw4log = new LambdaQueryWrapper<>(); |
||||
|
lqw4log.eq(TbsBudgetLog::getMigrateFlag,0); |
||||
|
lqw4log.eq(TbsBudgetLog::getRollbackFlag,0); |
||||
|
lqw4log.eq(TbsBudgetLog::getSupplierId, supplierId); |
||||
|
lqw4log.eq(TbsBudgetLog::getCenterType, regionType); |
||||
|
lqw4log.eq(TbsBudgetLog::getCenterId, orgRegionId); |
||||
|
List<TbsBudgetLog> budgetLogList = budgetLogService.list(lqw4log); |
||||
|
if(CollectionUtil.isNotEmpty(budgetLogList)){ |
||||
|
//查找符合条件的活动
|
||||
|
final List<Long> activityIds = budgetLogList.stream().map(TbsBudgetLog::getActivityId).distinct().collect(Collectors.toList()); |
||||
|
LambdaQueryWrapper<TbsActivity> lqwAct = new LambdaQueryWrapper<>(); |
||||
|
lqwAct.in(TbsActivity::getId,activityIds); |
||||
|
List<TbsActivity> activityList = activityMapper.selectList(lqwAct); |
||||
|
//查询符合条件的预算
|
||||
|
final List<Long> subjectIds = budgetLogList.stream().map(TbsBudgetLog::getSubjectId).distinct().collect(Collectors.toList()); |
||||
|
LambdaQueryWrapper<TbsBudget> lqwBudget = new LambdaQueryWrapper<>(); |
||||
|
lqwBudget.eq(TbsBudget::getCenterType,regionType); |
||||
|
lqwBudget.eq(TbsBudget::getCenterId, newRegionId); |
||||
|
lqwBudget.eq(TbsBudget::getBudgetState,1); |
||||
|
lqwBudget.and(wq->{ |
||||
|
wq.eq(TbsBudget::getSubjectId,0).or().in(TbsBudget::getSubjectId,subjectIds); |
||||
|
}); |
||||
|
// 将有科目条件的前置
|
||||
|
lqwBudget.orderByDesc(TbsBudget::getSubjectId); |
||||
|
List<TbsBudget> budgetList = budgetMapper.selectList(lqwBudget); |
||||
|
//查询预算考核期
|
||||
|
List<Long> budgetIds = budgetList.stream().map(TbsBudget::getId).distinct().collect(Collectors.toList()); |
||||
|
Map<Long,List<TbsScheduleItemBudget>> scheduleItemBudgetsMap = null; |
||||
|
if(CollectionUtil.isNotEmpty(budgetIds)){ |
||||
|
LambdaQueryWrapper<TbsScheduleItemBudget> budgetItemLqw = new LambdaQueryWrapper<>(); |
||||
|
budgetItemLqw.in(TbsScheduleItemBudget::getBudgetId,budgetIds); |
||||
|
List<TbsScheduleItemBudget> scheduleItemBudgets = scheduleItemBudgetMapper.selectList(budgetItemLqw); |
||||
|
scheduleItemBudgetsMap = scheduleItemBudgets.stream().collect(Collectors.groupingBy(TbsScheduleItemBudget::getBudgetId)); |
||||
|
} |
||||
|
//查询预算条件
|
||||
|
List<Long> conditionBudgetIds = budgetList.stream() |
||||
|
.filter(a->a.getConditionFlag().equals(1)) |
||||
|
.map(TbsBudget::getId).distinct().collect(Collectors.toList()); |
||||
|
Map<Long,List<TbsBudgetCondition>> budgetConditionsMap = null; |
||||
|
if(conditionBudgetIds.size()>0){ |
||||
|
LambdaQueryWrapper<TbsBudgetCondition> conditionLqw = new LambdaQueryWrapper<>(); |
||||
|
conditionLqw.in(TbsBudgetCondition::getBudgetId,conditionBudgetIds); |
||||
|
List<TbsBudgetCondition> budgetConditions = budgetConditionMapper.selectList(conditionLqw); |
||||
|
budgetConditionsMap = budgetConditions.stream().collect(Collectors.groupingBy(TbsBudgetCondition::getBudgetId)); |
||||
|
} |
||||
|
//用于循环筛选条件
|
||||
|
TbsGoodsType[] goodsTypes = new TbsGoodsType[]{ |
||||
|
TbsGoodsType.sku, |
||||
|
TbsGoodsType.spu, |
||||
|
TbsGoodsType.series, |
||||
|
TbsGoodsType.category, |
||||
|
TbsGoodsType.brand |
||||
|
}; |
||||
|
//已占用预算进行调增
|
||||
|
final Integer optType4Diff = regionType.equals(TbsCenterType.saleRegion.name())? |
||||
|
BudgetLogOptFlag.State_8:BudgetLogOptFlag.State_10; |
||||
|
final Integer optType4Add = regionType.equals(TbsCenterType.saleRegion.name())? |
||||
|
BudgetLogOptFlag.State_7:BudgetLogOptFlag.State_9; |
||||
|
List<TbsBudgetLog> budgetLog4Adds = budgetLogList.stream().map(budgetLog->{ |
||||
|
TbsBudgetLog obj = CopierUtil.copy(budgetLog,new TbsBudgetLog()); |
||||
|
obj.setId(null); |
||||
|
obj.setOptType(optType4Add); |
||||
|
obj.setMigrateFlag(1); |
||||
|
obj.setMigrateTime(nowTime); |
||||
|
obj.setAmount(obj.getAmount().negate()); |
||||
|
return obj; |
||||
|
}).collect(Collectors.toList()); |
||||
|
//规则:品类条件比科目条件优先级更高
|
||||
|
List<TbsBudgetLog> budgetLog4Diff = new ArrayList<>(); |
||||
|
//无匹配的费用支出
|
||||
|
List<TbsCostUnItem> costUnItemList = new ArrayList<>(); |
||||
|
for (TbsBudgetLog budgetLog : budgetLogList) { |
||||
|
//当前活动
|
||||
|
TbsActivity currActivity = null; |
||||
|
for (TbsActivity activity : activityList) { |
||||
|
if(budgetLog.getActivityId().equals(activity.getId())){ |
||||
|
currActivity = activity; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
//(数据结构:value=预算id列表,key=商品类型)
|
||||
|
Map<String,List<Long>> matchConditionBudgetIdsMap = new HashMap<>(); |
||||
|
if(budgetConditionsMap!=null){ |
||||
|
for (Long budgetId : budgetConditionsMap.keySet()) { |
||||
|
List<TbsBudgetCondition> budgetConditions = budgetConditionsMap.get(budgetId); |
||||
|
String pathName = budgetLog.getTargetLevelPathNames(); |
||||
|
for (TbsBudgetCondition condition : budgetConditions) { |
||||
|
if(pathName.contains(condition.getTargetLevelPathNames())){ |
||||
|
List<Long> tempList = matchConditionBudgetIdsMap.get(condition.getTargetType()); |
||||
|
if(tempList==null){ |
||||
|
tempList = new ArrayList<>(); |
||||
|
} |
||||
|
tempList.add(budgetId); |
||||
|
matchConditionBudgetIdsMap.put(condition.getTargetType(),tempList); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
//记录匹配品类条件的预算
|
||||
|
List<TbsBudget> matchBudgetConditionList = new ArrayList<>(); |
||||
|
List<TbsBudget> matchBudgetConditionList_tmp = new ArrayList<>(); |
||||
|
for (TbsBudget budget : budgetList) { |
||||
|
if(budget.getConditionFlag().equals(1)){ |
||||
|
for (TbsGoodsType goodsType : goodsTypes) { |
||||
|
List<Long> tempList = matchConditionBudgetIdsMap.get(goodsType.name()); |
||||
|
if(CollectionUtil.isNotEmpty(tempList)){ |
||||
|
for (Long budgetId01 : tempList) { |
||||
|
if(budgetId01.equals(budget.getId())){ |
||||
|
matchBudgetConditionList.add(budget); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}else { |
||||
|
matchBudgetConditionList_tmp.add(budget); |
||||
|
} |
||||
|
} |
||||
|
//有条件预算优先级更高
|
||||
|
matchBudgetConditionList.addAll(matchBudgetConditionList_tmp); |
||||
|
//第一个预算为最有匹配预算
|
||||
|
TbsBudget matchBudget = null; |
||||
|
TbsScheduleItemBudget matchItemBudget = null; |
||||
|
//记录匹配到的预算
|
||||
|
if(scheduleItemBudgetsMap!=null){ |
||||
|
for (TbsBudget budget : matchBudgetConditionList) { |
||||
|
if(matchBudget!=null){ |
||||
|
break; |
||||
|
} |
||||
|
List<TbsScheduleItemBudget> scheduleItemBudgets = scheduleItemBudgetsMap.get(budget.getId()); |
||||
|
for (TbsScheduleItemBudget itemBudget : scheduleItemBudgets) { |
||||
|
LocalDateTime actStartDate = currActivity.getPreStartDate().atStartOfDay(); |
||||
|
LocalDateTime actEndDate = currActivity.getPreStartDate().atStartOfDay().with(LocalDateTime.MAX); |
||||
|
if(actStartDate.isAfter(itemBudget.getStartDate())&&actEndDate.isBefore(itemBudget.getEndDate())){ |
||||
|
matchBudget = budget; |
||||
|
matchItemBudget = itemBudget; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if(matchBudget!=null){ |
||||
|
//有匹配的预算
|
||||
|
TbsBudgetLog newLog = CopierUtil.copy(budgetLog,new TbsBudgetLog()); |
||||
|
newLog.setId(null); |
||||
|
newLog.setOptType(optType4Diff); |
||||
|
newLog.setBudgetId(matchBudget.getId()); |
||||
|
newLog.setBudgetCode(matchBudget.getBudgetCode()); |
||||
|
newLog.setScheduleOrgId(budgetLog.getScheduleId()); |
||||
|
newLog.setScheduleItemOrgId(budgetLog.getScheduleItemId()); |
||||
|
newLog.setScheduleItemBudgetOrgId(budgetLog.getScheduleItemBudgetId()); |
||||
|
//匹配到的项
|
||||
|
newLog.setScheduleId(matchItemBudget.getScheduleId()); |
||||
|
newLog.setScheduleItemId(matchItemBudget.getScheduleItemId()); |
||||
|
newLog.setScheduleItemBudgetId(matchItemBudget.getId()); |
||||
|
budgetLog4Diff.add(newLog); |
||||
|
}else { |
||||
|
//无匹配的预算
|
||||
|
TbsCostUnItem costUnItem = budgetLog.toTbsCostUnItem(currActivity); |
||||
|
costUnItemList.add(costUnItem); |
||||
|
} |
||||
|
} |
||||
|
//保存
|
||||
|
budgetLogService.saveBatch(budgetLog4Adds); |
||||
|
if(CollectionUtil.isNotEmpty(budgetLog4Diff)){ |
||||
|
budgetLogService.saveBatch(budgetLog4Diff); |
||||
|
} |
||||
|
if(CollectionUtil.isNotEmpty(costUnItemList)){ |
||||
|
costUnItemService.saveBatch(costUnItemList); |
||||
|
} |
||||
|
//设置已迁移状态
|
||||
|
List<Long> budgetLogIds = budgetLogList.stream().map(TbsBudgetLog::getId).collect(Collectors.toList()); |
||||
|
TbsBudgetLog updLogParam = new TbsBudgetLog(); |
||||
|
updLogParam.setMigrateFlag(1); |
||||
|
updLogParam.setMigrateTime(nowTime); |
||||
|
LambdaQueryWrapper<TbsBudgetLog> updLogLqw = new LambdaQueryWrapper<>(); |
||||
|
updLogLqw.in(TbsBudgetLog::getId,budgetLogIds); |
||||
|
budgetLogService.update(updLogParam,updLogLqw); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue