CropLandController.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.sysu.admin.controller.crop;
  2. import com.alibaba.fastjson.JSON;
  3. import com.querydsl.core.BooleanBuilder;
  4. import com.querydsl.core.types.Predicate;
  5. import com.querydsl.core.types.dsl.BooleanExpression;
  6. import com.querydsl.core.types.dsl.PathBuilder;
  7. import com.querydsl.jpa.impl.JPAUpdateClause;
  8. import com.sysu.admin.api.crop.CropVo;
  9. import com.sysu.admin.api.interceptor.LogAspect;
  10. import com.sysu.admin.controller.city.*;
  11. import com.sysu.admin.controller.crop.CropLand;
  12. import com.sysu.admin.controller.crop.CropLandService;
  13. import com.sysu.admin.controller.crop.CropLandVo;
  14. import com.sysu.admin.controller.geo.land.LandTaskStatus;
  15. import com.sysu.admin.support.base.BaseVo;
  16. import com.sysu.admin.support.base.ServiceContext;
  17. import com.sysu.admin.support.shiro.ShiroService;
  18. import com.sysu.admin.utils.GenericsUtil;
  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.utils.CopyUtils;
  23. import org.locationtech.jts.geom.MultiPolygon;
  24. import org.springframework.beans.BeanUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.data.domain.Sort;
  27. import org.springframework.stereotype.Controller;
  28. import org.springframework.web.bind.annotation.RequestMapping;
  29. import org.springframework.web.bind.annotation.ResponseBody;
  30. import org.springframework.web.bind.annotation.RestController;
  31. import javax.transaction.Transactional;
  32. import java.util.ArrayList;
  33. import java.util.Date;
  34. import java.util.List;
  35. import java.util.concurrent.Executor;
  36. import java.util.concurrent.Executors;
  37. /**
  38. * 读取地图数据信息
  39. */
  40. @RequestMapping("/crop")
  41. @Controller
  42. public class CropLandController extends ServiceContext {
  43. @Autowired
  44. CropLandService cityLandService;
  45. @Autowired
  46. ShiroService shiroService;
  47. @Autowired
  48. DistrictRepository districtRepository;
  49. @Autowired
  50. CityRepository cityRepository;
  51. @RequestMapping("/getInfo")
  52. @ResponseBody
  53. public BaseResult getInfo(Double x, Double y){
  54. CropLand cityLand = cityLandService.findByPoint(new Double[]{x,y});
  55. if(cityLand == null){
  56. return R.fail();
  57. }
  58. cityLand.setWkt(GeoCastUtil.geomToWkt(cityLand.getGeom()));
  59. return R.succ(cityLand);
  60. }
  61. @RequestMapping("/getBuffer")
  62. @ResponseBody
  63. public BaseResult getBuffer(Double x, Double y, Integer m){
  64. return R.succ(cityLandService.getBuffer(new Double[]{x, y}, m));
  65. }
  66. @RequestMapping("/save")
  67. @ResponseBody
  68. public BaseResult save(CropLandVo vo){
  69. CropLand bean = cityLandService.findOne(vo.getId());
  70. if(bean.getStatus().intValue() == LandTaskStatus.unpublished.ordinal()){
  71. bean.setCreator(shiroService.getPrincipalId());
  72. bean.setCreateDate(new Date());
  73. }
  74. BeanUtils.copyProperties(vo, bean, CopyUtils.nullNames(vo));
  75. bean.setWkt(GeoCastUtil.geomToWkt(bean.getGeom()));
  76. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getId()));
  77. bean.getCenterPoint().setSRID(4326);
  78. District district = districtRepository.findByPoint(bean.getCenterPoint());
  79. if(district != null){
  80. cityLandService.setCity(bean, district);
  81. }
  82. cityLandService.save(bean);
  83. return R.succ(bean);
  84. }
  85. @RequestMapping("/publish")
  86. @ResponseBody
  87. public BaseResult publish(CropLandVo vo){
  88. CropLand bean = cityLandService.findOne(vo.getId());
  89. if(bean.getStatus().intValue() != LandTaskStatus.unpublished.ordinal()){
  90. return R.fail("不是未发布的地块!");
  91. }
  92. if(bean.getDistrict() == null || bean.getCenterPoint() == null){
  93. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  94. bean.getCenterPoint().setSRID(4326);
  95. District district = districtRepository.findByPoint(bean.getCenterPoint());
  96. if(district != null){
  97. cityLandService.setCity(bean, district);
  98. }
  99. }
  100. bean.setStatus(LandTaskStatus.published.ordinal());
  101. cityLandService.save(bean);
  102. return R.succ();
  103. }
  104. @RequestMapping("/publish_index")
  105. public String publishIndex(){
  106. return "page/publish_task";
  107. }
  108. @RequestMapping("/batchPublish")
  109. @ResponseBody
  110. @Transactional
  111. public BaseResult batchPublish(CropLandVo vo){
  112. QCropLand qCropLand = QCropLand.cropLand;
  113. JPAUpdateClause jpaUpdateClause = mJPAQueryFactory.update(qCropLand);
  114. jpaUpdateClause.set(qCropLand.status, LandTaskStatus.published.ordinal());
  115. jpaUpdateClause.set(qCropLand.updateDate, new Date());
  116. Predicate predicate = null;
  117. if(vo.getProvinceId() != null){
  118. predicate = qCropLand.province.eq(vo.getProvinceId());
  119. }
  120. if(vo.getCityId() != null){
  121. predicate = qCropLand.city.eq(vo.getCityId()).and(predicate);
  122. }
  123. if(vo.getDistrictId() != null){
  124. predicate = qCropLand.district.eq(vo.getDistrictId()).and(predicate);
  125. }
  126. if(predicate != null){
  127. predicate = qCropLand.status.eq(LandTaskStatus.unpublished.ordinal()).and(predicate);
  128. jpaUpdateClause.where(predicate);
  129. jpaUpdateClause.execute();
  130. }
  131. return R.succ();
  132. }
  133. @RequestMapping("/addTest")
  134. @ResponseBody
  135. public BaseResult addTest(String wkt){
  136. CropLand bean = new CropLand().setGeom((MultiPolygon)GeoCastUtil.wktToGeom(wkt))
  137. .setGridcode(2L).setStatus(LandTaskStatus.unpublished.ordinal());
  138. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  139. bean.getCenterPoint().setSRID(4326);
  140. District district = districtRepository.findByPoint(bean.getCenterPoint());
  141. if(district != null) {
  142. cityLandService.setCity(bean, district);
  143. }
  144. cityLandService.save(bean);
  145. return R.succ();
  146. }
  147. Executor executor = Executors.newFixedThreadPool(10);
  148. int size = 0;
  149. @RequestMapping("/saveTestAll")
  150. @ResponseBody
  151. public BaseResult saveTestAll(){
  152. QCropLand qCropLand = QCropLand.cropLand;
  153. List<CropLand> landList = cityLandService.findAll(qCropLand.district.isNull());
  154. List<District> districtList = districtRepository.findAll(Sort.by(District.FIELDS.code));
  155. List<City> cityList = cityRepository.findAll(Sort.by(City.FIELDS.code));
  156. int i=0;
  157. for(City city : cityList){
  158. List<District> districts = new ArrayList<>();
  159. while(i < districtList.size()){
  160. if(city.getCode() / 100 == districtList.get(i).getCode() / 100){
  161. districts.add(districtList.get(i));
  162. i++;
  163. }else{
  164. break;
  165. }
  166. }
  167. city.setDistrictList(districts);
  168. }
  169. size = landList.size();
  170. System.out.println("landList load finnish");
  171. GenericsUtil<CropLand> genericsUtil = new GenericsUtil<>(CropLand.class);
  172. List<List<CropLand>> list = genericsUtil.subListBySegment(landList, 10);
  173. for(List<CropLand> cropLandList : list){
  174. executor.execute(() -> {
  175. for(CropLand bean : cropLandList){
  176. District district = withinDistrict(cityList, bean);
  177. if(district != null){
  178. cityLandService.setCity(bean, district);
  179. cityLandService.save(bean);
  180. synchronized (this) {
  181. System.out.println("剩余:" + (--size));
  182. }
  183. }
  184. }
  185. });
  186. }
  187. return R.succ();
  188. }
  189. private District withinDistrict(List<City> cityList, CropLand bean){
  190. for(City city : cityList){
  191. if(bean.getCenterPoint() == null){
  192. bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom()));
  193. }
  194. if(bean.getCenterPoint().within(city.getGeom())){
  195. for(District district : city.getDistrictList()){
  196. if(bean.getCenterPoint().within(district.getGeom())){
  197. return district;
  198. }
  199. }
  200. }
  201. }
  202. return null;
  203. }
  204. }