Przeglądaj źródła

监理文件下载功能

zhuang 1 rok temu
rodzic
commit
39764625f0

+ 15 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/dao/SysFileItemDao.java

@@ -0,0 +1,15 @@
+package com.rongwei.bscommon.sys.dao;
+
+import com.rongwei.rwcommon.base.BaseDao;
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
+
+/**
+ * 文件表
+ * 
+ * @author chenshun
+ * @email sunlightcs@gmail.com
+ * @date 2019-10-08 14:53:10
+ */
+public interface SysFileItemDao extends BaseDao<SysFileItemDo> {
+	
+}

+ 18 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/service/SysFileItemService.java

@@ -0,0 +1,18 @@
+package com.rongwei.bscommon.sys.service;
+
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
+
+
+/**
+ * 文件表
+ *
+ * @author chenshun
+ * @email sunlightcs@gmail.com
+ * @date 2019-10-08 14:53:10
+ */
+public interface SysFileItemService extends IService<SysFileItemDo> {
+
+}
+

+ 12 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/SysFileItemServiceImpl.java

@@ -0,0 +1,12 @@
+package com.rongwei.bscommon.sys.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.rongwei.bscommon.sys.dao.SysFileItemDao;
+import com.rongwei.bscommon.sys.service.SysFileItemService;
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
+import org.springframework.stereotype.Service;
+
+@Service("sysFileItemService")
+public class SysFileItemServiceImpl extends ServiceImpl<SysFileItemDao, SysFileItemDo> implements SysFileItemService {
+
+}

+ 63 - 0
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ZhcxLoginServiceImpl.java

@@ -0,0 +1,63 @@
+package com.rongwei.bscommon.sys.service.impl;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.rongwei.bscommon.sys.feign.RwAdminServerFeignService;
+import com.rongwei.bscommon.sys.service.ZhcxLoginService;
+import com.rongwei.commonservice.service.RedisService;
+import com.rongwei.rwadmincommon.system.domain.UserLoginVo;
+import com.rongwei.rwcommon.base.R;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.Map;
+
+/**
+ * @author zhuang
+ */
+@Service
+public class ZhcxLoginServiceImpl implements ZhcxLoginService {
+
+    @Autowired
+    private RedisService redisService;
+    @Autowired
+    private RwAdminServerFeignService rwAdminServerFeignService;
+
+    @Override
+    public R loginIn(Map<String, Object> map) {
+        Object phoneObj = map.get("phone");
+        Object loginTypeObj = map.get("loginType");
+        Object accountObj = map.get("account");
+        Object passwordObj = map.get("password");
+        Object codeObj = map.get("code");
+        if(ObjectUtil.isEmpty(phoneObj) || ObjectUtil.isEmpty(loginTypeObj)
+                || ObjectUtil.isEmpty(accountObj) || ObjectUtil.isEmpty(passwordObj)
+                || ObjectUtil.isEmpty(codeObj)){
+            return R.error();
+        }
+        String phone = (String) phoneObj;
+        String loginType = (String) loginTypeObj;
+        String account = (String) accountObj;
+        String password = (String) passwordObj;
+        String code = (String) codeObj;
+        UserLoginVo userLoginVo = new UserLoginVo();
+        userLoginVo.setUsername(account);
+        userLoginVo.setPassword(password);
+        boolean exist = redisService.hasKey(loginType + account);
+        if(exist){
+            Object yzmObj = redisService.getRedisCatchObj(loginType + account);
+            if(ObjectUtil.isEmpty(yzmObj)){
+                return R.error("未获取到验证码");
+            }else{
+                String yzm = (String) yzmObj;
+                if(yzm.equals(code)){
+                    R r = rwAdminServerFeignService.loginIn(userLoginVo);
+                    return r;
+                }else {
+                    return R.error("验证码错误,请重新输入");
+                }
+            }
+        }else{
+            return R.error("验证码已过期,请先发送验证码");
+        }
+    }
+}

+ 135 - 1
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ZhcxOutsideInspectionItpServiceImpl.java

@@ -1,9 +1,13 @@
 package com.rongwei.bscommon.sys.service.impl;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.json.JSONUtil;
+import cn.hutool.core.util.ZipUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.rongwei.bscommon.sys.feign.SysNotifyFeginService;
 import com.rongwei.bscommon.sys.service.*;
@@ -24,13 +28,21 @@ import com.rongwei.rwcommon.utils.SecurityUtil;
 import com.rongwei.rwcommon.utils.StringUtils;
 import com.rongwei.rwcommon.vo.MailDo;
 import com.rongwei.rwcommon.vo.generalsql.MasterSlaveUpdateVo;
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
 import com.rongwei.rwcommonentity.commonservers.vo.SysSerialVo;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
