SiteCropLandController.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package com.sysu.admin.site;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.fastjson.JSON;
  4. import com.sysu.admin.controller.city.CityRepository;
  5. import com.sysu.admin.controller.city.District;
  6. import com.sysu.admin.controller.city.DistrictRepository;
  7. import com.sysu.admin.controller.crop.CropLand;
  8. import com.sysu.admin.controller.crop.CropLandService;
  9. import com.sysu.admin.controller.crop.CropLandVo;
  10. import com.sysu.admin.controller.crop.CropPoint;
  11. import com.sysu.admin.controller.crop.range.LandRangeIndexService;
  12. import com.sysu.admin.controller.crop.xls.CropExcel;
  13. import com.sysu.admin.controller.crop.xls.CropExcelService;
  14. import com.sysu.admin.controller.crop.xls.CustomHandler;
  15. import com.sysu.admin.controller.crop.xls.PublishCropExcelListener;
  16. import com.sysu.admin.controller.geo.land.LandTaskStatus;
  17. import com.sysu.admin.support.base.ServiceContext;
  18. import com.sysu.admin.support.shiro.ShiroService;
  19. import com.sysu.admin.utils.shape.GeoCastUtil;
  20. import com.xiesx.fastboot.base.result.BaseResult;
  21. import com.xiesx.fastboot.base.result.R;
  22. import com.xiesx.fastboot.core.token.annotation.GoToken;
  23. import com.xiesx.fastboot.core.token.handle.CurrentToken;
  24. import com.xiesx.fastboot.utils.CopyUtils;
  25. import org.apache.catalina.util.URLEncoder;
  26. import org.apache.commons.lang3.StringUtils;
  27. import org.locationtech.jts.geom.MultiPolygon;
  28. import org.springframework.beans.BeanUtils;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.stereotype.Controller;
  31. import org.springframework.web.bind.annotation.RequestBody;
  32. import org.springframework.web.bind.annotation.RequestMapping;
  33. import org.springframework.web.bind.annotation.ResponseBody;
  34. import javax.servlet.http.HttpServletResponse;
  35. import javax.transaction.Transactional;
  36. import java.io.IOException;
  37. import java.nio.charset.Charset;
  38. import java.nio.file.Path;
  39. import java.util.Date;
  40. import java.util.List;
  41. import java.util.Map;
  42. /**
  43. * 读取地图数据信息
  44. */
  45. @RequestMapping("/site/crop")
  46. @Controller
  47. public class SiteCropLandController extends ServiceContext {
  48. @Autowired
  49. CropLandService cityLandService;
  50. @Autowired
  51. ShiroService shiroService;
  52. @Autowired
  53. LandRangeIndexService mLandRangeIndexService;
  54. @Autowired
  55. DistrictRepository districtRepository;
  56. @Autowired
  57. CityRepository cityRepository;
  58. @Autowired
  59. CropExcelService cropExcelService;
  60. @RequestMapping("/getInfo")
  61. @ResponseBody
  62. public BaseResult getInfo(@RequestBody CommonVo commonVo){
  63. CropLand cityLand = cityLandService.findByPoint(new Double[]{commonVo.getX1(), commonVo.getY1()});
  64. if(cityLand == null){
  65. return R.fail();
  66. }
  67. cityLand.setWkt(GeoCastUtil.geomToWkt(cityLand.getGeom()));
  68. return R.succ(cityLand);
  69. }
  70. @RequestMapping("/getGeoJson")
  71. @ResponseBody
  72. public BaseResult getGeojson(@RequestBody CommonVo commonVo, @GoToken() CurrentToken currentToken){
  73. List<CropPoint> cropLandList = cityLandService.findCropLandNoGeomByTableNamesAndBBox(commonVo.getTableNames(),
  74. commonVo.getX1(),commonVo.getY1(),commonVo.getX2(),commonVo.getY2(), commonVo.getStatus());
  75. return R.succ(cropLandList);
  76. }
  77. @RequestMapping("/publish")
  78. @ResponseBody
  79. public BaseResult publish(@RequestBody CropLandVo vo, @GoToken() CurrentToken currentToken){
  80. CropLand bean = cityLandService.findOne(vo.getId());
  81. if(bean.getStatus().intValue() != LandTaskStatus.unpublished.ordinal()){
  82. return R.fail("不是未发布的地块!");
  83. }
  84. if(bean.getDistrict() == null || bean.getCenterPoint() == null){
  85. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  86. bean.getCenterPoint().setSRID(4326);
  87. District district = districtRepository.findByPoint(bean.getCenterPoint());
  88. if(district != null){
  89. cityLandService.setCity(bean, district);
  90. }
  91. }
  92. bean.setStatus(LandTaskStatus.published.ordinal());
  93. cityLandService.save(bean);
  94. return R.succ();
  95. }
  96. @RequestMapping("/batchPublish")
  97. @ResponseBody
  98. @Transactional
  99. public BaseResult batchPublish(@RequestBody CropLandVo vo){
  100. if(StringUtils.isNotBlank(vo.getFileBase64())){
  101. Path path = cityLandService.fileByBase64(vo.getFileBase64(),"xls",mShiroService.getPrincipalId());
  102. PublishCropExcelListener publishCropExcelListener = new PublishCropExcelListener(mLandRangeIndexService);
  103. EasyExcel.read(path.toFile(), CropExcel.class,publishCropExcelListener).doReadAllSync();
  104. Map<String,List<Long>> stringListMap = publishCropExcelListener.getTableIdsMap();
  105. for(String tableName : stringListMap.keySet()) {
  106. cityLandService.batchPublishByIds(tableName, stringListMap.get(tableName));
  107. }
  108. }else{
  109. if(vo.getDistrictId() != null) {
  110. String tableName = mLandRangeIndexService.getTableNameByCode(vo.getDistrictId());
  111. cityLandService.batchPublishByTableName(tableName);
  112. }else{
  113. return R.fail("请选择区县!");
  114. }
  115. }
  116. return R.succ();
  117. }
  118. @RequestMapping("/export_index")
  119. public String exportIndex(){
  120. return "page/export";
  121. }
  122. @RequestMapping("/export")
  123. @ResponseBody
  124. @Transactional
  125. public void export(@RequestBody CropLandVo vo, HttpServletResponse response){
  126. String tableNames;
  127. if(vo.getDistrictId() == null){
  128. tableNames = mLandRangeIndexService.getAllTableNames();
  129. }else{
  130. tableNames = mLandRangeIndexService.getTableNameByCode(vo.getDistrictId());
  131. }
  132. List<CropLand> cropLandList = cityLandService.findAllByStatusAndDistrict(tableNames, vo.getStatus());
  133. List<CropExcel> cropExcelList = cropExcelService.cropLandToCropExcel(cropLandList);
  134. String mimetype = "application/x-msdownload";
  135. String downFileName = "重庆耕地信息.xlsx";
  136. String inlineType = "attachment"; // 是否内联附件
  137. response.setContentType(mimetype);
  138. response.setHeader("Content-Disposition", inlineType
  139. + ";filename=" + URLEncoder.DEFAULT.encode(downFileName, Charset.defaultCharset()));
  140. try {
  141. EasyExcel.write(response.getOutputStream(), CropExcel.class).registerWriteHandler(new CustomHandler())
  142. .sheet("sheet1").doWrite(cropExcelList);
  143. } catch (IOException e) {
  144. e.printStackTrace();
  145. }
  146. }
  147. @RequestMapping("/addTest")
  148. @ResponseBody
  149. public BaseResult addTest(@RequestBody String data){
  150. String wkt = JSON.parseObject(data).get("wkt").toString();
  151. CropLand bean = new CropLand().setGeom((MultiPolygon)GeoCastUtil.wktToGeom(wkt))
  152. .setGridcode(2L).setStatus(LandTaskStatus.unpublished.ordinal());
  153. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  154. bean.getCenterPoint().setSRID(4326);
  155. District district = districtRepository.findByPoint(bean.getCenterPoint());
  156. if(district != null) {
  157. cityLandService.setCity(bean, district);
  158. }
  159. cityLandService.save(bean);
  160. return R.succ();
  161. }
  162. }