|
|
@@ -0,0 +1,238 @@
|
|
|
+package org.dromara.customer.listener;
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.http.HtmlUtil;
|
|
|
+import cn.idev.excel.context.AnalysisContext;
|
|
|
+import cn.idev.excel.event.AnalysisEventListener;
|
|
|
+import jakarta.validation.ConstraintViolation;
|
|
|
+import jakarta.validation.ConstraintViolationException;
|
|
|
+import org.dromara.common.core.exception.ServiceException;
|
|
|
+import org.dromara.common.core.utils.SpringUtils;
|
|
|
+import org.dromara.common.core.utils.StreamUtils;
|
|
|
+import org.dromara.common.core.utils.ValidatorUtils;
|
|
|
+import org.dromara.common.excel.core.ExcelListener;
|
|
|
+import org.dromara.common.excel.core.ExcelResult;
|
|
|
+import org.dromara.common.satoken.utils.LoginHelper;
|
|
|
+import org.dromara.customer.domain.bo.CustomerInfoBo;
|
|
|
+import org.dromara.customer.domain.bo.CustomerSalesInfoBo;
|
|
|
+import org.dromara.customer.domain.vo.CustomerInfoImportVo;
|
|
|
+import org.dromara.customer.domain.vo.CustomerInfoVo;
|
|
|
+import org.dromara.customer.domain.vo.EnterpriseScaleVo;
|
|
|
+import org.dromara.customer.domain.vo.IndustryCategoryVo;
|
|
|
+import org.dromara.customer.service.ICustomerInfoService;
|
|
|
+import org.dromara.customer.service.IEnterpriseScaleService;
|
|
|
+import org.dromara.customer.service.IIndustryCategoryService;
|
|
|
+import org.dromara.system.api.RemoteProductTaxrateService;
|
|
|
+import org.dromara.system.api.domain.vo.RemoteProductTaxrateVo;
|
|
|
+import org.dromara.system.api.*;
|
|
|
+import org.dromara.system.api.domain.vo.*;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.dromara.common.mybatis.core.mapper.BaseMapperPlus.log;
|
|
|
+
|
|
|
+public class CustomerImportListener extends AnalysisEventListener<CustomerInfoImportVo> implements ExcelListener<CustomerInfoImportVo> {
|
|
|
+
|
|
|
+
|
|
|
+ private final ICustomerInfoService customerInfoService;
|
|
|
+ private RemoteComCompanyService remoteComCompanyService;
|
|
|
+
|
|
|
+ private RemoteCreditLevelService remoteCreditLevelService;
|
|
|
+
|
|
|
+ private RemoteComCurrencyService remoteComCurrencyService;
|
|
|
+
|
|
|
+ private RemoteComCustomerTypeService remoteComCustomerTypeService;
|
|
|
+
|
|
|
+ private RemoteProductTaxrateService remoteProductTaxrateService;
|
|
|
+
|
|
|
+ private RemoteComCustomerLevelService remoteComCustomerLevelService;
|
|
|
+
|
|
|
+ private RemoteComStaffService remoteComStaffService;
|
|
|
+
|
|
|
+ private IEnterpriseScaleService scaleService;
|
|
|
+
|
|
|
+ private IIndustryCategoryService industryCategoryService;
|
|
|
+
|
|
|
+ private final Boolean isUpdateSupport;
|
|
|
+
|
|
|
+ private final Long operUserId;
|
|
|
+
|
|
|
+ private int successNum = 0;
|
|
|
+ private int failureNum = 0;
|
|
|
+ private final StringBuilder successMsg = new StringBuilder();
|
|
|
+ private final StringBuilder failureMsg = new StringBuilder();
|
|
|
+
|
|
|
+ public CustomerImportListener(Boolean isUpdateSupport, RemoteComCompanyService remoteComCompanyService,
|
|
|
+ RemoteComCustomerLevelService remoteComCustomerLevelService,
|
|
|
+ RemoteCreditLevelService remoteCreditLevelService,
|
|
|
+ RemoteComCurrencyService remoteComCurrencyService,
|
|
|
+ RemoteComCustomerTypeService remoteComCustomerTypeService,
|
|
|
+ RemoteProductTaxrateService remoteProductTaxrateService,
|
|
|
+ RemoteComStaffService remoteComStaffService) {
|
|
|
+ this.customerInfoService = SpringUtils.getBean(ICustomerInfoService.class);
|
|
|
+ this.remoteComCompanyService = remoteComCompanyService;
|
|
|
+ this.remoteComCustomerLevelService = remoteComCustomerLevelService;
|
|
|
+ this.remoteCreditLevelService = remoteCreditLevelService;
|
|
|
+ this.remoteComCurrencyService = remoteComCurrencyService;
|
|
|
+ this.remoteComCustomerTypeService = remoteComCustomerTypeService;
|
|
|
+ this.remoteProductTaxrateService = remoteProductTaxrateService;
|
|
|
+ this.remoteComStaffService = remoteComStaffService;
|
|
|
+ this.scaleService = SpringUtils.getBean(IEnterpriseScaleService.class);
|
|
|
+ this.industryCategoryService = SpringUtils.getBean(IIndustryCategoryService.class);
|
|
|
+ this.isUpdateSupport = isUpdateSupport;
|
|
|
+ this.operUserId = LoginHelper.getLoginUser().getUserId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void invoke(CustomerInfoImportVo customerInfoImportVo, AnalysisContext analysisContext) {
|
|
|
+ CustomerInfoVo customerInfo = this.customerInfoService.selectCustomerByName(customerInfoImportVo.getCustomerName());
|
|
|
+ try {
|
|
|
+ // 校验必填字段
|
|
|
+ if (ObjectUtil.isEmpty(customerInfoImportVo.getCustomerNo())) {
|
|
|
+ failureNum++;
|
|
|
+ failureMsg.append("<br/>").append(failureNum).append("、第 ").append(analysisContext.readRowHolder().getRowIndex() + 1).append(" 行,客户编号不能为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isEmpty(customerInfoImportVo.getCustomerName())) {
|
|
|
+ failureNum++;
|
|
|
+ failureMsg.append("<br/>").append(failureNum).append("、第 ").append(analysisContext.readRowHolder().getRowIndex() + 1).append(" 行,客户名称不能为空");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ RemoteComCompanyVo remoteComCompanyVo = remoteComCompanyService.selectByCompanyName(customerInfoImportVo.getCompanyName());
|
|
|
+ RemoteComCustomerLevelVo remoteComCustomerLevelVo = remoteComCustomerLevelService.selectCustomerLeveByLevelName(customerInfoImportVo.getCustomerLevelName());
|
|
|
+ RemoteComCurrencyVo remoteComCurrencyVo = remoteComCurrencyService.selectByCurrencyName(customerInfoImportVo.getDealCurrency());
|
|
|
+ RemoteComCustomerTypeVo remoteComCustomerTypeVo = remoteComCustomerTypeService.selectCustomerTypeByTypeName(customerInfoImportVo.getCustomerTypeName());
|
|
|
+ RemoteProductTaxrateVo remoteProductTaxrateVo = remoteProductTaxrateService.selectByTaxrateNo(customerInfoImportVo.getRateName());
|
|
|
+ EnterpriseScaleVo enterpriseScaleVo = scaleService.selectEnterpriseScaleByScaleName(customerInfoImportVo.getEnterpriseScale());
|
|
|
+ IndustryCategoryVo industryCategoryVo = industryCategoryService.selectIndustryCategoryByCategoryName(customerInfoImportVo.getIndustryCategory());
|
|
|
+ RemoteCreditLevelVo remoteCreditLevelVo = remoteCreditLevelService.selectByCreditLevelName(customerInfoImportVo.getCreditLevel().equals("0") ? "A"
|
|
|
+ : customerInfoImportVo.getCreditLevel().equals("1") ? "B" : customerInfoImportVo.getCreditLevel().equals("2") ? "C"
|
|
|
+ : customerInfoImportVo.getCreditLevel().equals("3") ? "D" : customerInfoImportVo.getCreditLevel().equals("4") ? "E"
|
|
|
+ : customerInfoImportVo.getCreditLevel().equals("5") ? "F" : "G");
|
|
|
+ RemoteComStaffVo SalesPerson = remoteComStaffService.selectStaffByStaffName(customerInfoImportVo.getSalesPersonName());
|
|
|
+ RemoteComStaffVo ServiceStaff = remoteComStaffService.selectStaffByStaffName(customerInfoImportVo.getServiceStaffName());
|
|
|
+ CustomerSalesInfoBo customerSalesInfo = new CustomerSalesInfoBo();
|
|
|
+ if (ObjectUtil.isNotEmpty(SalesPerson)) {
|
|
|
+ customerSalesInfo.setSalesPersonId(SalesPerson.getStaffId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(ServiceStaff)) {
|
|
|
+ customerSalesInfo.setServiceStaffId(ServiceStaff.getStaffId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteCreditLevelVo)) {
|
|
|
+ customerSalesInfo.setCreditLevelId(remoteCreditLevelVo.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证是否存在这个客户
|
|
|
+ if (ObjectUtil.isNull(customerInfo)) {
|
|
|
+
|
|
|
+ CustomerInfoBo customerInfoBo = BeanUtil.toBean(customerInfoImportVo, CustomerInfoBo.class);
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCompanyVo)) {
|
|
|
+ customerInfoBo.setBelongCompanyId(remoteComCompanyVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCustomerLevelVo)) {
|
|
|
+ customerInfoBo.setCustomerLevelId(remoteComCustomerLevelVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCurrencyVo)) {
|
|
|
+ customerInfoBo.setDealCurrencyId(remoteComCurrencyVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCustomerTypeVo)) {
|
|
|
+ customerInfoBo.setCustomerTypeId(remoteComCustomerTypeVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(enterpriseScaleVo)) {
|
|
|
+ customerInfoBo.setEnterpriseScaleId(enterpriseScaleVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(industryCategoryVo)) {
|
|
|
+ customerInfoBo.setIndustryCategoryId(industryCategoryVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteProductTaxrateVo)) {
|
|
|
+ customerInfoBo.setRateId(remoteProductTaxrateVo.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ ValidatorUtils.validate(customerInfoBo);
|
|
|
+ customerInfoBo.setCreateBy(operUserId);
|
|
|
+ customerInfoBo.setCustomerSalesInfoBo(customerSalesInfo);
|
|
|
+ customerInfoService.addCustomerInfo(customerInfoBo);
|
|
|
+ successNum++;
|
|
|
+ successMsg.append("<br/>").append(successNum).append("、客户名称 ").append(customerInfoBo.getCustomerName()).append(" 导入成功");
|
|
|
+ } else if (isUpdateSupport) {
|
|
|
+ Long customerId = customerInfo.getId();
|
|
|
+ CustomerInfoBo customerInfoBo = BeanUtil.toBean(customerInfoImportVo, CustomerInfoBo.class);
|
|
|
+ customerInfoBo.setId(customerId);
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCompanyVo)) {
|
|
|
+ customerInfoBo.setBelongCompanyId(remoteComCompanyVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCustomerLevelVo)) {
|
|
|
+ customerInfoBo.setCustomerLevelId(remoteComCustomerLevelVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCurrencyVo)) {
|
|
|
+ customerInfoBo.setDealCurrencyId(remoteComCurrencyVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteComCustomerTypeVo)) {
|
|
|
+ customerInfoBo.setCustomerTypeId(remoteComCustomerTypeVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(enterpriseScaleVo)) {
|
|
|
+ customerInfoBo.setEnterpriseScaleId(enterpriseScaleVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(industryCategoryVo)) {
|
|
|
+ customerInfoBo.setIndustryCategoryId(industryCategoryVo.getId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(remoteProductTaxrateVo)) {
|
|
|
+ customerInfoBo.setRateId(remoteProductTaxrateVo.getId());
|
|
|
+ }
|
|
|
+ ValidatorUtils.validate(customerInfoBo);
|
|
|
+
|
|
|
+ customerInfoBo.setUpdateBy(operUserId);
|
|
|
+ customerInfoBo.setCustomerSalesInfoBo(customerSalesInfo);
|
|
|
+ customerInfoService.updateCustomerInfo(customerInfoBo);
|
|
|
+ successNum++;
|
|
|
+ successMsg.append("<br/>").append(successNum).append("、客户名称 ").append(customerInfoBo.getCustomerName()).append(" 更新成功");
|
|
|
+ } else {
|
|
|
+ failureNum++;
|
|
|
+ failureMsg.append("<br/>").append(failureNum).append("、客户名称 ").append(customerInfo.getCustomerName()).append(" 已存在");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ failureNum++;
|
|
|
+ String msg = "<br/>" + failureNum + "、客户名称 " + HtmlUtil.cleanHtmlTag(customerInfoImportVo.getCustomerName()) + " 导入失败:";
|
|
|
+ String message = e.getMessage();
|
|
|
+ if (e instanceof ConstraintViolationException cvException) {
|
|
|
+ message = StreamUtils.join(cvException.getConstraintViolations(), ConstraintViolation::getMessage, ", ");
|
|
|
+ }
|
|
|
+ failureMsg.append(msg).append(message);
|
|
|
+ log.error(msg, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ExcelResult<CustomerInfoImportVo> getExcelResult() {
|
|
|
+ return new ExcelResult<CustomerInfoImportVo>() {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getAnalysis() {
|
|
|
+ if (failureNum > 0) {
|
|
|
+ failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
|
|
|
+ throw new ServiceException(failureMsg.toString());
|
|
|
+ } else {
|
|
|
+ successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
|
|
|
+ }
|
|
|
+ return successMsg.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<CustomerInfoImportVo> getList() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<String> getErrorList() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|