| 
					
				 | 
			
			
				@@ -0,0 +1,170 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+package com.sysu.admin.api.crop; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.alibaba.fastjson.JSON; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.mysql.jdbc.StringUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.controller.crop.CropLand; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.controller.crop.CropLandService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.controller.crop.CropPoint; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.controller.crop.images.CropImage; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.controller.crop.images.CropImageService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.controller.crop.images.ImageType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.support.base.BaseComponent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.support.shiro.ShiroService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.support.system.config.ConfigContext; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.sysu.admin.support.system.config.SConfigService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+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.apache.commons.codec.binary.Base64; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.opengis.referencing.FactoryException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.opengis.referencing.operation.TransformException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.BeanUtils; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.bind.annotation.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.io.IOException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.nio.file.Files; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.nio.file.Paths; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.nio.file.StandardOpenOption; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.Arrays; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.stream.Collectors; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@RequestMapping("/api/crop") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+@RestController 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+public class ApiCropController extends BaseComponent { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    CropLandService cityLandService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    SConfigService mSConfigService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    ShiroService mShiroService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    CropImageService cropImageService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    static Long testId = 102L; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @RequestMapping("/point_list") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public BaseResult pointList(@RequestBody String data) throws FactoryException, TransformException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CropVo vo = JSON.parseObject(data, CropVo.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(vo.getMeter() > 5000){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            vo.setMeter(5000); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<CropPoint> cropPoints = cityLandService.findByBuffer(vo.getPoint(), vo.getMeter()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return R.succ(cropPoints); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @RequestMapping("/info") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public BaseResult info(@RequestBody String data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CropVo vo = JSON.parseObject(data, CropVo.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CropLand cityLand = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(vo.getId() != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            cityLand = cityLandService.findOne(vo.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }else{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            cityLand = cityLandService.findByPoint(vo.getPoint()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(cityLand == null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return R.fail(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        cityLand.setGeojson(GeoCastUtil.gjson.toString(cityLand.getGeom())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<CropImage> cropImageList = cropImageService.findByCropId(cityLand.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for(CropImage cropImage : cropImageList){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ImageType imageType = CropImage.getImageType(cropImage.getType().intValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            switch (imageType){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                case fay: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    cityLand.setFay(cropImage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                case center: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    cityLand.setCenter(cropImage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                case near: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    cityLand.setNear(cropImage); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return R.succ(cityLand); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @RequestMapping("/data") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public BaseResult data(@RequestBody String data) throws FactoryException, TransformException { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CropVo vo = JSON.parseObject(data, CropVo.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<CropLand> cityLandList = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        cityLandList = cityLandService.findByExecutor(testId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<CropImage> cropImages = cropImageService.findByCropIds(cityLandList.stream().map(CropLand::getId).collect(Collectors.toList())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        setImages(cityLandList, cropImages); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        cityLandList.stream().forEach(cityLand -> { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            cityLand.setGeojson(GeoCastUtil.gjson.toString(cityLand.getGeom())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return R.succ(cityLandList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @RequestMapping("/updateStatus") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public BaseResult updateStatus(@RequestBody String data){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CropVo vo = JSON.parseObject(data, CropVo.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        cityLandService.updateStatus(vo.getId(),vo.getStatus()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return R.succ(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @PostMapping("/uploadData") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public BaseResult uploadData(@RequestBody String json){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CropVo vo = JSON.parseObject(json, CropVo.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        CropLand bean = cityLandService.findOne(vo.getId()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        BeanUtils.copyProperties(vo, bean, CopyUtils.nullNames(vo)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        bean.setExecutor(testId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        cityLandService.saveCropAndImages(bean); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return R.succ(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 把文件转化为base64. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param filePath 源文件路径 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return String 转化后的base64 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public static String encryptToBase64(String filePath) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!StringUtils.isNullOrEmpty(filePath)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                byte[] bytes = Files.readAllBytes(Paths.get(filePath)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return Base64.encodeBase64String(bytes); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } catch (IOException e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void setImages(List<CropLand> cropClassList, List<CropImage> cropImageList){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        int i = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        for(CropLand cropLand : cropClassList){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            while (i < cropImageList.size()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                CropImage cropImageItem = cropImageList.get(i); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if(cropImageItem.getCropId() > cropLand.getId()){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if(cropImageItem.getCropId().equals(cropLand.getId())){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    ImageType imageType = CropImage.getImageType(cropImageItem.getType().intValue()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    switch (imageType){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        case fay: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            cropLand.setFay(cropImageItem); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        case center: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            cropLand.setCenter(cropImageItem); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                        case near: 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            cropLand.setNear(cropImageItem); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                            break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                i++; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 |