1、添加生成文件编码功能

master
xiong_cl 7 months ago
parent 8b617889f3
commit bbf0e7f577
  1. 208
      shandan-system/src/main/java/com/keyware/shandan/system/controller/SysFileController.java

@ -4,27 +4,28 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keyware.shandan.bianmu.service.DataLabelsService;
import com.keyware.shandan.common.controller.BaseController;
import com.keyware.shandan.common.entity.Result;
import com.keyware.shandan.common.util.FileDownload;
import com.keyware.shandan.common.util.PoiFileReadUtil;
import com.keyware.shandan.frame.properties.CustomProperties;
import com.keyware.shandan.system.constants.FormTypeEnum;
import com.keyware.shandan.system.entity.SysFile;
import com.keyware.shandan.system.entity.SysFileChunk;
import com.keyware.shandan.system.entity.SysFormConfig;
import com.keyware.shandan.system.entity.SysSetting;
import com.keyware.shandan.system.entity.*;
import com.keyware.shandan.system.service.SysDictService;
import com.keyware.shandan.system.service.SysFileService;
import com.keyware.shandan.system.service.SysFormConfigService;
import com.keyware.shandan.system.service.SysSettingService;
import com.keyware.shandan.system.utils.DictUtil;
import com.keyware.shandan.system.utils.FileChunkUploadUtil;
import lombok.val;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
@ -32,12 +33,11 @@ import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.StreamSupport;
/**
* <p>
@ -63,9 +63,26 @@ public class SysFileController extends BaseController<SysFileService, SysFile, S
@Autowired
private DataLabelsService labelsService;
@Autowired
private SysFormConfigService formConfigService;
@Autowired
private SysDictService sysDictService;
//上传文件自动编号
private static Counter counter = new Counter();
private static String LAST_DATE = "";
static class Counter {
private final AtomicInteger count = new AtomicInteger(0);
public void increment() {
count.incrementAndGet();
}
public int getCount() {
return count.get();
}
}
@GetMapping("/layer")
public ModelAndView fileUploadLayer(ModelAndView mav) {
mav.setViewName("sys/file/fileUploadLayer");
@ -86,6 +103,9 @@ public class SysFileController extends BaseController<SysFileService, SysFile, S
public ModelAndView fileEditLayer(ModelAndView mav, String id) {
mav.setViewName("sys/file/fileEditLayer");
SysFile file = sysFileService.getById(id);
if(StringUtils.isEmpty(file.getDataType())){
file.setDataType("2");
}
mav.addObject("file", file);
mav.addObject("LabelConfigSet", labelsService.formConfigList());
return mav;
@ -119,6 +139,9 @@ public class SysFileController extends BaseController<SysFileService, SysFile, S
mav.addObject("fileViewUrl", fileViewUrl.replace("\\", "/"));
mav.addObject("downloadUrl", downloadUrl);
if(StringUtils.isEmpty(file.getDataType())){
file.setDataType("2");
}
mav.addObject("fileData", file);
mav.addObject("file_inputDate", inputDate);
String fileSize = FileUtil.readableFileSize((long) (file.getFileSize() * 1024 * 1024));
@ -181,8 +204,14 @@ public class SysFileController extends BaseController<SysFileService, SysFile, S
*/
@PostMapping("/upload/chunk")
public Result<Object> uploadChunk(MultipartFile file, SysFile fileInfo, SysFileChunk fileChunk) throws Exception {
//自动编号,每天从一开始
String curDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
if("".equals(LAST_DATE) || curDate.compareTo(LAST_DATE) > 0){
LAST_DATE = curDate;
counter = new Counter();
}
fileInfo.setFileCode(createFileCode(fileInfo));
return Result.of(FileChunkUploadUtil.uploadChunk(file, fileInfo, fileChunk));
}
/**
@ -193,14 +222,163 @@ public class SysFileController extends BaseController<SysFileService, SysFile, S
*/
@PostMapping("/upload/chunk/complete")
public Result<Object> mergerChunk(SysFile file) throws Exception {
//根据文件属性进行编码
System.out.println(file);
FileChunkUploadUtil.mergeChunk(file, sysFile -> sysFileService.save(sysFile));
return Result.of(sysFileService.autoCreateDirAndUpdateFile(file));
}
/**
* 生成文件编码
*
* @param file 文件信息
* @return 结果
*/
@PostMapping("/upload/file/code")
public Result<Object> fileCode(SysFile file) {
return Result.of(createFileCode(file));
}
/**
* 生成文件编码
*
* @param ext 文件后缀
* @return 结果
*/
@GetMapping("/upload/file/ext")
public Result<Object> initFileData(String ext) {
QueryWrapper qw = new QueryWrapper<SysFile>();
qw.eq("FILE_TYPE",ext);
qw.orderByDesc("CREATE_TIME");
qw.last("limit 1");
SysFile one = sysFileService.getOne(qw);
return Result.of(one);
}
/**
* 生成文件编码工具
* @param file
* @return
*/
public synchronized String createFileCode(SysFile file){
//根据文件属性进行编码
StringBuilder sb = new StringBuilder();
//日期
if(!ObjectUtils.isEmpty(file.getInputDate())) {
sb.append(new SimpleDateFormat("yyyyMMdd").format(file.getInputDate())).append("+");
}else{
sb.append(new SimpleDateFormat("yyyyMMdd").format(new Date())).append("+");
}
//文件密级
if(!ObjectUtils.isEmpty(file.getSecretLevel())){
QueryWrapper<SysDict> qw = new QueryWrapper<>();
qw.eq("DICT_CODE", file.getSecretLevel());
qw.eq("TYPE_ID","secret_level");
List<SysDict> dictCode = sysDictService.list(qw);
if(dictCode.size()>0){
sb.append(pinyinHelper(dictCode.get(0).getDictName(),false)).append("+");
}
}else{
sb.append("0").append("+");
}
//数据类型
if(!StringUtils.isEmpty(file.getDataType())){
QueryWrapper<SysDict> qw = new QueryWrapper<>();
qw.eq("DICT_CODE", file.getDataType());
qw.eq("TYPE_ID","data_type");
List<SysDict> dictCode = sysDictService.list(qw);
if(dictCode.size()>0){
sb.append(pinyinHelper(dictCode.get(0).getDictName(),true)).append("+");
}
}else{
sb.append("0").append("+");
}
//任务代号
if(!StringUtils.isEmpty(file.getTaskCode())){
QueryWrapper<SysDict> qw = new QueryWrapper<>();
qw.eq("DICT_CODE", file.getTaskCode());
qw.eq("TYPE_ID","task_code");
List<SysDict> dictCode = sysDictService.list(qw);
if(dictCode.size()>0){
sb.append(pinyinHelper(dictCode.get(0).getDictName(),true).replace(" ","-")).append("+");
}
}else{
sb.append("0").append("+");
}
//单位
if(!StringUtils.isEmpty(file.getTroopCode())){
QueryWrapper<SysDict> qw = new QueryWrapper<>();
qw.eq("DICT_CODE", file.getTroopCode());
qw.eq("TYPE_ID","troop_code");
List<SysDict> dictCode = sysDictService.list(qw);
if(dictCode.size()>0){
sb.append(pinyinHelper(dictCode.get(0).getDictName(),true)).append("+");
}
}else{
sb.append("0").append("+");
}
//装备型号
if(!StringUtils.isEmpty(file.getEquipmentModel())){
QueryWrapper<SysDict> qw = new QueryWrapper<>();
qw.eq("DICT_CODE", file.getEquipmentModel());
qw.eq("TYPE_ID","equipment_model");
List<SysDict> dictCode = sysDictService.list(qw);
if(dictCode.size()>0){
sb.append(pinyinHelper(dictCode.get(0).getDictName(),true)).append("+");
}
}else{
sb.append("0").append("+");
}
//资料类型,文件后缀大写
if(!StringUtils.isEmpty(file.getFileFullName())){
String fullName = file.getFileFullName();
sb.append(fullName.substring(fullName.lastIndexOf(".")+1).toUpperCase()).append("+");
}else{
sb.append("0").append("+");
}
if(!StringUtils.isEmpty(file.getFileNumbers())){
JSONArray objects = JSONArray.parseArray(file.getFileNumbers());
for(int i=0;i<objects.size();i++){
if(file.getFileFullName().equals(objects.getJSONObject(i).getString("name"))){
file.setFileNumber(objects.getJSONObject(i).getString("number"));
}
}
}
if(!StringUtils.isEmpty(file.getFileNumber())){
sb.append(file.getFileNumber());
}else {
//查询当天数据的数量
DecimalFormat g1 = new DecimalFormat("00000000");
counter.increment();
sb.append(g1.format(counter.getCount()));
}
return sb.toString();
}
/**
* 生成汉字拼音
* @param str
* @param isFirst 是否只保留首字母
* @return
*/
public String pinyinHelper(String str, boolean isFirst) {
if(StringUtils.isEmpty(str)){
return "";
}
StringBuilder temp = new StringBuilder();
for(char c : str.toCharArray()){
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
if (pinyinArray != null) {
if(isFirst){
temp.append(pinyinArray[0].substring(0, 1).toUpperCase());
}else {
temp.append((pinyinArray[0].substring(0, 1).toUpperCase() + pinyinArray[0].substring(1)).replaceAll("\\d", ""));
}
} else {
temp.append(c);
}
}
return temp.toString();
}
/**
* 文件下载
*