[回退更改](master): 回退到最新版本

回退错误代码
master
土豆兄弟 3 years ago
parent 6060c73073
commit 5e61db3a54

@ -9,7 +9,7 @@ import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-08-05T14:00:16+0800",
date = "2021-08-05T16:32:05+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)"
)
@Component

@ -9,7 +9,7 @@ import org.springframework.stereotype.Component;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2021-08-05T14:00:16+0800",
date = "2021-08-05T16:32:05+0800",
comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)"
)
@Component

@ -1,40 +1,41 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.upload.domain;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiModelProperty;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.persistence.*;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @website https://el-admin.vip
* @description /
* @author x
* @date 2021-08-05
**/
* @website https://el-admin.vip
* @description /
* @author x
* @date 2021-01-03
**/
@Entity
@Data
@Table(name="tb_upload_file")
public class UploadFile implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
@ -45,9 +46,16 @@ public class UploadFile implements Serializable {
@ApiModelProperty(value = "上传日期")
private Timestamp uploadTime;
@Column(name = "opration")
/**
* ps,
*/
@Column(name = "upload_file_task_name")
@ApiModelProperty(value = "上传任务名称")
private String uploadFileTaskName;
@Column(name = "operation")
@ApiModelProperty(value = "操作人")
private String opration;
private String operation;
@Column(name = "file_count")
@ApiModelProperty(value = "文件解析总数")
@ -61,6 +69,10 @@ public class UploadFile implements Serializable {
@ApiModelProperty(value = "上传状态")
private Integer uploadTag;
@Column(name = "local_save_path")
@ApiModelProperty(value = "文件上传保存路径")
private String localSavePath;
public void copy(UploadFile source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}

@ -1,48 +1,93 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.upload.rest;
import cn.hutool.core.collection.CollectionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.common.http.CommonResponse;
import me.zhengjie.common.http.ResponseCode;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.upload.consts.SysConst;
import me.zhengjie.modules.upload.domain.UploadFile;
import me.zhengjie.modules.upload.service.UploadFileService;
import me.zhengjie.modules.upload.service.dto.UploadFileDto;
import me.zhengjie.modules.upload.service.dto.UploadFileQueryCriteria;
import me.zhengjie.modules.upload.task.SaveToFileTask;
import me.zhengjie.modules.uploadnew.redis.RedisParentDao;
import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService;
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto;
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria;
import me.zhengjie.modules.uploadnew.task.SaveToFileNewTask;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import static me.zhengjie.modules.upload.consts.UploadFileConst.FILE_PATH_SPLIT;
import static me.zhengjie.modules.upload.consts.UploadFileConst.WHITE_LIST;
import static me.zhengjie.modules.uploadnew.consts.SysConstNew.*;
/**
* @website https://el-admin.vip
* @author x
* @date 2021-08-05
**/
@RestController
* @website https://el-admin.vip
* @author x
* @date 2021-01-03
**/
//@RestController
@RequiredArgsConstructor
@Controller
@Api(tags = "上传文件解析发送管理")
@RequestMapping("/api/uploadFile")
@Slf4j
public class UploadFileController {
private final UploadFileService uploadFileService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private SaveToFileNewTask saveToFileNewTask;
@Autowired
private TbUploadFileNewService tbUploadFileNewService;
@Autowired
private SaveToFileTask saveToFileTask;
@Log("导出数据")
@ApiOperation("导出数据")
@GetMapping(value = "/download")
@ -56,9 +101,28 @@ public class UploadFileController {
@ApiOperation("查询上传文件解析发送")
@PreAuthorize("@el.check('uploadFile:list')")
public ResponseEntity<Object> query(UploadFileQueryCriteria criteria, Pageable pageable){
filterOfWriteList(criteria);
log.info("======= [ criteria is {} ] =======", criteria.toString());
return new ResponseEntity<>(uploadFileService.queryAll(criteria,pageable),HttpStatus.OK);
}
/**
* ,,
*
* @param criteria
*
* @return
*/
private void filterOfWriteList(UploadFileQueryCriteria criteria) {
if (!SysConst.sysDebug){
if (!CollectionUtil.contains(WHITE_LIST, SecurityUtils.getCurrentUsername())){
criteria.setOperation(SecurityUtils.getCurrentUsername());
log.info("======= [UploadFileServiceImpl|filterOfWriteList, userName is {} ] =======", SecurityUtils.getCurrentUsername());
}
}
}
@PostMapping
@Log("新增上传文件解析发送")
@ApiOperation("新增上传文件解析发送")
@ -84,4 +148,139 @@ public class UploadFileController {
uploadFileService.deleteAll(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
// ==== 去重开发 =====
@Log("上传并加密任务")
@ApiOperation("上传并加密任务")
@PostMapping(value = "/sendTask")
@PreAuthorize("@el.check('uploadFile:list')")
// @AnonymousAccess // fixme 需要测试完成后进行去除和使用上面的权限注解
public ResponseEntity<Object> sendTask(@RequestParam(value = "files", required = false) MultipartFile[] files, @RequestParam(value = "taskName") String taskName ) {
// 任务名称检验,为必填参数,且不能重复
if (StringUtils.isNotBlank(taskName)){
// modify by x bug fix on POSTMAN request
if (StringUtils.contains(taskName, FILE_PATH_SPLIT)){
taskName = taskName.substring(0, taskName.length()-1);
}
UploadFileQueryCriteria criteria = new UploadFileQueryCriteria();
criteria.setUploadFileTaskName(taskName);
List<UploadFileDto> uploadFileDtos = uploadFileService.queryAll(criteria);
if (CollectionUtil.isNotEmpty(uploadFileDtos)){
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_NAME_IS_EXIST), HttpStatus.OK);
}
}else {
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.EMPTY_ARGUMENT), HttpStatus.OK);
}
// 校验上传是否有文件
if (files!= null && files.length<= 0){
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK);
}
// 生成本地文件
UploadFileDto uploadFileDto = uploadFileService.encryptDataAndSaveToFile(files, taskName);
// 如果临时生成失败就修改状态为失败
if (Objects.isNull(uploadFileDto)){
log.error("UploadFileController:sendTask | Operation Fail.UploadFile is Null");
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_FILE_SAVE_FAIL), HttpStatus.OK);
}
// 调用异步任务
saveToFileTask.doRunTask(uploadFileDto);
return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK);
}
/**
*
*
* @return
*/
@Log("上传并加密任务")
@ApiOperation("上传并加密任务")
@AnonymousAccess
@PostMapping("/fileUpload")
@PreAuthorize("@el.check('uploadFile:list')")
// @ResponseBody
public ResponseEntity<Object> fileUpload(@RequestParam("file") MultipartFile file,
@RequestParam(value = "taskName") String taskName,
@RequestParam(value = "isEcryptionNew") String isEcryptionNew,
ServletRequest servletRequest) {
// 记录日志
log.info("TbUploadFileNewController:fileUpload | taskName="+taskName+"===== isEcryptionNew="+isEcryptionNew);
// 校验上传是否有文件
if (file== null ){
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK);
}
Integer isEcryption = 1;
if (StringUtils.isNotBlank(isEcryptionNew)){
try {
isEcryption = Integer.parseInt(isEcryptionNew);
} catch (NumberFormatException e) {
log.error("TbUploadFileNewController|fileUpload 转换异常!!!!");
throw new BadRequestException("转换异常!!!");
}
}
String name=file.getOriginalFilename();
String keyName = "";
String token = "";
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
Cookie[] cookies = httpServletRequest.getCookies();
if (cookies != null){
for(Cookie cookie : cookies) {
//获取cookie的名字
token=cookie.getValue();
log.info("TbUploadFileNewController:fileUpload | token="+token);
}
}
RedisParentDao redisParentDao = new RedisParentDao();
redisParentDao.setRedisTemplate(redisTemplate);
keyName = token+TOKEN_NAME+name;
String keyValue = redisParentDao.getValue(keyName);
//防止用户多次提交
if (keyValue != null){
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_STAETING), HttpStatus.OK);
}
redisParentDao.cacheValue(keyName, TOKEN_VALUE, EXP_TIME);
int lastIndexOf = name.lastIndexOf(".");
// 校验文件格式
String nameStr = name.substring(lastIndexOf);
if (!((nameStr.equals(".xlsx")||nameStr.equals(".xls")) || nameStr.equals(".txt") ||nameStr.equals(".csv"))){
redisParentDao.remove(keyName);
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_FORMAT), HttpStatus.OK);
}
// 任务名称检验,为必填参数,且不能重复
if (org.apache.commons.lang3.StringUtils.isNotBlank(taskName)){
// modify by x bug fix on POSTMAN request
if (org.apache.commons.lang3.StringUtils.contains(taskName, FILE_PATH_SPLIT)){
taskName = taskName.substring(0, taskName.length()-1);
}
TbUploadFileNewQueryCriteria criteria = new TbUploadFileNewQueryCriteria();
criteria.setUploadFileTaskName(taskName);
List<TbUploadFileNewDto> uploadFileDtos = tbUploadFileNewService.queryAll(criteria);
if (CollectionUtil.isNotEmpty(uploadFileDtos)){
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_NAME_IS_EXIST), HttpStatus.OK);
}
}else {
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.EMPTY_ARGUMENT), HttpStatus.OK);
}
// 生成本地文件
TbUploadFileNewDto tbUploadFileNewDto = tbUploadFileNewService.encryptDataAndSaveToFileNew(file, taskName, isEcryption);
// 如果临时生成失败就修改状态为失败
if (Objects.isNull(tbUploadFileNewDto)){
log.error("TbUploadFileNewController:fileUpload | Operation Fail.tbUploadFileNewDto is Null");
return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_FILE_SAVE_FAIL), HttpStatus.OK);
}
// 调用异步任务
saveToFileNewTask.doRunTask(tbUploadFileNewDto);
redisParentDao.remove(keyName);
return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK);
}
}

