【通过数据库配置实现一个策略模式】1、策略思想
List sortPolicyCode = getSortPolicyCode(orderType,warehouseId);
if(CollectionUtil.isEmpty(sortPolicyCode)){
return level2Inventory;
}
level2Inventory.sort((a,b)->{
int ret = 0;
for (String s : sortPolicyCode) {
//其中s是spring的IOC的注解
ILevel2InventorySorter sorter = SpringBeanFactory.getBean(s,ILevel2InventorySorter.class);
ret = sorter.calculateSortNumber(a, b, orderType, warehouseId);
if(ret != 0){
break;
}
}
return ret;
});
return level2Inventory;
/**
通过
**/
private ListgetSortPolicyCode(String orderType,Long warehouseId) {
String format = String.format(formatKey, warehouseId, orderType);
List policyCodes = orderTypeWithPolicyCode.get(format);
if(CollectionUtil.isEmpty(policyCodes)){
List policyDTOS = policyService.selectByGroupCode(warehouseId, PickingPolicyCodes.LEVEL2_INVENTORY_SORT_GROUP_CODE);
if(CollectionUtil.isEmpty(policyDTOS)){
throw new WesPickingException(WesPickingErrorCode.POLICY_GROUP_CAN_NOT_FIND_POLICY.getCode(),WesPickingErrorCode.POLICY_GROUP_CAN_NOT_FIND_POLICY.getDesc(),PickingPolicyCodes.LEVEL2_INVENTORY_SORT_GROUP_CODE);
}
policyCodes = policyDTOS.stream().map(PolicyDTO::getPolicyCode).collect(Collectors.toList());
orderTypeWithPolicyCode.putIfAbsent(format,policyCodes);
}
return policyCodes;
}
@Override
public List selectByGroupCode(Long warehouseId, String policyGroupCode) {
return DozerBeanUtil.mapList(this.policyManager.selectByGroupCode(warehouseId, policyGroupCode), PolicyDTO.class);
}
public List selectByGroupCode(Long warehouseId, String policyGroupCode) {
return this.mapper.selectByGroupCode(warehouseId, policyGroupCode);
}
SELECT p.* from evo_wes_basic.policy p , evo_wes_basic.policy_group g, evo_wes_basic.policy_group_item i
where g.id = i.policy_group_id and i.policy_id = p.id
and g.warehouse_id = #{warehouseId} and g.policy_group_code = #{policyGroupCode}
order by i.sort_num asc
//比如按日期的排序
@Override
public int calculateSortNumber(Level2InventoryFlatDTO a, Level2InventoryFlatDTO b, String orderType, Long warehouseId) {
LotCompareReceivedDate input = new LotCompareReceivedDate();
input.setOrderType(orderType);
List
int ret = 0;
if(CollectionUtil.isNotEmpty(results)){
for (LotCompareReceivedDatePolicyResult result : results) {
//收货日期
Object af1 = FiledValueHelper.getFieldByClass(result.getFiledName(), a);
Object af2 = FiledValueHelper.getFieldByClass(result.getFiledName(), b);
//>7 -1
try {
if(af1 !=null && af2 != null){
//取到收货日期1,和收货日期2
Date dateOne = DateUtil.str2DateTime(af1.toString(),DateUtil.DEFAULT_DATE_TIME_FORMAT);
Date dateTwo = DateUtil.str2DateTime(af2.toString(),DateUtil.DEFAULT_DATE_TIME_FORMAT);
Long timeOne = DateUtil.diffMilli(dateOne);
Long timeTwo = DateUtil.diffMilli(dateTwo);
long receivedDate = result.getReceivedDate()*86400000L;
LOGGER.info("inventoryLevel2 sort by lotAtt8 ");
ret = compareTo(timeTwo,timeOne,receivedDate);
}else{
LOGGER.warn("date1:{} and date1:{}",af1,af2);
}
} catch (Exception e) {
LOGGER.error("date convertis error :{} ",e.getMessage());
}
}
}
return ret;
}
@Override
public
PolicyDTO policyDTO = policyEngine.applyPolicy(warehouseId, policyCode, inputObj);
return PolicyUtil.convertResult(policyDTO, resultType);
}
/**
*
* @param warehouseId 仓库ID
* @param policyCode 策略代码
* @param inputObj 出库单类型
* @return
*/
@SuppressWarnings({ "unchecked" })
public PolicyDTO applyPolicy(Long warehouseId, String policyCode, Object inputObj) {
if (inputObj == null) {
throw new WesException(WesErrorCode.METHOD_ARGUMENT_CANNOT_NULL, "inputObj");
}
PolicyDTO policyDTO = policyCache.get(policyCode);
if (policyDTO == null) {
policyDTO = policyService.selectOneWithRuleByCode(warehouseId, policyCode);
if (policyDTO == null) {
throw new WesException(PolicyErrorCode.POLICY_NOT_FOUND, policyCode);
}
this.cachePolicy(policyDTO);
}
this.validateInput(policyDTO, inputObj);
KieBase kieBase = kieBaseCache.get(policyCode);
StatelessKieSession kSession = kieBase.newStatelessKieSession();
List
BatchExecutionCommand batchCommand = CommandFactory.newBatchExecution(commands);
Object input = this.convertInput(policyDTO, inputObj);
log.info("drools.input: " + input);
commands.add(CommandFactory.newInsert(new Pair
推荐阅读
- 封装
- 多态和对象的转型
- 记一次根据实体类的属性名来给属性名赋值,根据属性名得到属性名的值,其中使用的是JsonProperty属性名
- 记一次代码的回滚到某一次
- 牛客Java专项练习错题总结
- java基础类库学习 java.sql(4)如何处理BLOB类型数据
- #|BigDecimal类中的double类型值的加减问题-java
- java开发|如何在Java中引入和存储大数量级的变量并进行计算,通过数组来实现
- Java基础知识之数据类型和运算符