管道基础大数据平台系统开发-【后端】-Server
1.6
13693261870
2023-01-06 d12b5b41ee33e7eb57f3c3fe00f4fae53eb93388
src/main/java/com/lf/server/controller/show/PipelineController.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,263 @@
package com.lf.server.controller.show;
import com.lf.server.annotation.SysLog;
import com.lf.server.controller.all.BaseController;
import com.lf.server.entity.all.HttpStatus;
import com.lf.server.entity.all.ResponseMsg;
import com.lf.server.entity.all.StaticData;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.show.PipelineEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.Md5Helper;
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.WebHelper;
import com.lf.server.service.data.DownloadService;
import com.lf.server.service.show.PipelineService;
import com.lf.server.service.sys.DownlogService;
import com.lf.server.service.sys.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
 * ç®¡é“分析
 * @author WWW
 */
@Api(tags = "综合展示\\管道分析")
@RestController
@RequestMapping("/pipeline")
public class PipelineController extends BaseController {
    @Autowired
    TokenService tokenService;
    @Autowired
    DownlogService downlogService;
    @Autowired
    DownloadService downloadService;
    @Autowired
    PipelineService pipelineService;
    @SysLog()
    @ApiOperation(value = "查询管段")
    @GetMapping(value = "/selectSegNames")
    public ResponseMsg<Object> selectSegNames() {
        try {
            List<PipelineEntity> rs = pipelineService.selectSegNames();
            return success(rs);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
        }
    }
    @SysLog()
    @ApiOperation(value = "查询管线分析")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pe", value = "管道分析实体类", dataType = "PipelineEntity", paramType = "body")
    })
    @ResponseBody
    @PostMapping(value = "/selectPipeAnalysis")
    public ResponseMsg<Object> selectPipeAnalysis(@RequestBody PipelineEntity pe) {
        try {
            if (null == pe.getGid() || pe.getGid() < 1) {
                return fail("请输入管段ID");
            }
            if (null == pe.getTabs() || 0 == pe.getTabs().size()) {
                return fail("请输入表名");
            }
            if (!checkTabs(pe.getTabs())) {
                return fail("存在非法表名");
            }
            Map<String, Object> map = new HashMap<>(4);
            for (String tab : pe.getTabs()) {
                List<PipelineEntity> rs = pipelineService.selectPipeAnalysis(tab, pe.getGid());
                map.put(tab, rs);
            }
            return success(map);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
        }
    }
    @SysLog()
    @ApiOperation(value = "请求管道分析结果下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pe", value = "管道分析实体类", dataType = "PipelineEntity", paramType = "body")
    })
    @ResponseBody
    @PostMapping(value = "/downloadReq")
    public ResponseMsg<Object> downloadReq(@RequestBody PipelineEntity pe, HttpServletRequest req) {
        try {
            if (null == pe || StringHelper.isEmpty(pe.getPwd())) {
                return fail("密码不能为空");
            }
            if (null == pe.getTabs() || 0 == pe.getTabs().size()) {
                return fail("请输入表名");
            }
            if (!checkTabs(pe.getTabs())) {
                return fail("存在非法表名");
            }
            if (!DownloadService.decryptPwd(pe)) {
                return fail("密码解密失败", null);
            }
            if (StringHelper.isPwdInvalid(pe.getPwd())) {
                return fail("密码不符合要求");
            }
            Map<String, List<PipelineEntity>> map = new HashMap<>(4);
            for (String tab : pe.getTabs()) {
                List<PipelineEntity> rs = pipelineService.selectPipeAnalysis(tab, pe.getGid());
                if (null != rs && rs.size() > 0) {
                    map.put(tab, rs);
                }
            }
            if (map.size() == 0) {
                return fail("查无数据");
            }
            UserEntity ue = tokenService.getCurrentUser(req);
            String guid = pipelineService.createZipFile(ue, map, pe.getPwd());
            return success(guid);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
        }
    }
    @SysLog()
    @ApiOperation(value = "分页查询下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query", example = ""),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "pageIndex", value = "分页数(从1开始)", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectPageCountForDownload")
    public ResponseMsg<List<DownloadEntity>> selectPageCountForDownload(String name, Integer pageSize, Integer pageIndex, HttpServletRequest req) {
        try {
            if (pageSize < 1 || pageIndex < 1) {
                return fail("每页页数或分页数小于1", null);
            }
            UserEntity ue = tokenService.getCurrentUser(req);
            if (ue == null) {
                return fail("用户未登录", null);
            }
            int count = downloadService.selectCountForUser(ue.getCreateUser(), 5, name);
            if (count == 0) {
                return success(0, null);
            }
            List<DownloadEntity> rs = downloadService.selectByPageForUser(ue.getCreateUser(), 3, name, pageSize, pageSize * (pageIndex - 1));
            return success(count, rs);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
        }
    }
    @SysLog()
    @ApiOperation(value = "查询下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guid", value = "文件GUID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", dataType = "String", paramType = "query")
    })
    @GetMapping(value = "/selectDownloadFile")
    public ResponseMsg<Boolean> selectDownloadFile(String guid, String pwd) {
        try {
            if (StringHelper.isEmpty(guid) || StringHelper.isEmpty(pwd)) {
                return fail("文件ID和密码不能为空", null);
            }
            if (!pwd.endsWith(StaticData.EQ)) {
                pwd = URLDecoder.decode(pwd, StandardCharsets.UTF_8.name());
            }
            String password = DownloadService.decryptPwd(pwd);
            if (null == password) {
                return fail("密码解密失败", null);
            }
            DownloadEntity de = downloadService.selectByGuid(guid);
            if (null == de) {
                return fail("文件不存在", null);
            }
            if (!StringHelper.isNull(de.getPwd()) && !Md5Helper.validatePassword(password, de.getPwd())) {
                return fail("密码不正确", null);
            }
            String filePath = downloadService.getDownloadFilePath(de);
            File file = new File(filePath);
            return success(file.exists());
        } catch (Exception ex) {
            return fail(ex.getMessage(), false);
        }
    }
    @SysLog()
    @ApiOperation(value = "下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guid", value = "文件GUID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", dataType = "String", paramType = "query")
    })
    @ResponseBody
    @GetMapping(value = "/downloadFile")
    public void downloadFile(String guid, String pwd, HttpServletRequest req, HttpServletResponse res) {
        try {
            if (StringHelper.isEmpty(guid) || StringHelper.isEmpty(pwd)) {
                WebHelper.writeInfo(HttpStatus.BAD_REQUEST, "文件ID和密码不能为空", res);
            }
            if (!pwd.endsWith(StaticData.EQ)) {
                pwd = URLDecoder.decode(pwd, StandardCharsets.UTF_8.name());
            }
            String password = DownloadService.decryptPwd(pwd);
            if (null == password) {
                WebHelper.writeInfo(HttpStatus.BAD_REQUEST, "密码解密失败", res);
            }
            DownloadEntity de = downloadService.selectByGuid(guid);
            if (null == de) {
                WebHelper.writeInfo(HttpStatus.NOT_FOUND, "文件不存在", res);
                return;
            }
            if (!StringHelper.isNull(de.getPwd()) && !Md5Helper.validatePassword(password, de.getPwd())) {
                WebHelper.writeInfo(HttpStatus.UNAUTHORIZED, "密码不正确", res);
            }
            UserEntity ue = tokenService.getCurrentUser(req);
            downlogService.updateInfos(ue, de, req);
            String filePath = downloadService.getDownloadFilePath(de);
            WebHelper.download(filePath, de.getName(), res);
        } catch (Exception ex) {
            WebHelper.writeInfo(HttpStatus.ERROR, ex.getMessage(), res);
        }
    }
    /**
     * æ£€æŸ¥è¡¨å
     */
    private boolean checkTabs(List<String> tabs) {
        for (String tab : tabs) {
            if (!StaticData.PIPE_ANALYSIS_TABS.contains(tab)) {
                return false;
            }
        }
        return true;
    }
}