@ -1,50 +1,52 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.upload.service;
import me.zhengjie.modules.upload.domain.UploadFile;
import me.zhengjie.modules.upload.service.dto.UploadFileDto;
import me.zhengjie.modules.upload.service.dto.UploadFileQueryCriteria;
import org.springframework.data.domain.Pageable;
import java.util.Map;
import java.util.List;
import java.io.IOException;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @website https://el-admin.vip
* @description
* @author x
* @date 2021-08-05
**/
* @website https://el-admin.vip
* @description
* @author x
* @date 2021-01-03
**/
public interface UploadFileService {
/**
*
* @param criteria
* @param pageable
* @return Map<String,Object>
*/
*
* @param criteria
* @param pageable
* @return Map<String,Object>
*/
Map<String,Object> queryAll(UploadFileQueryCriteria criteria, Pageable pageable);
/**
*
* @param criteria
* @return List<UploadFileDto>
*/
*
* @param criteria
* @return List<UploadFileDto>
*/
List<UploadFileDto> queryAll(UploadFileQueryCriteria criteria);
/**
@ -55,29 +57,36 @@ public interface UploadFileService {
UploadFileDto findById(Long id);
/**
*
* @param resources /
* @return UploadFileDto
*/
*
* @param resources /
* @return UploadFileDto
*/
UploadFileDto create(UploadFile resources);
/**
*
* @param resources /
*/
*
* @param resources /
*/
void update(UploadFile resources);
/**
*
* @param ids /
*/
*
* @param ids /
*/
void deleteAll(Long[] ids);
/**
*
* @param all
* @param response /
* @throws IOException /
*/
*
* @param all
* @param response /
* @throws IOException /
*/
void download(List<UploadFileDto> all, HttpServletResponse response) throws IOException;
/**
*
*
* @param files
*/
UploadFileDto encryptDataAndSaveToFile(MultipartFile[] files, String taskName);
}

