管道基础大数据平台系统开发-【后端】-Server
13693261870
2024-04-22 cc816055746ae0ffa870734e5a411b2a2aabb96a
src/main/java/com/lf/server/controller/data/MetaController.java
@@ -1,83 +1,176 @@
package com.lf.server.controller.data;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.ctrl.DownloadReqEntity;
import com.lf.server.entity.ctrl.IdNameEntity;
import com.lf.server.entity.data.DictEntity;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.data.VerEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.Md5Helper;
import com.lf.server.helper.RsaHelper;
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.WebHelper;
import com.lf.server.helper.*;
import com.lf.server.mapper.all.BasicMapper;
import com.lf.server.service.all.BaseQueryService;
import com.lf.server.service.data.DownloadService;
import com.lf.server.service.data.MetaService;
import com.lf.server.service.data.VerService;
import com.lf.server.service.show.CadService;
import com.lf.server.service.sys.DepService;
import com.lf.server.service.sys.DownlogService;
import com.lf.server.service.sys.TokenService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
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.ArrayList;
import java.util.List;
/**
 * 元数据
 * @author WWW
 */
@Api(tags = "数据管理\\元数据")
@Api(tags = "数据管理\\信息管理")
@RestController
@RequestMapping("/meta")
public class MetaController extends BaseController {
    @Autowired
    @Resource
    MetaService metaService;
    @Autowired
    @Resource
    TokenService tokenService;
    @Autowired
    @Resource
    BaseQueryService baseQueryService;
    @Resource
    DownlogService downlogService;
    @Resource
    DownloadService downloadService;
    @Resource
    VerService verService;
    @Resource
    DepService depService;
    @Resource
    CadService cadService;
    @SysLog()
    @ApiOperation(value = "分页查询并返回记录数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dirid", value = "目录ID", dataType = "Integer", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "depcode", value = "单位编码", dataType = "String", paramType = "query", example = "00"),
            @ApiImplicitParam(name = "dircode", value = "目录编码", dataType = "String", paramType = "query", example = "00"),
            @ApiImplicitParam(name = "verid", value = "版本ID", dataType = "Integer", paramType = "query", example = "0"),
            @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 = "/selectByPageAndCount")
    public ResponseMsg<List<MetaEntity>> selectByPageAndCount(Integer dirid, String name, Integer pageSize, Integer pageIndex) {
    public ResponseMsg<List<MetaEntity>> selectByPageAndCount(String depcode, String dircode, Integer verid, String name, Integer pageSize, Integer pageIndex) {
        try {
            if (pageSize < 1 || pageIndex < 1) {
                return fail("每页页数或分页数小于1", null);
            }
            int count = metaService.selectCount(dirid, name);
            int count = metaService.selectCount(depcode, dircode, verid, name);
            if (count == 0) {
                return success(0, null);
            }
            List<MetaEntity> rs = metaService.selectByPage(dirid, name, pageSize, pageSize * (pageIndex - 1));
            List<MetaEntity> rs = metaService.selectByPage(depcode, dircode, verid, name, pageSize, pageSize * (pageIndex - 1));
            return success(count, rs);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "查询所有")
    @GetMapping(value = "/selectAll")
    public ResponseMsg<List<MetaEntity>> selectAll() {
    @ApiOperation(value = "根据GUID查询GDB")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guid", value = "GUID", dataType = "String", paramType = "query", example = "d58e66d9606a4b85e7c8ab43b3db0b55")
    })
    @GetMapping(value = "/selectGdbByGuid")
    public ResponseMsg<Object> selectGdbByGuid(String guid) {
        try {
            List<MetaEntity> list = metaService.selectAll();
            if (StringHelper.isEmpty(guid)) {
                return fail("文件GUID值不能为空", null);
            }
            List<MetaEntity> rs = metaService.selectGdbByGuid(guid);
            return success(null == rs ? 0 : rs.size(), rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "根据父ID分页查询并返回记录数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "metaid", value = "父ID", dataType = "String", paramType = "query", example = "0"),
            @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 = "/selectPageAndCountByPid")
    public ResponseMsg<List<MetaEntity>> selectPageAndCountByPid(Integer metaid, String name, Integer pageSize, Integer pageIndex) {
        try {
            if (pageSize < 1 || pageIndex < 1) {
                return fail("每页页数或分页数小于1", null);
            }
            if (null == metaid || metaid < 1) {
                return fail("父ID不能为空且大于1", null);
            }
            int count = metaService.selectCountByPid(metaid, name);
            if (count == 0) {
                return success(0, null);
            }
            List<MetaEntity> rs = metaService.selectPageByPid(metaid, name, pageSize, pageSize * (pageIndex - 1));
            return success(count, rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "根据目录ID查询版本列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dirid", value = "目录ID", dataType = "Integer", paramType = "query")
    })
    @GetMapping(value = "/selectVerByDirid")
    public ResponseMsg<List<VerEntity>> selectVerByDirid(Integer dirid) {
        try {
            if (null == dirid || dirid < 0) {
                dirid = 0;
            }
            List<VerEntity> list = verService.selectByDirid(dirid);
            if (null == list || list.isEmpty()) {
                list = verService.selectByDirid(0);
            }
            return success(list);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
            return fail(ex, null);
        }
    }
@@ -93,7 +186,84 @@
            return success(entity);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "查询表中数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "元数据ID", dataType = "Integer", paramType = "query", example = "115"),
            @ApiImplicitParam(name = "pageIndex", value = "分页数(从1开始)", dataType = "Integer", paramType = "query", example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10")
    })
    @GetMapping(value = "/selectDbData")
    public ResponseMsg<Object> selectDbData(Integer id, Integer pageIndex, Integer pageSize) {
        // noinspection AlibabaRemoveCommentedCode
        try {
            if (null == id || id < 0) {
                return fail("元数据ID不能为空或小于0", null);
            }
            MetaEntity meta = metaService.selectById(id);
            if (null == meta || null == meta.getTab() || !meta.getTab().contains(StaticData.POINT)) {
                return fail("找不到元数据信息", null);
            }
            String entity = meta.getTab().substring(meta.getTab().indexOf(".") + 1).replace("_", "").toLowerCase();
            BasicMapper baseMapper = ClassHelper.getBasicMapper(entity);
            if (null == baseMapper) {
                return null;
            }
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("parentid", meta.getEventid());
            Page<Object> page = new Page<>(pageIndex, pageSize);
            page.addOrder(OrderItem.desc("gid"));
            IPage<Object> paged = baseMapper.selectPage(page, wrapper);
            return success(paged.getTotal(), paged.getRecords());
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "查询字段信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ns", value = "名称空间", dataType = "String", paramType = "query", example = "bd"),
            @ApiImplicitParam(name = "tab", value = "表名", dataType = "String", paramType = "query", example = "dlg25wAanp")
    })
    @GetMapping(value = "/selectTabFields")
    public ResponseMsg<List<DictEntity>> selectTabFields(String ns, String tab) {
        try {
            if (StringHelper.isEmpty(ns) || StringHelper.isEmpty(tab)) {
                return fail("名称空间和表名不能为空", null);
            }
            List<DictEntity> list = baseQueryService.selectFields(ns, tab);
            return success(list);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "查询Dwg转换")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "元数据ID", dataType = "int", paramType = "query", example = "7715"),
            @ApiImplicitParam(name = "isAttach", value = "是否为附件", dataType = "Boolean", paramType = "query", example = "false")
    })
    @GetMapping(value = "/selectConvertToDwg")
    public ResponseMsg<Object> selectConvertToDwg(int id, Boolean isAttach) {
        try {
            String rs = cadService.convert(id, isAttach);
            return success(rs);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
@@ -115,7 +285,7 @@
            return success(count);
        } catch (Exception ex) {
            return fail(ex.getMessage(), -1);
            return fail(ex, -1);
        }
    }
