refactor(添加esaypoi): 角色管理添加导出按钮

master
wayn 5 years ago
parent 035d3e17d4
commit 1d7182c728

@ -24,6 +24,7 @@
<commons-lang3.version>3.8.1</commons-lang3.version>
<fastjson.version>1.2.58</fastjson.version>
<swagger.version>2.9.2</swagger.version>
<easypoi.version>4.1.0</easypoi.version>
</properties>
<dependencies>
@ -135,6 +136,23 @@
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>${easypoi.version}</version>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>${easypoi.version}</version>
</dependency>
<!-- swagger2-->
<dependency>
<groupId>io.springfox</groupId>

@ -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;
/**

@ -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");

@ -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<SysRole> 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;
}
}

@ -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;
}
}

@ -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;

@ -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<Long> roleIds) {
return R.success().add("data", iRoleService.deleteRoleByIds(roleIds));
}
@GetMapping("/export")
public R export(SysRole role) {
List<SysRole> list = iRoleService.list(role);
return R.success(ExcelUtil.exportExcel(list, "角色数据.xls"));
}
}

@ -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() {
}

@ -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;

@ -11,4 +11,6 @@ public interface RoleMapper extends BaseMapper<SysRole> {
List<String> selectRoleByUserId(Long userId);
IPage<SysRole> selectListPage(Page<SysRole> page, SysRole role);
List<SysRole> selectList(SysRole role);
}

@ -18,7 +18,14 @@ public interface IRoleService extends IService<SysRole> {
IPage<SysRole> listPage(Page<SysRole> page, SysRole role);
/**
* id
*
* @param role
* @return
*/
List<SysRole> list(SysRole role);
/**
* id
*
* @param userId id
* @return
@ -50,6 +57,7 @@ public interface IRoleService extends IService<SysRole> {
/**
*
*
* @param role
* @return boolean
*/
@ -57,6 +65,7 @@ public interface IRoleService extends IService<SysRole> {
/**
*
*
* @param role
* @return boolean
*/
@ -64,6 +73,7 @@ public interface IRoleService extends IService<SysRole> {
/**
* id
*
* @param roleIds
* @return boolean
*/

@ -106,4 +106,9 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, SysRole> implements
public IPage<SysRole> listPage(Page<SysRole> page, SysRole role) {
return roleMapper.selectListPage(page, role);
}
@Override
public List<SysRole> list(SysRole role) {
return roleMapper.selectList(role);
}
}

@ -30,12 +30,16 @@
</select>
<select id="selectListPage" parameterType="com.wayn.project.system.domain.SysRole" resultMap="SysRoleResult">
<include refid="selectRoleVo" />
<include refid="selectRoleVo"/>
<where>
<if test="role.roleId != null ">and r.role_id = #{role.roleId}</if>
<if test="role.roleName != null and role.roleName != '' ">and r.role_name like CONCAT('%',#{role.roleName},'%')</if>
<if test="role.roleKey != null and role.roleKey != '' ">and r.role_key like CONCAT('%',#{role.roleKey},'%')</if>
<if test="role.roleStatus != null and role.roleStatus != '' ">and r.role_status = #{role.roleStatus}</if>
<if test="role.roleName != null and role.roleName != '' ">and r.role_name like
CONCAT('%',#{role.roleName},'%')
</if>
<if test="role.roleKey != null and role.roleKey != '' ">and r.role_key like
CONCAT('%',#{role.roleKey},'%')
</if>
<if test="role.roleStatus != null ">and r.role_status = #{role.roleStatus}</if>
<if test="role.startTime != null and role.startTime != ''">
and DATE_FORMAT(r.create_time,'%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{role.startTime},'%Y-%m-%d')
</if>
@ -45,4 +49,24 @@
and r.del_flag = 0
</where>
</select>
<select id="selectList" parameterType="com.wayn.project.system.domain.SysRole" resultMap="SysRoleResult">
<include refid="selectRoleVo"/>
<where>
<if test="roleId != null ">and r.role_id = #{roleId}</if>
<if test="roleName != null and roleName != '' ">and r.role_name like
CONCAT('%',#{roleName},'%')
</if>
<if test="roleKey != null and roleKey != '' ">and r.role_key like
CONCAT('%',#{roleKey},'%')
</if>
<if test="roleStatus != null">and r.role_status = #{roleStatus}</if>
<if test="startTime != null and startTime != ''">
and DATE_FORMAT(r.create_time,'%Y-%m-%d') <![CDATA[ >= ]]> DATE_FORMAT(#{startTime},'%Y-%m-%d')
</if>
<if test="endTime != null and endTime != ''">
and DATE_FORMAT(r.create_time,'%Y-%m-%d') <![CDATA[ <= ]]> DATE_FORMAT(#{endTime},'%Y-%m-%d')
</if>
and r.del_flag = 0
</where>
</select>
</mapper>
Loading…
Cancel
Save