-
+import org.springframework.util.ResourceUtils;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 
 /**
@@ -66,6 +78,9 @@ public class ZhcxOutsideInspectionItpServiceImpl extends ServiceImpl<ZhcxOutside
     @Autowired
     private SysGeneralCRUDService sysGeneralCRUDService;
 
+    @Autowired
+    private SysFileItemService sysFileItemService;
+
     @Autowired
     private SysNotifyFeginService notifyFeginService;
 
@@ -479,6 +494,125 @@ public class ZhcxOutsideInspectionItpServiceImpl extends ServiceImpl<ZhcxOutside
         return notifyDto;
     }
 
+    @Override
+    public int downloadFile(Map<String, Object> map, HttpServletResponse response) throws FileNotFoundException {
+        Object itpid = map.get("itpid");
+        if(ObjectUtil.isEmpty(itpid)){
+            return 0;
+        }
+        String itpId = (String) itpid;
+        ZhcxOutsideInspectionItpDo inspectionItpDo = getById(itpId);
+        LambdaQueryWrapper<ZhcxOutsideInspectionItpSupervisionDispatchDo> wrapper = Wrappers.lambdaQuery();
+        wrapper.eq(ZhcxOutsideInspectionItpSupervisionDispatchDo::getItpid,itpId);
+        ZhcxOutsideInspectionItpSupervisionDispatchDo dispatchDo = dispatchService.getOne(wrapper);
+        List<String> fileIds = new ArrayList<>();
+        if(dispatchDo != null){
+            String sfiles = dispatchDo.getSfiles();
+            String signature = dispatchDo.getSignature();
+            if(StringUtils.isNotBlank(signature)){
+                getIds(signature, fileIds);
+            }
+            if(StringUtils.isNotBlank(sfiles)){
+                getIds(sfiles, fileIds);
+            }
+        }
+        String files = inspectionItpDo.getFiles();
+        if(StringUtils.isNotBlank(files)){
+            getIds(files, fileIds);
+        }
+        if(fileIds.size() > 0){
+            QueryWrapper<SysFileItemDo> qw = new QueryWrapper();
+            qw.in("ID", fileIds);
+            List<SysFileItemDo> list = sysFileItemService.list(qw);
+            if(list.size() > 0){
+                // 获取类路径
+                String classPath = ResourceUtils.getURL("classpath:").getPath();
+                // 临时目录 以uuid+时间戳 为目录名称
+                String temp = classPath + IdUtil.simpleUUID() + System.currentTimeMillis();
+                // 临时目录如果存在则删除
+                File tempDir = new File(temp);
+                File zipFile = null;
+                try {
+
+                    if (tempDir.exists()) {
+                        FileUtil.del(tempDir);
+                    }
+                    // 创建临时目录
+                    boolean mkdir = tempDir.mkdir();
+                    if (mkdir) {
+
+                        list.forEach(item->
+                            FileUtil.copyFile(new File(item.getFullpath()),
+                            new File(temp+File.separator+item.getFilename()))
+                        );
+                        // 设置响应的一些格式
+                        String encodedFileName = URLEncoder.encode(inspectionItpDo.getInspectioncode()+".zip", "UTF-8").replaceAll("\\+", "%20");
+                        response.setContentType("application/octet-stream");
+                        response.setHeader("content-type", "application/octet-stream");
+                        //response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
+                        response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", encodedFileName));
+                        //response.setHeader("Pragma", "no-cache");
+                        //response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
+                        //response.setHeader("Expires", "0");
+                        // 将临时目录压缩成zip
+                        zipFile = ZipUtil.zip(temp);
+                        byte[] buffer = new byte[1024];
+                        FileInputStream fis = null;
+                        BufferedInputStream bis = null;
+                        try{
+                            fis = new FileInputStream(zipFile);
+                            bis = new BufferedInputStream(fis);
+                            OutputStream os = response.getOutputStream();
+                            int i = bis.read(buffer);
+                            while (i != -1) {
+                                os.write(buffer, 0, i);
+                                i = bis.read(buffer);
+                            }
+                        } catch (IOException e) {
+                            throw new RuntimeException(e);
+                        }finally {
+                            if (bis != null) {
+                                try {
+                                    bis.close();
+                                } catch (IOException e) {
+
+                                }
+                            }
+                            if (fis != null) {
+                                try {
+                                    fis.close();
+                                } catch (IOException e) {
+
+                                }
+                            }
+                        }
+
+                    }
+                } catch (Exception e) {
+                    //throw new CustomException("下载附件失败");
+                    return 1;
+                }finally {
+                    // 删除临时目录和压缩后的文件
+                    FileUtil.del(tempDir);
+                    FileUtil.del(zipFile);
+                }
+            }else{
+                return 0;
+            }
+        }else{
+            return 0;
+        }
+        return 2;
+    }
+
+    private void getIds(String files, List<String> fileIds) {
+        String[] split = files.split("\\^_\\^");
+        for(String fileItem : split){
+            String id = fileItem.split("-;-")[1];
+            fileIds.add(id);
+        }
+    }
+
     /**
      * 保存
      *

+ 14 - 0
business-server/src/main/java/com/rongwei/bsserver/controller/ZhcxOperOutsideInspectionController.java

@@ -5,13 +5,17 @@ import com.rongwei.bscommon.sys.service.ZhcxOutsideInspectionItpService;
 import com.rongwei.bscommon.sys.utils.LockUtils;
 import com.rongwei.bsentity.vo.OperOutsideInspectionVo;
 import com.rongwei.rwcommon.base.R;
+import com.rongwei.rwcommon.base.exception.CustomException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletResponse;
+import java.io.FileNotFoundException;
 import java.util.List;
+import java.util.Map;
 
 @RestController
 @RequestMapping("/zhcxOperOutsideInspection")
@@ -41,4 +45,14 @@ public class ZhcxOperOutsideInspectionController {
             }
         }
     }
+
+    @RequestMapping("/downloadFile")
+    public void downloadFile(@RequestBody Map<String,Object> map, HttpServletResponse response) throws FileNotFoundException {
+        int i = inspectionService.downloadFile(map, response);
+        if(i == 0){
+            throw new CustomException("暂无相关附件");
+        }else if(i == 1){
+            throw new CustomException("下载附件失败");
+        }
+    }
 }