Explorar el Código

ljx:新增企业实体导入和分类导入

ljx hace 1 mes
padre
commit
165f863c1a

+ 33 - 0
enteprise-admin/src/main/java/com/enteprise/enterprise/controller/EnterpriseController.java

@@ -4,6 +4,7 @@ import java.util.List;
 import javax.servlet.http.HttpServletResponse;
 
 import com.enteprise.enterprise.dto.EnterpriseDto;
+import io.swagger.annotations.ApiOperation;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
@@ -22,6 +23,7 @@ import com.enteprise.enterprise.domain.Enterprise;
 import com.enteprise.enterprise.service.IEnterpriseService;
 import com.enteprise.common.utils.poi.ExcelUtil;
 import com.enteprise.common.core.page.TableDataInfo;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * enterpriseController
@@ -124,4 +126,35 @@ public class EnterpriseController extends BaseController
         return getDataTable(list);
     }
 
+    /**
+     * 导入
+     */
+    @ApiOperation("导入企业实体(分类)数据")
+    @Log(title = "企业信息", businessType = BusinessType.IMPORT)
+    @PreAuthorize("@ss.hasPermi('enterprise:enterprise:add')")
+    @PostMapping("/importData")
+    public AjaxResult importData(MultipartFile file) {
+        try {
+            // 获取模板工具
+            ExcelUtil<EnterpriseDto> util = new ExcelUtil<>(EnterpriseDto.class);
+            List<EnterpriseDto> list = util.importExcel(file.getInputStream());
+            if (list.isEmpty()) {
+                return error("数据不得为空");
+            }
+            // 调用服务层进行插入或更新
+            int successCount = enterpriseService.insertOrUpdateBatch(list);
+            return AjaxResult.success("导入成功,共处理 " + successCount + " 条数据");
+        } catch (Exception e) {
+            return AjaxResult.error("导入失败:" + e.getMessage());
+        }
+    }
+
+    @ApiOperation("企业实体(分类)模板")
+    @PostMapping("/importTemplate")
+    public void importTemplate(HttpServletResponse response)
+    {
+        ExcelUtil<EnterpriseDto> util = new ExcelUtil<>(EnterpriseDto.class);
+        util.importTemplateExcel(response, "企业实体(分类)数据");
+    }
+
 }

+ 10 - 2
enteprise-admin/src/main/java/com/enteprise/enterprise/dto/EnterpriseDto.java

@@ -10,16 +10,24 @@ public class EnterpriseDto {
     private String enterpriseName;
 
     /** 坐落地 */
-    @Excel(name = "坐落地")
     private String location;
 
     /** 行业代码 */
-    @Excel(name = "行业代码")
     private String code;
 
+    @Excel(name = "分类编码")
+    private String typeNum;
     @Excel(name = "分类名称")
     private String typeName;
 
+    public String getTypeNum() {
+        return typeNum;
+    }
+
+    public void setTypeNum(String typeNum) {
+        this.typeNum = typeNum;
+    }
+
     public String getTypeName() {
         return typeName;
     }

+ 3 - 0
enteprise-admin/src/main/java/com/enteprise/enterprise/mapper/EnterpriseMapper.java

@@ -66,4 +66,7 @@ public interface EnterpriseMapper
      * @return 结果
      */
     public int deleteEnterpriseByIds(Long[] ids);
+
+
+    int insertOrUpdateBatch(List<EnterpriseDto> list);
 }

+ 2 - 0
enteprise-admin/src/main/java/com/enteprise/enterprise/service/IEnterpriseService.java

@@ -60,4 +60,6 @@ public interface IEnterpriseService
      * @return 结果
      */
     public int deleteEnterpriseById(Long id);
+
+    int insertOrUpdateBatch(List<EnterpriseDto> list);
 }

+ 43 - 0
enteprise-admin/src/main/java/com/enteprise/enterprise/service/impl/EnterpriseServiceImpl.java

@@ -1,8 +1,14 @@
 package com.enteprise.enterprise.service.impl;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
 
 import com.enteprise.enterprise.dto.EnterpriseDto;
+import com.enteprise.etype.domain.EnterpriseType;
+import com.enteprise.etype.mapper.EnterpriseTypeMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.enteprise.enterprise.mapper.EnterpriseMapper;
@@ -21,6 +27,9 @@ public class EnterpriseServiceImpl implements IEnterpriseService
     @Autowired
     private EnterpriseMapper enterpriseMapper;
 
