後端處理
使用技術點, 對象的拷貝
BeanUtils.copyProperties(A, B); A-->B,
PropertyUtils.copyProperties(A, B); B--A
兩個JavaBean的同名屬性為不同類型時,在支援的資料類型範圍内進行轉換,
而BeanUtils不支援這個功能,但是速度會更快一些
關鍵代碼
@ResponseBody
@RequiresPermissions("customer:import:do")
@PostMapping("/import/do")
public AjaxResult importDo(@Valid ImportCustomerImportDoForm form, BindingResult bindingResult) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
checkBindPageParam(bindingResult);
int dataCount = form.getFileData().size() - 1;
if (dataCount <= BaseConstant.ZERO) {
return errorLog(module, OperationTypeEnum.IMPORT, ResultEnum.IMPORT_DATA_NULL);
}
Integer totalLimit = Integer.parseInt(ConfigService.getByConfigKey(ConfigConstant.SYS_SINGLE_IMPORT_TOTAL_LIMIT));
if (dataCount > totalLimit) {
return errorLog(module, OperationTypeEnum.IMPORT, ResultEnum.IMPORT_TOTAL_LIMIT.getMessage() + totalLimit);
}
log.info("[客戶導入] 開始導入, 總條數:" + dataCount);
int successTotal = 0;
int errorTotal = 0;
// 儲存導入的客戶資訊
List<ImportCustomerVo> importCustomerVos = new ArrayList<>();
for (int i = 1; i <= dataCount; i++) {
ImportCustomerVo importCustomerVo = new ImportCustomerVo();
PropertyUtils.copyProperties(importCustomerVo, form.getFileData().get(i));
if (form.getFileData().get(i).containsKey("gender")) {
importCustomerVo.setGender(form.getFileData().get(i).get("gender"));
}
CrmCustomer customer = new CrmCustomer();
BeanUtils.copyProperties(form, customer);
BeanUtils.copyProperties(importCustomerVo, customer);
if (importCustomerService.checkImport(customer, importCustomerVo, importCustomerVos).isError()) {
errorTotal++;
continue;
}
if (service.insert(customer) > BaseConstant.ZERO) {
importCustomerVo.setImportResult("已導入");
customerOperationLogService.insert(form.getLeaderId(), customer.getId(), CustomerOperationTypeEnum.IMPORT.getCode());
successTotal++;
} else {
errorTotal++;
}
// 統一添加
importCustomerVos.add(importCustomerVo);
}
String msg = "資料導入"+ dataCount +"條,成功("+successTotal +"), 失敗("+ errorTotal +")";
if (successTotal > BaseConstant.ZERO) {
OperationLogService.success(module, OperationTypeEnum.IMPORT, msg);
return success(msg, importCustomerVos);
}
OperationLogService.error(module, OperationTypeEnum.IMPORT, msg);
return error(msg, importCustomerVos);
}