学院导入功能

This commit is contained in:
贾晓峰 2024-11-08 12:42:12 +08:00
parent f8aa4c983b
commit beae54ac1e
9 changed files with 132 additions and 15 deletions

View File

@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="jdk1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -51,6 +51,13 @@
<artifactId>ahut-framework</artifactId>
</dependency>
<!--excel工具-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
</dependencies>

View File

@ -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();
}
/**
* 获取部门列表
*/

View File

@ -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:
# 从数据源开关/默认关闭

View File

@ -74,7 +74,7 @@ spring:
# 数据库索引
database: 0
# 密码
password:
password: 123456
# 连接超时时间
timeout: 10s
lettuce:

View File

@ -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<SysDept> children = new ArrayList<SysDept>();
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;

View File

@ -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);
/**
* 查询部门管理数据
*

View File

@ -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<String, Long> 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();
}
}
/**
* 查询部门管理数据
*

View File

@ -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
</select>
<insert id="insertDept" parameterType="SysDept">
<insert id="insertDept" parameterType="SysDept" useGeneratedKeys="true" keyProperty="deptId">
insert into sys_dept(
<if test="deptId != null and deptId != 0">dept_id,</if>
<if test="parentId != null and parentId != 0">parent_id,</if>