From 6060c73073b5de2d00ef2d5b9d49ec35eb6dff42 Mon Sep 17 00:00:00 2001 From: qyx <565485304@qq.com> Date: Thu, 5 Aug 2021 16:23:53 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=96=B0=E5=A2=9E=E5=8A=9F=E8=83=BD](master):?= =?UTF-8?q?=20=E6=96=B0=E5=A2=9E=E5=8A=A0=E8=A1=A8=E5=8D=95=E5=88=86?= =?UTF-8?q?=E5=8F=91=E5=8A=9F=E8=83=BD=E9=9C=80=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增加表单分发功能需求骨架 --- .../me/zhengjie/common/http/ResponseCode.java | 4 + .../service/mapstruct/LogErrorMapperImpl.java | 2 +- .../service/mapstruct/LogSmallMapperImpl.java | 2 +- .../modules/constant/FileConstant.java | 29 +++ .../modules/formdata/domain/Formdata.java | 75 ++++++ .../repository/FormdataRepository.java | 28 +++ .../formdata/rest/FormdataController.java | 89 ++++++++ .../formdata/service/FormdataService.java | 101 +++++++++ .../formdata/service/dto/FormdataDto.java | 54 +++++ .../service/dto/FormdataQueryCriteria.java | 50 ++++ .../service/impl/FormdataServiceImpl.java | 150 ++++++++++++ .../service/mapstruct/FormdataMapper.java | 32 +++ .../modules/upload/domain/UploadFile.java | 26 +-- .../repository/UploadFileRepository.java | 2 +- .../upload/rest/UploadFileController.java | 209 +---------------- .../upload/service/UploadFileService.java | 17 +- .../upload/service/dto/UploadFileDto.java | 13 +- .../service/dto/UploadFileQueryCriteria.java | 20 +- .../service/impl/UploadFileServiceImpl.java | 213 ++++-------------- .../service/mapstruct/UploadFileMapper.java | 2 +- .../rest/TbUploadFileNewController.java | 94 +++++++- .../uploadnew/task/TransFormDataTask.java | 31 +++ .../src/test/java/me/zhengjie/DemoTest.java | 20 ++ pom.xml | 7 +- 24 files changed, 829 insertions(+), 441 deletions(-) create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/constant/FileConstant.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/domain/Formdata.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/repository/FormdataRepository.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/rest/FormdataController.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataDto.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataQueryCriteria.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/mapstruct/FormdataMapper.java create mode 100644 eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.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 e598194..66d9516 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 @@ -28,6 +28,10 @@ public enum ResponseCode { NO_MATCH_ARGUMENT_SET(1,"不能满足要求的参数设置"), /**没有文件输入*/ NO_FILE_INPUT(1,"没有文件输入"), + /** + * 文件处理失败 + */ + FILE_HANDLE_FAIL(1, "文件处理失败"), /** modeify by wzx */ diff --git a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java index c17c8f7..9cf588d 100644 --- a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java +++ b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogErrorMapperImpl.java @@ -9,7 +9,7 @@ import org.springframework.stereotype.Component; @Generated( value = "org.mapstruct.ap.MappingProcessor", - date = "2021-04-24T00:12:04+0800", + date = "2021-08-05T14:00:16+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)" ) @Component diff --git a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java index 7e9cbf6..4e9f242 100644 --- a/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java +++ b/eladmin-logging/target/generated-sources/annotations/me/zhengjie/service/mapstruct/LogSmallMapperImpl.java @@ -9,7 +9,7 @@ import org.springframework.stereotype.Component; @Generated( value = "org.mapstruct.ap.MappingProcessor", - date = "2021-04-24T00:12:04+0800", + date = "2021-08-05T14:00:16+0800", comments = "version: 1.3.1.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)" ) @Component diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/constant/FileConstant.java b/eladmin-system/src/main/java/me/zhengjie/modules/constant/FileConstant.java new file mode 100644 index 0000000..a71e9b0 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/constant/FileConstant.java @@ -0,0 +1,29 @@ +package me.zhengjie.modules.constant; + + +/** + * 文件相关 常量类 + */ +public class FileConstant { + + /** + * 文件分隔符 . + */ + public static final String SPLIT_FILE_SYMBOL = "."; + + /** + * 以 xlsx 结尾的文件 + */ + public static final String XLSX_FILE_SUB_NAME = "xlsx"; + + + /** + * 以 zip 结尾的文件 + */ + public static final String ZIP_FILE_SUB_NAME = "zip"; + + /** + * 以 rar 结尾的文件 + */ + public static final String RAR_FILE_SUB_NAME = "rar"; +} diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/domain/Formdata.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/domain/Formdata.java new file mode 100644 index 0000000..48fef73 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/domain/Formdata.java @@ -0,0 +1,75 @@ +/* +* 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.formdata.domain; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.bean.copier.CopyOptions; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.persistence.*; +import java.io.Serializable; +import java.sql.Timestamp; + +/** +* @website https://el-admin.vip +* @description / +* @author x +* @date 2021-08-05 +**/ +@Entity +@Data +@Table(name="t_formdata") +public class Formdata implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + @ApiModelProperty(value = "主键") + private Long id; + + @Column(name = "upload_time") + @ApiModelProperty(value = "上传时间") + private Timestamp uploadTime; + + @Column(name = "source_id") + @ApiModelProperty(value = "渠道方id") + private Integer sourceId; + + @Column(name = "name") + @ApiModelProperty(value = "称呼") + private String name; + + @Column(name = "phone") + @ApiModelProperty(value = "联系方式") + private String phone; + + @Column(name = "age") + @ApiModelProperty(value = "年龄") + private Integer age; + + @Column(name = "province") + @ApiModelProperty(value = "省份") + private String province; + + @Column(name = "city") + @ApiModelProperty(value = "城市") + private String city; + + public void copy(Formdata 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/formdata/repository/FormdataRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/repository/FormdataRepository.java new file mode 100644 index 0000000..25cc023 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/repository/FormdataRepository.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.formdata.repository; + +import me.zhengjie.modules.formdata.domain.Formdata; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +/** +* @website https://el-admin.vip +* @author x +* @date 2021-08-05 +**/ +public interface FormdataRepository extends JpaRepository, JpaSpecificationExecutor { +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/rest/FormdataController.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/rest/FormdataController.java new file mode 100644 index 0000000..be0a91f --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/rest/FormdataController.java @@ -0,0 +1,89 @@ +/* +* 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.formdata.rest; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import me.zhengjie.annotation.Log; +import me.zhengjie.modules.formdata.domain.Formdata; +import me.zhengjie.modules.formdata.service.FormdataService; +import me.zhengjie.modules.formdata.service.dto.FormdataQueryCriteria; +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.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** +* @website https://el-admin.vip +* @author x +* @date 2021-08-05 +**/ +@RestController +@RequiredArgsConstructor +@Api(tags = "表单文件解析接口管理") +@RequestMapping("/api/formdata") +public class FormdataController { + + private final FormdataService formdataService; + + @Log("导出数据") + @ApiOperation("导出数据") + @GetMapping(value = "/download") + @PreAuthorize("@el.check('formdata:list')") + public void download(HttpServletResponse response, FormdataQueryCriteria criteria) throws IOException { + formdataService.download(formdataService.queryAll(criteria), response); + } + + @GetMapping + @Log("查询表单文件解析接口") + @ApiOperation("查询表单文件解析接口") + @PreAuthorize("@el.check('formdata:list')") + public ResponseEntity query(FormdataQueryCriteria criteria, Pageable pageable){ + return new ResponseEntity<>(formdataService.queryAll(criteria,pageable),HttpStatus.OK); + } + + @PostMapping + @Log("新增表单文件解析接口") + @ApiOperation("新增表单文件解析接口") + @PreAuthorize("@el.check('formdata:add')") + public ResponseEntity create(@Validated @RequestBody Formdata resources){ + return new ResponseEntity<>(formdataService.create(resources),HttpStatus.CREATED); + } + + @PutMapping + @Log("修改表单文件解析接口") + @ApiOperation("修改表单文件解析接口") + @PreAuthorize("@el.check('formdata:edit')") + public ResponseEntity update(@Validated @RequestBody Formdata resources){ + formdataService.update(resources); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @Log("删除表单文件解析接口") + @ApiOperation("删除表单文件解析接口") + @PreAuthorize("@el.check('formdata:del')") + @DeleteMapping + public ResponseEntity delete(@RequestBody Long[] ids) { + formdataService.deleteAll(ids); + return new ResponseEntity<>(HttpStatus.OK); + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java new file mode 100644 index 0000000..fcdd78d --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/FormdataService.java @@ -0,0 +1,101 @@ +/* +* 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.formdata.service; + +import me.zhengjie.modules.formdata.domain.Formdata; +import me.zhengjie.modules.formdata.service.dto.FormdataDto; +import me.zhengjie.modules.formdata.service.dto.FormdataQueryCriteria; +import org.springframework.data.domain.Pageable; +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 +**/ +public interface FormdataService { + + /** + * 查询数据分页 + * @param criteria 条件 + * @param pageable 分页参数 + * @return Map + */ + Map queryAll(FormdataQueryCriteria criteria, Pageable pageable); + + /** + * 查询所有数据不分页 + * @param criteria 条件参数 + * @return List + */ + List queryAll(FormdataQueryCriteria criteria); + + /** + * 根据ID查询 + * @param id ID + * @return FormdataDto + */ + FormdataDto findById(Long id); + + /** + * 创建 + * @param resources / + * @return FormdataDto + */ + FormdataDto create(Formdata resources); + + /** + * 编辑 + * @param resources / + */ + void update(Formdata resources); + + /** + * 多选删除 + * @param ids / + */ + void deleteAll(Long[] ids); + + /** + * 导出数据 + * @param all 待导出的数据 + * @param response / + * @throws IOException / + */ + void download(List all, HttpServletResponse response) throws IOException; + + /** + * 解析文件并进行存储 + * + * @param file 需要进行处理的文件对象 + * @return 是否处理成功 + */ + boolean parseFileAndSaveToDB(MultipartFile file); + + /** + * 保存音频文件在本地 + * + * @param file + * @return + */ + String parseMediaFileToLocal(MultipartFile file); +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataDto.java new file mode 100644 index 0000000..dca3616 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataDto.java @@ -0,0 +1,54 @@ +/* +* 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.formdata.service.dto; + +import lombok.Data; +import java.sql.Timestamp; +import java.io.Serializable; + +/** +* @website https://el-admin.vip +* @description / +* @author x +* @date 2021-08-05 +**/ +@Data +public class FormdataDto implements Serializable { + + /** 主键 */ + private Long id; + + /** 上传时间 */ + private Timestamp uploadTime; + + /** 渠道方id */ + private Integer sourceId; + + /** 称呼 */ + private String name; + + /** 联系方式 */ + private String phone; + + /** 年龄 */ + private Integer age; + + /** 省份 */ + private String province; + + /** 城市 */ + private String city; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataQueryCriteria.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataQueryCriteria.java new file mode 100644 index 0000000..0dda496 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/dto/FormdataQueryCriteria.java @@ -0,0 +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. +*/ +package me.zhengjie.modules.formdata.service.dto; + +import lombok.Data; +import me.zhengjie.annotation.Query; + +import java.sql.Timestamp; +import java.util.List; + +/** +* @website https://el-admin.vip +* @author x +* @date 2021-08-05 +**/ +@Data +public class FormdataQueryCriteria{ + + /** 精确 */ + @Query + private Timestamp uploadTime; + + /** 精确 */ + @Query + private String phone; + + /** 精确 */ + @Query + private String province; + + /** 精确 */ + @Query + private String city; + /** BETWEEN */ + @Query(type = Query.Type.BETWEEN) + private List age; +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java new file mode 100644 index 0000000..295aaff --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/impl/FormdataServiceImpl.java @@ -0,0 +1,150 @@ +/* +* 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.formdata.service.impl; + +import lombok.RequiredArgsConstructor; +import me.zhengjie.modules.formdata.domain.Formdata; +import me.zhengjie.modules.formdata.repository.FormdataRepository; +import me.zhengjie.modules.formdata.service.FormdataService; +import me.zhengjie.modules.formdata.service.dto.FormdataDto; +import me.zhengjie.modules.formdata.service.dto.FormdataQueryCriteria; +import me.zhengjie.modules.formdata.service.mapstruct.FormdataMapper; +import me.zhengjie.modules.uploadnew.task.TransFormDataTask; +import me.zhengjie.utils.FileUtil; +import me.zhengjie.utils.PageUtil; +import me.zhengjie.utils.QueryHelp; +import me.zhengjie.utils.ValidationUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** +* @website https://el-admin.vip +* @description 服务实现 +* @author x +* @date 2021-08-05 +**/ +@Service +@RequiredArgsConstructor +public class FormdataServiceImpl implements FormdataService { + + private final FormdataRepository formdataRepository; + private final FormdataMapper formdataMapper; + + @Autowired + private TransFormDataTask transFormDataTask; + + @Override + public Map queryAll(FormdataQueryCriteria criteria, Pageable pageable){ + Page page = formdataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); + return PageUtil.toPage(page.map(formdataMapper::toDto)); + } + + @Override + public List queryAll(FormdataQueryCriteria criteria){ + return formdataMapper.toDto(formdataRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); + } + + @Override + @Transactional + public FormdataDto findById(Long id) { + Formdata formdata = formdataRepository.findById(id).orElseGet(Formdata::new); + ValidationUtil.isNull(formdata.getId(),"Formdata","id",id); + return formdataMapper.toDto(formdata); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public FormdataDto create(Formdata resources) { + return formdataMapper.toDto(formdataRepository.save(resources)); + } + + @Override + @Transactional(rollbackFor = Exception.class) + public void update(Formdata resources) { + Formdata formdata = formdataRepository.findById(resources.getId()).orElseGet(Formdata::new); + ValidationUtil.isNull( formdata.getId(),"Formdata","id",resources.getId()); + formdata.copy(resources); + formdataRepository.save(formdata); + } + + @Override + public void deleteAll(Long[] ids) { + for (Long id : ids) { + formdataRepository.deleteById(id); + } + } + + @Override + public void download(List all, HttpServletResponse response) throws IOException { + List> list = new ArrayList<>(); + for (FormdataDto formdata : all) { + Map map = new LinkedHashMap<>(); + map.put("上传时间", formdata.getUploadTime()); + map.put("渠道方id", formdata.getSourceId()); + map.put("称呼", formdata.getName()); + map.put("联系方式", formdata.getPhone()); + map.put("年龄", formdata.getAge()); + map.put("省份", formdata.getProvince()); + map.put("城市", formdata.getCity()); + list.add(map); + } + FileUtil.downloadExcel(list, response); + } + + /** + * 相关逻辑 + * + * @param file 需要进行处理的文件对象 + * @return 是否操作成功 + */ + @Override + public boolean parseFileAndSaveToDB(MultipartFile file) { + List formdataDtos = new ArrayList<>(); + // 1. todo 先进行解析 excel 文件 + + + // 2. todo 把解析后的结果进行入库 + + + // 3. todo 异步发送给 下游接口 + transFormDataTask.doRunTask(formdataDtos); + + return false; + } + + /** + * 相关逻辑 + * @param file 需要进行处理的文件对象 + * @return 是否操作成功 + */ + @Override + public String parseMediaFileToLocal(MultipartFile file) { + // todo 存储音频压缩文件到指定目录下 每次上传以 2021-8-5 为 文件名进行记录 每次记录一天的 并保存记录 + + return ""; + } +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/mapstruct/FormdataMapper.java b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/mapstruct/FormdataMapper.java new file mode 100644 index 0000000..942359e --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/formdata/service/mapstruct/FormdataMapper.java @@ -0,0 +1,32 @@ +/* +* 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.formdata.service.mapstruct; + +import me.zhengjie.base.BaseMapper; +import me.zhengjie.modules.formdata.domain.Formdata; +import me.zhengjie.modules.formdata.service.dto.FormdataDto; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; + +/** +* @website https://el-admin.vip +* @author x +* @date 2021-08-05 +**/ +@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) +public interface FormdataMapper extends BaseMapper { + +} \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java index be559b5..881ec2c 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/domain/UploadFile.java @@ -15,27 +15,26 @@ */ package me.zhengjie.modules.upload.domain; +import lombok.Data; import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.bean.copier.CopyOptions; import io.swagger.annotations.ApiModelProperty; -import lombok.Data; - +import cn.hutool.core.bean.copier.CopyOptions; import javax.persistence.*; -import java.io.Serializable; +import javax.validation.constraints.*; import java.sql.Timestamp; +import java.io.Serializable; /** * @website https://el-admin.vip * @description / * @author x -* @date 2021-01-03 +* @date 2021-08-05 **/ @Entity @Data @Table(name="tb_upload_file") public class UploadFile implements Serializable { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") @@ -46,16 +45,9 @@ public class UploadFile implements Serializable { @ApiModelProperty(value = "上传日期") private Timestamp uploadTime; - /** - * ps:支持一次上传多个文件,采用一个名称来进行标识 - */ - @Column(name = "upload_file_task_name") - @ApiModelProperty(value = "上传任务名称") - private String uploadFileTaskName; - - @Column(name = "operation") + @Column(name = "opration") @ApiModelProperty(value = "操作人") - private String operation; + private String opration; @Column(name = "file_count") @ApiModelProperty(value = "文件解析总数") @@ -69,10 +61,6 @@ 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)); } diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/repository/UploadFileRepository.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/repository/UploadFileRepository.java index 3809d21..493cd5c 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/repository/UploadFileRepository.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/repository/UploadFileRepository.java @@ -22,7 +22,7 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; /** * @website https://el-admin.vip * @author x -* @date 2021-01-03 +* @date 2021-08-05 **/ public interface UploadFileRepository extends JpaRepository, JpaSpecificationExecutor { } \ No newline at end of file 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 e29929c..94fc0b5 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 @@ -15,79 +15,34 @@ */ 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 org.springframework.data.redis.core.RedisTemplate; +import lombok.RequiredArgsConstructor; 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.ServletRequest; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.File; +import io.swagger.annotations.*; 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.*; +import javax.servlet.http.HttpServletResponse; /** * @website https://el-admin.vip * @author x -* @date 2021-01-03 +* @date 2021-08-05 **/ -//@RestController - +@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") @@ -101,28 +56,9 @@ public class UploadFileController { @ApiOperation("查询上传文件解析发送") @PreAuthorize("@el.check('uploadFile:list')") public ResponseEntity 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("新增上传文件解析发送") @@ -148,139 +84,4 @@ 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 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 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 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 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); - } - } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/UploadFileService.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/UploadFileService.java index 53ba72d..c6b6b50 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/UploadFileService.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/UploadFileService.java @@ -19,18 +19,16 @@ 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 org.springframework.web.multipart.MultipartFile; - -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; -import java.util.List; import java.util.Map; +import java.util.List; +import java.io.IOException; +import javax.servlet.http.HttpServletResponse; /** * @website https://el-admin.vip * @description 服务接口 * @author x -* @date 2021-01-03 +* @date 2021-08-05 **/ public interface UploadFileService { @@ -82,11 +80,4 @@ public interface UploadFileService { * @throws IOException / */ void download(List all, HttpServletResponse response) throws IOException; - - /** - * 加密并生成存储文件 - * - * @param files - */ - UploadFileDto encryptDataAndSaveToFile(MultipartFile[] files, String taskName); } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java index 16f8fd4..e5b7c06 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileDto.java @@ -16,15 +16,14 @@ package me.zhengjie.modules.upload.service.dto; import lombok.Data; - -import java.io.Serializable; import java.sql.Timestamp; +import java.io.Serializable; /** * @website https://el-admin.vip * @description / * @author x -* @date 2021-01-03 +* @date 2021-08-05 **/ @Data public class UploadFileDto implements Serializable { @@ -36,7 +35,7 @@ public class UploadFileDto implements Serializable { private Timestamp uploadTime; /** 操作人 */ - private String operation; + private String opration; /** 文件解析总数 */ private Long fileCount; @@ -46,10 +45,4 @@ public class UploadFileDto implements Serializable { /** 上传状态 */ private Integer uploadTag; - - /** 上传保存路径 */ - private String localSavePath; - - /** 上传的文件名称 **/ - private String uploadFileTaskName; } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java index a0eb2b8..31a77c1 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/dto/UploadFileQueryCriteria.java @@ -16,35 +16,29 @@ 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-01-03 +* @date 2021-08-05 **/ @Data public class UploadFileQueryCriteria{ /** 精确 */ - @Query(type = Query.Type.EQUAL) + @Query private Long id; - /** 精确 */ - @Query(type = Query.Type.EQUAL) - private String operation; + /** 模糊 */ + @Query(type = Query.Type.INNER_LIKE) + private String opration; /** 精确 */ - @Query(type = Query.Type.EQUAL) + @Query private Integer uploadTag; - - /** 精确 */ - @Query(type = Query.Type.EQUAL) - private String uploadFileTaskName; - /** BETWEEN */ @Query(type = Query.Type.BETWEEN) private List uploadTime; diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java index 8fc39a9..50adeba 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/impl/UploadFileServiceImpl.java @@ -1,111 +1,71 @@ /* - * 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 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.utils.ValidationUtil; +import me.zhengjie.utils.FileUtil; +import lombok.RequiredArgsConstructor; 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 me.zhengjie.utils.*; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; - -import javax.servlet.http.HttpServletResponse; -import java.io.File; +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 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.*; - +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; +import java.util.LinkedHashMap; /** - * @author x - * @website https://el-admin.vip - * @description 服务实现 - * @date 2021-01-03 - **/ +* @website https://el-admin.vip +* @description 服务实现 +* @author x +* @date 2021-08-05 +**/ @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 queryAll(UploadFileQueryCriteria criteria, Pageable pageable) { - - Page page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable); + public Map queryAll(UploadFileQueryCriteria criteria, Pageable pageable){ + Page page = uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); return PageUtil.toPage(page.map(uploadFileMapper::toDto)); } - @Override - public List queryAll(UploadFileQueryCriteria criteria) { - return uploadFileMapper.toDto(uploadFileRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder))); + public List 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); } @@ -119,7 +79,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); } @@ -135,9 +95,9 @@ public class UploadFileServiceImpl implements UploadFileService { public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (UploadFileDto uploadFile : all) { - Map map = new LinkedHashMap<>(); + Map map = new LinkedHashMap<>(); map.put("上传日期", uploadFile.getUploadTime()); - map.put("操作人", uploadFile.getOperation()); + map.put("操作人", uploadFile.getOpration()); map.put("文件解析总数", uploadFile.getFileCount()); map.put("上传成功总数", uploadFile.getFileTransSuccessCount()); map.put("上传状态", uploadFile.getUploadTag()); @@ -145,99 +105,4 @@ 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 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(new Date().getTime())); - 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 ""; - } - } } \ No newline at end of file diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/mapstruct/UploadFileMapper.java b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/mapstruct/UploadFileMapper.java index c1734f8..6dd7bc7 100644 --- a/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/mapstruct/UploadFileMapper.java +++ b/eladmin-system/src/main/java/me/zhengjie/modules/upload/service/mapstruct/UploadFileMapper.java @@ -24,7 +24,7 @@ import org.mapstruct.ReportingPolicy; /** * @website https://el-admin.vip * @author x -* @date 2021-01-03 +* @date 2021-08-05 **/ @Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE) public interface UploadFileMapper extends BaseMapper { 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 index 6fd8050..5ed1820 100644 --- 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 @@ -25,6 +25,7 @@ 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.formdata.service.FormdataService; import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; import me.zhengjie.modules.uploadnew.redis.RedisParentDao; import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; @@ -50,6 +51,7 @@ import java.io.IOException; import java.util.List; import java.util.Objects; +import static me.zhengjie.modules.constant.FileConstant.*; import static me.zhengjie.modules.upload.consts.UploadFileConst.FILE_PATH_SPLIT; import static me.zhengjie.modules.uploadnew.consts.SysConstNew.*; @@ -67,14 +69,22 @@ import static me.zhengjie.modules.uploadnew.consts.SysConstNew.*; @Slf4j public class TbUploadFileNewController { - private final TbUploadFileNewService tbUploadFileNewService; + private TbUploadFileNewService tbUploadFileNewService; - @Autowired private SaveToFileNewTask saveToFileNewTask; - @Autowired private RedisTemplate redisTemplate; + private FormdataService formdataService; + + @Autowired + public TbUploadFileNewController(TbUploadFileNewService tbUploadFileNewService, SaveToFileNewTask saveToFileNewTask, RedisTemplate redisTemplate, FormdataService formdataService) { + this.tbUploadFileNewService = tbUploadFileNewService; + this.saveToFileNewTask = saveToFileNewTask; + this.redisTemplate = redisTemplate; + this.formdataService = formdataService; + } + @Log("导出数据") @ApiOperation("导出数据") @GetMapping(value = "/download") @@ -117,6 +127,84 @@ public class TbUploadFileNewController { return new ResponseEntity<>(HttpStatus.OK); } + + /** + * 新的成单数据及录音文件导入接口 + * + * @param files 上传的多个文件 + * @param taskName 本次任务的任务名称 + * @return + */ + @Log("新的成单数据及录音文件导入接口") + @ApiOperation("新的成单数据及录音文件导入接口") +// @AnonymousAccess + @PostMapping("/newFileUpload") + @PreAuthorize("@el.check('tbUploadFileNew:list')") // 这个换下相关按钮的权限控制 +// @ResponseBody + public ResponseEntity newFileUpload(@RequestParam("file") MultipartFile[] files, + @RequestParam(value = "taskName") String taskName) { + // 记录日志 + log.info("TbUploadFileNewController:newFileUpload | taskName : {} start ", taskName); + + // 校验上传是否有文件 + if (null == files) { + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.NO_FILE_INPUT), HttpStatus.OK); + } + + // todo 记录文件上传记录 + + int finishTag = 0; + + for (MultipartFile file : files) { + String originalFilename = file.getOriginalFilename(); + + if (StringUtils.isNotBlank(originalFilename)) { + String subOriginalFilename = StringUtils.substringAfterLast(originalFilename, SPLIT_FILE_SYMBOL); + finishTag = handleEachUploadFile(file , subOriginalFilename, finishTag); + } + } + + // 确保所有文件都经过了响应的处理 + if (finishTag != files.length) { + // todo 上传完成更新文件上传记录 + log.error("TbUploadFileNewController:newFileUpload | taskName : {} fail ", taskName); + return new ResponseEntity<>(CommonResponse.createByError(ResponseCode.FILE_HANDLE_FAIL), HttpStatus.OK); + } + + // todo 上传完成更新文件上传记录 + + return new ResponseEntity<>(CommonResponse.createBySuccess(ResponseCode.SUCCESS), HttpStatus.OK); + + } + + /** + * 经过 finishTag 个步骤进行处理,然后返回处理后的步骤计数 + * + * @param subOriginalFilename 文件后缀名 + * @return 处理后的步骤计数 + */ + private int handleEachUploadFile(MultipartFile file, String subOriginalFilename, int finishTag) { + + if (XLSX_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { + // TODO: 2021/8/5 0005 解析 入库 异步发送到下游 + if (formdataService.parseFileAndSaveToDB(file)){ + finishTag += 1; + } + } + + if (RAR_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename) + || ZIP_FILE_SUB_NAME.equalsIgnoreCase(subOriginalFilename)) { + // TODO: 2021/8/5 0005 以 压缩文件结尾的格式 则直接进行保存文件到指定文件夹目录下 + String localSavePath = formdataService.parseMediaFileToLocal(file); + if (StringUtils.isNotBlank(localSavePath)){ + // todo 记录保存位置 + finishTag += 1; + } + } + return finishTag; + } + + /** * 文件上传接口 * diff --git a/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java new file mode 100644 index 0000000..5a500e4 --- /dev/null +++ b/eladmin-system/src/main/java/me/zhengjie/modules/uploadnew/task/TransFormDataTask.java @@ -0,0 +1,31 @@ +package me.zhengjie.modules.uploadnew.task; + +import lombok.extern.slf4j.Slf4j; +import me.zhengjie.modules.formdata.service.dto.FormdataDto; +import me.zhengjie.utils.ConvertUtil; +import org.springframework.context.annotation.Scope; +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Component; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.List; + +@Component +@Scope("prototype") +@Slf4j +public class TransFormDataTask { + + + @Async(value = "SendBigDataTaskExecutor") + public void doRunTask(List formdataDtos) { + Long satrtMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start running, task name is {} ] ======", "TransFormDataTask"); + runTask(formdataDtos); + Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); + log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "TransFormDataTask", ConvertUtil.secondToTime(endMilliSecond - satrtMilliSecond)); + } + + private void runTask(List formdataDtos) { + } +} diff --git a/eladmin-system/src/test/java/me/zhengjie/DemoTest.java b/eladmin-system/src/test/java/me/zhengjie/DemoTest.java index ce8f8ce..a10cac4 100644 --- a/eladmin-system/src/test/java/me/zhengjie/DemoTest.java +++ b/eladmin-system/src/test/java/me/zhengjie/DemoTest.java @@ -2,6 +2,8 @@ package me.zhengjie; import cn.hutool.core.util.RandomUtil; import me.zhengjie.utils.FileUtil; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import java.io.File; @@ -10,6 +12,14 @@ import java.util.Set; public class DemoTest { + + @Before + public void be(){ + // TODO: 2021/5/7 0007 dc1 dc2 ... + // shuju + } + + /** * 生成模拟号码的测试用例 */ @@ -29,4 +39,14 @@ public class DemoTest { } } + + + + @After + public void af(){ + // TODO: 2021/5/7 0007 dc1 dc2 ... + // shuju + } + + } diff --git a/pom.xml b/pom.xml index 049c02b..160ca92 100644 --- a/pom.xml +++ b/pom.xml @@ -151,7 +151,12 @@ true - + + + com.alibaba + easyexcel + 2.2.8 + org.apache.poi poi