@ -1,30 +1,31 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.upload.service.dto;
import lombok.Data;
import java.sql.Timestamp;
import java.io.Serializable;
import java.sql.Timestamp;
/**
* @website https://el-admin.vip
* @description /
* @author x
* @date 2021-08-05
**/
* @website https://el-admin.vip
* @description /
* @author x
* @date 2021-01-03
**/
@Data
public class UploadFileDto implements Serializable {
@ -35,7 +36,7 @@ public class UploadFileDto implements Serializable {
private Timestamp uploadTime;
/** 操作人 */
private String opration;
private String operation;
/** 文件解析总数 */
private Long fileCount;
@ -45,4 +46,10 @@ public class UploadFileDto implements Serializable {
/** 上传状态 */
private Integer uploadTag;
/** 上传保存路径 */
private String localSavePath;
/** 上传的文件名称 **/
private String uploadFileTaskName;
}

@ -1,44 +1,50 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.upload.service.dto;
import lombok.Data;
import me.zhengjie.annotation.Query;
import java.sql.Timestamp;
import java.util.List;
import me.zhengjie.annotation.Query;
/**
* @website https://el-admin.vip
* @author x
* @date 2021-08-05
**/
* @website https://el-admin.vip
* @author x
* @date 2021-01-03
**/
@Data
public class UploadFileQueryCriteria{
/** 精确 */
@Query
@Query(type = Query.Type.EQUAL)
private Long id;
/** 模糊 */
@Query(type = Query.Type.INNER_LIKE)
private String opration;
/** 精确 */
@Query(type = Query.Type.EQUAL)
private String operation;
/** 精确 */
@Query
@Query(type = Query.Type.EQUAL)
private Integer uploadTag;
/** 精确 */
@Query(type = Query.Type.EQUAL)
private String uploadFileTaskName;
/** BETWEEN */
@Query(type = Query.Type.BETWEEN)
private List<Timestamp> uploadTime;

@ -1,71 +1,111 @@
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.upload.service.impl;
import me.zhengjie.modules.upload.domain.UploadFile;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.upload.consts.SysConst;
import me.zhengjie.modules.upload.domain.UploadFile;
import me.zhengjie.modules.upload.repository.UploadFileRepository;
import me.zhengjie.modules.upload.service.UploadFileService;
import me.zhengjie.modules.upload.service.dto.UploadFileDto;
import me.zhengjie.modules.upload.service.dto.UploadFileQueryCriteria;
import me.zhengjie.modules.upload.service.mapstruct.UploadFileMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import static me.zhengjie.modules.upload.consts.SysConst.TEST_USER_NAME;
import static me.zhengjie.modules.upload.consts.UploadFileConst.*;
/**
* @website https://el-admin.vip
* @description
* @author x
* @date 2021-08-05
**/
* @author x
* @website https://el-admin.vip
* @description
* @date 2021-01-03
**/
@Service
@RequiredArgsConstructor
@Slf4j
public class UploadFileServiceImpl implements UploadFileService {
/**
*
*/
@Value("${remote.link.address}")
private String remoteFileServerAddress;
/**
* - linux
*/
@Value("${remote.link.file-base-path-linux}")
private String remoteLinkFileBasePathLinux;
/**
* - linux
*/
@Value("${remote.link.file-base-path-windows}")
private String remoteLinkFileBasePathWindows;
/**
* - linux
*/
@Value("${remote.link.file-base-path-mac}")
private String remoteLinkFileBasePathMac;
private final UploadFileRepository uploadFileRepository;
private final UploadFileMapper uploadFileMapper;
@Override
public Map<String,Object> queryAll(UploadFileQueryCriteria criteria, Pageable pageable){
Page<UploadFile> page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
public Map<String, Object> queryAll(UploadFileQueryCriteria criteria, Pageable pageable) {
Page<UploadFile> page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
return PageUtil.toPage(page.map(uploadFileMapper::toDto));
}
@Override
public List<UploadFileDto> queryAll(UploadFileQueryCriteria criteria){
return uploadFileMapper.toDto(uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
public List<UploadFileDto> queryAll(UploadFileQueryCriteria criteria) {
return uploadFileMapper.toDto(uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
}
@Override
@Transactional
public UploadFileDto findById(Long id) {
UploadFile uploadFile = uploadFileRepository.findById(id).orElseGet(UploadFile::new);
ValidationUtil.isNull(uploadFile.getId(),"UploadFile","id",id);
ValidationUtil.isNull(uploadFile.getId(), "UploadFile", "id", id);
return uploadFileMapper.toDto(uploadFile);
}
@ -79,7 +119,7 @@ public class UploadFileServiceImpl implements UploadFileService {
@Transactional(rollbackFor = Exception.class)
public void update(UploadFile resources) {
UploadFile uploadFile = uploadFileRepository.findById(resources.getId()).orElseGet(UploadFile::new);
ValidationUtil.isNull( uploadFile.getId(),"UploadFile","id",resources.getId());
ValidationUtil.isNull(uploadFile.getId(), "UploadFile", "id", resources.getId());
uploadFile.copy(resources);
uploadFileRepository.save(uploadFile);
}
@ -95,9 +135,9 @@ public class UploadFileServiceImpl implements UploadFileService {
public void download(List<UploadFileDto> all, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (UploadFileDto uploadFile : all) {
Map<String,Object> map = new LinkedHashMap<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("上传日期", uploadFile.getUploadTime());
map.put("操作人", uploadFile.getOpration());
map.put("操作人", uploadFile.getOperation());
map.put("文件解析总数", uploadFile.getFileCount());
map.put("上传成功总数", uploadFile.getFileTransSuccessCount());
map.put("上传状态", uploadFile.getUploadTag());
@ -105,4 +145,99 @@ public class UploadFileServiceImpl implements UploadFileService {
}
FileUtil.downloadExcel(list, response);
}
@Override
public UploadFileDto encryptDataAndSaveToFile(MultipartFile[] files, String taskName) {
// 1. 文件存储到本地
long count = 0; // 统计总数
String baseStr = ""; // 生成通用随机文件夹存放每次的文件
StringBuilder pathBuilder = new StringBuilder();
for (MultipartFile file : files) {
// 获取原来的文件后缀
String originalFilename = file.getOriginalFilename();
if (StringUtils.isBlank(originalFilename)) {
log.error("===================== [input file name is empty, please check ]===================");
}
String extName = FileUtil.extName(originalFilename);
baseStr += FileUtil.mainName(originalFilename);
String eachFilePath = buildFileWritePath(baseStr, extName);
try {
// 把文件保存到本地路径
file.transferTo(Paths.get(eachFilePath));
baseStr = "";
} catch (IOException e) {
log.error("============== [transferTo file fail, path is {} ] ==============", eachFilePath,e);
}
// 统计行数
List<String> tempList = FileUtil.readLines(eachFilePath, "UTF-8");
if (CollectionUtil.isNotEmpty(tempList)){
count += tempList.size();
}
// 保存所有的临时存放路径
pathBuilder.append(eachFilePath);
pathBuilder.append(FILE_PATH_SPLIT);
}
// 2. 更新上传记录为正在上传,解析了有多少条
UploadFile uploadFile = new UploadFile();
uploadFile.setUploadFileTaskName(taskName);
uploadFile.setUploadTime(new Timestamp(System.currentTimeMillis()));
String currentUsername;
if (SysConst.sysDebug){
uploadFile.setOperation(TEST_USER_NAME);//fixme 这边测试环境补充一下需要的操作人
}else {
currentUsername = SecurityUtils.getCurrentUsername();
uploadFile.setOperation(currentUsername);
}
uploadFile.setFileCount(count);
uploadFile.setFileTransSuccessCount(0L);
uploadFile.setUploadTag(DOING_TAG);
String tempFilesPath = pathBuilder.toString().substring(0, pathBuilder.length() - 1);
if (StringUtils.isNotBlank(tempFilesPath)){
uploadFile.setLocalSavePath(tempFilesPath); // 去掉最后的那个通配符 FILE_PATH_SPLIT
}
UploadFileDto uploadFileDto = create(uploadFile);
return uploadFileDto;
}
/**
*
*/
private String buildFileWritePath(String baseStr, String extName) {
// 获取环境配置信息
OsInfo osInfo = SystemUtil.getOsInfo();
// 定义的时间格式
String timeFormate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String dirPath;
// 生成一个随机文件夹目录,方便整理和打包
String filePath = RandomUtil.randomString(6) + FILE_NAME_SPLIT + baseStr + FILE_SPLIT + extName;
if (osInfo.isWindows()) {
dirPath = remoteLinkFileBasePathWindows + timeFormate + File.separator;
FileUtil.mkdir(new File(dirPath));
// 构建存储文件
return dirPath + filePath;
} else if (osInfo.isLinux()) {
dirPath = remoteLinkFileBasePathLinux + timeFormate + File.separator;
FileUtil.mkdir(new File(dirPath));
// 构建存储文件
return dirPath + filePath;
} else if (osInfo.isMac()) {
dirPath = remoteLinkFileBasePathMac + timeFormate + File.separator;
FileUtil.mkdir(new File(dirPath));
// 构建存储文件
return dirPath + filePath;
} else {
return "";
}
}
}

@ -137,9 +137,9 @@ public class TbUploadFileNewController {
*/
@Log("新的成单数据及录音文件导入接口")
@ApiOperation("新的成单数据及录音文件导入接口")
// @AnonymousAccess
@AnonymousAccess
@PostMapping("/newFileUpload")
@PreAuthorize("@el.check('tbUploadFileNew:list')") // 这个换下相关按钮的权限控制
// @PreAuthorize("@el.check('tbUploadFileNew:list')") // 这个换下相关按钮的权限控制
// @ResponseBody
public ResponseEntity<Object> newFileUpload(@RequestParam("file") MultipartFile[] files,
@RequestParam(value = "taskName") String taskName) {

@ -27,5 +27,6 @@ public class TransFormDataTask {
}
private void runTask(List<FormdataDto> formdataDtos) {
}
}

@ -5,7 +5,7 @@ spring:
freemarker:
check-template-location: false
profiles:
active: prod
active: dev
jackson:
time-zone: GMT+8
data:

@ -77,7 +77,7 @@
<root level="INFO">
<!-- TODO prod 环境去掉std -->
<!-- <appender-ref ref="stdAppender"/>-->
<appender-ref ref="fileAppender"/>
<appender-ref ref="stdAppender"/>
<!-- <appender-ref ref="fileAppender"/>-->
</root>
</configuration>

@ -54,7 +54,7 @@ public class SpringApplicationTest {
UploadFileDto uploadFileDto = new UploadFileDto();
uploadFileDto.setUploadTime(new Timestamp(new java.util.Date().getTime()));
uploadFileDto.setOperation("测试用户1");
// uploadFileDto.setOperation("测试用户1");
uploadFileDto.setFileCount(1000000L);
uploadFileDto.setFileTransSuccessCount(999999L);
uploadFileDto.setUploadFileTaskName("测试任务1");
@ -73,7 +73,7 @@ public class SpringApplicationTest {
Pageable pageable = PageRequest.of(1, 10);
UploadFileQueryCriteria uploadFileQueryCriteria = new UploadFileQueryCriteria();
uploadFileQueryCriteria.setOperation("xiaoxing");
// uploadFileQueryCriteria.setOperation("xiaoxing");
Page<UploadFile> page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, uploadFileQueryCriteria, criteriaBuilder), pageable);

Loading…
Cancel
Save