package com.terra.system.controller.data; import com.terra.system.annotation.SysLog; import com.terra.common.controller.all.BaseController; import com.terra.common.entity.all.ResponseMsg; import com.terra.common.entity.all.StaticData; import com.terra.system.entity.ctrl.PubEntity; import com.terra.system.entity.data.MetaEntity; import com.terra.system.entity.data.PublishEntity; import com.terra.system.entity.sys.UserEntity; import com.terra.system.helper.PathHelper; import com.terra.system.helper.StringHelper; import com.terra.system.helper.WebHelper; import com.terra.system.service.data.MetaService; import com.terra.system.service.data.PublishService; import com.terra.system.service.data.RasterService; import com.terra.system.service.sys.LayerService; 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 java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * 数据发布 * @author WWW */ @Tag(name = "数据管理\\发布管理") @RestController @RequestMapping("/publish") public class PublishController extends BaseController { @Resource PublishService publishService; @Resource TokenService tokenService; @Resource LayerService layerService; @Resource MetaService metaService; @Resource RasterService rasterService; @Resource PathHelper pathHelper; @Operation(summary = "测试查询") @Parameters({ @Parameter(name = "seconds", description = "秒", in = ParameterIn.QUERY, example = "30") }) @GetMapping(value = "/selectForTest") public ResponseMsg selectForTest(Integer seconds) { try { if (null == seconds || seconds < 1) { seconds = 1; } String str = publishService.selectForTest(seconds); return success(str); } catch (Exception ex) { return fail(ex.getMessage(), null); } } @SysLog() @Operation(summary = "分页查询元数据") @Parameters({ @Parameter(name = "depcode", description = "单位编码", in = ParameterIn.QUERY, example = "00"), @Parameter(name = "dircode", description = "目录编码", in = ParameterIn.QUERY, example = "01"), @Parameter(name = "verid", description = "版本ID", in = ParameterIn.QUERY, example = "0"), @Parameter(name = "type", description = "类别", in = ParameterIn.QUERY, example = "DOM"), @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 = "/selectMetasByPage") public ResponseMsg selectMetasByPage(String depcode, String dircode, Integer verid, String type, String name, Integer pageSize, Integer pageIndex) { try { if (pageSize < 1 || pageIndex < 1) { return fail("每页页数或分页数小于1", null); } if (StringHelper.isEmpty(type)) { return fail("数据类别为空", null); } String types = getType(dircode, type); int count = publishService.selectMetasByCount(depcode, dircode, verid, types, name); if (count == 0) { return success(0, null); } List rs = publishService.selectMetasByPage(depcode, dircode, verid, types, name, pageSize, pageSize * (pageIndex - 1)); return success(count, rs); } catch (Exception ex) { return fail(ex.getMessage(), null); } } /** * 获取类型 */ private String getType(String dircode, String type) throws Exception { switch (type) { case "DOM": return "type in ('tif', 'tiff', 'img')" + getFilter(dircode, type); case "DEM": return "type in ('tif', 'tiff')" + getFilter(dircode, type); case "MPT": return "type = 'mpt'"; case "3DML": return "type = '3dml'"; case "CPT": return "type = 'cpt'"; case "BIM": return "type in ('ifc', 'fbx', 'rvt')"; case "LAS": return "type in ('las', 'laz')"; case "OSGB": return "type = 'osgb'"; default: throw new Exception("数据类型不匹配"); } } /** * 获取过滤条件 */ private String getFilter(String dircode, String type) { dircode = StringHelper.isEmpty(dircode) ? "" : StringHelper.getRightLike(dircode); List list = null; switch (type) { case "DOM": list = publishService.selectCodesForDir(dircode, 0); break; case "DEM": list = publishService.selectCodesForDir(dircode, 1); break; default: break; } if (null == list || list.isEmpty()) { return ""; } for (int i = 0, c = list.size(); i < c; i++) { list.set(i, "'" + list.get(i) + "'"); } return " and dircode not in (" + StringHelper.join(list, ",") + ")"; } @SysLog() @Operation(summary = "分页查询并返回记录数") @Parameters({ @Parameter(name = "name", description = "名称", in = ParameterIn.QUERY, example = ""), @Parameter(name = "dircode", description = "目录", in = ParameterIn.QUERY, example = ""), @Parameter(name = "type", 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 name, String dircode, String type, Integer pageSize, Integer pageIndex) { try { if (pageSize < 1 || pageIndex < 1) { return fail("每页页数或分页数小于1", null); } type = getPubType(type); int count = publishService.selectCount(name, dircode, type); if (count == 0) { return success(0, null); } List rs = publishService.selectByPage(name, dircode, type, pageSize, pageSize * (pageIndex - 1)); return success(count, rs); } catch (Exception ex) { return fail(ex.getMessage(), null); } } /** * 获取发布类型 */ private String getPubType(String type) { if (StringHelper.isEmpty(type)) { return null; } switch (type) { case "DOM": return "type = 'DOM'"; case "DEM": return "type = 'DEM'"; case "MPT": return "type = 'mpt'"; case "3DML": return "type = '3dml'"; case "CPT": return "type = 'cpt'"; case "BIM": return "type in ('ifc', 'fbx', 'rvt')"; case "LAS": return "type in ('las', 'laz')"; case "OSGB": return "type = 'osgb'"; default: return 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 { PublishEntity entity = publishService.selectById(id); return success(entity); } catch (Exception ex) { return fail(ex.getMessage(), null); } } @SysLog() @Operation(summary = "查询Las文件坐标系ID") @Parameters({ @Parameter(name = "ids", description = "元数据ID集合", schema = @Schema(type = "array"), in = ParameterIn.QUERY, example = "10483,10481,10456,10285") }) @GetMapping(value = "/selectLasCs") public ResponseMsg selectLasCs(Integer[] ids, HttpServletRequest req) { try { if (null == ids || ids.length == 0) { return fail("元数据ID集合不能为空"); } PubEntity entity = new PubEntity(); entity.setIds(Arrays.asList(ids)); entity.setDircode("00"); entity.setToken(WebHelper.getToken(req)); List list = publishService.selectLasCs(entity, "/Convert/ReadLasCs", req); return success(list); } catch (Exception ex) { return fail(ex.getMessage(), null); } } @SysLog() @Operation(summary = "查询栅格数据的坐标系ID") @Parameters({ @Parameter(name = "ids", description = "元数据ID集合", schema = @Schema(type = "array"), in = ParameterIn.QUERY, example = "10531,10527,10526") }) @GetMapping(value = "/selectRasterCs") public ResponseMsg selectRasterCs(Integer[] ids, HttpServletRequest req) { try { if (null == ids || ids.length == 0) { return fail("元数据ID集合不能为空"); } List list = metaService.selectByIds(StringHelper.join(Arrays.asList(ids), ",")); if (null == list || list.isEmpty()) { return fail("查无数据"); } String basePath = pathHelper.getConfig().getUploadPath() + File.separator; List errList = new ArrayList<>(); for (MetaEntity me : list) { if (!StaticData.RASTER_EXT.contains("." + me.getType())) { me.setBak("不是栅格数据"); errList.add(me); continue; } String filePath = basePath + me.getPath(); File f = new File(filePath); if (!f.exists() || f.isDirectory()) { me.setBak("文件不存在"); errList.add(me); continue; } // Integer epsg = rasterService.getRaterEpsg(filePath) Integer epsg = rasterService.getEpsgByGdal(filePath); if (null == epsg || epsg < 1) { me.setBak("坐标系ID无效"); errList.add(me); } } return success(errList.size(), errList); } catch (Exception ex) { return fail(ex.getMessage(), null); } } @SysLog() @Operation(summary = "查询坐标转换") @Parameters({ @Parameter(name = "x", description = "X坐标", in = ParameterIn.QUERY, example = "400925.079"), @Parameter(name = "y", description = "Y坐标", in = ParameterIn.QUERY, example = "2541768.173"), @Parameter(name = "epsg", description = "EPSG编码", in = ParameterIn.QUERY, example = "4548") }) @GetMapping(value = "/selectCsTransform") public Object selectCsTransform(double x, double y, int epsg) { try { return rasterService.transformByProj4j(x, y, epsg); } catch (Exception ex) { return null; } } @SysLog() @Operation(summary = "插入发布数据") @Parameters({ @Parameter(name = "entity", description = "实体类") }) @PostMapping(value = "/insertForPub", produces = "application/json; charset=UTF-8") public ResponseMsg insertForPub(@RequestBody PubEntity entity, HttpServletRequest req) { try { if (null == entity || null == entity.getIds() || entity.getIds().isEmpty()) { return fail("实体类为空或找不到元数据ID", 0); } if (StringHelper.isEmpty(entity.getType())) { return fail("数据类别为空", null); } UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setUserId(ue.getId()); entity.setToken(WebHelper.getToken(req)); } layerService.clearCache(); String method = getConvertMethod(entity.getType()); long count = publishService.postForPub(entity, method, req); return success(count); } catch (Exception ex) { return fail(ex.getMessage(), -1); } } /** * 获取转换方法 */ private String getConvertMethod(String type) throws Exception { switch (type) { case "DOM": return "/Convert/ToTiles"; case "DEM": return "/Convert/ToTerra"; case "MPT": case "3DML": case "CPT": return "/Convert/ToSG"; case "BIM": return "/Convert/ToTileset"; case "LAS": return "/Convert/ToLasByPy"; case "OSGB": return "/Convert/ToOsgb"; default: throw new Exception("数据类型不匹配"); } } @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, HttpServletRequest req) { try { if (ids == null || ids.isEmpty()) { return fail("id数组不能为空", -1); } String strs = StringHelper.join(ids, ","); List list = publishService.selectByIds(strs); if (null == list || list.isEmpty()) { return fail("没有找到要删除的数据", -1); } // publishService.deleteFiles(list) layerService.clearCache(); publishService.deleteFiles(ids, req); int count = publishService.deletes(ids, req); return success(count); } catch (Exception ex) { return fail(ex.getMessage(), -1); } } @SysLog() @Operation(summary = "更新一条") @Parameters({ @Parameter(name = "entity", description = "实体类") }) @ResponseBody @PostMapping(value = "/update", produces = "application/json; charset=UTF-8") public ResponseMsg update(@RequestBody PublishEntity entity, HttpServletRequest req) { try { UserEntity ue = tokenService.getCurrentUser(req); if (ue != null) { entity.setUpdateUser(ue.getId()); } layerService.clearCache(); int count = publishService.update(entity); return success(count); } catch (Exception ex) { return fail(ex.getMessage(), -1); } } }