feat 用户增加导出excel功能
This commit is contained in:
parent
845d5a526a
commit
9ab3d7e624
|
|
@ -1,5 +1,6 @@
|
|||
package com.mdd.admin.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.mdd.admin.aop.Log;
|
||||
import com.mdd.admin.service.IUserService;
|
||||
import com.mdd.admin.validate.user.UserSearchValidate;
|
||||
|
|
@ -9,6 +10,7 @@ import com.mdd.admin.validate.user.UserWalletValidate;
|
|||
import com.mdd.admin.vo.user.UserVo;
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.common.core.PageResult;
|
||||
import com.mdd.common.util.StringUtils;
|
||||
import com.mdd.common.validator.annotation.IDMust;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
|
|
@ -27,10 +29,20 @@ public class UserController {
|
|||
|
||||
@GetMapping("/lists")
|
||||
@ApiOperation(value="用户列表")
|
||||
public AjaxResult<PageResult<UserVo>> list(@Validated PageValidate pageValidate,
|
||||
public AjaxResult<Object> list(@Validated PageValidate pageValidate,
|
||||
@Validated UserSearchValidate searchValidate) {
|
||||
PageResult<UserVo> list = iUserService.list(pageValidate, searchValidate);
|
||||
return AjaxResult.success(list);
|
||||
if (StringUtils.isNotNull(searchValidate.getExport()) && searchValidate.getExport().equals(1)) {
|
||||
JSONObject ret = iUserService.getExportData(pageValidate, searchValidate);
|
||||
return AjaxResult.success(ret);
|
||||
} else if (StringUtils.isNotNull(searchValidate.getExport()) && searchValidate.getExport().equals(2)) {
|
||||
String path = iUserService.export(searchValidate);
|
||||
return AjaxResult.success(new JSONObject() {{
|
||||
put("url", path);
|
||||
}});
|
||||
} else {
|
||||
PageResult<UserVo> list = iUserService.list(pageValidate, searchValidate);
|
||||
return AjaxResult.success(list);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/detail")
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.mdd.admin.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.mdd.admin.validate.user.UserSearchValidate;
|
||||
import com.mdd.admin.validate.user.UserUpdateValidate;
|
||||
import com.mdd.admin.validate.commons.PageValidate;
|
||||
|
|
@ -47,4 +48,14 @@ public interface IUserService {
|
|||
*/
|
||||
void adjustWallet(UserWalletValidate userWalletValidate);
|
||||
|
||||
/**
|
||||
* 返回导出格式
|
||||
* @return
|
||||
*/
|
||||
JSONObject getExportData(PageValidate pageValidate, UserSearchValidate searchValidate);
|
||||
|
||||
/**
|
||||
* 导出
|
||||
*/
|
||||
String export(UserSearchValidate searchValidate);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
package com.mdd.admin.service.impl;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.support.ExcelTypeEnum;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
|
|
@ -9,6 +12,7 @@ import com.mdd.admin.validate.user.UserSearchValidate;
|
|||
import com.mdd.admin.validate.user.UserUpdateValidate;
|
||||
import com.mdd.admin.validate.commons.PageValidate;
|
||||
import com.mdd.admin.validate.user.UserWalletValidate;
|
||||
import com.mdd.admin.vo.user.UserListExportVo;
|
||||
import com.mdd.admin.vo.user.UserVo;
|
||||
import com.mdd.common.core.PageResult;
|
||||
import com.mdd.common.entity.user.User;
|
||||
|
|
@ -17,15 +21,15 @@ import com.mdd.common.enums.LogMoneyEnum;
|
|||
import com.mdd.common.exception.OperateException;
|
||||
import com.mdd.common.mapper.log.UserAccountLogMapper;
|
||||
import com.mdd.common.mapper.user.UserMapper;
|
||||
import com.mdd.common.util.StringUtils;
|
||||
import com.mdd.common.util.TimeUtils;
|
||||
import com.mdd.common.util.UrlUtils;
|
||||
import com.mdd.common.util.*;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
|
@ -232,4 +236,94 @@ public class UserServiceImpl implements IUserService {
|
|||
userMapper.updateById(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject getExportData(PageValidate pageValidate, UserSearchValidate searchValidate) {
|
||||
Integer page = pageValidate.getPage_no();
|
||||
Integer limit = pageValidate.getPage_size();
|
||||
PageResult<UserVo> userVoPageResult = this.list(pageValidate, searchValidate);
|
||||
JSONObject ret = ToolUtils.getExportData(userVoPageResult.getCount(), limit, searchValidate.getPage_start(), searchValidate.getPage_end(),"用户记录列表");
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String export(UserSearchValidate searchValidate) {
|
||||
PageValidate pageValidate = new PageValidate();
|
||||
if (StringUtils.isNotNull(searchValidate.getPage_start())) {
|
||||
pageValidate.setPage_no(searchValidate.getPage_start());
|
||||
} else {
|
||||
pageValidate.setPage_no(1);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotNull(searchValidate.getPage_end()) && StringUtils.isNotNull(searchValidate.getPage_size())) {
|
||||
pageValidate.setPage_size(searchValidate.getPage_end() * searchValidate.getPage_size());
|
||||
} else {
|
||||
pageValidate.setPage_size(20);
|
||||
}
|
||||
Boolean isAll = StringUtils.isNull(searchValidate.getPage_type()) || searchValidate.getPage_type().equals(0) ? true : false;
|
||||
List<UserListExportVo> excellist = this.getExcellist(isAll, pageValidate, searchValidate);
|
||||
String fileName = StringUtils.isNull(searchValidate.getFile_name()) ? ToolUtils.makeUUID() : searchValidate.getFile_name();
|
||||
String folderPath = "/excel/export/"+ TimeUtils.timestampToDay(System.currentTimeMillis() / 1000) +"/" ;
|
||||
String path = folderPath + fileName +".xlsx";
|
||||
String filePath = YmlUtils.get("like.upload-directory") + path;
|
||||
File folder = new File(YmlUtils.get("like.upload-directory") + folderPath);
|
||||
if (!folder.exists()) {
|
||||
if (!folder.mkdirs()) {
|
||||
throw new OperateException("创建文件夹失败");
|
||||
}
|
||||
}
|
||||
EasyExcel.write(filePath)
|
||||
.head(UserListExportVo.class)
|
||||
.excelType(ExcelTypeEnum.XLSX)
|
||||
.sheet("兑换记录")
|
||||
.doWrite(excellist);
|
||||
return UrlUtils.toAbsoluteUrl(path);
|
||||
}
|
||||
|
||||
private List<UserListExportVo> getExcellist(boolean isAll, PageValidate pageValidate, UserSearchValidate searchValidate) {
|
||||
Integer page = pageValidate.getPage_no();
|
||||
Integer limit = pageValidate.getPage_size();
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.isNull("delete_time");
|
||||
queryWrapper.orderByDesc("id");
|
||||
queryWrapper.select(User.class, info->
|
||||
!info.getColumn().equals("delete_time") &&
|
||||
!info.getColumn().equals("update_time") &&
|
||||
!info.getColumn().equals("password") &&
|
||||
!info.getColumn().equals("salt")
|
||||
);
|
||||
|
||||
if (StringUtils.isNotNull(searchValidate.getKeyword()) && StringUtils.isNotEmpty(searchValidate.getKeyword())) {
|
||||
String keyword = searchValidate.getKeyword();
|
||||
queryWrapper.nested(wq->wq
|
||||
.like("sn", keyword).or()
|
||||
.like("nickname", keyword).or()
|
||||
.like("mobile", keyword));
|
||||
}
|
||||
|
||||
userMapper.setSearch(queryWrapper, searchValidate, new String[]{
|
||||
"=:channel:int",
|
||||
"datetime:createTimeStart-createTimeEnd@create_time:str"
|
||||
});
|
||||
|
||||
List<UserListExportVo> retList = new ArrayList<>();
|
||||
List<User> userList = new ArrayList<>();
|
||||
if (isAll) {
|
||||
userList = userMapper.selectList(queryWrapper);
|
||||
} else {
|
||||
IPage<User> iPage = userMapper.selectPage( new Page<>(page, limit), queryWrapper);
|
||||
userList = iPage.getRecords();
|
||||
}
|
||||
for (User user : userList) {
|
||||
UserListExportVo vo = new UserListExportVo();
|
||||
BeanUtils.copyProperties(user, vo);
|
||||
vo.setSex(user.getSex());
|
||||
vo.setChannel(ClientEnum.getMsgByCode(user.getChannel()));
|
||||
vo.setAvatar(UrlUtils.toAbsoluteUrl(user.getAvatar()));
|
||||
vo.setLoginTime(TimeUtils.timestampToDate(user.getLoginTime()));
|
||||
vo.setCreateTime(TimeUtils.timestampToDate(user.getCreateTime()));
|
||||
retList.add(vo);
|
||||
}
|
||||
|
||||
return retList;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,4 +24,21 @@ public class UserSearchValidate implements Serializable {
|
|||
@ApiModelProperty(value = "结束时间")
|
||||
private String createTimeEnd;
|
||||
|
||||
@ApiModelProperty(value = "导出信息")
|
||||
private Integer export;
|
||||
@ApiModelProperty(value = "file_name")
|
||||
private String file_name;
|
||||
|
||||
@ApiModelProperty(value = "page_start")
|
||||
private Integer page_start;
|
||||
|
||||
@ApiModelProperty(value = "page_end")
|
||||
private Integer page_end;
|
||||
|
||||
@ApiModelProperty(value = "page_size")
|
||||
private Integer page_size;
|
||||
|
||||
@ApiModelProperty(value = "page_type")
|
||||
private Integer page_type;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,80 @@
|
|||
package com.mdd.admin.vo.user;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
@Data
|
||||
@ApiModel("用户记录列表Vo")
|
||||
public class UserListExportVo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@ApiModelProperty(value = "用户ID")
|
||||
@ExcelProperty("ID")
|
||||
@ColumnWidth(50)
|
||||
private Integer id;
|
||||
@ApiModelProperty(value = "用户编码")
|
||||
@ExcelProperty("用户编码")
|
||||
@ColumnWidth(50)
|
||||
private Integer sn;
|
||||
@ApiModelProperty(value = "用户头像")
|
||||
@ExcelProperty("用户头像")
|
||||
@ColumnWidth(50)
|
||||
private String avatar;
|
||||
@ApiModelProperty(value = "真实姓名")
|
||||
@ExcelProperty("真实姓名")
|
||||
@ColumnWidth(50)
|
||||
private String realName;
|
||||
@ApiModelProperty(value = "用户昵称")
|
||||
@ExcelProperty("用户昵称")
|
||||
@ColumnWidth(50)
|
||||
private String nickname;
|
||||
@ApiModelProperty(value = "登录账号")
|
||||
@ExcelProperty("登录账号")
|
||||
@ColumnWidth(50)
|
||||
private String account;
|
||||
@ApiModelProperty(value = "手机号码")
|
||||
@ExcelProperty("手机号码")
|
||||
@ColumnWidth(50)
|
||||
private String mobile;
|
||||
@ApiModelProperty(value = "用户性别")
|
||||
@ExcelProperty("用户性别")
|
||||
@ColumnWidth(50)
|
||||
private String sex;
|
||||
@ApiModelProperty(value = "注册渠道")
|
||||
@ExcelProperty("注册渠道")
|
||||
@ColumnWidth(50)
|
||||
private String channel;
|
||||
@ApiModelProperty(value = "最后登录IP")
|
||||
@ExcelProperty("最后登录IP")
|
||||
@ColumnWidth(50)
|
||||
private String loginIp;
|
||||
|
||||
@ApiModelProperty(value = "最后登录时间")
|
||||
@ExcelProperty("最后登录时间")
|
||||
@ColumnWidth(50)
|
||||
private String loginTime;
|
||||
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
@ExcelProperty("创建时间")
|
||||
@ColumnWidth(50)
|
||||
private String createTime;
|
||||
public void setSex(Integer sex) {
|
||||
switch (sex) {
|
||||
case 0:
|
||||
this.sex = "未知";
|
||||
break;
|
||||
case 1:
|
||||
this.sex = "男";
|
||||
break;
|
||||
case 2:
|
||||
this.sex = "女";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
package com.mdd.common.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 全局配置
|
||||
*/
|
||||
public class ProjectConfig {
|
||||
// 列表页
|
||||
public static Map<String, Integer> lists = new HashMap<String, Integer>(){{
|
||||
put("pageSizeMax", 25000);
|
||||
put("pageSize", 25);
|
||||
}};
|
||||
}
|
||||
|
|
@ -538,4 +538,15 @@ public class TimeUtils {
|
|||
// long sec = diff % nd % nh % nm / ns;
|
||||
return day + "天" + hour + "小时" + min + "分钟";
|
||||
}
|
||||
|
||||
/**
|
||||
* 时间戳转日期
|
||||
*
|
||||
* @author fzr
|
||||
* @param time 时间戳
|
||||
* @return String
|
||||
*/
|
||||
public static String timestampToDay(Long time) {
|
||||
return new SimpleDateFormat("yyyy-MM-dd").format(new Date(time * 1000));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package com.mdd.common.util;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.mdd.common.config.GlobalConfig;
|
||||
import com.mdd.common.config.ProjectConfig;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
|
|
@ -202,4 +204,29 @@ public class ToolUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static JSONObject getExportData(Long total, Integer pageSize, Integer pageStart, Integer pageEnd, String fileName) {
|
||||
JSONObject ret = new JSONObject();
|
||||
Integer cpPageSize = StringUtils.isNull(pageSize) ? ProjectConfig.lists.get("pageSize") : pageSize;
|
||||
Integer sumPage = getMaxCeil(total.intValue(), cpPageSize);
|
||||
Integer pageSizeMax = ProjectConfig.lists.get("pageSizeMax");
|
||||
ret.put("count", total); //所有数据记录数
|
||||
ret.put("page_size", cpPageSize); //每页记录数
|
||||
ret.put("sum_page", sumPage); //一共多少页
|
||||
ret.put("max_page", Math.ceil(pageSizeMax / cpPageSize));
|
||||
ret.put("all_max_size", pageSizeMax);
|
||||
ret.put("page_end", Math.min(sumPage, StringUtils.isNull(pageEnd) ? 200 : pageEnd));
|
||||
ret.put("page_start", StringUtils.isNull(pageStart) ? 1 : pageStart);
|
||||
ret.put("file_name", fileName);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 与1比较,返回更大的值
|
||||
* @param count
|
||||
* @param pageSize
|
||||
* @return
|
||||
*/
|
||||
public static Integer getMaxCeil(Integer count, Integer pageSize) {
|
||||
return Math.max((int)Math.ceil((double)count / (double)pageSize), 1);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue