完成了导入,没加Token验证,需要excel值全部引号包裹
parent
0db64e3fad
commit
5b7f552530
5
pom.xml
5
pom.xml
|
@ -112,6 +112,11 @@
|
||||||
<artifactId>java-jwt</artifactId>
|
<artifactId>java-jwt</artifactId>
|
||||||
<version>3.19.2</version>
|
<version>3.19.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-ooxml</artifactId>
|
||||||
|
<version>5.2.2</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package cn.edu.hfut.rmdjzz.projectmanagement.controller;
|
||||||
|
|
||||||
|
import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
|
||||||
|
import cn.edu.hfut.rmdjzz.projectmanagement.service.IStaffService;
|
||||||
|
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author 张韬
|
||||||
|
* created at 2022/7/7 17:08
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
public class AdditionalFunctionsController {
|
||||||
|
@Autowired
|
||||||
|
private IStaffService staffService;
|
||||||
|
@SneakyThrows
|
||||||
|
@PostMapping(value = "/staff/import")
|
||||||
|
public ResponseMap upload(@RequestHeader("Token") String token, @RequestParam("uploadFile") MultipartFile uploadFile){
|
||||||
|
if (null == uploadFile) {
|
||||||
|
throw new BadRequestException("空的文件参数");
|
||||||
|
}
|
||||||
|
String fileName = Objects.requireNonNull(uploadFile.getOriginalFilename()).toLowerCase();
|
||||||
|
if (!fileName.endsWith(".xlsx")) {
|
||||||
|
throw new BadRequestException("文件类型错误");
|
||||||
|
}
|
||||||
|
Integer successCount=staffService.multiImport(token,uploadFile);
|
||||||
|
return ResponseMap.ofSuccess("成功导入"+successCount+"条数据");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -17,4 +17,5 @@ public class Staff {
|
||||||
private String staffFullname;
|
private String staffFullname;
|
||||||
private String staffGender;
|
private String staffGender;
|
||||||
private String staffSalt;
|
private String staffSalt;
|
||||||
|
private Integer staffLevel;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import cn.edu.hfut.rmdjzz.projectmanagement.exception.BadRequestException;
|
||||||
import cn.edu.hfut.rmdjzz.projectmanagement.exception.TokenException;
|
import cn.edu.hfut.rmdjzz.projectmanagement.exception.TokenException;
|
||||||
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
|
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author 佘语殊
|
* @author 佘语殊
|
||||||
|
@ -14,4 +15,6 @@ public interface IStaffService extends IService<Staff> {
|
||||||
ResponseMap login(String username, String password) throws BadRequestException, TokenException;
|
ResponseMap login(String username, String password) throws BadRequestException, TokenException;
|
||||||
|
|
||||||
Boolean logout(String token) throws TokenException;
|
Boolean logout(String token) throws TokenException;
|
||||||
|
|
||||||
|
Integer multiImport(String token, MultipartFile file) throws BadRequestException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,11 +9,22 @@ import cn.edu.hfut.rmdjzz.projectmanagement.utils.TokenUtils;
|
||||||
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
|
import cn.edu.hfut.rmdjzz.projectmanagement.utils.http.ResponseMap;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
import org.apache.poi.ss.usermodel.CellType;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
import org.jetbrains.annotations.Contract;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Isolation;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.DigestUtils;
|
import org.springframework.util.DigestUtils;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@ -67,4 +78,101 @@ public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements
|
||||||
redisTemplate.delete(staffId);
|
redisTemplate.delete(staffId);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional(isolation = Isolation.SERIALIZABLE, rollbackFor = Exception.class)
|
||||||
|
@Override
|
||||||
|
public Integer multiImport(String token, MultipartFile file) throws BadRequestException {
|
||||||
|
//TODO:check Token here
|
||||||
|
|
||||||
|
try {
|
||||||
|
XSSFWorkbook xwb = new XSSFWorkbook(file.getInputStream());
|
||||||
|
XSSFSheet sheet = xwb.getSheetAt(0);
|
||||||
|
|
||||||
|
if (sheet == null) {
|
||||||
|
throw new BadRequestException("无法获取Sheet");
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<String> staffUsernameC = new ArrayList<>();
|
||||||
|
ArrayList<String> staffPasswordC = new ArrayList<>();
|
||||||
|
ArrayList<String> staffFullnameC = new ArrayList<>();
|
||||||
|
ArrayList<String> staffGenderC = new ArrayList<>();
|
||||||
|
ArrayList<String> staffLevelC = new ArrayList<>();
|
||||||
|
if (!xlsxColumnGetter("staffUsername", 0, staffUsernameC, sheet)) {
|
||||||
|
throw new BadRequestException("读取列staffUsername失败");
|
||||||
|
}
|
||||||
|
int totalCount = staffUsernameC.size();
|
||||||
|
|
||||||
|
if (totalCount < 1) {
|
||||||
|
throw new BadRequestException("读取不到条目");
|
||||||
|
}
|
||||||
|
System.out.println(totalCount);
|
||||||
|
System.out.println(staffUsernameC);
|
||||||
|
if (!xlsxColumnGetter("staffPassword", 1, staffPasswordC, sheet) ||
|
||||||
|
staffPasswordC.size() != totalCount) {
|
||||||
|
throw new BadRequestException("读取列staffPassword失败");
|
||||||
|
}
|
||||||
|
System.out.println(staffPasswordC);
|
||||||
|
if (!xlsxColumnGetter("staffFullname", 2, staffFullnameC, sheet) ||
|
||||||
|
staffFullnameC.size() != totalCount) {
|
||||||
|
throw new BadRequestException("读取列staffFullname失败");
|
||||||
|
}
|
||||||
|
System.out.println(staffFullnameC);
|
||||||
|
if (!xlsxColumnGetter("staffGender", 3, staffGenderC, sheet) ||
|
||||||
|
staffGenderC.size() != totalCount) {
|
||||||
|
throw new BadRequestException("读取列staffGender失败");
|
||||||
|
}
|
||||||
|
System.out.println(staffGenderC);
|
||||||
|
if (!xlsxColumnGetter("staffLevel", 4, staffLevelC, sheet) ||
|
||||||
|
staffLevelC.size() != totalCount) {
|
||||||
|
throw new BadRequestException("读取列staffLevel失败");
|
||||||
|
}
|
||||||
|
System.out.println(staffLevelC);
|
||||||
|
for (int i = 0; i < totalCount; i++) {
|
||||||
|
if (Integer.parseInt(staffLevelC.get(i)) != 2 && Integer.parseInt(staffLevelC.get(i)) != 3) {
|
||||||
|
throw new BadRequestException("列staffLevel无效");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < totalCount; i++) {
|
||||||
|
Staff staff = new Staff();
|
||||||
|
staff.setStaffId(null);
|
||||||
|
staff.setStaffUsername(staffUsernameC.get(i));
|
||||||
|
staff.setStaffSalt(RandomStringUtils.randomAlphanumeric(10));
|
||||||
|
if (staffPasswordC.get(i).length() < 5) {
|
||||||
|
throw new BadRequestException("检测到存在密码长度过短");
|
||||||
|
}
|
||||||
|
staff.setStaffPassword(DigestUtils.md5DigestAsHex((DigestUtils.md5DigestAsHex(staffPasswordC.get(i).getBytes()) + staff.getStaffSalt()).getBytes()));
|
||||||
|
staff.setStaffFullname(staffFullnameC.get(i));
|
||||||
|
staff.setStaffGender(staffGenderC.get(i));
|
||||||
|
staff.setStaffLevel(Integer.parseInt(staffLevelC.get(i)));
|
||||||
|
System.out.println(staff);
|
||||||
|
if (baseMapper.insert(staff) != 1) {
|
||||||
|
throw new BadRequestException("第" + (i + 1) + "行数据错误");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return totalCount;
|
||||||
|
} catch (BadRequestException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new BadRequestException("导入失败");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private Boolean xlsxColumnGetter(String columnName, int columnIndex, ArrayList<String> result, XSSFSheet sheet) {
|
||||||
|
try {
|
||||||
|
if (!columnName.equals(sheet.getRow(0).getCell(columnIndex).getStringCellValue())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
||||||
|
String value=sheet.getRow(i).getCell(columnIndex).getStringCellValue();
|
||||||
|
if(value==null||value.length()<=2)
|
||||||
|
return true;
|
||||||
|
result.add(value.substring(1,value.length()-1));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue