package org.jeecg.modules.demo.hzz.shjsgc.dfgc.controller; import java.io.File; import java.text.DateFormat; import java.util.*; import java.util.stream.Collectors; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.alibaba.fastjson.JSONObject; import io.netty.util.internal.StringUtil; import org.geotools.data.DataStore; import org.jeecg.common.api.vo.Result; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.util.oConvertUtils; import org.jeecg.modules.demo.hzz.shjsgc.dfgc.entity.RmDfgc; import org.jeecg.modules.demo.hzz.shjsgc.dfgc.geo.service.IRmDfgcgeoService; import org.jeecg.modules.demo.hzz.shjsgc.dfgc.service.IRmDfgcService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import lombok.extern.slf4j.Slf4j; import org.jeecg.modules.demo.onemap.utils.Geotools; import org.jeecg.modules.demo.onemap.utils.PGDatastore; import org.jeecg.modules.demo.onemap.utils.Utility; import org.jeecg.modules.demo.onemap.utils.ZipAndRarTools; import org.jeecgframework.poi.excel.ExcelImportUtil; import org.jeecgframework.poi.excel.def.NormalExcelConstants; import org.jeecgframework.poi.excel.entity.ExportParams; import org.jeecgframework.poi.excel.entity.ImportParams; import org.jeecgframework.poi.excel.view.JeecgEntityExcelView; import org.jeecg.common.system.base.controller.JeecgController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.jeecg.common.aspect.annotation.AutoLog; /** * @Description: 堤防工程 * @Author: jeecg-boot * @Date: 2021-11-04 * @Version: V1.0 */ @Api(tags = "堤防工程") @RestController @RequestMapping("/hzz.shjsgc.dfgc/rmDfgc") @Slf4j public class RmDfgcController extends JeecgController { @Autowired private IRmDfgcService rmDfgcService; @Autowired private IRmDfgcgeoService rmDfgcgeoService; private Utility utility = new Utility(); @Value(value = "${jeecg.path.upload}") private String uploadpath; /** * 分页列表查询 * * @param rmDfgc * @param pageNo * @param pageSize * @param req * @return */ @AutoLog(value = "堤防工程-分页列表查询") @ApiOperation(value = "堤防工程-分页列表查询", notes = "堤防工程-分页列表查询") @GetMapping(value = "/list") public Result queryPageList(RmDfgc rmDfgc, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(rmDfgc, req.getParameterMap()); Page page = new Page(pageNo, pageSize); IPage pageList = rmDfgcService.page(page, queryWrapper); return Result.OK(pageList); } /** * 添加 * * @param rmDfgc * @return */ @AutoLog(value = "堤防工程-添加") @ApiOperation(value = "堤防工程-添加", notes = "堤防工程-添加") @PostMapping(value = "/add") public Result add(@RequestBody RmDfgc rmDfgc) { rmDfgcService.save(rmDfgc); /**数据添加至涉河项目表中*/ String id = UUID.randomUUID().toString().replace("-", ""); String dfmc = rmDfgc.getDfmc(); String gcjsqk = rmDfgc.getGcjsqk(); String xmid = rmDfgc.getId(); String dfgcgldwmc = rmDfgc.getDfgcgldwmc(); String szhlmc = rmDfgc.getSzhlmc(); String kgny = null; String jcny = null; if (rmDfgc.getKgny() != null && rmDfgc.getJcny() != null) { kgny = DateFormat.getDateInstance().format(rmDfgc.getKgny()); jcny = DateFormat.getDateInstance().format(rmDfgc.getJcny()); } /**数据矢量信息处理*/ String msg = "矢量数据解析成功!"; if (!StringUtil.isNullOrEmpty(rmDfgc.getGeopath())) { msg = handleGeometry(id, rmDfgc.getGeopath()); rmDfgc.setFlag(1); } String geom = rmDfgcgeoService.getGeojson(id); rmDfgc.setGeoinfo(geom); rmDfgcService.saveOrUpdate(rmDfgc); rmDfgcService.insertshxm(id, dfmc, gcjsqk, xmid, "dfgc", dfgcgldwmc, kgny, jcny, szhlmc, geom); return Result.OK("添加成功!" + msg); } /** * 矢量数据处理 */ private String handleGeometry(String id, String geoPath) { //解压上传的zip包 String warnMsg = "矢量数据解析成功!"; if (StringUtil.isNullOrEmpty(id) || StringUtil.isNullOrEmpty(geoPath)) { warnMsg = "参数不全,无法处理!"; return warnMsg; } String upFilePath = uploadpath + "/" + geoPath; String upFileName = upFilePath.substring(upFilePath.lastIndexOf("/") + 1); String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.')); String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt; try { if (geoPath.endsWith(".zip")) { ZipAndRarTools.unZip(upFilePath, unZipPath); } else if (geoPath.endsWith(".rar")) { ZipAndRarTools.unrar(upFilePath, unZipPath); } else { return "矢量数据格式不正确!"; } //获取解压后的shp文件及坐标系文件的路径 String shpfilepath = ""; String prjFilePath = ""; File file = new File(unZipPath); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { File shpFile = files[i]; String fileName = shpFile.getName(); if (fileName.endsWith(".shp")) { shpfilepath = unZipPath + "/" + fileName; continue; } if (fileName.endsWith(".prj")) { prjFilePath = unZipPath + "/" + fileName; continue; } } //创建数据库连接 PGDatastore pgDatastore = new PGDatastore(); DataStore datastore = pgDatastore.getDefeaultDatastore(); Geotools geotools = new Geotools(datastore); //根据坐标系文件,判断上传的矢量是否需要转换坐标 int prjType = 4490; if (StringUtil.isNullOrEmpty(prjFilePath)) { warnMsg = "上传的SHP文件没有坐标信息(*.prj),默认按照2000大地坐标处理!"; } else { String prjWkt = geotools.getShpPrjWkt(prjFilePath); if (!prjWkt.contains("CGCS2000")) { warnMsg = "上传的SHP文件坐标系不是2000国家大地坐标系,无法处理!"; return warnMsg; } if (prjWkt.startsWith("PROJCS")) { if (prjWkt.contains("Zone_39")) { prjType = 4527; } else if (prjWkt.contains("CM_117E")) { prjType = 4548; } else { prjType = 4490; } } } //将shp文件解析为wkt,然后根据坐标系情况,转为4490,然后更新geom String shpWkt = geotools.shp2wkt(shpfilepath); if (!StringUtil.isNullOrEmpty(shpWkt)) { if (shpWkt.startsWith("LineString")) { shpWkt = shpWkt.replace("LineString", "MULTILINESTRING"); shpWkt = shpWkt.replace("(", "(("); shpWkt = shpWkt.replace(")", "))"); } switch (prjType) { case 4490: { rmDfgcgeoService.updDfgc3(shpWkt, id); break; } case 4527: { rmDfgcgeoService.updDfgc2(shpWkt, id); break; } case 4548: { rmDfgcgeoService.updDfgc(shpWkt, id); break; } default: break; } } else { warnMsg = "矢量数据解析异常:上传的SHP文件没有解析到空间图形信息!"; return warnMsg; } } catch (IOException e) { e.printStackTrace(); warnMsg = e.getMessage(); return "矢量数据解析异常:" + warnMsg; } catch (Exception e) { e.printStackTrace(); warnMsg = e.getMessage(); return "矢量数据解析异常:" + warnMsg; } return warnMsg; } /** * 编辑 * * @param rmDfgc * @return */ @AutoLog(value = "堤防工程-编辑") @ApiOperation(value = "堤防工程-编辑", notes = "堤防工程-编辑") @PostMapping(value = "/edit") public Result edit(@RequestBody RmDfgc rmDfgc) { String dfmc = rmDfgc.getDfmc(); String gcjsqk = rmDfgc.getGcjsqk(); String dfgcgldwmc = rmDfgc.getDfgcgldwmc(); String id = rmDfgc.getId(); String szhlmc = rmDfgc.getSzhlmc(); String geoinfo = rmDfgc.getGeoinfo(); String kgny = null; String jcny = null; if (rmDfgc.getKgny() != null && rmDfgc.getJcny() != null) { kgny = DateFormat.getDateInstance().format(rmDfgc.getKgny()); jcny = DateFormat.getDateInstance().format(rmDfgc.getJcny()); } RmDfgc oldDfgc = rmDfgcService.getById(id); String oldGeoPath = oldDfgc.getGeopath(); String newGeoPath = rmDfgc.getGeopath(); if (oldGeoPath != newGeoPath) { //1、删除原来上传的shp文件 if (!StringUtil.isNullOrEmpty(oldGeoPath)) { delShpPath(oldGeoPath); } else { rmDfgc.setGeoinfo(""); } //2、更新geom和geoinfo if (StringUtil.isNullOrEmpty(newGeoPath)) { rmDfgc.setGeoinfo(""); } rmDfgcService.updateById(rmDfgc); handleGeometry(id, newGeoPath); geoinfo = rmDfgcgeoService.getGeojson(id); rmDfgc.setGeoinfo(geoinfo); rmDfgcService.saveOrUpdate(rmDfgc); } else { rmDfgcService.updateById(rmDfgc); } rmDfgcService.updateshxm(dfmc, gcjsqk, dfgcgldwmc, kgny, jcny, szhlmc, geoinfo, id); return Result.OK("编辑成功!"); } /** * 根据shp上传的文件名,删除压缩文件、解压后的文件夹及文件 */ private void delShpPath(String shpPath) { String upFilePath = uploadpath + "/" + shpPath; if (StringUtil.isNullOrEmpty(upFilePath)) { File zipFile = new File(upFilePath); zipFile.delete(); } String upFileName = upFilePath.substring(upFilePath.lastIndexOf("/") + 1); String fileNameNoExt = upFileName.substring(0, upFileName.lastIndexOf('.')); String unZipPath = uploadpath + "/shpPath/" + fileNameNoExt; File oldGeoFolder = new File(unZipPath); Utility.deleteDir(oldGeoFolder); } /** * 通过id删除 * * @param id * @return */ @AutoLog(value = "堤防工程-通过id删除") @ApiOperation(value = "堤防工程-通过id删除", notes = "堤防工程-通过id删除") @PostMapping(value = "/delete") public Result delete(@RequestParam(name = "id", required = true) String id) { // rmDfgcgeoService.delDfgc(rmDfgcService.getById(id).getId()); rmDfgcService.deshxm(id); RmDfgc rmDfgc = rmDfgcService.getById(id); if (rmDfgc == null) { return Result.error("未找到对应数据,删除失败!"); } String geoPath = rmDfgc.getGeopath(); if (!StringUtil.isNullOrEmpty(geoPath)) { delShpPath(geoPath); } rmDfgcService.removeById(id); rmDfgcService.delete(id); return Result.OK("删除成功!"); } /** * 批量删除 * * @param ids * @return */ @AutoLog(value = "堤防工程-批量删除") @ApiOperation(value = "堤防工程-批量删除", notes = "堤防工程-批量删除") @PostMapping(value = "/deleteBatch") public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { // this.rmDfgcService.removeByIds(Arrays.asList(ids.split(","))); List dfidList = Arrays.asList(ids.split(",")); for (int i = 0; i < dfidList.size(); i++) { delete(dfidList.get(i)); } return Result.OK("批量删除成功!"); } /** * 通过id查询 * * @param id * @return */ @AutoLog(value = "堤防工程-通过id查询") @ApiOperation(value = "堤防工程-通过id查询", notes = "堤防工程-通过id查询") @GetMapping(value = "/queryById") public Result queryById(@RequestParam(name = "id", required = true) String id) { RmDfgc rmDfgc = rmDfgcService.getById(id); if (rmDfgc == null) { return Result.error("未找到对应数据"); } return Result.OK(rmDfgc); } @GetMapping(value = "/queryByIdInMap") public Result queryByIdInMap(RmDfgc rmDfgc, @RequestParam(name = "id",required = true) String id, HttpServletRequest req){ QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(rmDfgc, req.getParameterMap()); queryWrapper.eq("id",id); return Result.OK(rmDfgcService.getOne(queryWrapper)); } /** * 通过id查询Geometry * * @param id * @return */ @AutoLog(value = "堤防工程-通过id查询Geometry") @ApiOperation(value = "堤防工程-通过id查询Geometry", notes = "堤防工程-通过id查询Geometry") @GetMapping(value = "/getGeojsonById") public Result getGeojsonById(@RequestParam(name = "id", required = true) String id) { String geom = rmDfgcgeoService.getGeojson(id); JSONObject geoJson = JSONObject.parseObject(geom); return Result.OK(geoJson); } /** * 生成所有的flag为0的图形,然后地图生成标志flag设置为1 *

* * @return */ @AutoLog(value = "") @ApiOperation(value = "生成堤防工程的图形", notes = "生成堤防工程的图形") @GetMapping(value = "/genarateGeo") public Result genarateGeo() { rmDfgcService.GenarateDfgcGeo(); return Result.OK("生成图形成功!"); } /** * 导出excel * * @param request * @param rmDfgc */ @RequestMapping(value = "/exportXls") public ModelAndView exportXls(HttpServletRequest request, RmDfgc rmDfgc) { return super.exportXls(request, rmDfgc, RmDfgc.class, "堤防工程"); } /** * 通过excel导入数据 * * @param request * @param response * @return */ @RequestMapping(value = "/importExcel", method = RequestMethod.POST) public Result importExcel(HttpServletRequest request, HttpServletResponse response) { return super.importExcel(request, response, RmDfgc.class); } /** * 通过id查询 * * @return */ @AutoLog(value = "堤防工程-查询提防长度") @ApiOperation(value = "堤防工程-查询提防长度", notes = "堤防工程-查询提防长度") @GetMapping(value = "/selectdfcd") public Result selectdfcd() { List> selectdfcd = rmDfgcService.selectdfcd(); if (selectdfcd == null) { return Result.error("未找到对应数据"); } return Result.OK(selectdfcd); } }