|
@@ -0,0 +1,100 @@
|
|
|
+package com.gis.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.ZipUtil;
|
|
|
+import com.gis.common.constant.ConfigConstant;
|
|
|
+import com.gis.common.constant.MsgCode;
|
|
|
+import com.gis.common.exception.BaseRuntimeException;
|
|
|
+import com.gis.common.util.FileUtils;
|
|
|
+import com.gis.common.util.Result;
|
|
|
+import com.gis.domain.dto.PageDto;
|
|
|
+import com.gis.domain.po.MaterialEntity;
|
|
|
+import com.gis.mapper.IBaseMapper;
|
|
|
+import com.gis.mapper.MaterialMapper;
|
|
|
+import com.gis.service.MaterialService;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import tk.mybatis.mapper.entity.Condition;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2020/3/11 0011 16:16
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class MaterialServiceImpl extends IBaseServiceImpl<MaterialEntity, Long> implements MaterialService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MaterialMapper entityMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ConfigConstant configConstant;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IBaseMapper<MaterialEntity, Long> getBaseMapper() {
|
|
|
+ return this.entityMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result materialUpload(MultipartFile file, String code) {
|
|
|
+
|
|
|
+
|
|
|
+ if (!FileUtils.checkFile(file)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3002, "非法文件");
|
|
|
+ }
|
|
|
+ String basePath = configConstant.serverBasePath + "material/" + code + File.separator;
|
|
|
+ String filename = file.getOriginalFilename();
|
|
|
+ String savePath = basePath + filename;
|
|
|
+
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ log.info("史料文件写入完成: {}", savePath);
|
|
|
+ return Result.success();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return Result.failure("长传失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result search(PageDto param) {
|
|
|
+ Condition condition = new Condition(MaterialEntity.class);
|
|
|
+ String searchKey = param.getSearchKey();
|
|
|
+ if (StringUtils.isNotBlank(searchKey)) {
|
|
|
+ condition.and().orLike("description", "%" + searchKey + "%");
|
|
|
+ }
|
|
|
+ PageInfo<MaterialEntity> pageInfo = findAll(condition, param.getPageNum(), param.getPageSize());
|
|
|
+ return Result.success(pageInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result download(Long id) {
|
|
|
+ MaterialEntity entity = this.findById(id);
|
|
|
+ if (entity == null) {
|
|
|
+ return Result.failure(MsgCode.e3001, "对象不存在, id:" + id);
|
|
|
+ }
|
|
|
+ String code = entity.getCode();
|
|
|
+ if (StringUtils.isAllEmpty(code)) {
|
|
|
+ return Result.failure(MsgCode.e3003, "文件为空, id:" + id);
|
|
|
+ }
|
|
|
+ String srcPath = configConstant.serverBasePath + "material/" + code;
|
|
|
+ String zipName = "material/" + code + ".zip";
|
|
|
+ String zipPath = configConstant.serverBasePath+ zipName;
|
|
|
+ ZipUtil.zip(srcPath, zipPath, true);
|
|
|
+ log.info("压缩文件完成:{}", zipPath);
|
|
|
+
|
|
|
+ Object url = "/data/" + zipName;
|
|
|
+ log.info("url: {}", url);
|
|
|
+ return Result.success(url);
|
|
|
+ }
|
|
|
+}
|