|
|
@@ -1,21 +1,33 @@
|
|
|
package com.jtzx.crm.module.api.web.sys;
|
|
|
|
|
|
|
|
|
+import cc.uncarbon.framework.core.exception.BusinessException;
|
|
|
import cc.uncarbon.framework.core.page.PageResult;
|
|
|
import cc.uncarbon.framework.web.model.response.ApiResult;
|
|
|
import cn.dev33.satoken.annotation.SaCheckLogin;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.google.common.collect.Lists;
|
|
|
import com.jtzx.crm.module.api.constant.ApiConstant;
|
|
|
import com.jtzx.crm.module.api.util.CrmAdminStpUtil;
|
|
|
+import com.jtzx.crm.module.sys.entity.SysParamBaseDataEntity;
|
|
|
import com.jtzx.crm.module.sys.model.request.*;
|
|
|
import com.jtzx.crm.module.sys.model.response.*;
|
|
|
+import com.jtzx.crm.module.sys.service.DictCityRegionService;
|
|
|
+import com.jtzx.crm.module.sys.service.DictProvinceRegionService;
|
|
|
import com.jtzx.crm.module.sys.service.SysCustomerDataPublicService;
|
|
|
+import com.jtzx.crm.module.sys.service.SysCustomerUserCommonService;
|
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.Parameter;
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
import jakarta.validation.Valid;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
|
|
|
@SaCheckLogin(type = CrmAdminStpUtil.TYPE)
|
|
|
@@ -28,6 +40,11 @@ public class CustomerDataPublicController {
|
|
|
|
|
|
private final SysCustomerDataPublicService sysCustomerDataPublicService;
|
|
|
|
|
|
+ private final SysCustomerUserCommonService sysCustomerUserCommonService;
|
|
|
+
|
|
|
+ private final DictProvinceRegionService dictProvinceRegionService;
|
|
|
+
|
|
|
+ private final DictCityRegionService dictCityRegionService;
|
|
|
|
|
|
@Operation(summary = "查询顶部筛选条件信息接口")
|
|
|
@GetMapping(value = "/head/select/data")
|
|
|
@@ -114,4 +131,115 @@ public class CustomerDataPublicController {
|
|
|
SysCustomerOperLogDataBO result = sysCustomerDataPublicService.queryOperLogData(dto);
|
|
|
return ApiResult.data(result);
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Operation(summary = "导入客户数据接口")
|
|
|
+ @PostMapping(value = "/importCustomerData")
|
|
|
+ public ApiResult<Void> importCustomerData(@Parameter(description = "文件参数") @RequestPart MultipartFile file) {
|
|
|
+ this.handleImportCustomerData(file);
|
|
|
+ return ApiResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理导入客户数据
|
|
|
+ *
|
|
|
+ * @param file
|
|
|
+ */
|
|
|
+ public void handleImportCustomerData(MultipartFile file) {
|
|
|
+ try {
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
+ List<LinkedHashMap<Integer, Object>> allData = EasyExcel.read(inputStream)
|
|
|
+ .sheet(0)
|
|
|
+ .headRowNumber(0)
|
|
|
+ .doReadSync();
|
|
|
+ // 验证数据,除标题外的行不能为空
|
|
|
+ if (allData == null || allData.size() < 2) {
|
|
|
+ throw new BusinessException("excel数据异常,导入失败");
|
|
|
+ }
|
|
|
+ //组装数据
|
|
|
+ List<AdminSysCustomerDataInsertOrUpdateDTO> dtoList = this.getDTOList(allData);
|
|
|
+ if (CollUtil.isEmpty(dtoList)) {
|
|
|
+ throw new BusinessException("excel数据异常,导入失败");
|
|
|
+ }
|
|
|
+ //批量插入数据
|
|
|
+ dtoList.forEach(sysCustomerDataPublicService::adminInsertCustomerData);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导入客户数据失败", e);
|
|
|
+ throw new RuntimeException("导入客户数据失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组装数据列表
|
|
|
+ *
|
|
|
+ * @param allData
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<AdminSysCustomerDataInsertOrUpdateDTO> getDTOList(List<LinkedHashMap<Integer, Object>> allData) {
|
|
|
+ // 查询学段数据
|
|
|
+ List<SysParamBaseDataEntity> sysParamBaseDataEntities = sysCustomerUserCommonService.queryParamBaseData(0);
|
|
|
+ if (CollUtil.isEmpty(sysParamBaseDataEntities)) {
|
|
|
+ throw new BusinessException("参数配置学段未设置,导入失败");
|
|
|
+ }
|
|
|
+ // 查询省份数据
|
|
|
+ List<DictProvinceRegionBO> dictProvinceRegionBOS = dictProvinceRegionService.listUsed();
|
|
|
+ if (CollUtil.isEmpty(dictProvinceRegionBOS)) {
|
|
|
+ throw new BusinessException("参数配置省份未设置,导入失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询城市数据
|
|
|
+ List<DictCityRegionBO> dictCityRegionBOS =
|
|
|
+ dictCityRegionService.queryCityByParentCodes(dictProvinceRegionBOS.stream().map(DictProvinceRegionBO::getCode).toList());
|
|
|
+ // 返回结束数据
|
|
|
+ List<AdminSysCustomerDataInsertOrUpdateDTO> dtoLists = Lists.newArrayList();
|
|
|
+ //获取标题行数据
|
|
|
+ LinkedHashMap<Integer, Object> titleRowData = allData.getFirst();
|
|
|
+ // 删除标题行数据
|
|
|
+ allData.remove(titleRowData);
|
|
|
+
|
|
|
+ for (LinkedHashMap<Integer, Object> rowData : allData) {
|
|
|
+ AdminSysCustomerDataInsertOrUpdateDTO dto = new AdminSysCustomerDataInsertOrUpdateDTO();
|
|
|
+ // 设置标题行数据到dto中
|
|
|
+ dto.setName((String) rowData.get(0));
|
|
|
+
|
|
|
+ if (rowData.get(1) == null) {
|
|
|
+ throw new BusinessException("学段不能为空");
|
|
|
+ }
|
|
|
+ dto.setStageName((String) rowData.get(1));
|
|
|
+ List<SysParamBaseDataEntity> stageList = sysParamBaseDataEntities.stream()
|
|
|
+ .filter(sysParamBaseDataEntity -> sysParamBaseDataEntity.getTitle().equals(rowData.get(1)))
|
|
|
+ .toList();
|
|
|
+ if (CollUtil.isEmpty(stageList)) {
|
|
|
+ throw new BusinessException(rowData.get(1)+"学段不存在,导入失败");
|
|
|
+ }
|
|
|
+ dto.setStageId(stageList.getFirst().getId());
|
|
|
+
|
|
|
+ if (rowData.get(2) == null) {
|
|
|
+ throw new BusinessException("省份不能为空");
|
|
|
+ }
|
|
|
+ dto.setProvinceName((String) rowData.get(2));
|
|
|
+ List<DictProvinceRegionBO> provinceList = dictProvinceRegionBOS.stream()
|
|
|
+ .filter(dictProvinceRegionBO -> dictProvinceRegionBO.getName().equals(rowData.get(2)))
|
|
|
+ .toList();
|
|
|
+ if (CollUtil.isEmpty(provinceList)) {
|
|
|
+ throw new BusinessException(rowData.get(2)+"省份不存在,导入失败");
|
|
|
+ }
|
|
|
+ dto.setProvinceId(provinceList.getFirst().getId());
|
|
|
+ if (rowData.get(3) == null) {
|
|
|
+ throw new BusinessException("城市不能为空");
|
|
|
+ }
|
|
|
+ dto.setCityName((String) rowData.get(3));
|
|
|
+ List<DictCityRegionBO> cityList = dictCityRegionBOS.stream()
|
|
|
+ .filter(dictCityRegionBO -> dictCityRegionBO.getName().equals(rowData.get(3)))
|
|
|
+ .toList();
|
|
|
+ if (CollUtil.isEmpty(cityList)) {
|
|
|
+ throw new BusinessException(rowData.get(3)+"城市不存在,导入失败");
|
|
|
+ }
|
|
|
+ dto.setCityId(cityList.getFirst().getId());
|
|
|
+
|
|
|
+ dto.setAddress((String) rowData.get(4));
|
|
|
+ dtoLists.add(dto);
|
|
|
+ }
|
|
|
+ return dtoLists;
|
|
|
+ }
|
|
|
}
|