32 changed files with 530 additions and 208 deletions
@ -0,0 +1,63 @@ |
|||||
|
package com.qs.serve.modules.tbs.common.dto; |
||||
|
|
||||
|
import com.qs.serve.common.util.CollectionUtil; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsActivity; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsBudget; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsBudgetCondition; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsScheduleItemBudget; |
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.*; |
||||
|
|
||||
|
/** |
||||
|
* 用于排序 |
||||
|
* @author YenHex |
||||
|
* @since 2023/5/18 |
||||
|
*/ |
||||
|
@Data |
||||
|
public class CostSortWrapper { |
||||
|
|
||||
|
Set<Long> budgetSet4Subject = new HashSet<>(); |
||||
|
Set<Long> budgetSet4NoSubject = new HashSet<>(); |
||||
|
|
||||
|
Set<Long> budgetSet4Condition = new HashSet<>(); |
||||
|
Set<Long> budgetSet4NoCondition = new HashSet<>(); |
||||
|
|
||||
|
|
||||
|
Map<Long,List<TbsBudgetCondition>> activityCostConditionsOfSort = new HashMap<>(); |
||||
|
Map<Long,List<Long>> activityAllowBudgetId; |
||||
|
|
||||
|
/** |
||||
|
* sort of timeline,filter timeline |
||||
|
* key is activity_id, value is schedule list |
||||
|
*/ |
||||
|
Map<Long,List<TbsScheduleItemBudget>> activityScheduleItemMap = new HashMap<>(); |
||||
|
|
||||
|
|
||||
|
public void putActivityCostCondition(Long activityId, List<TbsBudgetCondition> list){ |
||||
|
activityCostConditionsOfSort.put(activityId, list); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 初始化科目排序因子 |
||||
|
* @param budgetList |
||||
|
*/ |
||||
|
public void initializeBudget(List<TbsBudget> budgetList){ |
||||
|
if(CollectionUtil.isNotEmpty(budgetList)){ |
||||
|
for (TbsBudget budget : budgetList) { |
||||
|
if(budget.getSubjectId()==null||budget.getSubjectId().equals(0L)){ |
||||
|
budgetSet4NoSubject.add(budget.getId()); |
||||
|
}else { |
||||
|
budgetSet4Subject.add(budget.getId()); |
||||
|
} |
||||
|
if(budget.getConditionFlag()==null||budget.getConditionFlag().equals(0)){ |
||||
|
budgetSet4NoCondition.add(budget.getId()); |
||||
|
}else { |
||||
|
budgetSet4Condition.add(budget.getId()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
@ -0,0 +1,137 @@ |
|||||
|
package com.qs.serve.modules.tbs.common.util; |
||||
|
|
||||
|
import com.qs.serve.common.util.Assert; |
||||
|
import com.qs.serve.common.util.CollectionUtil; |
||||
|
import com.qs.serve.modules.tbs.common.dto.CostSortWrapper; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsActivity; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsBudget; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsBudgetCondition; |
||||
|
import com.qs.serve.modules.tbs.entity.TbsScheduleItemBudget; |
||||
|
import lombok.Data; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.time.Duration; |
||||
|
import java.util.*; |
||||
|
|
||||
|
/** |
||||
|
* 用于排序 |
||||
|
* @author YenHex |
||||
|
* @since 2023/5/18 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Data |
||||
|
public class CostSortWrapperUtil { |
||||
|
|
||||
|
|
||||
|
public static List<TbsScheduleItemBudget> initializeSort(TbsActivity activity, List<TbsScheduleItemBudget> list, CostSortWrapper wrapper){ |
||||
|
List<TbsScheduleItemBudget> resultSortList = new ArrayList<>(); |
||||
|
if(CollectionUtil.isNotEmpty(list)){ |
||||
|
//1.科目限制>品类之间>时间区间
|
||||
|
//2.相同品类,取纬度最小值
|
||||
|
//3.时间区间内,区间长度取最短
|
||||
|
//3.时间区间内,区间长度相同,取最近结束时间
|
||||
|
List<TbsScheduleItemBudget> tmpList4Subject = new ArrayList<>(); |
||||
|
List<TbsScheduleItemBudget> tmpList4NotSubject = new ArrayList<>(); |
||||
|
for (TbsScheduleItemBudget scheduleItemBudget : list) { |
||||
|
Long budgetId = scheduleItemBudget.getBudgetId(); |
||||
|
if(wrapper.getBudgetSet4Subject().contains(budgetId)){ |
||||
|
tmpList4Subject.add(scheduleItemBudget); |
||||
|
}else{ |
||||
|
tmpList4NotSubject.add(scheduleItemBudget); |
||||
|
} |
||||
|
} |
||||
|
log.debug("活动【{}】匹配有科目预算数:{},无科目预算:{}",activity.getActivityCode() ,tmpList4Subject.size() , tmpList4NotSubject.size()); |
||||
|
if(CollectionUtil.isNotEmpty(tmpList4Subject)){ |
||||
|
CostSortWrapperUtil.sort4Condition(activity,tmpList4Subject,resultSortList,wrapper); |
||||
|
} |
||||
|
if(CollectionUtil.isNotEmpty(tmpList4NotSubject)){ |
||||
|
CostSortWrapperUtil.sort4Condition(activity,tmpList4NotSubject,resultSortList,wrapper); |
||||
|
} |
||||
|
} |
||||
|
if(resultSortList.size()!=list.size()){ |
||||
|
log.error("排序有误"); |
||||
|
Assert.throwEx("排序有误"); |
||||
|
} |
||||
|
return resultSortList; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 按有无条件分割 |
||||
|
* @param activity |
||||
|
* @param list |
||||
|
* @param result |
||||
|
* @param wrapper |
||||
|
*/ |
||||
|
private static void sort4Condition(TbsActivity activity,List<TbsScheduleItemBudget> list,List<TbsScheduleItemBudget> result,CostSortWrapper wrapper) { |
||||
|
List<TbsScheduleItemBudget> tmpList4Condition = new ArrayList<>(); |
||||
|
List<TbsScheduleItemBudget> tmpList4NoCondition = new ArrayList<>(); |
||||
|
for (TbsScheduleItemBudget scheduleItemBudget : list) { |
||||
|
Long budgetId = scheduleItemBudget.getBudgetId(); |
||||
|
if(wrapper.getBudgetSet4Condition().contains(budgetId)){ |
||||
|
tmpList4Condition.add(scheduleItemBudget); |
||||
|
}else{ |
||||
|
tmpList4NoCondition.add(scheduleItemBudget); |
||||
|
} |
||||
|
} |
||||
|
log.debug("活动【{}】匹配有科目预算数:{},无科目预算:{}",activity.getActivityCode() ,tmpList4Condition.size() , tmpList4NoCondition.size()); |
||||
|
if(CollectionUtil.isNotEmpty(tmpList4Condition)){ |
||||
|
CostSortWrapperUtil.sort4GoodsCondition(activity,tmpList4Condition,result,wrapper); |
||||
|
} |
||||
|
if(CollectionUtil.isNotEmpty(tmpList4NoCondition)){ |
||||
|
CostSortWrapperUtil.sort4NotGoodsCondition(tmpList4NoCondition,result,wrapper); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static void sort4GoodsCondition(TbsActivity activity,List<TbsScheduleItemBudget> list,List<TbsScheduleItemBudget> result,CostSortWrapper wrapper){ |
||||
|
if(CollectionUtil.isNotEmpty(list)){ |
||||
|
List<TbsBudgetCondition> activityCostConditions = wrapper.getActivityCostConditionsOfSort().get(activity.getId()); |
||||
|
Collections.sort(activityCostConditions, (o1, o2) -> { |
||||
|
int len1 = o1.getTargetLevelPathIds().split("_").length; |
||||
|
int len2 = o2.getTargetLevelPathIds().split("_").length; |
||||
|
if(len1==len2){ |
||||
|
List<TbsScheduleItemBudget> scheduleItemBudgets = list; |
||||
|
Long day1 = null; |
||||
|
Long day2 = null; |
||||
|
for (TbsScheduleItemBudget itemBudget : scheduleItemBudgets) { |
||||
|
if(itemBudget.getBudgetId().equals(o1.getBudgetId())){ |
||||
|
day1 = Duration.between(itemBudget.getStartDate(), itemBudget.getEndDate()).toDays(); |
||||
|
}else if (itemBudget.getBudgetId().equals(o2.getBudgetId())){ |
||||
|
day2 = Duration.between(itemBudget.getStartDate(), itemBudget.getEndDate()).toDays(); |
||||
|
} |
||||
|
} |
||||
|
log.debug("活动【{}】 时间区间相同,day1:{} , day2:{}",activity.getActivityCode(),day1,day2); |
||||
|
if(day1!=null&&day2!=null){ |
||||
|
return (int) (day1-day2); |
||||
|
} |
||||
|
} |
||||
|
return len2 - len1; |
||||
|
}); |
||||
|
List<TbsScheduleItemBudget> newSortList = new ArrayList<>(); |
||||
|
for (TbsBudgetCondition costCondition : activityCostConditions) { |
||||
|
for (TbsScheduleItemBudget scheduleItemBudget : list) { |
||||
|
if(scheduleItemBudget.getBudgetId().equals(costCondition.getBudgetId())){ |
||||
|
newSortList.add(scheduleItemBudget); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
if(newSortList.size()!=list.size()){ |
||||
|
log.error("排序有误"); |
||||
|
Assert.throwEx("排序有误"); |
||||
|
} |
||||
|
result.addAll(newSortList); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static void sort4NotGoodsCondition(List<TbsScheduleItemBudget> list,List<TbsScheduleItemBudget> result,CostSortWrapper wrapper){ |
||||
|
if(CollectionUtil.isNotEmpty(list)){ |
||||
|
Collections.sort(list, (o1, o2) -> { |
||||
|
Long day1 = Duration.between(o1.getStartDate(), o1.getEndDate()).toDays(); |
||||
|
Long day2 = Duration.between(o2.getStartDate(), o2.getEndDate()).toDays(); |
||||
|
log.debug("时间区间相同,day1:{} , day2:{}",day1,day2); |
||||
|
return (int) (day1-day2); |
||||
|
}); |
||||
|
result.addAll(list); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue