优化一下这段代码: public PaymentForm selectPaymentProduct(Long sampleId) { PaymentForm form = new PaymentForm(); String key = configService.selectConfigByKey("sys.sample.payment"); if (!StringUtils.isEmpty(key) && Convert.toBool(key)) { CoreSample sample = selectCoreSampleBySampleId(sampleId); if(sample != null) { //产品类型 String projectType = sample.getSampleProjectType(); //产品价格 String productPrice = configService.selectConfigByKey(SampleEnum.ProjectType.getAmountKey(projectType)); form.setSampleNo(sample.getSampleNo()); form.setProductName(SampleEnum.ProjectType.getDescription(projectType)); form.setProductPrice(productPrice); return form; } } return null; }
时间: 2024-02-10 21:21:25 浏览: 130
可以进行如下优化:
1. 将configService.selectConfigByKey("sys.sample.payment")和configService.selectConfigByKey(SampleEnum.ProjectType.getAmountKey(projectType))的结果缓存起来,避免重复查询。
2. 将代码进行简化,避免嵌套和重复判断,如下所示:
public PaymentForm selectPaymentProduct(Long sampleId) {
String paymentConfig = configService.getConfig("sys.sample.payment");
if (StringUtils.isEmpty(paymentConfig) || !Convert.toBool(paymentConfig)) {
return null;
}
CoreSample sample = selectCoreSampleBySampleId(sampleId);
if (sample == null) {
return null;
}
String projectType = sample.getSampleProjectType();
String productPriceConfig = configService.getConfig(SampleEnum.ProjectType.getAmountKey(projectType));
PaymentForm form = new PaymentForm();
form.setSampleNo(sample.getSampleNo());
form.setProductName(SampleEnum.ProjectType.getDescription(projectType));
form.setProductPrice(productPriceConfig);
return form;
}
阅读全文