@@ -138,30 +308,14 @@
            return success(count);
        } catch (Exception ex) {
            return fail(ex.getMessage(), -1);
        }
    }
    @SysLog()
    @ApiOperation(value = "删除一条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/delete")
    public ResponseMsg<Integer> delete(int id) {
        try {
            int count = metaService.delete(id);
            return success(count);
        } catch (Exception ex) {
            return fail(ex.getMessage(), -1);
            return fail(ex, -1);
        }
    }
    @SysLog()
    @ApiOperation(value = "删除多条")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "ID数组", dataType = "Integer", paramType = "query", example = "1,2")
            @ApiImplicitParam(name = "ids", value = "ID数组", dataType = "Integer", paramType = "query", allowMultiple = true, example = "1")
    })
    @GetMapping(value = "/deletes")
    public ResponseMsg<Integer> deletes(@RequestParam List<Integer> ids) {
@@ -174,7 +328,7 @@
            return success(count);
        } catch (Exception ex) {
            return fail(ex.getMessage(), -1);
            return fail(ex, -1);
        }
    }
@@ -196,7 +350,7 @@
            return success(count);
        } catch (Exception ex) {
            return fail(ex.getMessage(), -1);
            return fail(ex, -1);
        }
    }
@@ -220,12 +374,12 @@
            return success(count);
        } catch (Exception ex) {
            return fail(ex.getMessage(), -1);
            return fail(ex, -1);
        }
    }
    @SysLog()
    @ApiOperation(value = "分页查询下载记录并返回记录数")
    @ApiOperation(value = "分页查询下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query", example = ""),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10"),
