package com.sysu.admin.controller.crop; import com.alibaba.fastjson.JSON; import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.Predicate; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.core.types.dsl.PathBuilder; import com.querydsl.jpa.impl.JPAUpdateClause; import com.sysu.admin.api.crop.CropVo; import com.sysu.admin.api.interceptor.LogAspect; import com.sysu.admin.controller.city.*; import com.sysu.admin.controller.crop.CropLand; import com.sysu.admin.controller.crop.CropLandService; import com.sysu.admin.controller.crop.CropLandVo; import com.sysu.admin.controller.crop.range.LandRangeIndexService; import com.sysu.admin.controller.geo.land.LandTaskStatus; import com.sysu.admin.support.base.BaseVo; import com.sysu.admin.support.base.ServiceContext; import com.sysu.admin.support.shiro.ShiroService; import com.sysu.admin.utils.GenericsUtil; import com.sysu.admin.utils.shape.GeoCastUtil; import com.xiesx.fastboot.base.result.BaseResult; import com.xiesx.fastboot.base.result.R; import com.xiesx.fastboot.utils.CopyUtils; import org.locationtech.jts.geom.MultiPolygon; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.transaction.Transactional; import java.util.*; import java.util.concurrent.Executor; import java.util.concurrent.Executors; /** * 读取地图数据信息 */ @RequestMapping("/crop") @Controller public class CropLandController extends ServiceContext { @Autowired CropLandService cityLandService; @Autowired ShiroService shiroService; @Autowired LandRangeIndexService mLandRangeIndexService; @Autowired DistrictRepository districtRepository; @Autowired CityRepository cityRepository; @RequestMapping("/getInfo") @ResponseBody public BaseResult getInfo(Double x, Double y){ CropLand cityLand = cityLandService.findByPoint(new Double[]{x,y}); if(cityLand == null){ return R.fail(); } cityLand.setWkt(GeoCastUtil.geomToWkt(cityLand.getGeom())); return R.succ(cityLand); } @RequestMapping("/getGeoJson") @ResponseBody public BaseResult getGeojson(String tableNames, Double x1, Double y1,Double x2, Double y2){ // String[] tableNames = mLandRangeIndexService.getTableNames(x1,y1,x2,y2); List cropLandList = cityLandService.findCropLandNoGeomByTableNamesAndBBox(tableNames, x1,y1,x2,y2); return R.succ(cropLandList); } @RequestMapping("/getBuffer") @ResponseBody public BaseResult getBuffer(Double x, Double y, Integer m){ return R.succ(cityLandService.getBuffer(new Double[]{x, y}, m)); } @RequestMapping("/save") @ResponseBody public BaseResult save(CropLandVo vo){ CropLand bean = cityLandService.findOne(vo.getId()); if(bean.getStatus().intValue() == LandTaskStatus.unpublished.ordinal()){ bean.setCreator(shiroService.getPrincipalId()); bean.setCreateDate(new Date()); } BeanUtils.copyProperties(vo, bean, CopyUtils.nullNames(vo)); bean.setWkt(GeoCastUtil.geomToWkt(bean.getGeom())); bean.setCenterPoint(cityLandService.getCenterPoint(bean.getId())); bean.getCenterPoint().setSRID(4326); District district = districtRepository.findByPoint(bean.getCenterPoint()); if(district != null){ cityLandService.setCity(bean, district); } cityLandService.save(bean); return R.succ(bean); } @RequestMapping("/publish") @ResponseBody public BaseResult publish(CropLandVo vo){ CropLand bean = cityLandService.findOne(vo.getId()); if(bean.getStatus().intValue() != LandTaskStatus.unpublished.ordinal()){ return R.fail("不是未发布的地块!"); } if(bean.getDistrict() == null || bean.getCenterPoint() == null){ bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom())); bean.getCenterPoint().setSRID(4326); District district = districtRepository.findByPoint(bean.getCenterPoint()); if(district != null){ cityLandService.setCity(bean, district); } } bean.setStatus(LandTaskStatus.published.ordinal()); cityLandService.save(bean); return R.succ(); } @RequestMapping("/publish_index") public String publishIndex(){ return "page/publish_task"; } @RequestMapping("/batchPublish") @ResponseBody @Transactional public BaseResult batchPublish(CropLandVo vo){ QCropLand qCropLand = QCropLand.cropLand; JPAUpdateClause jpaUpdateClause = mJPAQueryFactory.update(qCropLand); jpaUpdateClause.set(qCropLand.status, LandTaskStatus.published.ordinal()); jpaUpdateClause.set(qCropLand.updateDate, new Date()); Predicate predicate = null; if(vo.getProvinceId() != null){ predicate = qCropLand.province.eq(vo.getProvinceId()); } if(vo.getCityId() != null){ predicate = qCropLand.city.eq(vo.getCityId()).and(predicate); } if(vo.getDistrictId() != null){ predicate = qCropLand.district.eq(vo.getDistrictId()).and(predicate); } if(predicate != null){ predicate = qCropLand.status.eq(LandTaskStatus.unpublished.ordinal()).and(predicate); jpaUpdateClause.where(predicate); jpaUpdateClause.execute(); } return R.succ(); } @RequestMapping("/addTest") @ResponseBody public BaseResult addTest(String wkt){ CropLand bean = new CropLand().setGeom((MultiPolygon)GeoCastUtil.wktToGeom(wkt)) .setGridcode(2L).setStatus(LandTaskStatus.unpublished.ordinal()); bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom())); bean.getCenterPoint().setSRID(4326); District district = districtRepository.findByPoint(bean.getCenterPoint()); if(district != null) { cityLandService.setCity(bean, district); } cityLandService.save(bean); return R.succ(); } Executor executor = Executors.newFixedThreadPool(10); int size = 0; @RequestMapping("/saveTestAll") @ResponseBody public BaseResult saveTestAll(){ QCropLand qCropLand = QCropLand.cropLand; List landList = cityLandService.findAll(qCropLand.district.isNull()); List districtList = districtRepository.findAll(Sort.by(District.FIELDS.code)); List cityList = cityRepository.findAll(Sort.by(City.FIELDS.code)); int i=0; for(City city : cityList){ List districts = new ArrayList<>(); while(i < districtList.size()){ if(city.getCode() / 100 == districtList.get(i).getCode() / 100){ districts.add(districtList.get(i)); i++; }else{ break; } } city.setDistrictList(districts); } size = landList.size(); System.out.println("landList load finnish"); GenericsUtil genericsUtil = new GenericsUtil<>(CropLand.class); List> list = genericsUtil.subListBySegment(landList, 10); for(List cropLandList : list){ executor.execute(() -> { for(CropLand bean : cropLandList){ District district = withinDistrict(cityList, bean); if(district != null){ cityLandService.setCity(bean, district); cityLandService.save(bean); synchronized (this) { System.out.println("剩余:" + (--size)); } } } }); } return R.succ(); } private District withinDistrict(List cityList, CropLand bean){ for(City city : cityList){ if(bean.getCenterPoint() == null){ bean.setCenterPoint(cityLandService.getCenterPoint(bean.getGeom())); } if(bean.getCenterPoint().within(city.getGeom())){ for(District district : city.getDistrictList()){ if(bean.getCenterPoint().within(district.getGeom())){ return district; } } } } return null; } }