publish 0.0.3
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
package com.tiesheng.core.pojos.dto;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
public class PageDTO {
|
||||
|
||||
private Long pageNum;
|
||||
private Long pageSize;
|
||||
private String keyword;
|
||||
private String between;
|
||||
private String equals;
|
||||
private String order;
|
||||
|
||||
|
||||
/**
|
||||
* 启用分页
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public <T> Page<T> pageObj() {
|
||||
Page<T> page = new Page<>();
|
||||
page.setSize(getPageSize());
|
||||
page.setCurrent(getPageNum());
|
||||
return page;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 启用分页
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Page<Map<String, Object>> pageMap() {
|
||||
Page<Map<String, Object>> page = new Page<>();
|
||||
page.setSize(getPageSize());
|
||||
page.setCurrent(getPageNum());
|
||||
return page;
|
||||
}
|
||||
|
||||
/***
|
||||
* 启用排序
|
||||
* @param queryWrapper
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> void doOrder(QueryWrapper<T> queryWrapper) {
|
||||
if (!StrUtil.isEmpty(order)) {
|
||||
if (order.contains("-")) {
|
||||
queryWrapper.orderByDesc(StrUtil.toUnderlineCase(order.replace("-", "")));
|
||||
} else {
|
||||
queryWrapper.orderByAsc(StrUtil.toUnderlineCase(order));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 关键字查询
|
||||
*
|
||||
* @param queryWrapper
|
||||
* @param columns
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> void likeColumns(QueryWrapper<T> queryWrapper, String... columns) {
|
||||
keyword = StrUtil.trim(keyword);
|
||||
if (!StrUtil.isEmpty(keyword) && columns != null && columns.length > 0) {
|
||||
queryWrapper.and(s -> {
|
||||
for (String col : columns) {
|
||||
s.like(col, keyword).or();
|
||||
}
|
||||
});
|
||||
}
|
||||
betweenColumns(queryWrapper);
|
||||
equalsColumns(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 区间
|
||||
*
|
||||
* @param queryWrapper
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> void betweenColumns(QueryWrapper<T> queryWrapper) {
|
||||
if (!StrUtil.isEmpty(between)) {
|
||||
String[] bets = between.split("=");
|
||||
if (bets.length == 2) {
|
||||
String[] vals = bets[1].split(",");
|
||||
if (vals.length == 2) {
|
||||
queryWrapper.between(StrUtil.toUnderlineCase(bets[0]), vals[0], vals[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 匹配列
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
public <T> void equalsColumns(QueryWrapper<T> queryWrapper) {
|
||||
if (!StrUtil.isEmpty(equals)) {
|
||||
String[] kv = equals.split(",");
|
||||
for (String key : kv) {
|
||||
String[] cols = key.split("=");
|
||||
if (cols.length == 2) {
|
||||
queryWrapper.eq(StrUtil.toUnderlineCase(cols[0]), cols[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* between
|
||||
*
|
||||
* @param alias
|
||||
* @return
|
||||
*/
|
||||
public String betweenSql(String alias) {
|
||||
String sql = "";
|
||||
if (!StrUtil.isEmpty(between)) {
|
||||
String[] bets = between.split("=");
|
||||
if (bets.length == 2) {
|
||||
String[] vals = bets[1].split(",");
|
||||
if (vals.length == 2) {
|
||||
sql = sql + " and (" + alias + "." + StrUtil.toUnderlineCase(bets[0])
|
||||
+ " between '" + vals[0] + "' and '" + vals[1] + "')";
|
||||
}
|
||||
}
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取排序的sql
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getOrderSql(String alias) {
|
||||
String sql = "";
|
||||
if (!StrUtil.isEmpty(order)) {
|
||||
if (order.contains("-")) {
|
||||
sql = StrUtil.toUnderlineCase(order.replace("-", "")) + " desc";
|
||||
} else {
|
||||
sql = StrUtil.toUnderlineCase(order) + " asc";
|
||||
}
|
||||
sql = "order by " + alias + "." + sql;
|
||||
}
|
||||
return sql;
|
||||
}
|
||||
|
||||
public String getEqualsSql(String alias) {
|
||||
StringBuilder sql = new StringBuilder();
|
||||
if (!StrUtil.isEmpty(equals)) {
|
||||
String[] kv = equals.split(",");
|
||||
for (String key : kv) {
|
||||
String[] cols = key.split("=");
|
||||
if (cols.length == 2) {
|
||||
sql.append(" and ").append(alias).append(".").append(StrUtil.toUnderlineCase(cols[0])).append("=").append(cols[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
public Long getPageNum() {
|
||||
return pageNum == null ? 1 : pageNum;
|
||||
}
|
||||
|
||||
public void setPageNum(Long pageNum) {
|
||||
this.pageNum = pageNum;
|
||||
}
|
||||
|
||||
public Long getPageSize() {
|
||||
return pageSize == null ? 10 : pageSize;
|
||||
}
|
||||
|
||||
public void setPageSize(Long pageSize) {
|
||||
this.pageSize = pageSize;
|
||||
}
|
||||
|
||||
public String getKeyword() {
|
||||
return keyword;
|
||||
}
|
||||
|
||||
public void setKeyword(String keyword) {
|
||||
this.keyword = keyword;
|
||||
}
|
||||
|
||||
public String getBetween() {
|
||||
return between;
|
||||
}
|
||||
|
||||
public void setBetween(String between) {
|
||||
this.between = between;
|
||||
}
|
||||
|
||||
public String getEquals() {
|
||||
return equals;
|
||||
}
|
||||
|
||||
public void setEquals(String equals) {
|
||||
this.equals = equals;
|
||||
}
|
||||
|
||||
public String getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(String order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.tiesheng.core.service;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import com.tiesheng.core.util.servlet.ServletKit;
|
||||
import com.tiesheng.util.TimedCacheHelper;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
* @ProjectName cmcc
|
||||
* @Copyright Hangzhou ShuoChuang Technology Co.,Ltd All Right Reserved
|
||||
* @Description 这里是对文件的描述
|
||||
* @data 2019-06-19
|
||||
* @note 这里写文件的详细功能和改动
|
||||
* @note
|
||||
*/
|
||||
@Service
|
||||
public class TimedCacheService {
|
||||
|
||||
|
||||
/**
|
||||
* 清空数据
|
||||
*
|
||||
* @param key
|
||||
*/
|
||||
public void clearByKey(String key) {
|
||||
TimedCacheHelper.getTimedCache().remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 放入缓存
|
||||
*
|
||||
* @param key
|
||||
* @param val
|
||||
*/
|
||||
public void putCache(String key, String val) {
|
||||
TimedCacheHelper.getTimedCache().put(key, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取缓存
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public String getCache(String key) {
|
||||
return TimedCacheHelper.getTimedCache().get(key);
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// 图片验证码缓存
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/**
|
||||
* 缓存 图片验证码
|
||||
*
|
||||
* @param session
|
||||
* @param value
|
||||
*/
|
||||
public void setImageCode(String session, String value) {
|
||||
putCache(session, value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证 图片验证码
|
||||
* 该验证兼容V2的图片验证码
|
||||
*
|
||||
* @param session
|
||||
*/
|
||||
public void verifyImage(String session, String value) {
|
||||
String captchaKey = ServletUtil.getHeader(ServletKit.getRequest(), "captcha", "utf-8");
|
||||
String cache = !StrUtil.isEmpty(captchaKey) ? getCache(captchaKey) : getCache(session);
|
||||
if (StrUtil.isEmpty(cache) || !StrUtil.equals(cache, value)) {
|
||||
throw new ApiException("验证码不正确");
|
||||
}
|
||||
clearByKey(session);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package com.tiesheng.core.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.io.FileTypeUtil;
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.io.file.FileNameUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.tiesheng.util.exception.ApiException;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ToolsFileService {
|
||||
|
||||
private String ext;
|
||||
private String absPath;
|
||||
private String folder = "/upload";
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// 方法
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/**
|
||||
* 检查文件类型
|
||||
*
|
||||
* @param fileType
|
||||
*/
|
||||
private void checkFileType(String fileType) {
|
||||
if (!StrUtil.isEmpty(ext) && !StrUtil.split(ext, ",").contains(fileType)) {
|
||||
throw new ApiException("该文件格式不支持上传");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取upload目录的绝对路径
|
||||
*
|
||||
* @param dest 文件名称,如果没有/upload,则会添加/upload
|
||||
* @return
|
||||
*/
|
||||
public String getUploadAbsPath(String dest) {
|
||||
String destFile = absPath;
|
||||
if (StrUtil.isEmpty(destFile)) {
|
||||
ClassPathResource classPathResource = new ClassPathResource("static");
|
||||
File rootFile = new File(classPathResource.getPath());
|
||||
destFile = rootFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
if (!StrUtil.startWith(dest, folder)) {
|
||||
destFile = destFile + folder;
|
||||
}
|
||||
|
||||
return String.format("%s/%s", destFile, dest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文件
|
||||
*
|
||||
* @param file
|
||||
*/
|
||||
public String saveMultipartFile(MultipartFile file) {
|
||||
try {
|
||||
|
||||
// 获取文件类型
|
||||
String fileType = FileNameUtil.extName(file.getOriginalFilename());
|
||||
if (StrUtil.isEmpty(fileType)) {
|
||||
fileType = FileTypeUtil.getType(file.getInputStream(), file.getOriginalFilename());
|
||||
}
|
||||
if (StrUtil.isEmpty(fileType)) {
|
||||
fileType = "unknow";
|
||||
}
|
||||
checkFileType(fileType);
|
||||
|
||||
String filePath = String.format("/%s/%s.%s", DateUtil.format(DateUtil.date(), "yyyy-MM"), IdUtil.simpleUUID(), fileType);
|
||||
|
||||
InputStream stream = file.getInputStream();
|
||||
FileUtil.writeFromStream(stream, getUploadAbsPath(filePath));
|
||||
IoUtil.close(stream);
|
||||
|
||||
return folder + filePath;
|
||||
} catch (Exception e) {
|
||||
throw new ApiException("文件流读取失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// 文件分包上传
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* 得到块文件的根目录,上传完成之后便于删除
|
||||
*
|
||||
* @param fileMd5
|
||||
* @return
|
||||
*/
|
||||
private String getChunkFolder(String fileMd5) {
|
||||
String folder = getUploadAbsPath(fileMd5);
|
||||
FileUtil.mkdir(folder);
|
||||
return folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查待上传文件是否存在
|
||||
*
|
||||
* @param fileMd5
|
||||
* @param fileExt
|
||||
*/
|
||||
public void register(String fileMd5, String fileExt) {
|
||||
checkFileType(fileExt);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查块文件
|
||||
*
|
||||
* @param fileMd5
|
||||
* @param chunk
|
||||
* @return
|
||||
*/
|
||||
public boolean checkChunk(String fileMd5, Integer chunk) {
|
||||
// 1,得到块文件所在路径
|
||||
String chunkFileFolder = getChunkFolder(fileMd5);
|
||||
// 2,块文件的文件名称以1,2,3..序号命名,没有扩展名
|
||||
File chunkFile = FileUtil.file(chunkFileFolder, chunk.toString());
|
||||
return chunkFile.exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* 块文件上传
|
||||
*
|
||||
* @param file
|
||||
* @param fileMd5
|
||||
* @param chunk
|
||||
*/
|
||||
public void uploadChunk(MultipartFile file, String fileMd5, Integer chunk) {
|
||||
if (file == null) {
|
||||
throw new ApiException("请选择文件后上传");
|
||||
}
|
||||
|
||||
try {
|
||||
// 块文件
|
||||
File chunkfile = new File(getChunkFolder(fileMd5), chunk.toString());
|
||||
InputStream stream = file.getInputStream();
|
||||
FileUtil.writeFromStream(stream, chunkfile);
|
||||
IoUtil.close(stream);
|
||||
} catch (Exception ignored) {
|
||||
throw new ApiException("块文件上传失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并块文件
|
||||
*
|
||||
* @param fileMd5
|
||||
* @param fileExt
|
||||
*/
|
||||
public String mergeChunks(String fileMd5, String fileExt) {
|
||||
|
||||
// 1,获取块文件的路径
|
||||
File chunkFileFolder = new File(getChunkFolder(fileMd5));
|
||||
|
||||
// 2,合并文件目录
|
||||
String filePath = String.format("/%s/%s.%s", DateUtil.format(DateUtil.date(), "yyyy-MM"), fileMd5, fileExt);
|
||||
String outputPath = getUploadAbsPath(filePath);
|
||||
|
||||
// 3,如果文件不存在
|
||||
if (!FileUtil.exist(outputPath)) {
|
||||
|
||||
// 4,获取块文件,此列表是已经排好序的列表
|
||||
List<File> chunkFiles = getChunkFiles(chunkFileFolder);
|
||||
|
||||
// 5,合并文件
|
||||
File newFile = FileUtil.newFile(outputPath);
|
||||
for (File file : chunkFiles) {
|
||||
byte[] bytes = FileUtil.readBytes(file);
|
||||
FileUtil.writeBytes(bytes, newFile, 0, bytes.length, true);
|
||||
}
|
||||
}
|
||||
|
||||
// 6,删除块文件目录
|
||||
FileUtil.del(chunkFileFolder);
|
||||
|
||||
return folder + filePath;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有块文件
|
||||
*
|
||||
* @param chunkFileFolder
|
||||
* @return
|
||||
*/
|
||||
private List<File> getChunkFiles(File chunkFileFolder) {
|
||||
|
||||
//获取路径下的所有块文件
|
||||
File[] chunkFiles = chunkFileFolder.listFiles();
|
||||
if (chunkFiles == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
//将文件数组转成list,并排序
|
||||
List<File> chunkFileList = new ArrayList<>(Arrays.asList(chunkFiles));
|
||||
//排序
|
||||
chunkFileList.sort((o1, o2) -> {
|
||||
if (Integer.parseInt(o1.getName()) > Integer.parseInt(o2.getName())) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
return chunkFileList;
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter、getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getAbsPath() {
|
||||
return absPath;
|
||||
}
|
||||
|
||||
public void setAbsPath(String absPath) {
|
||||
this.absPath = absPath;
|
||||
}
|
||||
|
||||
public String getExt() {
|
||||
return ext;
|
||||
}
|
||||
|
||||
public void setExt(String ext) {
|
||||
this.ext = ext;
|
||||
}
|
||||
|
||||
public String getFolder() {
|
||||
return folder;
|
||||
}
|
||||
|
||||
public void setFolder(String folder) {
|
||||
this.folder = folder;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user