3 changed files with 222 additions and 7 deletions
@ -0,0 +1,63 @@ |
|||
package com.qs.serve.modules.tzc.common.dto; |
|||
|
|||
import com.qs.serve.common.util.CollectionUtil; |
|||
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.*; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* 用于排序 |
|||
* @author YenHex |
|||
* @since 2023/5/18 |
|||
*/ |
|||
@Data |
|||
public class PolicySortWrapper { |
|||
|
|||
Set<Long> budgetSet4Subject = new HashSet<>(); |
|||
Set<Long> budgetSet4NoSubject = new HashSet<>(); |
|||
|
|||
Set<Long> budgetSet4Condition = new HashSet<>(); |
|||
Set<Long> budgetSet4NoCondition = new HashSet<>(); |
|||
|
|||
|
|||
Map<Long,List<TbsBudgetCondition>> policyItemCostConditionsOfSort = new HashMap<>(); |
|||
Map<Long,List<Long>> policyItemAllowBudgetId; |
|||
|
|||
/** |
|||
* sort of timeline,filter timeline |
|||
* key is policy_item_id, value is schedule list |
|||
*/ |
|||
Map<Long,List<TbsScheduleItemBudget>> policyItemScheduleItemMap = new HashMap<>(); |
|||
|
|||
|
|||
public void putPolicyItemCondition(Long policyItemId, List<TbsBudgetCondition> list){ |
|||
policyItemCostConditionsOfSort.put(policyItemId, 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.tzc.common.util; |
|||
|
|||
import com.qs.serve.common.util.Assert; |
|||
import com.qs.serve.common.util.CollectionUtil; |
|||
import com.qs.serve.modules.tbs.entity.TbsBudgetCondition; |
|||
import com.qs.serve.modules.tbs.entity.TbsScheduleItemBudget; |
|||
import com.qs.serve.modules.tzc.common.dto.PolicySortWrapper; |
|||
import com.qs.serve.modules.tzc.entity.TzcPolicyItem; |
|||
import lombok.Data; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
|
|||
import java.time.Duration; |
|||
import java.util.ArrayList; |
|||
import java.util.Collections; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 用于排序 |
|||
* @author YenHex |
|||
* @since 2023/5/18 |
|||
*/ |
|||
@Slf4j |
|||
@Data |
|||
public class PolicySortWrapperUtil { |
|||
|
|||
public static List<TbsScheduleItemBudget> initializeSort(TzcPolicyItem policyItem, List<TbsScheduleItemBudget> list, PolicySortWrapper 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("政策项【{}】匹配有科目预算数:{},无科目预算:{}",policyItem.getPolicyItemCode() ,tmpList4Subject.size() , tmpList4NotSubject.size()); |
|||
if(CollectionUtil.isNotEmpty(tmpList4Subject)){ |
|||
PolicySortWrapperUtil.sort4Condition(policyItem,tmpList4Subject,resultSortList,wrapper); |
|||
} |
|||
if(CollectionUtil.isNotEmpty(tmpList4NotSubject)){ |
|||
PolicySortWrapperUtil.sort4Condition(policyItem,tmpList4NotSubject,resultSortList,wrapper); |
|||
} |
|||
} |
|||
if(resultSortList.size()!=list.size()){ |
|||
log.error("排序有误"); |
|||
Assert.throwEx("排序有误"); |
|||
} |
|||
return resultSortList; |
|||
} |
|||
|
|||
/** |
|||
* 按有无条件分割 |
|||
* @param policyItem |
|||
* @param list |
|||
* @param result |
|||
* @param wrapper |
|||
*/ |
|||
private static void sort4Condition(TzcPolicyItem policyItem,List<TbsScheduleItemBudget> list,List<TbsScheduleItemBudget> result,PolicySortWrapper 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("活动【{}】匹配有科目预算数:{},无科目预算:{}",policyItem.getPolicyItemCode() ,tmpList4Condition.size() , tmpList4NoCondition.size()); |
|||
if(CollectionUtil.isNotEmpty(tmpList4Condition)){ |
|||
PolicySortWrapperUtil.sort4GoodsCondition(policyItem,tmpList4Condition,result,wrapper); |
|||
} |
|||
if(CollectionUtil.isNotEmpty(tmpList4NoCondition)){ |
|||
PolicySortWrapperUtil.sort4NotGoodsCondition(tmpList4NoCondition,result,wrapper); |
|||
} |
|||
} |
|||
|
|||
private static void sort4GoodsCondition(TzcPolicyItem policyItem,List<TbsScheduleItemBudget> list,List<TbsScheduleItemBudget> result,PolicySortWrapper wrapper){ |
|||
if(CollectionUtil.isNotEmpty(list)){ |
|||
List<TbsBudgetCondition> activityCostConditions = wrapper.getPolicyItemCostConditionsOfSort().get(policyItem.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:{}",policyItem.getPolicyItemCode(),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,PolicySortWrapper 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