背景:以下没结构化代码之前,定时器同步店铺信息的代码。
结构化的思想:SRP(单一职责),一个方法是做一件事,层次是相关的,逻辑和数据操作进行拆分,数据操作从业务流程上定义分层的步骤
结论:下面代码不规范,命名上不能见名知意,没有结构化代码,逻辑处理和数据库操作堆积到一起,还有实体的赋值,本身是实体的职责,却扔到了业务逻辑,所有东西堆积到一起,业务代码量少的时候还好,但是一旦业务代码量多,没有分层会造成难以理解逻辑,看代码的成本很大。
@Service
public class SynVCStoreAppService {@Autowiredprivate VCStoreClient vcStoreClient;@AutowiredIMultiPlatformStoreInfoService iMultiPlatformStoreInfoService;/*** 同步店铺信息* 全量同步** @return 结果*/public Integer syncAll() {//调用客户端获取店铺信息LxErpMessage<List<VCStore>> lxErpMessage = vcStoreClient.list(MultiPlatformStoreInfoRequest.builder().offset(0).length(200).build());List<VCStore> list = lxErpMessage.getData();//判断是否存在数据if(lxErpMessage.getTotal()==0){return 0;}// 先查询数量int count = lxErpMessage.getTotal();// 每页查询数量int length = 200;List<VCStore> updataList = list;// 根据count 分批查询数据,如果是超过200则继续查询外部数据进行同步List<VCStore> list1 = null;if(count>200){for (int i = 200; i < count; i += length) {list1 = vcStoreClient.list(MultiPlatformStoreInfoRequest.builder().offset(i).length(length).build()).getData();}updataList.addAll(list1);}MultiPlatformStore mpsi = null;List<MultiPlatformStore> multiPlatformStoreList = new ArrayList<>();for (VCStore vcStore : updataList) {mpsi = new MultiPlatformStore();//因为是定时器,所以写死创建人信息,其他设置省略mpsi.setCreateId(1l);mpsi.setCreateBy("admin");mpsi.setCreateName("管理员");multiPlatformStoreList.add(mpsi);}iMultiPlatformStoreInfoService.saveOrUpdateBatch(multiPlatformStoreList);return count;}
}
重点:以下是结构化后的代码,只需要看syncAll()这个方法即可,其他都是分层处理,把逻辑处理和业务分离,以单一原则进行划分代码,同时也有了层次感,一目了然。在一层就是逻辑处理分配,在分批里调用具体的业务处理,业务里继续划分业务步骤进行数据处理
@Slf4j
@Service
public class SynVCStoreAppService {private int length = 200;@Autowiredprivate VCStoreClient vcStoreClient;@AutowiredIMultiPlatformStoreInfoService iMultiPlatformStoreInfoService;/*** 同步店铺信息* 全量同步** @return 结果*/public Integer syncAll() {log.info("同步店铺--开始");int offset = 0;int total = doExcute(offset);while (total >this.length) {offset += this.length;doExcute(offset);total -= this.length;}log.info("同步店铺--结束");return total;}//分批获取店铺信息private int doExcute(int offset) {LxErpMessage<List<VCStoreVO>> erpVCStoreResponse = getErpVCStore(offset);List<MultiPlatformStore> multiPlatformStores = getMultiPlatformStoreByErpVCStoreResponse(erpVCStoreResponse);log.info("multiPlatformStores:{}",multiPlatformStores);iMultiPlatformStoreInfoService.saveOrUpdateBatch(multiPlatformStores);return erpVCStoreResponse.getTotal();}//调用第三方接口LxErpMessage<List<VCStoreVO>> getErpVCStore(int offset){return vcStoreClient.list(MultiPlatformStoreInfoRequest.builder().offset(offset).length(this.length).build());}
//处理返回的数据,这里存在代码严谨性,直接根据返回code再进行处理数据,而不是直接处理List<MultiPlatformStore> getMultiPlatformStoreByErpVCStoreResponse(LxErpMessage<List<VCStoreVO>> erpVCStoreResponse){List<MultiPlatformStore> multiPlatformStores = new ArrayList<>();if(erpVCStoreResponse.isSuccess()&& !CollectionUtils.isEmpty(erpVCStoreResponse.getData())){erpVCStoreResponse.getData().forEach(e->{multiPlatformStores.add(e.getMultiPlatformStoreByVCStore());});}return multiPlatformStores;}
}
那些赋值的for循环放到实体里进行,创建实体的时候直接调用
public class VCStoreVO {
public MultiPlatformStore getMultiPlatformStoreByVCStore(){MultiPlatformStore multiPlatformStore = new MultiPlatformStore();multiPlatformStore.setCreateBy("admin");multiPlatformStore.setCreateId(1L);multiPlatformStore.setCreateBy("admin");//其他省略return multiPlatformStore;}}