Merge branch 'pr_7'

This commit is contained in:
阿沐 2022-11-06 21:39:45 +08:00
commit 21153cb897
17 changed files with 350 additions and 25 deletions

View File

@ -431,6 +431,9 @@ INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sor
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (37, 34, '删除', '', 'sys:attachment:delete', 1, 0, '', 1, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (38, 0, '日志管理', '', '', 0, 0, 'icon-filedone', 3, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (39, 38, '登录日志', 'sys/log/login', 'sys:log:login', 0, 0, 'icon-solution', 0, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (40, 39, '导出登录日志', '', 'sys:log:login:export', 1, 0, '', 1, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (41, 28, '导入', '', 'sys:user:import', 1, 0, '', 5, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (42, 28, '导出', '', 'sys:user:export', 1, 0, '', 6, 0, 0, 10000, now(), 10000, now());
SET IDENTITY_INSERT sys_dict_type ON;
INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (1, 'post_status', '状态', '岗位管理', 0, 0, 0, 10000, now(), 10000, now());

View File

@ -261,6 +261,9 @@ INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sor
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (37, 34, '删除', '', 'sys:attachment:delete', 1, 0, '', 1, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (38, 0, '日志管理', '', '', 0, 0, 'icon-filedone', 3, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (39, 38, '登录日志', 'sys/log/login', 'sys:log:login', 0, 0, 'icon-solution', 0, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (40, 39, '导出登录日志', '', 'sys:log:login:export', 1, 0, '', 1, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (41, 28, '导入', '', 'sys:user:import', 1, 0, '', 5, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (42, 28, '导出', '', 'sys:user:export', 1, 0, '', 6, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (1, 'post_status', '状态', '岗位管理', 0, 0, 0, 10000, now(), 10000, now());

View File

@ -431,6 +431,9 @@ INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sor
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (37, 34, '删除', '', 'sys:attachment:delete', 1, 0, '', 1, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (38, 0, '日志管理', '', '', 0, 0, 'icon-filedone', 3, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (39, 38, '登录日志', 'sys/log/login', 'sys:log:login', 0, 0, 'icon-solution', 0, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (40, 39, '导出登录日志', '', 'sys:log:login:export', 1, 0, '', 1, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (41, 28, '导入', '', 'sys:user:import', 1, 0, '', 5, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_menu (id, pid, name, url, authority, type, open_style, icon, sort, version, deleted, creator, create_time, updater, update_time) VALUES (42, 28, '导出', '', 'sys:user:export', 1, 0, '', 6, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (1, 'post_status', '状态', '岗位管理', 0, 0, 0, 10000, now(), 10000, now());
INSERT INTO sys_dict_type (id, dict_type, dict_name, remark, sort, version, deleted, creator, create_time, updater, update_time) VALUES (2, 'user_gender', '性别', '用户管理', 0, 0, 0, 10000, now(), 10000, now());

View File

@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;
/**
@ -40,7 +39,8 @@ public class SysLogLoginController {
@GetMapping("export")
@Operation(summary = "导出excel")
public Result<Map<String, String>> export() throws IOException {
@PreAuthorize("hasAuthority('sys:log:login:export')")
public Result<Map<String, String>> export() {
Map<String, String> map = sysLogLoginService.export();
return Result.ok(map);

View File

@ -19,9 +19,11 @@ import net.maku.system.vo.SysUserVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
@ -137,4 +139,25 @@ public class SysUserController {
return Result.ok();
}
@PostMapping("import")
@Operation(summary = "导入用户")
@PreAuthorize("hasAuthority('sys:user:import')")
public Result<String> importExcel(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return Result.error("请选择需要上传的文件");
}
sysUserService.importByExcel(file, passwordEncoder.encode("123456"));
return Result.ok();
}
@GetMapping("export")
@Operation(summary = "导出用户")
@PreAuthorize("hasAuthority('sys:user:export')")
public Result<Map<String, String>> export() {
Map<String, String> map = sysUserService.export();
return Result.ok(map);
}
}

View File

@ -2,6 +2,7 @@ package net.maku.system.convert;
import net.maku.framework.security.user.UserDetail;
import net.maku.system.entity.SysUserEntity;
import net.maku.system.vo.SysUserExcelVO;
import net.maku.system.vo.SysUserVO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@ -23,4 +24,8 @@ public interface SysUserConvert {
List<SysUserVO> convertList(List<SysUserEntity> list);
List<SysUserExcelVO> convert2List(List<SysUserEntity> list);
List<SysUserEntity> convertListEntity(List<SysUserExcelVO> list);
}

View File

@ -3,14 +3,41 @@ package net.maku.system.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Objects;
/**
* 超级管理员枚举
*/
@Getter
@AllArgsConstructor
public enum SuperAdminEnum {
YES(1),
NO(0);
/**
*
*/
YES(1, ""),
/**
*
*/
NO(0, "");
private final Integer value;
private final String name;
public static String getNameByValue(int value) {
for (SuperAdminEnum s : SuperAdminEnum.values()) {
if (s.getValue() == value) {
return s.getName();
}
}
return "";
}
public static Integer getValueByName(String name) {
for (SuperAdminEnum s : SuperAdminEnum.values()) {
if (Objects.equals(s.getName(), name)) {
return s.getValue();
}
}
return null;
}
}

View File

@ -0,0 +1,49 @@
package net.maku.system.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Objects;
/**
* 用户性别状态
*
* @author 阿沐 babamu@126.com
*/
@Getter
@AllArgsConstructor
public enum UserGenderEnum {
/**
*
*/
MAN(0, ""),
/**
*
*/
WOMEN(1, ""),
/**
* 未知
*/
UNKNOWN(2,"未知");
private final int value;
private final String name;
public static String getNameByValue(int value) {
for (UserGenderEnum s : UserGenderEnum.values()) {
if (s.getValue() == value) {
return s.getName();
}
}
return "";
}
public static Integer getValueByName(String name) {
for (UserGenderEnum s : UserGenderEnum.values()) {
if (Objects.equals(s.getName(), name)) {
return s.getValue();
}
}
return null;
}
}

View File

@ -3,6 +3,8 @@ package net.maku.system.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.Objects;
/**
* 用户状态
*
@ -14,11 +16,30 @@ public enum UserStatusEnum {
/**
* 停用
*/
DISABLE(0),
DISABLE(0, "停用"),
/**
* 正常
*/
ENABLED(1);
ENABLED(1, "正常");
private final int value;
private final String name;
public static String getNameByValue(int value) {
for (UserStatusEnum s : UserStatusEnum.values()) {
if (s.getValue() == value) {
return s.getName();
}
}
return "";
}
public static Integer getValueByName(String name) {
for (UserStatusEnum s : UserStatusEnum.values()) {
if (Objects.equals(s.getName(), name)) {
return s.getValue();
}
}
return null;
}
}

View File

@ -37,7 +37,6 @@ public interface SysLogLoginService extends BaseService<SysLogLoginEntity> {
* 导出登录日志
*
* @return the map
* @throws IOException the io exception
*/
Map<String, String> export() throws IOException;
Map<String, String> export();
}

View File

@ -6,8 +6,10 @@ import net.maku.system.entity.SysUserEntity;
import net.maku.system.query.SysRoleUserQuery;
import net.maku.system.query.SysUserQuery;
import net.maku.system.vo.SysUserVO;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
/**
* 用户管理
@ -39,4 +41,18 @@ public interface SysUserService extends BaseService<SysUserEntity> {
*/
PageResult<SysUserVO> roleUserPage(SysRoleUserQuery query);
/**
* 批量导入用户
*
* @param file excel文件
* @param password 密码
*/
void importByExcel(MultipartFile file, String password);
/**
* 导出用户信息表格
*
* @return map
*/
Map<String, String> export();
}

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.service.impl.BaseServiceImpl;
import net.maku.framework.common.utils.AddressUtils;
@ -24,7 +25,6 @@ import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
@ -78,18 +78,19 @@ public class SysLogLoginServiceImpl extends BaseServiceImpl<SysLogLoginDao, SysL
}
@Override
public Map<String, String> export() throws IOException {
@SneakyThrows
public Map<String, String> export() {
List<SysLogLoginEntity> list = list();
List<SysLogLoginVO> sysLogLoginVOS = SysLogLoginConvert.INSTANCE.convertList(list);
File file = File.createTempFile("log_excel", ".xlsx");
File file = File.createTempFile("system_login_log_excel", ".xlsx");
// 写入到文件
ExcelUtils.excelExport(SysLogLoginVO.class, file, sysLogLoginVOS);
byte[] data = IoUtil.readBytes(Files.newInputStream(file.toPath()));
String path = storageService.getPath(file.getName());
String url = storageService.upload(data, path);
Map<String, String> map = new HashMap<>();
Map<String, String> map = new HashMap<>(2);
map.put("path", url);
map.put("filename", file.getName());
return map;

View File

@ -1,11 +1,17 @@
package net.maku.system.service.impl;
import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import net.maku.framework.common.constant.Constant;
import net.maku.framework.common.excel.ExcelFinishCallBack;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.service.impl.BaseServiceImpl;
import net.maku.framework.common.utils.ExcelUtils;
import net.maku.storage.service.StorageService;
import net.maku.system.convert.SysUserConvert;
import net.maku.system.dao.SysUserDao;
import net.maku.system.entity.SysUserEntity;
@ -15,10 +21,14 @@ import net.maku.system.query.SysUserQuery;
import net.maku.system.service.SysUserPostService;
import net.maku.system.service.SysUserRoleService;
import net.maku.system.service.SysUserService;
import net.maku.system.vo.SysUserExcelVO;
import net.maku.system.vo.SysUserVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -33,6 +43,7 @@ import java.util.Map;
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {
private final SysUserRoleService sysUserRoleService;
private final SysUserPostService sysUserPostService;
private final StorageService storageService;
@Override
public PageResult<SysUserVO> page(SysUserQuery query) {
@ -131,7 +142,7 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
@Override
public SysUserVO getByMobile(String mobile) {
SysUserEntity user = baseMapper.getByMobile(mobile);
return SysUserConvert.INSTANCE.convert(user);
}
@ -160,4 +171,47 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserDao, SysUserEntit
return new PageResult<>(SysUserConvert.INSTANCE.convertList(list), page.getTotal());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void importByExcel(MultipartFile file, String password) {
ExcelUtils.readAnalysis(file, SysUserExcelVO.class, new ExcelFinishCallBack<SysUserExcelVO>() {
@Override
public void doAfterAllAnalysed(List<SysUserExcelVO> result) {
saveUser(result);
}
@Override
public void doSaveBatch(List<SysUserExcelVO> result) {
saveUser(result);
}
private void saveUser(List<SysUserExcelVO> result) {
List<SysUserEntity> sysUserEntities = SysUserConvert.INSTANCE.convertListEntity(result);
sysUserEntities.forEach(user -> user.setPassword(password));
saveBatch(sysUserEntities);
}
});
}
@Override
@SneakyThrows
public Map<String, String> export() {
List<SysUserEntity> list = list(Wrappers.lambdaQuery(SysUserEntity.class).eq(SysUserEntity::getSuperAdmin, SuperAdminEnum.NO.getValue()));
List<SysUserExcelVO> userExcelVOS = SysUserConvert.INSTANCE.convert2List(list);
File file = File.createTempFile("system_user_excel", ".xlsx");
// 写入到文件
ExcelUtils.excelExport(SysUserExcelVO.class, file, userExcelVOS);
byte[] data = IoUtil.readBytes(Files.newInputStream(file.toPath()));
String path = storageService.getPath(file.getName());
String url = storageService.upload(data, path);
Map<String, String> map = new HashMap<>(2);
map.put("path", url);
map.put("filename", file.getName());
return map;
}
}

View File

@ -0,0 +1,96 @@
package net.maku.system.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import lombok.Data;
import net.maku.framework.common.excel.DateConverter;
import net.maku.framework.common.utils.DateUtils;
import net.maku.system.enums.SuperAdminEnum;
import net.maku.system.enums.UserGenderEnum;
import net.maku.system.enums.UserStatusEnum;
import java.io.Serializable;
import java.util.Date;
/**
* excel用户表
*
* @author 阿沐 babamu@126.com
*/
@Data
public class SysUserExcelVO implements Serializable {
private static final long serialVersionUID = 1L;
@ExcelProperty("用户名")
private String username;
@ExcelProperty("姓名")
private String realName;
@ExcelProperty(value = "性别", converter = GenderConverter.class)
private Integer gender;
@ExcelProperty("邮箱")
private String email;
@ExcelProperty("手机号")
private String mobile;
@ExcelProperty("机构ID")
private Long orgId;
@ExcelProperty(value = "状态", converter = StatusConverter.class)
private Integer status;
@ExcelProperty(value = "超级管理员", converter = SuperConverter.class)
private Integer superAdmin;
@ExcelProperty(value = "创建时间", converter = DateConverter.class)
private Date createTime;
public static class GenderConverter implements Converter<Integer> {
@Override
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String dateString = cellData.getStringValue();
return UserGenderEnum.getValueByName(dateString);
}
@Override
public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(UserGenderEnum.getNameByValue(value));
}
}
public static class StatusConverter implements Converter<Integer> {
@Override
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String dateString = cellData.getStringValue();
return UserStatusEnum.getValueByName(dateString);
}
@Override
public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(UserStatusEnum.getNameByValue(value));
}
}
public static class SuperConverter implements Converter<Integer> {
@Override
public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String dateString = cellData.getStringValue();
return SuperAdminEnum.getValueByName(dateString);
}
@Override
public WriteCellData<String> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
return new WriteCellData<>(SuperAdminEnum.getNameByValue(value));
}
}
}

View File

@ -41,9 +41,10 @@ public class ExcelDataListener<T> extends AnalysisEventListener<T> {
@Override
public void invoke(T data, AnalysisContext context) {
list.add(data);
if (list.size() == 1) {
System.out.println("自己逻辑");
if (list.size() == 500) {
System.out.println(("自己逻辑..."));
this.callBack.doSaveBatch(list);
list.clear();
}
}

View File

@ -4,6 +4,7 @@ import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import net.maku.framework.common.excel.ExcelDataListener;
import net.maku.framework.common.excel.ExcelFinishCallBack;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
@ -18,6 +19,31 @@ import java.util.List;
*/
public class ExcelUtils {
/**
* 判断excel文件类型
*
* @param file 源头文件
* @return type
*/
public static ExcelTypeEnum getExcelFileType(MultipartFile file) {
String filename = file.getOriginalFilename();
if (StringUtils.isNotBlank(filename)) {
filename = filename.substring(filename.lastIndexOf("."));
switch (filename) {
case ".csv":
return ExcelTypeEnum.CSV;
case ".xls":
return ExcelTypeEnum.XLS;
case ".xlsx":
return ExcelTypeEnum.XLSX;
default:
throw new IllegalArgumentException("无效的文件");
}
}
throw new IllegalArgumentException("无效的文件");
}
/**
* 读取excel文件
*

View File

@ -8,7 +8,6 @@ import net.maku.framework.common.utils.ExcelUtils;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@ -21,7 +20,7 @@ import java.util.List;
public class EasyExcelTest {
@Test
public void doImport() throws FileNotFoundException {
public void doImport() {
File file = new File("D://upload//test01.xlsx");
ExcelClass excelClass = new ExcelClass();
excelClass.setNumber(1);
@ -29,12 +28,11 @@ public class EasyExcelTest {
excelClass.setString("test");
excelClass.setDate(new Date());
List<ExcelClass> data = Arrays.asList(excelClass, excelClass, excelClass);
if (!file.exists()) {
if (file.exists()) {
ExcelUtils.excelExport(ExcelClass.class, file, data);
List<ExcelClass> list = ExcelUtils.readSync(file, ExcelClass.class);
list.forEach(System.out::println);
}
List<ExcelClass> list = ExcelUtils.readSync(file, ExcelClass.class);
list.forEach(System.out::println);
}
@ -49,10 +47,10 @@ public class EasyExcelTest {
List<ExcelClass> data = Arrays.asList(excelClass, excelClass, excelClass, excelClass, excelClass, excelClass, excelClass);
if (!file.exists()) {
ExcelUtils.excelExport(ExcelClass.class, file, data);
ExcelUtils.readAnalysis(file, ExcelClass.class, new ServiceA());
}
ExcelUtils.readAnalysis(file, ExcelClass.class, new ServiceA());
}
}
@Data
public static class ExcelClass {