package com.terra.system.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.terra.system.annotation.SysLog; import com.terra.system.controller.all.BaseController; import com.terra.system.entity.all.HttpStatus; import com.terra.system.entity.all.ResponseMsg; import com.terra.system.entity.all.StaticData; import com.terra.system.entity.ctrl.DownloadReqEntity; import com.terra.system.entity.ctrl.IdNameEntity; import com.terra.system.entity.data.DictEntity; import com.terra.system.entity.data.DownloadEntity; import com.terra.system.entity.data.MetaEntity; import com.terra.system.entity.data.VerEntity; import com.terra.system.entity.sys.UserEntity; import com.terra.system.helper.*; import com.terra.system.mapper.all.BasicMapper; import com.terra.system.service.all.BaseQueryService; import com.terra.system.service.data.DownloadService; import com.terra.system.service.data.MetaService; import com.terra.system.service.data.VerService; import com.terra.system.service.show.CadService; import com.terra.system.service.sys.DepService; import com.terra.system.service.sys.DownlogService; import com.terra.system.service.sys.TokenService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.enums.ParameterIn; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; 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 */ @Tag(name = "数据管理\\信息管理") @RestController @RequestMapping("/meta") public class MetaController extends BaseController { @Resource MetaService metaService; @Resource TokenService tokenService; @Resource BaseQueryService baseQueryService; @Resource DownlogService downlogService; @Resource DownloadService downloadService; @Resource VerService verService; @Resource DepService depService; @Resource CadService cadService; @SysLog() @Operation(summary = "分页查询并返回记录数") @Parameters({ @Parameter(name = "depcode", description = "单位编码", in = ParameterIn.QUERY, example = "00"), @Parameter(name = "dircode", description = "目录编码", in = ParameterIn.QUERY, example = "00"), @Parameter(name = "verid", description = "版本ID", in = ParameterIn.QUERY, example = "0"), @Parameter(name = "name", description = "名称", in = ParameterIn.QUERY, example = ""), @Parameter(name = "pageSize", description = "每页条数", in = ParameterIn.QUERY, example = "10"), @Parameter(name = "pageIndex", description = "分页数(从1开始)", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectByPageAndCount") public ResponseMsg> 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(depcode, dircode, verid, name); if (count == 0) { return success(0, null); } List rs = metaService.selectByPage(depcode, dircode, verid, name, pageSize, pageSize * (pageIndex - 1)); return success(count, rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "根据GUID查询GDB") @Parameters({ @Parameter(name = "guid", description = "GUID", in = ParameterIn.QUERY, example = "d58e66d9606a4b85e7c8ab43b3db0b55") }) @GetMapping(value = "/selectGdbByGuid") public ResponseMsg selectGdbByGuid(String guid) { try { if (StringHelper.isEmpty(guid)) { return fail("文件GUID值不能为空", null); } List rs = metaService.selectGdbByGuid(guid); return success(null == rs ? 0 : rs.size(), rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "根据父ID分页查询并返回记录数") @Parameters({ @Parameter(name = "metaid", description = "父ID", in = ParameterIn.QUERY, example = "0"), @Parameter(name = "name", description = "名称", in = ParameterIn.QUERY, example = ""), @Parameter(name = "pageSize", description = "每页条数", in = ParameterIn.QUERY, example = "10"), @Parameter(name = "pageIndex", description = "分页数(从1开始)", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectPageAndCountByPid") public ResponseMsg> 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 rs = metaService.selectPageByPid(metaid, name, pageSize, pageSize * (pageIndex - 1)); return success(count, rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "根据目录ID查询版本列表") @Parameters({ @Parameter(name = "dirid", description = "目录ID", in = ParameterIn.QUERY) }) @GetMapping(value = "/selectVerByDirid") public ResponseMsg> selectVerByDirid(Integer dirid) { try { if (null == dirid || dirid < 0) { dirid = 0; } List list = verService.selectByDirid(dirid); if (null == list || list.isEmpty()) { list = verService.selectByDirid(0); } return success(list); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "根据ID查询") @Parameters({ @Parameter(name = "id", description = "ID", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectById") public ResponseMsg selectById(int id) { try { MetaEntity entity = metaService.selectById(id); return success(entity); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "查询表中数据") @Parameters({ @Parameter(name = "id", description = "元数据ID", in = ParameterIn.QUERY, example = "115"), @Parameter(name = "pageIndex", description = "分页数(从1开始)", in = ParameterIn.QUERY, example = "1"), @Parameter(name = "pageSize", description = "每页条数", in = ParameterIn.QUERY, example = "10") }) @GetMapping(value = "/selectDbData") public ResponseMsg 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 page = new Page<>(pageIndex, pageSize); page.addOrder(OrderItem.desc("gid")); IPage paged = baseMapper.selectPage(page, wrapper); return success(paged.getTotal(), paged.getRecords()); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "查询字段信息") @Parameters({ @Parameter(name = "ns", description = "名称空间", in = ParameterIn.QUERY, example = "bd"), @Parameter(name = "tab", description = "表名", in = ParameterIn.QUERY, example = "dlg25wAanp") }) @GetMapping(value = "/selectTabFields") public ResponseMsg> selectTabFields(String ns, String tab) { try { if (StringHelper.isEmpty(ns) || StringHelper.isEmpty(tab)) { return fail("名称空间和表名不能为空", null); } List list = baseQueryService.selectFields(ns, tab); return success(list); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "查询Dwg转换") @Parameters({ @Parameter(name = "id", description = "元数据ID", in = ParameterIn.QUERY, example = "7715"), @Parameter(name = "isAttach", description = "是否为附件", in = ParameterIn.QUERY, example = "false") }) @GetMapping(value = "/selectConvertToDwg") public ResponseMsg selectConvertToDwg(int id, Boolean isAttach) { try { String rs = cadService.convert(id, isAttach); return success(rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "插入一条") @Parameters({ @Parameter(name = "entity", description = "实体类") }) @PostMapping(value = "/insert", produces = "application/json; charset=UTF-8") public ResponseMsg insert(@RequestBody MetaEntity entity, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setCreateUser(ue.getId()); } entity.setCreateTime(WebHelper.getCurrentTimestamp()); int count = metaService.insert(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "插入多条") @Parameters({ @Parameter(name = "list", description = "实体类集合") }) @PostMapping(value = "/inserts", produces = "application/json; charset=UTF-8") public ResponseMsg inserts(@RequestBody List list, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { for (MetaEntity entity : list) { entity.setCreateUser(ue.getId()); } } int count = metaService.inserts(list); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "删除多条") @Parameters({ @Parameter(name = "ids", description = "ID数组", in = ParameterIn.QUERY, schema = @Schema(type = "array"), example = "1") }) @GetMapping(value = "/deletes") public ResponseMsg deletes(@RequestParam List ids) { try { if (ids == null || ids.isEmpty()) { return fail("id数组不能为空", -1); } int count = metaService.deletes(ids); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "更新一条") @Parameters({ @Parameter(name = "entity", description = "实体类") }) @ResponseBody @PostMapping(value = "/update", produces = "application/json; charset=UTF-8") public ResponseMsg update(@RequestBody MetaEntity entity, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setUpdateUser(ue.getId()); } int count = metaService.update(entity); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "更新多条") @Parameters({ @Parameter(name = "list", description = "实体类集合") }) @ResponseBody @PostMapping(value = "/updates", produces = "application/json; charset=UTF-8") public ResponseMsg updates(@RequestBody List list, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { for (MetaEntity entity : list) { entity.setUpdateUser(ue.getId()); } } int count = metaService.updates(list); return success(count); } catch (Exception ex) { return fail(ex, -1); } } @SysLog() @Operation(summary = "分页查询下载文件") @Parameters({ @Parameter(name = "name", description = "名称", in = ParameterIn.QUERY, example = ""), @Parameter(name = "pageSize", description = "每页条数", in = ParameterIn.QUERY, example = "10"), @Parameter(name = "pageIndex", description = "分页数(从1开始)", in = ParameterIn.QUERY, example = "1") }) @GetMapping(value = "/selectPageCountForDownload") public ResponseMsg> 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.getId(), "3,4", name); if (count == 0) { return success(0, null); } List rs = downloadService.selectByPageForUser(ue.getId(), "3,4", name, pageSize, pageSize * (pageIndex - 1)); return success(count, rs); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "查询元数据中溢出的单位ID") @Parameters({ @Parameter(name = "reqEntity", description = "请求下载实体") }) @ResponseBody @PostMapping(value = "/selectMetaOverflowDep") public ResponseMsg 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()); } List list = metaService.selectMetaOverflowDep(ue, dr); return success(list); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "根据单位编码数组查询单位") @Parameters({ @Parameter(name = "codes", description = "单位编码数组", in = ParameterIn.QUERY, schema = @Schema(type = "array"), example = "00,0001") }) @GetMapping(value = "/selectDepsByCodes") public ResponseMsg selectDepsByCodes(String[] codes) { try { if (null == codes || codes.length == 0) { return fail("单位ID集合为空"); } List list = depService.selectDepsByCodes(codes); return success(list); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "请求元数据下载") @Parameters({ @Parameter(name = "dr", description = "请求下载实体类") }) @ResponseBody @PostMapping(value = "/downloadReq") public ResponseMsg downloadReq(@RequestBody DownloadReqEntity dr, HttpServletRequest req, HttpServletResponse res) { try { if (null == dr || StringHelper.isEmpty(dr.getPwd())) { return fail("密码不能为空"); } if (null == dr.getIds() || dr.getIds().isEmpty()) { return fail("请选择要下载的文件ID"); } 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(guid); } catch (Exception ex) { return fail(ex, null); } } @SysLog() @Operation(summary = "查询下载文件") @Parameters({ @Parameter(name = "guid", description = "文件GUID", in = ParameterIn.QUERY), @Parameter(name = "pwd", description = "密码", in = ParameterIn.QUERY) }) @GetMapping(value = "/selectDownloadFile") public ResponseMsg 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() @Operation(summary = "下载文件") @Parameters({ @Parameter(name = "guid", description = "文件GUID", in = ParameterIn.QUERY), @Parameter(name = "pwd", description = "密码", in = ParameterIn.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); } } @SysLog() @Operation(summary = "查看文件") @Parameters({ @Parameter(name = "guid", description = "附件Guid") }) @GetMapping(value = "/downloadForView") public void downloadForView(String guid, HttpServletResponse res) { metaService.downloadForView(guid, true, res); } }