后端处理
使用技术点, 对象的拷贝
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);
}