如何写出优雅的代码
如何写出优雅的代码
如何写出优雅的代码,在我看来 首先就是要 调理清晰,单一原则,一个方法只做一件事,一步一步走。
代码实例
@Transactional(rollbackFor = Exception.class)
@Override
public Boolean copyFlow(FlowCopyDTO flowCopyDTO) {
Long flowId = flowCopyDTO.getFlowId();
SrmFlow flow = getById(flowId);
CustomAssert.isTrue(null != flow,"流程不存在");
CustomAssert.isTrue(Objects.equals(FlowSourceEnum.PLATFORM.getCode(),flow.getSource()),"只有平台级流程才允许被复制");
Integer flowType = flow.getFlowType();
// 采购执行规则
if(Objects.equals(FlowTypeEnum.PURCHASE_EXECUTION.getCode(),flowType)){
copyPurchaseExecution(flow,flowCopyDTO);
}
// 供应商管理配置的规则通过
if(Objects.equals(FlowTypeEnum.SUPPLIER_MANAGEMENT.getCode(),flowType)){
copySupplierManagement(flow,flowCopyDTO);
}
return true;
}
@Override
public FlowTO detailFlow(FlowDetailDTO dto) {
FlowTO flow = flowConvert.toFlowTO(baseMapper.selectByFlowIdAndOrgId(dto.getId(), dto.getOrganizationId()));
flow.setFlowTypeName(FlowTypeEnum.getFlowTypeEnum(flow.getFlowType()).getDesc());
flow.setCntTypeName(CntTypeEnum.getByCode(flow.getCntType()).getDesc());
flow.setSourceName(FlowSourceEnum.getEnumByCode(flow.getSource()).getDesc());
flow.setCompanyList(flowCompanyConvert.toFlowCompanyTOList(srmFlowCompanyService.getFlowCompanyList(dto.getId())));
return flow;
}
/**
* copy 供应商管理配置
* @param flow
* @param flowCopyDTO
*/
private void copySupplierManagement(SrmFlow flow,FlowCopyDTO flowCopyDTO){
// 供应商管理配置的规则通过 流程分类+公司维度 两个条件控制唯一性,
// 即在同一个租户的公司维度下,同一个公司仅允许存在1条规则配置流程
String companyIds = flowCopyDTO.getCompanyIds();
if(StringUtils.isNotBlank(companyIds)){
Set<Long> companyIdList = Arrays.stream(companyIds.split(","))
.map(Long::valueOf)
.collect(Collectors.toSet());
Long organizationId = flowCopyDTO.getOrganizationId();
Long flowId = flowCopyDTO.getFlowId();
LambdaQueryWrapper<SrmFlowCompany> lambdaQueryWrapper = Wrappers.lambdaQuery(SrmFlowCompany.class)
.eq(SrmFlowCompany::getFlowId, flowId)
.eq(SrmFlowCompany::getOrganizationId, organizationId)
.in(SrmFlowCompany::getCompanyId, companyIdList);
long count = srmFlowCompanyService.count(lambdaQueryWrapper);
CustomAssert.isTrue(count <= 0,"同一个公司也仅允许存在1条规则配置流程");
}
copy(flow,flowCopyDTO);
}
/**
* copy采购执行规则
* @param flow
* @param flowCopyDTO
*/
private void copyPurchaseExecution(SrmFlow flow,FlowCopyDTO flowCopyDTO){
//同一个租户,来源为“租户级”的只允许复制/创建一条采购执行规则配置
Long organizationId = flowCopyDTO.getOrganizationId();
LambdaQueryWrapper<SrmFlow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(SrmFlow::getOrganizationId,organizationId);
lambdaQueryWrapper.eq(SrmFlow::getFlowType,FlowTypeEnum.PURCHASE_EXECUTION.getCode());
long count = count(lambdaQueryWrapper);
CustomAssert.isTrue(count <= 0,"同一个租户的只允许创建一条采购执行规则");
copy(flow,flowCopyDTO);
}
/**
* 复制数据
* @param flow
* @param flowCopyDTO
*/
private void copy(SrmFlow flow,FlowCopyDTO flowCopyDTO){
// 复制流程
Long flowId = flow.getId();
SrmFlow newFlow = copyFlow(flow, flowCopyDTO);
// 复制公司
copyFlowCompany(flowId,newFlow,flowCopyDTO);
// 复制节点
Map<String, Long> flowIdNodeIdAndIdMap = copyFlowNode(flowId, newFlow, flowCopyDTO);
// 复制规则执行配置数据
copyFlowNodeRuleData(flowId,newFlow,flowCopyDTO,flowIdNodeIdAndIdMap);
// 复制流程规则
copyFlowRule(flowId,newFlow,flowCopyDTO,flowIdNodeIdAndIdMap);
// 复制流程规则配置
copyFlowRuleConfig(flowId,newFlow,flowCopyDTO,flowIdNodeIdAndIdMap);
}
@Transactional(rollbackFor = Exception.class)
public void insert(CakeOrderDO entity) {
//创建订单
saveOrder(entity);
//创建订单项
saveOrderItems(entity);
//收货地址
saveDeliveryAddress(entity);
//物流信息
saveDeliveryOrder(entity);
//订单日志
saveOrderStatusHistory(entity);
}
思考
一辈子只做一件事 也很酷!
elk