@@ -243,75 +397,166 @@
                return fail("用户未登录", null);
            }
            int count = downloadService.selectCountForUser(ue.getCreateUser(), 3, name);
            int count = downloadService.selectCountForUser(ue.getId(), "3,4", name);
            if (count == 0) {
                return success(0, null);
            }
            List<DownloadEntity> rs = downloadService.selectByPageForUser(ue.getCreateUser(), 3, name, pageSize, pageSize * (pageIndex - 1));
            List<DownloadEntity> rs = downloadService.selectByPageForUser(ue.getId(), "3,4", name, pageSize, pageSize * (pageIndex - 1));
            return success(count, rs);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "请求下载")
    @ApiOperation(value = "查询元数据中溢出的单位ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pwd", value = "密码", dataType = "String", paramType = "body", example = ""),
            @ApiImplicitParam(name = "ids", value = "ID数组", dataType = "Integer", paramType = "body", example = "1,2")
            @ApiImplicitParam(name = "reqEntity", value = "请求下载实体", dataType = "DownloadReqEntity", paramType = "body")
    })
    @ResponseBody
    @PostMapping(value = "/selectMetaOverflowDep")
    public ResponseMsg<Object> selectMetaOverflowDep(@RequestBody DownloadReqEntity dr, HttpServletRequest req, HttpServletResponse res) {
        try {
            if (null == dr || null == dr.getIds() || dr.getIds().isEmpty()) {
                return fail("请选择要下载的文件ID");
            }
            UserEntity ue = tokenService.getCurrentUser(req);
            if (StaticData.ADMIN.equals(ue.getUid())) {
                return success(new ArrayList<String>());
            }
            List<String> list = metaService.selectMetaOverflowDep(ue, dr);
            return success(list);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "根据单位编码数组查询单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codes", value = "单位编码数组", dataType = "String", paramType = "query", allowMultiple = true, example = "00,0001")
    })
    @GetMapping(value = "/selectDepsByCodes")
    public ResponseMsg<Object> selectDepsByCodes(String[] codes) {
        try {
            if (null == codes || codes.length == 0) {
                return fail("单位ID集合为空");
            }
            List<IdNameEntity> list = depService.selectDepsByCodes(codes);
            return success(list);
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
    @SysLog()
    @ApiOperation(value = "请求元数据下载")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dr", value = "请求下载实体类", dataType = "DownloadReqEntity", paramType = "body")
    })
    @ResponseBody
    @PostMapping(value = "/downloadReq")
    public ResponseMsg<Object> downloadReq(@RequestBody DownloadReqEntity reqEntity, HttpServletRequest req, HttpServletResponse res) {
    public ResponseMsg<Object> downloadReq(@RequestBody DownloadReqEntity dr, HttpServletRequest req, HttpServletResponse res) {
        try {
            if (null == reqEntity || StringHelper.isEmpty(reqEntity.getPwd())) {
            if (null == dr || StringHelper.isEmpty(dr.getPwd())) {
                return fail("密码不能为空");
            }
            if (null == reqEntity.getIds() || reqEntity.getIds().isEmpty()) {
                return fail("请选择要下载的文件");
            if (null == dr.getIds() || dr.getIds().isEmpty()) {
                return fail("请选择要下载的文件ID");
            }
            if (!downloadService.validatePwd(reqEntity)) {
            if (!DownloadService.decryptPwd(dr)) {
                return fail("密码解密失败", null);
            }
            if (StringHelper.isPwdInvalid(dr.getPwd())) {
                return fail("密码不符合要求");
            }
            UserEntity ue = tokenService.getCurrentUser(req);
            String guid = metaService.downloadMeteReq(ue, dr);
            return success(true);
            return success(guid);
        } catch (Exception ex) {
            return fail(ex.getMessage(), null);
            return fail(ex, 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, false);
        }
    }
    @SysLog()
    @ApiOperation(value = "下载文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guid", value = "文件GUID", dataType = "String", paramType = "query")
            @ApiImplicitParam(name = "guid", value = "文件GUID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pwd", value = "密码", dataType = "String", paramType = "query")
    })
    @ResponseBody
    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    public void downloadFile(@RequestBody DownloadReqEntity reqEntity, HttpServletRequest req, HttpServletResponse res) {
    @GetMapping(value = "/downloadFile")
    public void downloadFile(String guid, String pwd, HttpServletRequest req, HttpServletResponse res) {
        try {
            if (null == reqEntity || StringHelper.isEmpty(reqEntity.getPwd())) {
                WebHelper.writeInfo(HttpStatus.BAD_REQUEST, "密码不能为空", res);
            if (StringHelper.isEmpty(guid) || StringHelper.isEmpty(pwd)) {
                WebHelper.writeInfo(HttpStatus.BAD_REQUEST, "文件ID和密码不能为空", res);
            }
            if (StringHelper.isEmpty(reqEntity.getGuid())) {
                WebHelper.writeInfo(HttpStatus.BAD_REQUEST, "找不到文件ID", res);
                return;
            if (!pwd.endsWith(StaticData.EQ)) {
                pwd = URLDecoder.decode(pwd, StandardCharsets.UTF_8.name());
            }
            DownloadEntity de = downloadService.selectByGuid(reqEntity.getGuid());
            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(reqEntity.getPwd(), de.getPwd())) {
            if (!StringHelper.isNull(de.getPwd()) && !Md5Helper.validatePassword(password, de.getPwd())) {
                WebHelper.writeInfo(HttpStatus.UNAUTHORIZED, "密码不正确", res);
            }
            UserEntity ue = tokenService.getCurrentUser(req);
            de.setDcount(de.getDcount() + 1);
            de.setDownloadUser(ue.getId());
            int rows = downloadService.update(de);
            downlogService.updateInfos(ue, de, req);
            String filePath = downloadService.getDownloadFilePath(de);
            WebHelper.download(filePath, de.getName(), res);
@@ -319,4 +564,14 @@
            WebHelper.writeInfo(HttpStatus.ERROR, ex.getMessage(), res);
        }
    }
    @SysLog()
    @ApiOperation(value = "查看文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "guid", value = "附件Guid", dataType = "String", paramType = "body")
    })
    @GetMapping(value = "/downloadForView")
    public void downloadForView(String guid, HttpServletResponse res) {
        metaService.downloadForView(guid, true, res);
    }
}