+    @Autowired
+    private EnterpriseTypeMapper enterpriseTypeMapper;
+
     /**
      * 查询enterprise
      * 
@@ -97,4 +106,38 @@ public class EnterpriseServiceImpl implements IEnterpriseService
     {
         return enterpriseMapper.deleteEnterpriseById(id);
     }
+
+    @Override
+    public int insertOrUpdateBatch(List<EnterpriseDto> list) {
+        int i = 0;
+        //获取导入分类表的数据
+        List<EnterpriseType> typesToInsertOrUpdate = new ArrayList<>();
+        for (EnterpriseDto entity : list) {
+            String typeNumber = entity.getTypeNum();
+            String typeName = entity.getTypeName();
+            if (typeNumber != null && typeName != null) {
+                // 检查已有列表中是否已经有此 typeNumber
+                Optional<EnterpriseType> existingType = typesToInsertOrUpdate.stream()
+                        .filter(t -> t.getNumber().equals(typeNumber))
+                        .findFirst();
+
+                if (!existingType.isPresent()) {
+                    // 不存在则添加新的 Type 到列表中
+                    EnterpriseType newType = new EnterpriseType();
+                    newType.setNumber(typeNumber);
+                    newType.setName(typeName);
+                    typesToInsertOrUpdate.add(newType);
+                }
+            }
+        }
+        //导入分类表
+        if (!typesToInsertOrUpdate.isEmpty()) {
+            enterpriseTypeMapper.insertBatch(typesToInsertOrUpdate);
+        }
+        //导入企业表
+        if (!list.isEmpty()) {
+            i = enterpriseMapper.insertOrUpdateBatch(list);
+        }
+        return i;
+    }
 }

+ 1 - 12
enteprise-admin/src/main/java/com/enteprise/etype/service/impl/EnterpriseTypeServiceImpl.java

@@ -76,20 +76,9 @@ public class EnterpriseTypeServiceImpl implements IEnterpriseTypeService
 
     @Override
     public boolean batchTbEnterpriseType(List<EnterpriseType> tbEnterpriseType) {
-        // 1. 校验数据(检查分类编码是否重复)
-        for (EnterpriseType enterpriseType : tbEnterpriseType) {
-            if (enterpriseType.getNumber() == null || enterpriseType.getName() == null) {
-                throw new RuntimeException("分类编码或名称为空!");
-            }
-            // 检查数据库是否已存在相同编码
-            EnterpriseType exist = enterpriseTypeMapper.selectEnterpriseTypeByNumber(enterpriseType.getNumber());
-            if (exist != null) {
-                throw new RuntimeException("分类编码 " + enterpriseType.getNumber() + " 已存在!");
-            }
-        }
         // 2. 批量插入数据库
         enterpriseTypeMapper.insertBatch(tbEnterpriseType);
-        return false;
+        return true;
     }
 
     /**

+ 15 - 0
enteprise-admin/src/main/resources/mapper/enterprise/EnterpriseMapper.xml

@@ -69,5 +69,20 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         from enterprise a
         left join enterprise_type b
         on a.type_num = b.number
+        <where>
+            <if test="enterpriseName != null  and enterpriseName != ''"> and a.enterprise_name like concat('%', #{enterpriseName}, '%')</if>
+            <if test="location != null  and location != ''"> and a.location = #{location}</if>
+            <if test="code != null  and code != ''"> and a.code = #{code}</if>
+        </where>
     </select>
+    
+    <insert id="insertOrUpdateBatch">
+        INSERT INTO enterprise (enterprise_name, type_num)
+        VALUES
+        <foreach collection="list" item="item" separator=",">
+            (#{item.enterpriseName}, #{item.typeNum})
+        </foreach>
+        ON DUPLICATE KEY UPDATE
+        type_num = IF(VALUES(type_num) != '', VALUES(type_num), type_num)
+    </insert>
 </mapper>

+ 4 - 1
enteprise-admin/src/main/resources/mapper/etype/EnterpriseTypeMapper.xml

@@ -14,7 +14,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 
 
     <sql id="selectEnterpriseTypeVo">
-        select number, name from enterprise_type
+        select id, number, name from enterprise_type
+        order by number
     </sql>
 
     <select id="selectEnterpriseTypeList" parameterType="EnterpriseType" resultMap="EnterpriseTypeResult">
@@ -69,5 +70,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <foreach collection="list" item="item" separator=",">
             (#{item.number}, #{item.name})
         </foreach>
+        ON DUPLICATE KEY UPDATE
+        name = IF(VALUES(name) != '', VALUES(name), name)
     </insert>
 </mapper>