JAVA---数据导入-后端

后端处理

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值