|
|
@ -2,6 +2,7 @@ package com.qs.serve.modules.bms.service.impl; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.qs.serve.common.model.consts.BudgetLogOptFlag; |
|
|
|
import com.qs.serve.common.util.Assert; |
|
|
|
import com.qs.serve.common.util.CollectionUtil; |
|
|
|
import com.qs.serve.common.util.CopierUtil; |
|
|
@ -16,12 +17,10 @@ import com.qs.serve.modules.sys.entity.SysUser; |
|
|
|
import com.qs.serve.modules.sys.service.SysUserService; |
|
|
|
import com.qs.serve.modules.tbs.common.TbsCenterType; |
|
|
|
import com.qs.serve.modules.tbs.common.TbsGoodsType; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsBudget; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsBudgetCondition; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsBudgetLog; |
|
|
|
import com.qs.serve.modules.tbs.mapper.TbsBudgetConditionMapper; |
|
|
|
import com.qs.serve.modules.tbs.mapper.TbsBudgetLogMapper; |
|
|
|
import com.qs.serve.modules.tbs.mapper.TbsBudgetMapper; |
|
|
|
import com.qs.serve.modules.tbs.entity.*; |
|
|
|
import com.qs.serve.modules.tbs.mapper.*; |
|
|
|
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; |
|
|
@ -30,6 +29,8 @@ import com.qs.serve.modules.bms.service.BmsSupplierService; |
|
|
|
import com.qs.serve.modules.bms.mapper.BmsSupplierMapper; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
|
|
|
|
import java.time.LocalDate; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.List; |
|
|
@ -49,9 +50,13 @@ public class BmsSupplierServiceImpl extends ServiceImpl<BmsSupplierMapper,BmsSup |
|
|
|
private BmsRegionService regionService; |
|
|
|
private BmsRegion2Service region2Service; |
|
|
|
private SysUserService sysUserService; |
|
|
|
private TbsBudgetLogMapper budgetLogMapper; |
|
|
|
private TbsBudgetLogService budgetLogService; |
|
|
|
private TbsCostUnItemService costUnItemService; |
|
|
|
|
|
|
|
private TbsBudgetMapper budgetMapper; |
|
|
|
private TbsBudgetConditionMapper budgetConditionMapper; |
|
|
|
private TbsScheduleItemBudgetMapper scheduleItemBudgetMapper; |
|
|
|
private TbsActivityMapper activityMapper; |
|
|
|
|
|
|
|
@Override |
|
|
|
public List<BmsSupplier> selectSupplierList(BmsSupplier bmsSupplier) { |
|
|
@ -91,22 +96,26 @@ public class BmsSupplierServiceImpl extends ServiceImpl<BmsSupplierMapper,BmsSup |
|
|
|
supplier.setPid(null); |
|
|
|
supplier.setParentCode(null); |
|
|
|
} |
|
|
|
if(param.getRegionId()!=null){ |
|
|
|
String saleRegion = param.getRegionId(); |
|
|
|
if(saleRegion!=null){ |
|
|
|
//绑定销售区域逻辑
|
|
|
|
this.initRegion(supplier,param.getRegionId()); |
|
|
|
this.initRegion(supplier,saleRegion); |
|
|
|
//判断是否需要迁移数据
|
|
|
|
if(editId!=null |
|
|
|
&& orgSaleRegionId!=null |
|
|
|
&&!orgSaleRegionId.equals(param.getRegionId())){ |
|
|
|
migrateRegion(editId,TbsCenterType.saleRegion.name(), orgSaleRegionId, orgBizRegionId); |
|
|
|
&&!orgSaleRegionId.equals(saleRegion)){ |
|
|
|
migrateRegion(editId,TbsCenterType.saleRegion.name(), orgSaleRegionId,saleRegion); |
|
|
|
} |
|
|
|
} |
|
|
|
if(param.getRegionId2()!=null){ |
|
|
|
String bizRegion = param.getRegionId2(); |
|
|
|
if(bizRegion!=null){ |
|
|
|
//绑定行政区域逻辑
|
|
|
|
this.initRegion2(supplier,param.getRegionId2()); |
|
|
|
this.initRegion2(supplier,bizRegion); |
|
|
|
//判断是否需要迁移数据
|
|
|
|
if(!orgBizRegionId.equals(param.getRegionId2())){ |
|
|
|
|
|
|
|
if(editId!=null |
|
|
|
&&orgBizRegionId != null |
|
|
|
&&!orgBizRegionId.equals(bizRegion)){ |
|
|
|
migrateRegion(editId,TbsCenterType.bizRegion.name(), orgBizRegionId, bizRegion); |
|
|
|
} |
|
|
|
} |
|
|
|
this.saveOrUpdate(supplier); |
|
|
@ -115,24 +124,41 @@ public class BmsSupplierServiceImpl extends ServiceImpl<BmsSupplierMapper,BmsSup |
|
|
|
private void migrateRegion(String supplierId, String regionType, String orgRegionId, String newRegionId) { |
|
|
|
//todo 开发中
|
|
|
|
if(1==1){return;} |
|
|
|
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 = budgetLogMapper.selectList(lqw4log); |
|
|
|
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); //将有科目条件的前置
|
|
|
|
// 将有科目条件的前置
|
|
|
|
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)) |
|
|
@ -152,8 +178,33 @@ public class BmsSupplierServiceImpl extends ServiceImpl<BmsSupplierMapper,BmsSup |
|
|
|
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){ |
|
|
@ -173,38 +224,85 @@ public class BmsSupplierServiceImpl extends ServiceImpl<BmsSupplierMapper,BmsSup |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//记录匹配到的预算
|
|
|
|
TbsBudget matchBudget = null; |
|
|
|
//记录匹配品类条件的预算
|
|
|
|
List<TbsBudget> matchBudgetConditionList = new ArrayList<>(); |
|
|
|
List<TbsBudget> matchBudgetConditionList_tmp = new ArrayList<>(); |
|
|
|
for (TbsBudget budget : budgetList) { |
|
|
|
if(matchBudget!=null){break;} |
|
|
|
if(budget.getConditionFlag().equals(1)){ |
|
|
|
for (TbsGoodsType goodsType : goodsTypes) { |
|
|
|
if(matchBudget!=null){break;} |
|
|
|
List<Long> tempList = matchConditionBudgetIdsMap.get(goodsType.name()); |
|
|
|
if(CollectionUtil.isNotEmpty(tempList)){ |
|
|
|
for (Long budgetId01 : tempList) { |
|
|
|
if(budgetId01.equals(budget.getId())){ |
|
|
|
matchBudget = budget; |
|
|
|
break; |
|
|
|
matchBudgetConditionList.add(budget); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}else { |
|
|
|
matchBudget = budget; |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//todo 设置已迁移记录
|
|
|
|
//todo 匹配条件
|
|
|
|
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); |
|
|
|
budgetLogMapper.update(updLogParam,updLogLqw); |
|
|
|
|
|
|
|
budgetLogService.update(updLogParam,updLogLqw); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|