SiteCropLandController.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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(Double x, Double y){
  63. CropLand cityLand = cityLandService.findByPoint(new Double[]{x,y});
  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("/getBuffer")
  78. @ResponseBody
  79. public BaseResult getBuffer(Double x, Double y, Integer m){
  80. return R.succ(cityLandService.getBuffer(new Double[]{x, y}, m));
  81. }
  82. @RequestMapping("/save")
  83. @ResponseBody
  84. public BaseResult save(CropLandVo vo){
  85. CropLand bean = cityLandService.findOne(vo.getId());
  86. if(bean.getStatus().intValue() == LandTaskStatus.unpublished.ordinal()){
  87. bean.setCreator(shiroService.getPrincipalId());
  88. bean.setCreateDate(new Date());
  89. }
  90. BeanUtils.copyProperties(vo, bean, CopyUtils.nullNames(vo));
  91. bean.setWkt(GeoCastUtil.geomToWkt(bean.getGeom()));
  92. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  93. District district = districtRepository.findByPoint(bean.getCenterPoint());
  94. if(district != null){
  95. cityLandService.setCity(bean, district);
  96. }
  97. cityLandService.save(bean);
  98. return R.succ(bean);
  99. }
  100. @RequestMapping("/publish")
  101. @ResponseBody
  102. public BaseResult publish(@RequestBody CropLandVo vo, @GoToken() CurrentToken currentToken){
  103. CropLand bean = cityLandService.findOne(vo.getId());
  104. if(bean.getStatus().intValue() != LandTaskStatus.unpublished.ordinal()){
  105. return R.fail("不是未发布的地块!");
  106. }
  107. if(bean.getDistrict() == null || bean.getCenterPoint() == null){
  108. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  109. bean.getCenterPoint().setSRID(4326);
  110. District district = districtRepository.findByPoint(bean.getCenterPoint());
  111. if(district != null){
  112. cityLandService.setCity(bean, district);
  113. }
  114. }
  115. bean.setStatus(LandTaskStatus.published.ordinal());
  116. cityLandService.save(bean);
  117. return R.succ();
  118. }
  119. @RequestMapping("/publish_index")
  120. public String publishIndex(){
  121. return "page/publish_task";
  122. }
  123. @RequestMapping("/batchPublish")
  124. @ResponseBody
  125. @Transactional
  126. public BaseResult batchPublish(CropLandVo vo, String fileBase64){
  127. if(StringUtils.isNotBlank(fileBase64)){
  128. Path path = cityLandService.fileByBase64(fileBase64,"xls",mShiroService.getPrincipalId());
  129. PublishCropExcelListener publishCropExcelListener = new PublishCropExcelListener(mLandRangeIndexService);
  130. EasyExcel.read(path.toFile(), CropExcel.class,publishCropExcelListener).doReadAllSync();
  131. Map<String,List<Long>> stringListMap = publishCropExcelListener.getTableIdsMap();
  132. for(String tableName : stringListMap.keySet()) {
  133. cityLandService.batchPublishByIds(tableName, stringListMap.get(tableName));
  134. }
  135. }else{
  136. if(vo.getDistrictId() != null) {
  137. String tableName = mLandRangeIndexService.getTableNameByCode(vo.getDistrictId());
  138. cityLandService.batchPublishByTableName(tableName);
  139. }
  140. }
  141. return R.succ();
  142. }
  143. @RequestMapping("/export_index")
  144. public String exportIndex(){
  145. return "page/export";
  146. }
  147. @RequestMapping("/export")
  148. @ResponseBody
  149. @Transactional
  150. public void export(CropLandVo vo, HttpServletResponse response){
  151. String tableNames;
  152. if(vo.getDistrictId() == null){
  153. tableNames = mLandRangeIndexService.getAllTableNames();
  154. }else{
  155. tableNames = mLandRangeIndexService.getTableNameByCode(vo.getDistrictId());
  156. }
  157. List<CropLand> cropLandList = cityLandService.findAllByStatusAndDistrict(tableNames, vo.getStatus());
  158. List<CropExcel> cropExcelList = cropExcelService.cropLandToCropExcel(cropLandList);
  159. String mimetype = "application/x-msdownload";
  160. String downFileName = "重庆耕地信息.xlsx";
  161. String inlineType = "attachment"; // 是否内联附件
  162. response.setContentType(mimetype);
  163. response.setHeader("Content-Disposition", inlineType
  164. + ";filename=" + URLEncoder.DEFAULT.encode(downFileName, Charset.defaultCharset()));
  165. try {
  166. EasyExcel.write(response.getOutputStream(), CropExcel.class).registerWriteHandler(new CustomHandler())
  167. .sheet("sheet1").doWrite(cropExcelList);
  168. } catch (IOException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. @RequestMapping("/addTest")
  173. @ResponseBody
  174. public BaseResult addTest(@RequestBody String data){
  175. String wkt = JSON.parseObject(data).get("wkt").toString();
  176. CropLand bean = new CropLand().setGeom((MultiPolygon)GeoCastUtil.wktToGeom(wkt))
  177. .setGridcode(2L).setStatus(LandTaskStatus.unpublished.ordinal());
  178. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  179. bean.getCenterPoint().setSRID(4326);
  180. District district = districtRepository.findByPoint(bean.getCenterPoint());
  181. if(district != null) {
  182. cityLandService.setCity(bean, district);
  183. }
  184. cityLandService.save(bean);
  185. return R.succ();
  186. }
  187. }