diff --git a/pom.xml b/pom.xml index 6ade2b0..157204b 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,7 @@ 3.8.1 1.2.58 2.9.2 + 4.1.0 @@ -135,6 +136,23 @@ ${fastjson.version} + + cn.afterturn + easypoi-base + ${easypoi.version} + + + com.google.guava + guava + + + + + cn.afterturn + easypoi-annotation + ${easypoi.version} + + io.springfox diff --git a/src/main/java/com/wayn/common/base/BaseEntity.java b/src/main/java/com/wayn/common/base/BaseEntity.java index 0a04070..73c4e37 100644 --- a/src/main/java/com/wayn/common/base/BaseEntity.java +++ b/src/main/java/com/wayn/common/base/BaseEntity.java @@ -1,5 +1,6 @@ package com.wayn.common.base; +import cn.afterturn.easypoi.excel.annotation.Excel; import com.baomidou.mybatisplus.annotation.TableField; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; @@ -18,6 +19,7 @@ public class BaseEntity implements Serializable { /** * 创建时间 */ + @Excel(name = "创建时间", format="yyyy-MM-dd HH:mm:ss" ,width = 25) @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; @@ -36,6 +38,7 @@ public class BaseEntity implements Serializable { /** * 备注 */ + @Excel(name = "备注", width = 25) private String remark; /** diff --git a/src/main/java/com/wayn/common/base/CommonController.java b/src/main/java/com/wayn/common/base/CommonController.java index 4810564..0bc6658 100644 --- a/src/main/java/com/wayn/common/base/CommonController.java +++ b/src/main/java/com/wayn/common/base/CommonController.java @@ -17,7 +17,6 @@ import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.File; /** * 通用请求处理 @@ -43,7 +42,7 @@ public class CommonController { } String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); - String filePath = WaynConfig.getUploadDir() + File.separatorChar + fileName; + String filePath = WaynConfig.getDownloadPath() + fileName; response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); diff --git a/src/main/java/com/wayn/common/util/excel/ExcelUtil.java b/src/main/java/com/wayn/common/util/excel/ExcelUtil.java new file mode 100644 index 0000000..cc57fde --- /dev/null +++ b/src/main/java/com/wayn/common/util/excel/ExcelUtil.java @@ -0,0 +1,67 @@ +package com.wayn.common.util.excel; + +import cn.afterturn.easypoi.excel.ExcelExportUtil; +import cn.afterturn.easypoi.excel.entity.ExportParams; +import com.wayn.framework.config.WaynConfig; +import com.wayn.project.system.domain.SysRole; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Workbook; + +import java.io.*; +import java.util.List; +import java.util.UUID; + +/** + * excel操作帮助类 + */ +@Slf4j +public class ExcelUtil { + + /** + * 编码文件名 + * + * @param filename 源文件名称 + * @return 编码后文件名 eg: 990xx002_测试.xlsx + */ + public static String encodingFilename(String filename) { + filename = UUID.randomUUID().toString() + "_" + filename; + return filename; + } + + /** + * 获取文件绝对路径 + * + * @param filename 文件 + * @return 绝对路径 + */ + public static String getAbsoluteFile(String filename) { + String downloadPath = WaynConfig.getDownloadPath() + filename; + File desc = new File(downloadPath); + if (!desc.getParentFile().exists()) { + desc.getParentFile().mkdirs(); + } + return downloadPath; + } + + /** + * 导出excel到指定文件中 + * @param list excel数据 + * @param originalName 文件名 + * @return 返回指定文件的名称 + */ + public static String exportExcel(List list, String originalName) { + ExportParams exportParams = new ExportParams(); + exportParams.setStyle(IExcelExportStylerImpl.class); + Workbook workbook = ExcelExportUtil.exportExcel(exportParams, + SysRole.class, list); + String filename = ExcelUtil.encodingFilename(originalName); + try (OutputStream out = new FileOutputStream(ExcelUtil.getAbsoluteFile(filename))) { + workbook.write(out); + } catch (FileNotFoundException e) { + log.error(e.getMessage(), e); + } catch (IOException exception) { + log.error(exception.getMessage(), exception); + } + return filename; + } +} diff --git a/src/main/java/com/wayn/common/util/excel/IExcelExportStylerImpl.java b/src/main/java/com/wayn/common/util/excel/IExcelExportStylerImpl.java new file mode 100644 index 0000000..d888f3c --- /dev/null +++ b/src/main/java/com/wayn/common/util/excel/IExcelExportStylerImpl.java @@ -0,0 +1,68 @@ +package com.wayn.common.util.excel; + + +import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler; +import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler; +import org.apache.poi.ss.usermodel.*; + +/** + * easypoi 自定义样式 + */ +public class IExcelExportStylerImpl extends AbstractExcelExportStyler + implements IExcelExportStyler{ + + public IExcelExportStylerImpl(Workbook workbook) { + super.createStyles(workbook); + } + + + @Override + public CellStyle getHeaderStyle(short headerColor) { + CellStyle titleStyle = workbook.createCellStyle(); + Font font = workbook.createFont(); + font.setFontHeightInPoints((short) 14); + titleStyle.setFont(font); + titleStyle.setAlignment(HorizontalAlignment.CENTER); + titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex()); + titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + return titleStyle; + } + + @Override + public CellStyle getTitleStyle(short color) { + CellStyle titleStyle = workbook.createCellStyle(); + titleStyle.setAlignment(HorizontalAlignment.CENTER); + titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); + titleStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex()); + titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + titleStyle.setWrapText(true); + return titleStyle; + } + + + @Override + public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) { + CellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + style.setDataFormat(STRING_FORMAT); + if (isWarp) { + style.setWrapText(true); + } + return style; + } + + @Override + public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) { + CellStyle style = workbook.createCellStyle(); + style.setAlignment(HorizontalAlignment.CENTER); + style.setVerticalAlignment(VerticalAlignment.CENTER); + style.setDataFormat(STRING_FORMAT); + if (isWarp) { + style.setWrapText(true); + } + return style; + } + +} diff --git a/src/main/java/com/wayn/framework/config/WaynConfig.java b/src/main/java/com/wayn/framework/config/WaynConfig.java index 4e2b963..10e4aed 100644 --- a/src/main/java/com/wayn/framework/config/WaynConfig.java +++ b/src/main/java/com/wayn/framework/config/WaynConfig.java @@ -27,6 +27,9 @@ public class WaynConfig { public static String getUploadDir() { return uploadDir; } + public static String getDownloadPath() { + return getUploadDir() + "/download/"; + } public void setUploadDir(String uploadDir) { WaynConfig.uploadDir = uploadDir; diff --git a/src/main/java/com/wayn/project/system/controller/RoleController.java b/src/main/java/com/wayn/project/system/controller/RoleController.java index 1a49f13..13a4350 100644 --- a/src/main/java/com/wayn/project/system/controller/RoleController.java +++ b/src/main/java/com/wayn/project/system/controller/RoleController.java @@ -5,10 +5,12 @@ import com.wayn.common.base.BaseController; import com.wayn.common.constant.SysConstants; import com.wayn.common.util.R; import com.wayn.common.util.SecurityUtils; +import com.wayn.common.util.excel.ExcelUtil; import com.wayn.project.system.domain.SysRole; import com.wayn.project.system.service.IRoleService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -16,6 +18,7 @@ import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.List; +@Slf4j @Api(value = "角色接口") @RestController @RequestMapping("system/role") @@ -76,4 +79,12 @@ public class RoleController extends BaseController { public R deleteRole(@PathVariable List roleIds) { return R.success().add("data", iRoleService.deleteRoleByIds(roleIds)); } + + @GetMapping("/export") + public R export(SysRole role) { + List list = iRoleService.list(role); + return R.success(ExcelUtil.exportExcel(list, "角色数据.xls")); + } + + } diff --git a/src/main/java/com/wayn/project/system/domain/SysRole.java b/src/main/java/com/wayn/project/system/domain/SysRole.java index 9a5146d..ff33467 100644 --- a/src/main/java/com/wayn/project/system/domain/SysRole.java +++ b/src/main/java/com/wayn/project/system/domain/SysRole.java @@ -1,5 +1,6 @@ package com.wayn.project.system.domain; +import cn.afterturn.easypoi.excel.annotation.Excel; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; @@ -8,6 +9,7 @@ import io.swagger.annotations.ApiModel; import lombok.Data; import lombok.EqualsAndHashCode; +import javax.validation.constraints.DecimalMin; import javax.validation.constraints.NotBlank; import java.util.List; @@ -24,30 +26,35 @@ public class SysRole extends BaseEntity { * 角色ID */ @TableId(type = IdType.AUTO) + @Excel(name = "角色编号", type = 10) private Long roleId; /** * 角色名称 */ @NotBlank(message = "角色名称不能为空") + @Excel(name = "角色名称") private String roleName; /** - * 角色权限 + * 权限字符 */ @NotBlank(message = "权限字符不能为空") + @Excel(name = "权限字符") private String roleKey; /** * 角色排序 */ - @NotBlank(message = "角色排序不能为空") - private String roleSort; + @DecimalMin(value = "0", message = "角色排序不能为空") + @Excel(name = "角色排序", type = 10) + private Integer roleSort; /** * 角色状态(0正常 1停用) */ - private String roleStatus; + @Excel(name = "角色状态", replace = {"启用_0", "禁用_1"}) + private Byte roleStatus; /** * 关联菜单id集合 @@ -58,11 +65,12 @@ public class SysRole extends BaseEntity { /** * 删除标志(0代表存在 1代表删除) */ - private String delFlag; + private Byte delFlag; public SysRole(Long roleId) { this.roleId = roleId; } + public SysRole() { } diff --git a/src/main/java/com/wayn/project/system/domain/SysUser.java b/src/main/java/com/wayn/project/system/domain/SysUser.java index 164c59d..74fbc48 100644 --- a/src/main/java/com/wayn/project/system/domain/SysUser.java +++ b/src/main/java/com/wayn/project/system/domain/SysUser.java @@ -65,7 +65,7 @@ public class SysUser extends BaseEntity { * 删除标志(0代表存在 1代表删除) */ @TableLogic - private Integer delFlag; + private Byte delFlag; public static boolean isAdmin(Long userId) { return userId != null && 1L == userId; diff --git a/src/main/java/com/wayn/project/system/mapper/RoleMapper.java b/src/main/java/com/wayn/project/system/mapper/RoleMapper.java index 57f9bb5..1a7e11c 100644 --- a/src/main/java/com/wayn/project/system/mapper/RoleMapper.java +++ b/src/main/java/com/wayn/project/system/mapper/RoleMapper.java @@ -11,4 +11,6 @@ public interface RoleMapper extends BaseMapper { List selectRoleByUserId(Long userId); IPage selectListPage(Page page, SysRole role); + + List selectList(SysRole role); } diff --git a/src/main/java/com/wayn/project/system/service/IRoleService.java b/src/main/java/com/wayn/project/system/service/IRoleService.java index 15872e3..ce48c92 100644 --- a/src/main/java/com/wayn/project/system/service/IRoleService.java +++ b/src/main/java/com/wayn/project/system/service/IRoleService.java @@ -18,7 +18,14 @@ public interface IRoleService extends IService { IPage listPage(Page page, SysRole role); /** - * 查询用户id查询权限 + * 查询角色列表 + * @param role 查询参数 + * @return 角色列表 + */ + List list(SysRole role); + + /** + * 根据用户id查询权限 * * @param userId 用户id * @return 权限列表 @@ -50,6 +57,7 @@ public interface IRoleService extends IService { /** * 保存角色信息和关联菜单 + * * @param role 角色信息 * @return boolean */ @@ -57,6 +65,7 @@ public interface IRoleService extends IService { /** * 更新角色信息和关联菜单 + * * @param role 角色信息 * @return boolean */ @@ -64,6 +73,7 @@ public interface IRoleService extends IService { /** * 通过角色id删除角色 + * * @param roleIds * @return boolean */ diff --git a/src/main/java/com/wayn/project/system/service/impl/RoleServiceImpl.java b/src/main/java/com/wayn/project/system/service/impl/RoleServiceImpl.java index cb7f161..5e1e7d1 100644 --- a/src/main/java/com/wayn/project/system/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/wayn/project/system/service/impl/RoleServiceImpl.java @@ -106,4 +106,9 @@ public class RoleServiceImpl extends ServiceImpl implements public IPage listPage(Page page, SysRole role) { return roleMapper.selectListPage(page, role); } + + @Override + public List list(SysRole role) { + return roleMapper.selectList(role); + } } diff --git a/src/main/resources/mapper/system/RoleMapper.xml b/src/main/resources/mapper/system/RoleMapper.xml index a0247f0..8cb0df8 100644 --- a/src/main/resources/mapper/system/RoleMapper.xml +++ b/src/main/resources/mapper/system/RoleMapper.xml @@ -30,12 +30,16 @@ + \ No newline at end of file