From beae54ac1eeb36820b5f67c8e5120671f42db711 Mon Sep 17 00:00:00 2001 From: junruyue <2294678909@qq.com> Date: Fri, 8 Nov 2024 12:42:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E9=99=A2=E5=AF=BC=E5=85=A5=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- ahut-admin/pom.xml | 7 ++ .../controller/system/SysDeptController.java | 38 +++++++--- .../src/main/resources/application-dev.yml | 2 +- ahut-admin/src/main/resources/application.yml | 2 +- .../common/core/domain/entity/SysDept.java | 19 +++++ .../ahut/system/service/ISysDeptService.java | 5 ++ .../service/impl/SysDeptServiceImpl.java | 70 ++++++++++++++++++- .../resources/mapper/system/SysDeptMapper.xml | 2 +- 9 files changed, 132 insertions(+), 15 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index e9a5617..0c41917 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/ahut-admin/pom.xml b/ahut-admin/pom.xml index e39d09e..6fc6c9d 100644 --- a/ahut-admin/pom.xml +++ b/ahut-admin/pom.xml @@ -51,6 +51,13 @@ ahut-framework + + + org.apache.poi + poi-ooxml + ${poi.version} + + diff --git a/ahut-admin/src/main/java/com/ahut/web/controller/system/SysDeptController.java b/ahut-admin/src/main/java/com/ahut/web/controller/system/SysDeptController.java index 6acc046..b87e9dd 100644 --- a/ahut-admin/src/main/java/com/ahut/web/controller/system/SysDeptController.java +++ b/ahut-admin/src/main/java/com/ahut/web/controller/system/SysDeptController.java @@ -1,18 +1,20 @@ package com.ahut.web.controller.system; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.HashMap; import java.util.List; +import java.util.Map; + import org.apache.commons.lang3.ArrayUtils; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.ahut.common.annotation.Log; import com.ahut.common.constant.UserConstants; import com.ahut.common.core.controller.BaseController; @@ -21,6 +23,7 @@ import com.ahut.common.core.domain.entity.SysDept; import com.ahut.common.enums.BusinessType; import com.ahut.common.utils.StringUtils; import com.ahut.system.service.ISysDeptService; +import org.springframework.web.multipart.MultipartFile; /** * 部门信息 @@ -34,6 +37,25 @@ public class SysDeptController extends BaseController @Autowired private ISysDeptService deptService; + /** + * 导入学院、系(专业)和班级 + */ + @PostMapping("/import") + @Transactional + public AjaxResult importInfo(@RequestParam("file") MultipartFile file) { + if (file.isEmpty()) { + return error("文件大小为空!"); + } + //将三级部门插入 + try { + deptService.importDepts(file.getInputStream()); + } catch (IOException e) { + e.printStackTrace(); + } + return AjaxResult.success(); + } + + /** * 获取部门列表 */ diff --git a/ahut-admin/src/main/resources/application-dev.yml b/ahut-admin/src/main/resources/application-dev.yml index 798958e..cbb135f 100644 --- a/ahut-admin/src/main/resources/application-dev.yml +++ b/ahut-admin/src/main/resources/application-dev.yml @@ -8,7 +8,7 @@ spring: master: url: jdbc:mysql://localhost:3306/ahut_review?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 username: root - password: root + password: 123456 # 从库数据源 slave: # 从数据源开关/默认关闭 diff --git a/ahut-admin/src/main/resources/application.yml b/ahut-admin/src/main/resources/application.yml index 771cd23..6673638 100644 --- a/ahut-admin/src/main/resources/application.yml +++ b/ahut-admin/src/main/resources/application.yml @@ -74,7 +74,7 @@ spring: # 数据库索引 database: 0 # 密码 - password: + password: 123456 # 连接超时时间 timeout: 10s lettuce: diff --git a/ahut-common/src/main/java/com/ahut/common/core/domain/entity/SysDept.java b/ahut-common/src/main/java/com/ahut/common/core/domain/entity/SysDept.java index 5908657..ea76cb3 100644 --- a/ahut-common/src/main/java/com/ahut/common/core/domain/entity/SysDept.java +++ b/ahut-common/src/main/java/com/ahut/common/core/domain/entity/SysDept.java @@ -20,6 +20,7 @@ public class SysDept extends BaseEntity { private static final long serialVersionUID = 1L; + /** 部门ID */ private Long deptId; @@ -56,6 +57,24 @@ public class SysDept extends BaseEntity /** 子部门 */ private List children = new ArrayList(); + public SysDept(){ + + } + + public SysDept(Long deptId, Long parentId, String ancestors, String deptName, Integer orderNum, String leader, String phone, String email, String status, String delFlag, String parentName) { + this.deptId = deptId; + this.parentId = parentId; + this.ancestors = ancestors; + this.deptName = deptName; + this.orderNum = orderNum; + this.leader = leader; + this.phone = phone; + this.email = email; + this.status = status; + this.delFlag = delFlag; + this.parentName = parentName; + } + public Long getDeptId() { return deptId; diff --git a/ahut-system/src/main/java/com/ahut/system/service/ISysDeptService.java b/ahut-system/src/main/java/com/ahut/system/service/ISysDeptService.java index 1aec5e5..a416091 100644 --- a/ahut-system/src/main/java/com/ahut/system/service/ISysDeptService.java +++ b/ahut-system/src/main/java/com/ahut/system/service/ISysDeptService.java @@ -1,5 +1,7 @@ package com.ahut.system.service; +import java.io.FileInputStream; +import java.io.InputStream; import java.util.List; import com.ahut.common.core.domain.TreeSelect; import com.ahut.common.core.domain.entity.SysDept; @@ -11,6 +13,9 @@ import com.ahut.common.core.domain.entity.SysDept; */ public interface ISysDeptService { + + public void importDepts(InputStream inputStream); + /** * 查询部门管理数据 * diff --git a/ahut-system/src/main/java/com/ahut/system/service/impl/SysDeptServiceImpl.java b/ahut-system/src/main/java/com/ahut/system/service/impl/SysDeptServiceImpl.java index b935143..222d547 100644 --- a/ahut-system/src/main/java/com/ahut/system/service/impl/SysDeptServiceImpl.java +++ b/ahut-system/src/main/java/com/ahut/system/service/impl/SysDeptServiceImpl.java @@ -1,9 +1,16 @@ package com.ahut.system.service.impl; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.*; import java.util.stream.Collectors; + +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.ahut.common.annotation.DataScope; @@ -35,6 +42,63 @@ public class SysDeptServiceImpl implements ISysDeptService @Autowired private SysRoleMapper roleMapper; + @Override + public void importDepts(InputStream inputStream) { + // 保存已插入的部门信息,用于查找parent_id + Map deptMap = new HashMap<>(); + try (Workbook workbook = new HSSFWorkbook(inputStream)) { + Sheet sheet = workbook.getSheetAt(0); + int corderNum = 1; // 显示顺序 + int dorderNum = 1; // 显示顺序 + int clorderNum = 1; // 显示顺序 + for (Row row : sheet) { + if (row.getRowNum() == 0) continue; // 跳过表头 + + String collegeName = row.getCell(0) != null ? row.getCell(0).getStringCellValue().trim() : null; + String departmentName = row.getCell(1) != null ? row.getCell(1).getStringCellValue().trim() : null; + String className = row.getCell(2) != null ? row.getCell(2).getStringCellValue().trim() : null; + + Long parentId = 0L; + String ancestors = "0"; + + // 处理学院 + if (collegeName != null && !deptMap.containsKey(collegeName)) { + SysDept sysDept = new SysDept(null, parentId, ancestors, collegeName, corderNum++, + null, null, null, null, null, null); + deptMapper.insertDept(sysDept); + deptMap.put(collegeName, sysDept.getDeptId()); + } + + parentId = deptMap.getOrDefault(collegeName, 0L); + ancestors = ancestors+","+parentId; + + // 处理系 + if (departmentName != null && !deptMap.containsKey(departmentName)) { + SysDept sysDept = new SysDept(null, parentId, ancestors, departmentName, dorderNum++, + null, null, null, null, null, null); + deptMapper.insertDept(sysDept); + Long departmentId = sysDept.getDeptId(); + deptMap.put(departmentName, departmentId); + } + + parentId = deptMap.getOrDefault(departmentName, 0L); + ancestors = ancestors+","+parentId; + + // 处理班级 + if (className != null && !deptMap.containsKey(className)) { + SysDept sysDept = new SysDept(null, parentId, ancestors, className, clorderNum++, + null, null, null, null, null, null); + deptMapper.insertDept(sysDept); + Long classId = sysDept.getDeptId(); + deptMap.put(className, classId); + } + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + /** * 查询部门管理数据 * diff --git a/ahut-system/src/main/resources/mapper/system/SysDeptMapper.xml b/ahut-system/src/main/resources/mapper/system/SysDeptMapper.xml index b60dc10..a9c79ce 100644 --- a/ahut-system/src/main/resources/mapper/system/SysDeptMapper.xml +++ b/ahut-system/src/main/resources/mapper/system/SysDeptMapper.xml @@ -87,7 +87,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" where dept_name=#{deptName} and parent_id = #{parentId} and del_flag = '0' limit 1 - + insert into sys_dept( dept_id, parent_id,