From 8823eca4788535ba137798aef40290b8a9ee0599 Mon Sep 17 00:00:00 2001 From: weizhongxi <2412380450@qq.com> Date: Thu, 22 Apr 2021 10:46:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9F=A6=E5=BF=A0=E5=96=9C=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/zhengjie/common/http/ResponseCode.java | 3 + .../sms/rest/TbTemplateController.java | 38 ++ .../me/zhengjie/modules/sms/vo/SendVo.java | 9 + .../modules/upload/consts/SysConst.java | 2 +- .../upload/rest/UploadFileController.java | 7 +- .../modules/uploadnew/consts/SysConstNew.java | 55 +++ .../uploadnew/consts/UploadFileNewConst.java | 46 +++ .../uploadnew/domain/TbUploadFileNew.java | 91 +++++ .../repository/TbUploadFileNewRepository.java | 28 ++ .../rest/TbUploadFileNewController.java | 217 +++++++++++ .../service/TbUploadFileNewService.java | 94 +++++ .../service/dto/TbUploadFileNewDto.java | 66 ++++ .../dto/TbUploadFileNewQueryCriteria.java | 60 +++ .../impl/TbUploadFileNewServiceImpl.java | 262 +++++++++++++ .../mapstruct/TbUploadFileNewMapper.java} | 13 +- .../uploadnew/task/SaveToFileNewTask.java | 355 ++++++++++++++++++ .../modules/uploadnew/util/ExcelUtils.java | 93 +++++ .../modules/uploadnew/util/TxtUtils.java | 151 ++++++++ .../main/resources/config/application-dev.yml | 5 +- 19 files changed, 1585 insertions(+), 10 deletions(-) create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/UploadFileNewConst.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/domain/TbUploadFileNew.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/repository/TbUploadFileNewRepository.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/TbUploadFileNewService.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewDto.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java rename eladmin-system/src/main/java/me/zhengjie/modules/{sms/service/mapstruct/TbBlacklistMapper.java => uploadnew/service/mapstruct/TbUploadFileNewMapper.java} (73%) create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/SaveToFileNewTask.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/ExcelUtils.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/TxtUtils.java diff --git a/eladmin-common/src/main/java/me/zhengjie/common/http/ResponseCode.java b/eladmin-common/src/main/java/me/zhengjie/common/http/ResponseCode.java index 102572e..0f64059 100644 --- a/eladmin-common/src/main/java/me/zhengjie/common/http/ResponseCode.java +++ b/eladmin-common/src/main/java/me/zhengjie/common/http/ResponseCode.java @@ -21,9 +21,12 @@ public enum ResponseCode { EMPTY_ARGUMENT(1,"请求参数为空"), NO_MATCH_ARGUMENT_SET(1,"不能满足要求的参数设置"), NO_FILE_INPUT(1,"没有文件输入"), + NO_WORD_INPUT(1,"短信和url链接不能为空!"), + NO_FILE_FORMAT(1,"文件格式不对"), // 特殊需要进行前端返回说明的参数定义 TASK_NAME_IS_EXIST(1,"任务名称已经存在"), TASK_FILE_SAVE_FAIL(1,"文件存储失败"), + TASK_STAETING(1,"文件已经在上传!"), ONCE_LINK_MSG_ERROR(1,"链接失效,下载文件失败~"), // 请求结果性的错误 NODATA_ERROR(1,"查询结果为空"), diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java index d255553..9910698 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/rest/TbTemplateController.java @@ -15,11 +15,16 @@ */ package me.zhengjie.modules.sms.rest; +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.modules.sms.domain.TbTemplate; import me.zhengjie.modules.sms.service.TbTemplateService; import me.zhengjie.modules.sms.service.dto.TbTemplateQueryCriteria; +import me.zhengjie.modules.sms.vo.SendVo; import org.springframework.data.domain.Pageable; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; @@ -29,6 +34,8 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import io.swagger.annotations.*; import java.io.IOException; +import java.sql.Timestamp; +import java.util.Date; import javax.servlet.http.HttpServletResponse; /** @@ -40,6 +47,7 @@ import javax.servlet.http.HttpServletResponse; @RequiredArgsConstructor @Api(tags = "TemplateController管理") @RequestMapping("/api/tbTemplate") +@Slf4j public class TbTemplateController { private final TbTemplateService tbTemplateService; @@ -85,4 +93,34 @@ public class TbTemplateController { tbTemplateService.deleteAll(ids); return new ResponseEntity<>(HttpStatus.OK); } + + /** + * 接收大数据传过来的短信内容 + * + * @param sendVo 发送任务对应的VO映射 + * @return 返回建立任务完成的状态 + */ + @Log("接收短信内容和url") + @ApiOperation("接收短信内容和url") +// @PreAuthorize("@el.check('taskRecord:list')") + @PostMapping(value = "/getSmsInfo") + @AnonymousAccess // fixme 需要测试完成后进行去除和使用上面的权限注解 + public ResponseEntity getSmsInfo(@RequestBody SendVo sendVo){ + log.info("========== [TbTemplateController|getSmsInfo ========== SmsContent:"+sendVo.getSmsContent()+" LinkUrl:"+sendVo.getLinkUrl()); + if (sendVo.getSmsContent() != null && sendVo.getLinkUrl() != null){ + TbTemplate tbTemplate = new TbTemplate(); + tbTemplate.setLinkUrl(sendVo.getLinkUrl()); + tbTemplate.setTaskName(sendVo.getTaskName()); + tbTemplate.setSendMessage(sendVo.getSmsContent()); + tbTemplate.setReviewer(sendVo.getOperator()); + Date date = new Date(); + Timestamp nousedate = new Timestamp(date.getTime()); + tbTemplate.setLastUpdateTime(nousedate); + tbTemplate.setGmtModified(nousedate); + tbTemplate.setGmtCreate(nousedate); + tbTemplateService.create(tbTemplate); + return new ResponseEntity<>(CommonResponse.createBySuccess(), HttpStatus.OK); + } + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_WORD_INPUT), HttpStatus.OK); + } } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/vo/SendVo.java b/eladmin-system/src/main/java/me/zhengjie/modules/sms/vo/SendVo.java index 66878a1..6557c8a 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/vo/SendVo.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/sms/vo/SendVo.java @@ -17,4 +17,13 @@ public class SendVo { private String linkUrl; @ApiModelProperty("模板id") private String sendMessage; + + @ApiModelProperty("短信内容") + private String smsContent; + + @ApiModelProperty("任务名称") + private String taskName; + + @ApiModelProperty("审核人") + private String operator; } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/consts/SysConst.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/consts/SysConst.java index 58e1cfc..b78c547 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/consts/SysConst.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/consts/SysConst.java @@ -30,7 +30,7 @@ public class SysConst { public static final String REMOTE_TRANS_DIR_PATH = "/home/"; // fixme 以后改成rpc调用的地址 - public static final String REMOTE_UPDATE_ADDR = "http://116.62.197.152:8000/api/remoteRecord/remote/rec"; + public static final String REMOTE_UPDATE_ADDR = "http://116.62.197.152:8001/api/remoteRecord/remote/rec"; // 测试内容临时定义 public static final String TEST_USER_NAME = "测试用户"; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java index 35fec04..2c7708f 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/rest/UploadFileController.java @@ -20,6 +20,7 @@ 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; @@ -36,11 +37,13 @@ import org.springframework.data.domain.Pageable; 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 org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.io.IOException; import java.util.List; import java.util.Objects; @@ -53,8 +56,10 @@ import static me.zhengjie.modules.upload.consts.UploadFileConst.WHITE_LIST; * @author x * @date 2021-01-03 **/ -@RestController +//@RestController + @RequiredArgsConstructor +@Controller @Api(tags = "上传文件解析发送管理") @RequestMapping("/api/uploadFile") @Slf4j diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java new file mode 100644 index 0000000..b94d34b --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/SysConstNew.java @@ -0,0 +1,55 @@ +package me.zhengjie.modules.uploadnew.consts; + +import org.springframework.beans.factory.annotation.Value; + +/** + * 系统相关的配置 + */ +public class SysConstNew { + + // Test相关的 - start + + // 远程服务器的相关配置 +// public static final String LOCAL_HOST = "localhost"; +// +// public static final String LOCAL_DIR = "C:\\Users\\Administrator\\Desktop\\trans\\"; + + // Test相关的 - end + + public static final String REMOTE_TRANS_HOST = "116.62.197.152"; // 118.178.137.129 116.62.197.152 + + public static final Integer REMOTE_TRANS_PORT = 22; + + public static final String REMOTE_TRANS_SSH_USER = "root"; + + public static final String REMOTE_TRANS_SSH_PW = "yuyou@ECS2020"; + + /** + * 远程SFTP保存文件的路径 + */ + public static final String REMOTE_TRANS_DIR_PATH = "/home/"; + + // fixme 以后改成rpc调用的地址 + public static final String REMOTE_UPDATE_ADDR = "http://116.62.197.152:8000/api/remoteRecord/remote/rec"; + + // 测试内容临时定义 + public static final String TEST_USER_NAME = "测试用户"; + + // 系统开发,上线环境下进行关闭 - DEBUG开关 - 默认是打开的 + public static Boolean sysDebug = false; + + /** + * 临时文件后缀 + */ + public static String TEMP_FILE_END_STR = "bak"; + + /** + * 使用Set方式注入 + * + * @param debug debug开关 + */ + @Value(value = "${sys.debug}") + public static void setUrl(Boolean debug) { + SysConstNew.sysDebug = debug; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/UploadFileNewConst.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/UploadFileNewConst.java new file mode 100644 index 0000000..8aa8cc9 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/consts/UploadFileNewConst.java @@ -0,0 +1,46 @@ +package me.zhengjie.modules.uploadnew.consts; + +import java.util.Arrays; +import java.util.List; + +/** + * 常量定义 + */ +public class UploadFileNewConst { + + /** + * 白名单 - 对特殊账号做查询限制 + */ + public static final List WHITE_LIST = Arrays.asList("admin", "xiaoxing", "yangbin"); + + /** + * ext 文件类型分隔符 - 通配 + */ + public static final String FILE_SPLIT = "."; + + /** + * 文件路径的分隔 + */ + public static final String FILE_PATH_SPLIT = ","; + + /** + * 正在执行的标识 + */ + public static final Integer DOING_TAG = 1; + + /** + * 成功执行的标识 + */ + public static final Integer SUCCESS_TAG = 0; + + /** + * 失败执行的标识 + */ + public static final Integer FAIL_TAG = 2; + + + /** + * 文件名的分隔符 _ + */ + public static final String FILE_NAME_SPLIT = "_"; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/domain/TbUploadFileNew.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/domain/TbUploadFileNew.java new file mode 100644 index 0000000..db22b03 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/domain/TbUploadFileNew.java @@ -0,0 +1,91 @@ +/* +* 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.uploadnew.domain; + +import lombok.Data; +import cn.hutool.core.bean.BeanUtil; +import io.swagger.annotations.ApiModelProperty; +import cn.hutool.core.bean.copier.CopyOptions; +import javax.persistence.*; +import javax.validation.constraints.*; +import java.sql.Timestamp; +import java.io.Serializable; + +/** +* @website https://el-admin.vip +* @description / +* @author weizhongxi +* @date 2021-04-14 +**/ +@Entity +@Data +@Table(name="tb_upload_file_new") +public class TbUploadFileNew implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + @ApiModelProperty(value = "主键") + private Integer id; + + @Column(name = "gmt_create") + @ApiModelProperty(value = "创建时间") + private Timestamp gmtCreate; + + @Column(name = "gmt_modified") + @ApiModelProperty(value = "修改时间") + private Timestamp gmtModified; + + @Column(name = "upload_file_task_name") + @ApiModelProperty(value = "上传任务名称") + private String uploadFileTaskName; + + @Column(name = "operation") + @ApiModelProperty(value = "操作人") + private String operation; + + @Column(name = "file_count") + @ApiModelProperty(value = "文件解析总数") + private Long fileCount; + + @Column(name = "file_trans_success_count") + @ApiModelProperty(value = "上传成功总数") + private Long fileTransSuccessCount; + + @Column(name = "upload_tag") + @ApiModelProperty(value = "上传状态(0-失败 1-成功)") + private Integer uploadTag; + + @Column(name = "local_save_path") + @ApiModelProperty(value = "文件上传保存路径") + private String localSavePath; + + @Column(name = "upload_time") + @ApiModelProperty(value = "上传日期") + private Timestamp uploadTime; + + @Column(name = "is_encryption") + @ApiModelProperty(value = "加密状态(0-未加密,1-加密)") + private Integer isEncryption; + + @Column(name = "file_format") + @ApiModelProperty(value = "文件格式") + private String fileFormat; + + public void copy(TbUploadFileNew source){ + BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true)); + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/repository/TbUploadFileNewRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/repository/TbUploadFileNewRepository.java new file mode 100644 index 0000000..ec9202e --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/repository/TbUploadFileNewRepository.java @@ -0,0 +1,28 @@ +/* +* 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.uploadnew.repository; + +import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +/** +* @website https://el-admin.vip +* @author weizhongxi +* @date 2021-04-14 +**/ +public interface TbUploadFileNewRepository extends JpaRepository, JpaSpecificationExecutor { +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java new file mode 100644 index 0000000..a3dc458 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/rest/TbUploadFileNewController.java @@ -0,0 +1,217 @@ +/* +* 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.uploadnew.rest; + +import cn.hutool.core.collection.CollectionUtil; +import io.swagger.models.auth.In; +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.modules.security.security.TokenFilter; +import me.zhengjie.modules.upload.service.dto.UploadFileDto; +import me.zhengjie.modules.upload.service.dto.UploadFileQueryCriteria; +import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; +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 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.util.StringUtils; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; +import io.swagger.annotations.*; +import org.springframework.web.multipart.MultipartFile; +import org.apache.commons.lang3.StringUtils; + +import java.io.IOException; +import java.util.List; +import java.util.Objects; +import javax.servlet.ServletRequest; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import static me.zhengjie.modules.upload.consts.UploadFileConst.FILE_PATH_SPLIT; + +/** +* @website https://el-admin.vip +* @author weizhongxi +* @date 2021-04-14 +**/ +@RestController +@RequiredArgsConstructor +@Api(tags = "文件上传接口管理") +@RequestMapping("/api/tbUploadFileNew") +@Slf4j +public class TbUploadFileNewController { + + private final TbUploadFileNewService tbUploadFileNewService; + + @Autowired + private SaveToFileNewTask saveToFileNewTask; + + @Autowired + private RedisTemplate redisTemplate; + + @Log("导出数据") + @ApiOperation("导出数据") + @GetMapping(value = "/download") + @PreAuthorize("@el.check('tbUploadFileNew:list')") + public void download(HttpServletResponse response, TbUploadFileNewQueryCriteria criteria) throws IOException { + tbUploadFileNewService.download(tbUploadFileNewService.queryAll(criteria), response); + } + + @GetMapping + @Log("查询文件上传接口") + @ApiOperation("查询文件上传接口") + @PreAuthorize("@el.check('tbUploadFileNew:list')") + public ResponseEntity query(TbUploadFileNewQueryCriteria criteria, Pageable pageable){ + return new ResponseEntity<>(tbUploadFileNewService.queryAll(criteria,pageable),HttpStatus.OK); + } + + @PostMapping + @Log("新增文件上传接口") + @ApiOperation("新增文件上传接口") + @PreAuthorize("@el.check('tbUploadFileNew:add')") + public ResponseEntity create(@Validated @RequestBody TbUploadFileNew resources){ + return new ResponseEntity<>(tbUploadFileNewService.create(resources),HttpStatus.CREATED); + } + + @PutMapping + @Log("修改文件上传接口") + @ApiOperation("修改文件上传接口") + @PreAuthorize("@el.check('tbUploadFileNew:edit')") + public ResponseEntity update(@Validated @RequestBody TbUploadFileNew resources){ + tbUploadFileNewService.update(resources); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @Log("删除文件上传接口") + @ApiOperation("删除文件上传接口") + @PreAuthorize("@el.check('tbUploadFileNew:del')") + @DeleteMapping + public ResponseEntity delete(@RequestBody Integer[] ids) { + tbUploadFileNewService.deleteAll(ids); + return new ResponseEntity<>(HttpStatus.OK); + } + + /** + * 文件上传接口 + * + * @return 返回调用信息 + */ + @Log("上传并加密任务") + @ApiOperation("上传并加密任务") + @AnonymousAccess + @PostMapping("/fileUpload") +// @ResponseBody + public ResponseEntity 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); + Integer isEcryption = Integer.parseInt(isEcryptionNew); + String name=file.getOriginalFilename(); + + String keyName = ""; + String token = ""; + HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; + Cookie[] cookies = httpServletRequest.getCookies(); + for(Cookie cookie : cookies) { + //获取cookie的名字 + String cookieName = cookie.getName(); + token=cookie.getValue(); + System.out.println(cookieName); + System.out.println(cookieName+" : "+ cookie.getValue() ); + } + keyName = token+"==="+name; + Object keyValue = redisTemplate.opsForValue().get(keyName); + //防止用户多次提交 + if (keyValue != null){ + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.TASK_STAETING), HttpStatus.OK); + } + redisTemplate.opsForValue().set(keyName, "value11"); + + + + int lastIndexOf = name.lastIndexOf("."); + String nameStr = name.substring(lastIndexOf); + // 校验文件格式 + if (!((nameStr.equals(".xlsx")||nameStr.equals(".xls")) || nameStr.equals(".txt") ||nameStr.equals(".csv"))){ + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_FORMAT), HttpStatus.OK); + } + // 任务名称检验,为必填参数,且不能重复 + 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); + } + + TbUploadFileNewQueryCriteria criteria = new TbUploadFileNewQueryCriteria(); + criteria.setUploadFileTaskName(taskName); + List 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); + } + + // 校验上传是否有文件 + if (file== null ){ + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), 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); + + redisTemplate.delete(keyName); + return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); + } + + @PostMapping("/aa") + @Log("查询文件上传接口") + @ApiOperation("查询文件上传接口") + @AnonymousAccess + public ResponseEntity aa(){ + redisTemplate.opsForValue().set("key111", "value11"); + Object key111 = redisTemplate.opsForValue().get("key111"); + System.out.println("key111 = " + key111.toString()); + + redisTemplate.delete("key111"); + + Object aaaa = redisTemplate.opsForValue().get("key111"); + System.out.println("aaaa = " + aaaa); + + return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); + } + + +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/TbUploadFileNewService.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/TbUploadFileNewService.java new file mode 100644 index 0000000..55befca --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/TbUploadFileNewService.java @@ -0,0 +1,94 @@ +/* +* 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.uploadnew.service; + +import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; +import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; +import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria; +import org.springframework.data.domain.Pageable; +import org.springframework.web.multipart.MultipartFile; + +import java.util.Map; +import java.util.List; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + +/** +* @website https://el-admin.vip +* @description 服务接口 +* @author weizhongxi +* @date 2021-04-14 +**/ +public interface TbUploadFileNewService { + + /** + * 查询数据分页 + * @param criteria 条件 + * @param pageable 分页参数 + * @return Map + */ + Map queryAll(TbUploadFileNewQueryCriteria criteria, Pageable pageable); + + /** + * 查询所有数据不分页 + * @param criteria 条件参数 + * @return List + */ + List queryAll(TbUploadFileNewQueryCriteria criteria); + + /** + * 根据ID查询 + * @param id ID + * @return TbUploadFileNewDto + */ + TbUploadFileNewDto findById(Integer id); + + /** + * 创建 + * @param resources / + * @return TbUploadFileNewDto + */ + TbUploadFileNewDto create(TbUploadFileNew resources); + + /** + * 编辑 + * @param resources / + */ + void update(TbUploadFileNew resources); + + /** + * 多选删除 + * @param ids / + */ + void deleteAll(Integer[] ids); + + /** + * 导出数据 + * @param all 待导出的数据 + * @param response / + * @throws IOException / + */ + void download(List all, HttpServletResponse response) throws IOException; + + /** + * 加密并生成存储文件 + * + * @param file + * @param taskName + * @param isEcryption + */ + TbUploadFileNewDto encryptDataAndSaveToFileNew(MultipartFile file, String taskName,Integer isEcryption); +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewDto.java new file mode 100644 index 0000000..0c6b390 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewDto.java @@ -0,0 +1,66 @@ +/* +* 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.uploadnew.service.dto; + +import lombok.Data; +import java.sql.Timestamp; +import java.io.Serializable; + +/** +* @website https://el-admin.vip +* @description / +* @author weizhongxi +* @date 2021-04-14 +**/ +@Data +public class TbUploadFileNewDto implements Serializable { + + /** 主键 */ + private Integer id; + + /** 创建时间 */ + private Timestamp gmtCreate; + + /** 修改时间 */ + private Timestamp gmtModified; + + /** 上传任务名称 */ + private String uploadFileTaskName; + + /** 操作人 */ + private String operation; + + /** 文件解析总数 */ + private Long fileCount; + + /** 上传成功总数 */ + private Long fileTransSuccessCount; + + /** 上传状态(0-失败 1-成功) */ + private Integer uploadTag; + + /** 文件上传保存路径 */ + private String localSavePath; + + /** 上传日期 */ + private Timestamp uploadTime; + + /** 加密状态(0-未加密,1-加密) */ + private Integer isEncryption; + + /** 文件格式 */ + private String fileFormat; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java new file mode 100644 index 0000000..4692219 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/dto/TbUploadFileNewQueryCriteria.java @@ -0,0 +1,60 @@ +/* +* 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.uploadnew.service.dto; + +import lombok.Data; +import java.sql.Timestamp; +import java.util.List; +import me.zhengjie.annotation.Query; + +/** +* @website https://el-admin.vip +* @author weizhongxi +* @date 2021-04-14 +**/ +@Data +public class TbUploadFileNewQueryCriteria{ + + /** 模糊 */ + @Query(type = Query.Type.INNER_LIKE) + private String uploadFileTaskName; + + /** 模糊 */ + @Query(type = Query.Type.INNER_LIKE) + private String operation; + + /** 不为空 */ + @Query(type = Query.Type.NOT_NULL) + private Integer uploadTag; + + /** 模糊 */ + @Query(type = Query.Type.INNER_LIKE) + private String localSavePath; + + /** 不为空 */ + @Query(type = Query.Type.NOT_NULL) + private Integer isEncryption; + + /** 模糊 */ + @Query(type = Query.Type.INNER_LIKE) + private String fileFormat; + /** BETWEEN */ + @Query(type = Query.Type.BETWEEN) + private List gmtModified; + /** BETWEEN */ + @Query(type = Query.Type.BETWEEN) + private List uploadTime; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java new file mode 100644 index 0000000..275f434 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/impl/TbUploadFileNewServiceImpl.java @@ -0,0 +1,262 @@ +/* +* 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.uploadnew.service.impl; + +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.RandomUtil; +import cn.hutool.system.OsInfo; +import cn.hutool.system.SystemUtil; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.upload.consts.SysConst; +import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; +import me.zhengjie.modules.uploadnew.util.ExcelUtils; +import me.zhengjie.modules.uploadnew.util.TxtUtils; +import me.zhengjie.utils.*; +import lombok.RequiredArgsConstructor; +import me.zhengjie.modules.uploadnew.repository.TbUploadFileNewRepository; +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.service.mapstruct.TbUploadFileNewMapper; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.nio.file.Paths; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; +import java.util.*; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + +import static me.zhengjie.modules.uploadnew.consts.SysConstNew.TEST_USER_NAME; +import static me.zhengjie.modules.uploadnew .consts.UploadFileNewConst.*; + +/** +* @website https://el-admin.vip +* @description 服务实现 +* @author weizhongxi +* @date 2021-04-14 +**/ +@Service +@RequiredArgsConstructor +@Slf4j +public class TbUploadFileNewServiceImpl implements TbUploadFileNewService { + + /** + * 远程上传临时存放文件地址 - 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 TbUploadFileNewRepository tbUploadFileNewRepository; + private final TbUploadFileNewMapper tbUploadFileNewMapper; + + @Override + public Map queryAll(TbUploadFileNewQueryCriteria criteria, Pageable pageable){ + Page page = tbUploadFileNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + return PageUtil.toPage(page.map(tbUploadFileNewMapper::toDto)); + } + + @Override + public List queryAll(TbUploadFileNewQueryCriteria criteria){ + return tbUploadFileNewMapper.toDto(tbUploadFileNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + } + + @Override + @Transactional + public TbUploadFileNewDto findById(Integer id) { + TbUploadFileNew tbUploadFileNew = tbUploadFileNewRepository.findById(id).orElseGet(TbUploadFileNew::new); + ValidationUtil.isNull(tbUploadFileNew.getId(),"TbUploadFileNew","id",id); + return tbUploadFileNewMapper.toDto(tbUploadFileNew); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public TbUploadFileNewDto create(TbUploadFileNew resources) { + return tbUploadFileNewMapper.toDto(tbUploadFileNewRepository.save(resources)); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void update(TbUploadFileNew resources) { + TbUploadFileNew tbUploadFileNew = tbUploadFileNewRepository.findById(resources.getId()).orElseGet(TbUploadFileNew::new); + ValidationUtil.isNull( tbUploadFileNew.getId(),"TbUploadFileNew","id",resources.getId()); + tbUploadFileNew.copy(resources); + tbUploadFileNewRepository.save(tbUploadFileNew); + } + + @Override + public void deleteAll(Integer[] ids) { + for (Integer id : ids) { + tbUploadFileNewRepository.deleteById(id); + } + } + + @Override + public void download(List all, HttpServletResponse response) throws IOException { + List> list = new ArrayList<>(); + for (TbUploadFileNewDto tbUploadFileNew : all) { + Map map = new LinkedHashMap<>(); + map.put("创建时间", tbUploadFileNew.getGmtCreate()); + map.put("修改时间", tbUploadFileNew.getGmtModified()); + map.put("上传任务名称", tbUploadFileNew.getUploadFileTaskName()); + map.put("操作人", tbUploadFileNew.getOperation()); + map.put("文件解析总数", tbUploadFileNew.getFileCount()); + map.put("上传成功总数", tbUploadFileNew.getFileTransSuccessCount()); + map.put("上传状态(0-失败 1-成功)", tbUploadFileNew.getUploadTag()); + map.put("文件上传保存路径", tbUploadFileNew.getLocalSavePath()); + map.put("上传日期", tbUploadFileNew.getUploadTime()); + map.put("加密状态(0-未加密,1-加密)", tbUploadFileNew.getIsEncryption()); + map.put("文件格式", tbUploadFileNew.getFileFormat()); + list.add(map); + } + FileUtil.downloadExcel(list, response); + } + + @Override + public TbUploadFileNewDto encryptDataAndSaveToFileNew(MultipartFile file, String taskName, Integer isEcryption) { + // 1. 文件存储到本地 + long count = 0; // 统计总数 + String baseStr = ""; // 生成通用随机文件夹存放每次的文件 + StringBuilder pathBuilder = new StringBuilder(); + // 获取原来的文件后缀 + 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 = buildFileWritePathNew(baseStr, extName); + try { + // 把文件保存到本地路径 + file.transferTo(Paths.get(eachFilePath)); + baseStr = ""; + } catch (IOException e) { + log.error("============== [transferTo file fail, path is {} ] ==============", eachFilePath,e); + } + + String name=file.getOriginalFilename(); + int lastIndexOf = name.lastIndexOf("."); + String nameStr = name.substring(lastIndexOf); + //记录文件格式 + String fileFormat = ""; + List list = null; + //根据文件类型进行解析 + try { + if(nameStr.equals(".xlsx")||nameStr.equals(".xls")){ + fileFormat = "excel文件"; + list = FileUtil.readLines(eachFilePath, "UTF-8"); + + }else if (nameStr.equals(".txt")){ + fileFormat = "txt文件"; + list = FileUtil.readLines(eachFilePath, "UTF-8"); + }else if (nameStr.equals(".csv")){ + fileFormat = "csv文件"; + list = TxtUtils.csvParseList(file.getInputStream()); + } + }catch (Exception e){ + e.printStackTrace(); + } + + // 统计行数 + if (CollectionUtil.isNotEmpty(list)){ + count += list.size(); + } + // 保存所有的临时存放路径 + pathBuilder.append(eachFilePath); + pathBuilder.append(FILE_PATH_SPLIT); + + // 2. 更新上传记录为正在上传,解析了有多少条 + TbUploadFileNew tbUploadFileNew = new TbUploadFileNew(); + + tbUploadFileNew.setUploadFileTaskName(taskName); + tbUploadFileNew.setUploadTime(new Timestamp(new Date().getTime())); + String currentUsername; + if (SysConst.sysDebug){ + tbUploadFileNew.setOperation(TEST_USER_NAME);//fixme 这边测试环境补充一下需要的操作人 + }else { + currentUsername = SecurityUtils.getCurrentUsername(); + tbUploadFileNew.setOperation(currentUsername); + } + tbUploadFileNew.setFileFormat(fileFormat); + tbUploadFileNew.setIsEncryption(isEcryption); + tbUploadFileNew.setFileCount(count); + tbUploadFileNew.setFileTransSuccessCount(0L); + tbUploadFileNew.setUploadTag(DOING_TAG); + Date date = new Date(); + Timestamp nousedate = new Timestamp(date.getTime()); + tbUploadFileNew.setGmtCreate(nousedate); + tbUploadFileNew.setGmtModified(nousedate); + String tempFilesPath = pathBuilder.toString().substring(0, pathBuilder.length() - 1); + if (StringUtils.isNotBlank(tempFilesPath)){ + tbUploadFileNew.setLocalSavePath(tempFilesPath); // 去掉最后的那个通配符 FILE_PATH_SPLIT + } + TbUploadFileNewDto tbUploadFileNewDto = create(tbUploadFileNew); + return tbUploadFileNewDto; + } + + /** + * 构建文件上传保存路径 + */ + private String buildFileWritePathNew(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 ""; + } + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/mapstruct/TbBlacklistMapper.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/mapstruct/TbUploadFileNewMapper.java similarity index 73% rename from eladmin-system/src/main/java/me/zhengjie/modules/sms/service/mapstruct/TbBlacklistMapper.java rename to eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/mapstruct/TbUploadFileNewMapper.java index 8fd0c44..2c11a58 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/sms/service/mapstruct/TbBlacklistMapper.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/service/mapstruct/TbUploadFileNewMapper.java @@ -13,21 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package me.zhengjie.modules.sms.service.mapstruct; - +package me.zhengjie.modules.uploadnew.service.mapstruct; import me.zhengjie.base.BaseMapper; -import me.zhengjie.modules.sms.domain.TbBlacklist; -import me.zhengjie.modules.sms.service.dto.TbBlacklistDto; +import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; +import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; import org.mapstruct.Mapper; import org.mapstruct.ReportingPolicy; /** * @website https://el-admin.vip -* @author Enzo +* @author weizhongxi * @date 2021-04-14 **/ @Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) -public interface TbBlacklistMapper extends BaseMapper { +public interface TbUploadFileNewMapper extends BaseMapper { -} +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/SaveToFileNewTask.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/SaveToFileNewTask.java new file mode 100644 index 0000000..01a5407 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/SaveToFileNewTask.java @@ -0,0 +1,355 @@ +package me.zhengjie.modules.uploadnew.task; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.codec.Base64; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.RandomUtil; +import cn.hutool.crypto.SecureUtil; +import cn.hutool.extra.ssh.JschUtil; +import cn.hutool.extra.ssh.Sftp; +import cn.hutool.http.HttpRequest; +import cn.hutool.http.HttpResponse; +import cn.hutool.poi.excel.ExcelUtil; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.google.common.collect.Lists; +import com.jcraft.jsch.Session; +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.upload.domain.UploadFile; +import me.zhengjie.modules.upload.service.dto.UploadFileDto; +import me.zhengjie.modules.upload.task.model.ResponseEncryptJsonContent; +import me.zhengjie.modules.upload.task.model.SendEncryptJsonContent; +import me.zhengjie.modules.upload.task.model.SendRemoteUpdateJsonContent; +import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; +import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; +import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; +import me.zhengjie.modules.uploadnew.util.ExcelUtils; +import me.zhengjie.modules.uploadnew.util.TxtUtils; +import me.zhengjie.utils.ConvertUtil; +import me.zhengjie.utils.DateUtil; +import me.zhengjie.utils.FileUtil; +import me.zhengjie.utils.StringUtils; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Scope; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static me.zhengjie.modules.upload.consts.SysConst.*; +import static me.zhengjie.modules.upload.consts.SysConst.REMOTE_TRANS_DIR_PATH; +import static me.zhengjie.modules.upload.consts.UploadFileConst.*; + +@Component +@Scope("prototype") +@Slf4j +public class SaveToFileNewTask { + + private Boolean booleanTag = true; + + @Value(value = "${inter.appid}") + private String encryptAppId; + + @Value(value = "${inter.tk}") + private String encryptTK; + + /** + * 加密请求需要的各种配置信息 + */ + @Value(value = "${inter.address}") + private String encryptAddress; + + /** + * 分割标识 - 逗号 + */ + private static final String SPLIT_TAG = ","; + + + /** + * 调用加密接口的加密内容数量限制 + */ + private final static int SEND_ENCRYPT_LIMIT = 200; + + /** + * 时间格式 + */ + private final static String FORMATE_TIMESTAMP = "yyyyMMddHHmmss"; + + /** + * 号码长度 + */ + public static final Integer PRE_SEND_NUM_LENGTH = 11; + + /** + * 加密号码长度 + */ + public static final Integer SEND_NUM_LENGTH = 24; + + @Autowired + private TbUploadFileNewService tbUploadFileNewService; + + @Async(value = "SendBigDataTaskExecutor") + public void doRunTask(TbUploadFileNewDto tbUploadFileNewDto) { + Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start running, task name is {} ] ======", "SendBigDataTask"); + runTask(tbUploadFileNewDto); + Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "SendBigDataTask", ConvertUtil.secondToTime(endMilliSecond - satrtMilliSecond)); + } + /** + * 执行异步任务 + * + * @param tbUploadFileNewDto 需要传输用到的Bean + */ + private void runTask(TbUploadFileNewDto tbUploadFileNewDto) { + boolean finalTag = false; + // 获取需求的地址 + String tempFilesPath = tbUploadFileNewDto.getLocalSavePath(); + + // 单文件处理 + finalTag = handleEachFileContent(tempFilesPath, tbUploadFileNewDto); + + + // 更新状态为成功,更新解析成功的条数 + TbUploadFileNew tbUploadFileNew = new TbUploadFileNew(); + if (finalTag) { + BeanUtils.copyProperties(tbUploadFileNewDto, tbUploadFileNew); + tbUploadFileNew.setFileTransSuccessCount(tbUploadFileNewDto.getFileCount()); + tbUploadFileNew.setUploadTag(SUCCESS_TAG); + tbUploadFileNewService.update(tbUploadFileNew); + } else { + // 失败进行容错 + BeanUtils.copyProperties(tbUploadFileNewDto, tbUploadFileNew); + tbUploadFileNew.setUploadTag(FAIL_TAG); + tbUploadFileNew.setFileTransSuccessCount(tbUploadFileNewDto.getFileCount()); + tbUploadFileNewService.update(tbUploadFileNew); + } + } + + /** + * 对每一个文件的处理操作 + * + * @param filePath 进行操作的每一个文件的路径 + */ + private boolean handleEachFileContent(String filePath, TbUploadFileNewDto tbUploadFileNewDto) { + //根据文件类型进行解析 + List list = null; + try { + if("excel文件".equals(tbUploadFileNewDto.getFileFormat())){ + list = ExcelUtils.excelParseListByUrl(filePath); + }else if ("txt文件".equals(tbUploadFileNewDto.getFileFormat())){ + list = TxtUtils.txtParseListVyUrl(filePath); + }else if ("csv文件".equals(tbUploadFileNewDto.getFileFormat())){ + list = TxtUtils.csvParseListByUrl(filePath); + } + }catch (Exception e){ + e.printStackTrace(); + } + if (CollectionUtil.isNotEmpty(list)){ + batchSendToEncrypt(filePath, list); + + } + + // 加入一个全局控制开关 + if (!booleanTag){ + return Boolean.FALSE; + } + + // 把临时存储的文件进行删除 + boolean delFileTag = delTempSaveFile(filePath); + + // 文件传输给2号库服务器 - 走sftp协议 - 支持断点续传 + transFileToOtherServer(filePath + TEMP_FILE_END_STR); + + // 调用远程接口完成一条记录更新 + boolean sendUpdatePostReqTag = sendUpdatePostReq(filePath+ TEMP_FILE_END_STR, tbUploadFileNewDto); + // fixme 这里要修改之前的平台给一个更新接口,然后这边可以用rpc调用,也可以用http,也可以考虑直接消息中间件进行解耦 + if (delFileTag && sendUpdatePostReqTag) { + return Boolean.TRUE; + } + return Boolean.FALSE; + } + + private void batchSendToEncrypt(String filePath, List fileAllLinesList) { + List> partition = Lists.partition(fileAllLinesList, SEND_ENCRYPT_LIMIT); + + partition.forEach( + list -> { + // 装配需要的Json参数 + SendEncryptJsonContent sendEncryptJsonContent = new SendEncryptJsonContent(); + //fixme 还有一个过期时间参数为选填参数,暂时不做设置 + String tels = StringUtils.listPrintWithSpecialSplit(list, null); + sendEncryptJsonContent.setTels(Base64.encode(tels)); + sendEncryptJsonContent.setReqId(RandomUtil.randomString(10)); + sendEncryptJsonContent.setAppId(encryptAppId); + // 配置 sign + String signStr = makeSign(); + if (StringUtils.isBlank(signStr)) { + log.error("SaveToFileTask|makeSign fail!"); + } + sendEncryptJsonContent.setSig(signStr); + // 组装成JSON + String readSendJsonStr = JSON.toJSONString(sendEncryptJsonContent); + log.info("SaveToFileTask|batchSendToEncrypt ready send json is : {}", readSendJsonStr); + int count = 0; + while (count < 3) { + // 调用HTTP请求发送数据 + HttpResponse httpResponse = sendPostReq(readSendJsonStr); + if (httpResponse.isOk() && httpResponse.body().contains("success")) { + log.info("========== [SaveToFileTask|batchSendToEncrypt request success, response is {} ] ==========", httpResponse.body()); + String responseStr = httpResponse.body(); + // 解析返回的结果,并写回本地 + if (!parseResponseStr(filePath, responseStr)) { + log.error("========== [ SaveToFileTask|batchSendToEncrypt parse fail ] =========="); + booleanTag = false; + } + break; + } else { + count++; + try { + // 重新发送前休眠3秒 + Thread.sleep(3_0000); + } catch (InterruptedException e) { + log.error("SaveToFileTask|batchSendToEncrypt sleep ERROR. message is", e.getMessage(), e); + } + log.error("========== [SaveToFileTask|batchSendToEncrypt request fail, response is {} ] ==========", httpResponse.body()); + } + } + if (count >= 3) { + log.error("========== [SaveToFileTask|batchSendToEncrypt update send status fail, url is {} ] ==========", encryptAddress); + } + + } + ); + } + + private String makeSign() { + + String signBuilder = encryptAppId + + encryptTK + + DateUtil.localDateTimeFormat(LocalDateTime.now(), FORMATE_TIMESTAMP); + + return SecureUtil.md5(signBuilder).toUpperCase(); + } + + /** + * 调用HTTP请求发送Post请求 + * + * @param json 请求的body内容 + * @return 返回请求结果 + */ + private HttpResponse sendPostReq(String json) { + + HttpResponse httpResponse = HttpRequest + .post(encryptAddress) + .header("Content-Type", "application/json;charset=utf-8") + .body(json) + .execute(); + + return httpResponse; + } + + private boolean parseResponseStr(String filePath, String responseStr) { + ResponseEncryptJsonContent responseContent = JSONArray.parseObject(responseStr, ResponseEncryptJsonContent.class); + // 处理需要的加密号串 + String tels = responseContent.getTels(); + + String[] splitResTels = StringUtils.split(Base64.decodeStr(tels), SPLIT_TAG); + + if (splitResTels != null && splitResTels.length > 0) { + List stringList = Arrays.asList(splitResTels); + // TODO: 2021/1/5 0005 这里保存文件的路径进行定义下 + String responseFilePath = filePath + TEMP_FILE_END_STR; + // 写入指定路径 + writeToFile(stringList, responseFilePath); + return Boolean.TRUE; + } + return Boolean.FALSE; + } + private void writeToFile(List collect, String fullPath) { + // 构建存储文件 + try { + if (!FileUtil.exist(fullPath)) { + Files.write(Paths.get(fullPath), collect, StandardOpenOption.CREATE_NEW); + } else { + Files.write(Paths.get(fullPath), collect, StandardOpenOption.APPEND); + } + } catch (IOException e) { + log.error("write prepare send file fail, please check param, fullPath is {}, {}", fullPath, e); + } + } + + private boolean delTempSaveFile(String tempFilesPath) { + boolean del = FileUtil.del(tempFilesPath); + if (del) { + log.info("======== [success del file, file path is {} ] ========", tempFilesPath); + return Boolean.TRUE; + } else { + log.error("======== [fail del file, file path is {} ] ========", tempFilesPath); + return Boolean.FALSE; + } + } + + private void transFileToOtherServer(String filePath) { + + Session session = JschUtil.createSession(REMOTE_TRANS_HOST, REMOTE_TRANS_PORT, REMOTE_TRANS_SSH_USER, REMOTE_TRANS_SSH_PW); + + Sftp sftp = JschUtil.createSftp(session); + + sftp.put(filePath, REMOTE_TRANS_DIR_PATH, Sftp.Mode.RESUME); + + sftp.close(); + } + + + /** + * 调用HTTP请求发送更新记录的Post请求 + * + * @param path 单个存储在本地的路径地址 + * @return 返回请求结果 + */ + private boolean sendUpdatePostReq(String path,TbUploadFileNewDto tbUploadFileNewDto) { + // 构建发送参数 + SendRemoteUpdateJsonContent sendRemoteUpdateJsonContent = new SendRemoteUpdateJsonContent(); + + BeanUtil.copyProperties(tbUploadFileNewDto, sendRemoteUpdateJsonContent); + String fileFullName = FileUtil.getName(path); + if (StringUtils.isNotBlank(fileFullName)) { + // FIXME: 2021/1/12 0012 + sendRemoteUpdateJsonContent.setSftpFilePath(REMOTE_TRANS_DIR_PATH + fileFullName); + } + sendRemoteUpdateJsonContent.setFileTransSuccessCount(tbUploadFileNewDto.getFileCount()); + sendRemoteUpdateJsonContent.setUploadRemoteTaskName(tbUploadFileNewDto.getUploadFileTaskName()); + + // 转成Json字符串 + String readySendJson = JSON.toJSONString(sendRemoteUpdateJsonContent); + log.info("====== [SaveToFileTask|sendUpdatePostReq , readySendJson is {} ] ======", readySendJson); + // 发送请求 + HttpResponse httpResponse = HttpRequest + .post(REMOTE_UPDATE_ADDR) + .header("Content-Type", "application/json;charset=utf-8") + .body(readySendJson) + .execute(); + + // 解析回送请求 + if (httpResponse.isOk() && httpResponse.body().contains("SUCCESS")) { + log.info("====== [success send upload record request! ]======"); + return Boolean.TRUE; + } else { + log.error("====== [ SaveToFileTask|sendUpdatePostReq , send upload record request Fail! , reason is {} ]======", httpResponse.body()); + } + + return Boolean.FALSE; + } +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/ExcelUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/ExcelUtils.java new file mode 100644 index 0000000..0222f6d --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/ExcelUtils.java @@ -0,0 +1,93 @@ +package me.zhengjie.modules.uploadnew.util; + +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; +@Slf4j +public class ExcelUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtils.class); + + /** + * excel解析方法 + * @param inputStream 文件流 + * @return + * @throws IOException + */ + public static List excelParseList(InputStream inputStream) throws IOException { + List list = new ArrayList<>(); + try { + Workbook workbook = new XSSFWorkbook(inputStream); + + //获取第一张工作表 + Sheet sheet = workbook.getSheetAt(0); + //从第二行开始获取 + for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { + //循环获取工作表的每一行 + Row sheetRow = sheet.getRow(i); + + for (int j = 0; j < sheetRow.getPhysicalNumberOfCells(); j++) { + //将每一个单元格的值装入列集合 + sheetRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING); + list.add(sheetRow.getCell(j).getStringCellValue()); + } + //关闭资源 + workbook.close(); + } + } catch (FileNotFoundException e) { + log.info("ExcelUtils | excelParseList ==========未找到文件"); + e.printStackTrace(); + } catch (IOException e) { + log.info("ExcelUtils | excelParseList ==========解析失败!!"); + e.printStackTrace(); + } + return list; + } + + /** + * excel解析方法 + * @param url 文件路径 + * @return + * @throws IOException + */ + public static List excelParseListByUrl(String url) throws IOException { + List list = new ArrayList<>(); + InputStream inputStream = new FileInputStream(new File(url)) ; + try { + Workbook workbook = new XSSFWorkbook(inputStream); + + //获取第一张工作表 + Sheet sheet = workbook.getSheetAt(0); + //从第二行开始获取 + for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) { + //循环获取工作表的每一行 + Row sheetRow = sheet.getRow(i); + + for (int j = 0; j < sheetRow.getPhysicalNumberOfCells(); j++) { + //将每一个单元格的值装入列集合 + sheetRow.getCell(j).setCellType(Cell.CELL_TYPE_STRING); + list.add(sheetRow.getCell(j).getStringCellValue()); + } + //关闭资源 + workbook.close(); + } + } catch (FileNotFoundException e) { + log.info("ExcelUtils | excelParseList ==========未找到文件"); + e.printStackTrace(); + } catch (IOException e) { + log.info("ExcelUtils | excelParseList ==========解析失败!!"); + e.printStackTrace(); + } + return list; + } + + +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/TxtUtils.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/TxtUtils.java new file mode 100644 index 0000000..f6aaa6e --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/util/TxtUtils.java @@ -0,0 +1,151 @@ +package me.zhengjie.modules.uploadnew.util; + +import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; +@Slf4j +public class TxtUtils { + private static final Logger LOGGER = LoggerFactory.getLogger(TxtUtils.class); + /** + * txt解析方法 + * @param inputStream 文件流 + * @return + * @throws IOException + */ + public static List txtParseList(InputStream inputStream) throws IOException { + List list = new ArrayList<>(); + BufferedReader d = new BufferedReader(new InputStreamReader(inputStream)); + try { + String count; + while((count = d.readLine()) != null){ + String u = count.toUpperCase(); + list.add(u); + } + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | txtParseList ==========未找到文件"); + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | txtParseList ==========解析失败!!"); + e.printStackTrace(); + }finally { + d.close(); + } + return list; + } + + /** + * txt解析方法 + * @param url 文件路径 + * @return + * @throws IOException + */ + public static List txtParseListVyUrl(String url) throws IOException { + List list = new ArrayList<>(); + InputStream inputStream = new FileInputStream(new File(url)) ; + BufferedReader d = new BufferedReader(new InputStreamReader(inputStream)); + try { + String count; + while((count = d.readLine()) != null){ + String u = count.toUpperCase(); + list.add(u); + } + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | txtParseList ==========未找到文件"); + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | txtParseList ==========解析失败!!"); + e.printStackTrace(); + }finally { + d.close(); + } + return list; + } + /** + * csv解析方法 + * @param inputStream 文件流 + * @return + * @throws IOException + */ + public static List csvParseList(InputStream inputStream) throws IOException { + List list = new ArrayList<>(); + BufferedReader d = new BufferedReader(new InputStreamReader(inputStream)); + StringBuilder builder = new StringBuilder(); + try { + String count; + while((count = d.readLine()) != null){ + String u = count.toUpperCase(); + builder.append(u+","); + } + log.error("========== [builder.toString] =========="+builder.toString()); + if (builder!=null){ + String str= builder.toString(); + String[] split = str.split(","); + for (String sp : split) { + list.add(sp); + } + } + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | csvParseList ==========未找到文件"); + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | csvParseList ==========解析失败!!"); + e.printStackTrace(); + }finally { + d.close(); + } + + return list; + } + + /** + * csv解析方法 + * @param url 文件路径 + * @return + * @throws IOException + */ + public static List csvParseListByUrl(String url) throws IOException { + List list = new ArrayList<>(); + InputStream inputStream = new FileInputStream(new File(url)) ; + BufferedReader d = new BufferedReader(new InputStreamReader(inputStream)); + StringBuilder builder = new StringBuilder(); + try { + String count; + while((count = d.readLine()) != null){ + String u = count.toUpperCase(); + builder.append(u+","); + } + log.error("========== [builder.toString] =========="+builder.toString()); + if (builder!=null){ + String str= builder.toString(); + String[] split = str.split(","); + for (String sp : split) { + list.add(sp); + } + } + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | csvParseList ==========未找到文件"); + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + log.info("TxtUtils | csvParseList ==========解析失败!!"); + e.printStackTrace(); + }finally { + d.close(); + } + + return list; + } +} diff --git a/eladmin-system/src/main/resources/config/application-dev.yml b/eladmin-system/src/main/resources/config/application-dev.yml index a428315..c3ce0c4 100644 --- a/eladmin-system/src/main/resources/config/application-dev.yml +++ b/eladmin-system/src/main/resources/config/application-dev.yml @@ -4,7 +4,7 @@ spring: druid: db-type: com.alibaba.druid.pool.DruidDataSource driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy - url: jdbc:log4jdbc:mysql://localhost:3306/eladmin-plat2?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false + url: jdbc:log4jdbc:mysql://localhost:3306/eladmin1?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false username: root password: root # 初始连接数 @@ -122,3 +122,6 @@ save: # 切换系统环境相关的配置 sys: debug: true + + +