Administrator 2 năm trước cách đây
mục cha
commit
8261ab6a79

+ 2 - 2
src/main/java/com/sysu/admin/api/crop/ApiLandController.java

@@ -23,12 +23,12 @@ public class ApiLandController extends BaseComponent {
 
     @RequestMapping("/land_area_stat")
     public BaseResult getLandAreaStat(@RequestBody CommonVo commonVo){
-        return R.succ(landService.getLandAreaStat(commonVo.getDate(),"440882"));
+        return R.succ(landService.getLandAreaStat(commonVo.getDate(), commonVo.getDistrict(), commonVo.getCity()));
     }
 
     @RequestMapping("/crop_area_stat")
     public BaseResult getCropAreaStat(@RequestBody CommonVo commonVo){
-        return R.succ(landService.getCropAreaStat(commonVo.getDate(),"440882"));
+        return R.succ(landService.getCropAreaStat(commonVo.getDate(),commonVo.getDistrict(), commonVo.getCity()));
     }
 
     @RequestMapping("/list")

+ 27 - 7
src/main/java/com/sysu/admin/controller/aland/CropAreaStat.java

@@ -2,28 +2,48 @@ package com.sysu.admin.controller.aland;
 
 import lombok.Data;
 
+import java.util.Arrays;
+
 @Data
 public class CropAreaStat {
+    public CropAreaStat(){}
+
+    public CropAreaStat(String cropType, String name, String color) {
+        this.cropType = cropType;
+        this.name = name;
+        this.color = color;
+    }
+
+    private String cropType;
     private String name;
-    private Double area;
+    private Double area = 0.0;
     private String color;
-    private Integer count;
+    private Integer count = 0;
 
-    static  String[] names = {"corn","rice","other"};
+    static  String[] cropTypes = {"corn","rice","other"};
     static  String[] name2s = {"玉米","水稻","其他"};
     static  String[] colors = {"#E99209","#979B49","#6E7F85"};
 
+    public static Integer getCropTypeIndex(String cropType){
+        for(int i=0;i<cropTypes.length;i++){
+            if(cropTypes[i].equals(cropType)){
+                return i;
+            }
+        }
+        return null;
+    }
+
     public static String castName(String name){
-        for(int i=0;i<names.length;i++){
-            if(names[i].equals(name)){
+        for(int i = 0; i< cropTypes.length; i++){
+            if(cropTypes[i].equals(name)){
                 return name2s[i];
             }
         }
         return "";
     }
     public static String castColor(String name){
-        for(int i=0;i<names.length;i++){
-            if(names[i].equals(name)){
+        for(int i = 0; i< cropTypes.length; i++){
+            if(cropTypes[i].equals(name)){
                 return colors[i];
             }
         }

+ 9 - 6
src/main/java/com/sysu/admin/controller/aland/LandRepository.java

@@ -7,14 +7,17 @@ import java.util.List;
 
 public interface LandRepository extends JpaPlusRepository<Land, Long> {
 
-    @Query(value = "select crop_type,sum(area),count(id) from  leizhou_land  group by crop_type",nativeQuery = true)
-    List<Object[]> statCropTypeAll();
+    @Query(value = "select town_id,crop_type,sum(area),count(id) from  leizhou_land where district_code = ?1 and town_id is not null  group by town_id,crop_type",nativeQuery = true)
+    List<Object[]> statTownCropTypeAll(String districtCode);
 
-    @Query(value = "select crop_type,sum(area),count(id) from  leizhou_land where St_within(geom,st_geomfromtext(?1,4326)) group by crop_type",nativeQuery = true)
-    List<Object[]> statCropType(String wkt);
+    @Query(value = "select district_code,crop_type,sum(area),count(id) from  leizhou_land where district_code is not null and district_code like ?1  group by district_code,crop_type",nativeQuery = true)
+    List<Object[]> statDistrictCropTypeAll(String likeCity);
 
-    @Query(value = "select town_id,parcel_type,sum(area),count(id) from leizhou_land where town_id is not null group by town_id,parcel_type",nativeQuery = true)
-    List<Object[]> statParcelTypeAll();
+    @Query(value = "select town_id,parcel_type,sum(area),count(id) from leizhou_land where district_code = ?1 and town_id is not null group by town_id,parcel_type",nativeQuery = true)
+    List<Object[]> statTownParcelTypeAll(String district);
+
+    @Query(value = "select district_code,parcel_type,sum(area),count(id) from leizhou_land where district_code is not null and district_code like ?1 group by district_code,parcel_type",nativeQuery = true)
+    List<Object[]> statDistrictParcelTypeAll(String likeCity);
 
     @Query(value = "select * from  leizhou_land where St_within(geom,st_geomfromtext(?1,4326)) limit ?2",nativeQuery = true)
     List<Land> findListByWkt(String wkt,Integer limit);

+ 70 - 28
src/main/java/com/sysu/admin/controller/aland/LandService.java

@@ -3,7 +3,7 @@ package com.sysu.admin.controller.aland;
 import com.querydsl.core.types.Predicate;
 import com.sysu.admin.controller.city.CityRepository;
 import com.sysu.admin.controller.city.DistrictRepository;
-import com.sysu.admin.controller.city.TownRepository;
+import com.sysu.admin.controller.city.DistrictService;
 import com.sysu.admin.controller.city.TownService;
 import com.sysu.admin.support.base.BaseService;
 import com.sysu.admin.utils.MySimpleDateFormat;
@@ -25,21 +25,38 @@ public class LandService extends BaseService<Land,Long> {
     @Autowired
     TownService townService;
     @Autowired
+    DistrictService districtService;
+    @Autowired
     DistrictRepository districtRepository;
     @Autowired
     CityRepository cityRepository;
 
-
-    public List<TownStat> getLandAreaStat(Date date, String districtCode){
+    public List<TownStat> getLandAreaStat(Date date, String districtCode, String city){
         Double percent = getPercent(date);
-        List<Object[]> res = landRepository.statParcelTypeAll();
+        List<Object[]> res = null;
+        Map<Integer,String> namesMap = null;
+        if(districtCode != null){
+            namesMap = townService.getNames(districtCode);
+            res = landRepository.statTownParcelTypeAll(districtCode);
+            return getLandAreaStat(res,percent,namesMap);
+        }
+        if(city != null){
+            namesMap = districtService.getNames(city);
+            res = landRepository.statDistrictParcelTypeAll(city.substring(0,4) + "%");
+            return getLandAreaStat(res,percent,namesMap);
+        }
+        throw new RuntimeException("district 和 city 至少需要一个");
+    }
+
+
+    public List<TownStat> getLandAreaStat(List<Object[]> res ,Double percent, Map<Integer,String> namesMap){
         List<TownStat> townStatList = new ArrayList<>(30);
-        Integer currentTownId = null;
-        TownStat townStat = null;
-        Map<Integer,String> namesMap = townService.getNames(districtCode);
         TownStat sumStat = new TownStat();
         sumStat.setTownName("汇总信息");
         townStatList.add(sumStat);
+        Integer currentTownId = null;
+        TownStat townStat = null;
+
         HashMap<String,LandAreaStat > sumMap = new HashMap<>();
 
         for(Object[] o : res){
@@ -90,21 +107,38 @@ public class LandService extends BaseService<Land,Long> {
         return percent;
     }
 
-    public List<TownStatCrop> getCropAreaStat(Date date, String districtCode) {
+
+
+    public List<TownStatCrop> getCropAreaStat(Date date, String districtCode, String city){
         Double percent = getPercent(date);
-        List<Object[]> res = landRepository.statParcelTypeAll();
-        List<TownStat> townStatList = new ArrayList<>(30);
-        Integer currentTownId = null;
-        TownStat townStat = null;
-        Map<Integer,String> namesMap = townService.getNames(districtCode);
-        TownStat sumStat = new TownStat();
+        List<Object[]> res = null;
+        Map<Integer,String> namesMap = null;
+        if(districtCode != null){
+            namesMap = townService.getNames(districtCode);
+            res = landRepository.statTownCropTypeAll(districtCode);
+            return getCropAreaStat(res,percent,namesMap);
+        }
+        if(city != null){
+            namesMap = districtService.getNames(city);
+            res = landRepository.statDistrictCropTypeAll(city.substring(0,4) + "%");
+            return getCropAreaStat(res,percent,namesMap);
+        }
+        throw new RuntimeException("district 和 city 至少需要一个");
+    }
+
+    public List<TownStatCrop> getCropAreaStat(List<Object[]> res ,Double percent, Map<Integer,String> namesMap){
+        List<TownStatCrop> townStatList = new ArrayList<>(30);
+        TownStatCrop sumStat = new TownStatCrop();
         sumStat.setTownName("汇总信息");
-//        sumStat.setSumMap(new HashMap<>());
         townStatList.add(sumStat);
+        Integer currentTownId = null;
+        TownStatCrop townStat = null;
+
+        HashMap<String,CropAreaStat > sumMap = new HashMap<>();
 
         for(Object[] o : res){
             if(currentTownId == null || !o[0].toString().equals(currentTownId.toString())){
-                townStat = new TownStat();
+                townStat = new TownStatCrop();
                 townStat.setList(new ArrayList<>());
                 currentTownId = Integer.valueOf(o[0].toString());
                 townStat.setTownId(currentTownId);
@@ -112,28 +146,36 @@ public class LandService extends BaseService<Land,Long> {
                 townStatList.add(townStat);
             }
 
-            LandAreaStat bean = new LandAreaStat();
-            bean.setParcelType(Integer.valueOf(o[1].toString()));
-            bean.setColor(LandAreaStat.castColor(bean.getParcelType()));
-            bean.setName(LandAreaStat.castName(bean.getParcelType()));
+            CropAreaStat bean = new CropAreaStat();
+            bean.setCropType(o[1].toString());
+            bean.setColor(CropAreaStat.castColor(bean.getCropType()));
+            bean.setName(CropAreaStat.castName(bean.getCropType()));
             Double area = (Double)o[2];
             bean.setArea((area + area / 2) / 1000);
             bean.setArea(bean.getArea() * percent);
             bean.setCount(Integer.valueOf(o[3].toString()));
             townStat.getList().add(bean);
             //汇总信息
-//            LandAreaStat sum = sumStat.getSumMap().get(bean.getParcelType());
-//            if(sum == null){
-//                sum = new LandAreaStat(bean.getParcelType(), bean.getName(), bean.getColor());
-//                sumStat.getSumMap().put(bean.getParcelType().toString(), sum);
-//            }
-//            sum.setArea(sum.getArea() + bean.getArea());
-//            sum.setCount(sum.getCount() + bean.getCount());
+            CropAreaStat sum = sumMap.get(bean.getCropType());
+            if(sum == null){
+                sum = new CropAreaStat(bean.getCropType(), bean.getName(), bean.getColor());
+                sumMap.put(bean.getCropType(), sum);
+            }
+            sum.setArea(sum.getArea() + bean.getArea());
+            sum.setCount(sum.getCount() + bean.getCount());
         }
 
-        return null;
+        //组装汇总List
+        CropAreaStat[] sumArrays = new CropAreaStat[sumMap.size()];
+        for(String key : sumMap.keySet()){
+            sumArrays[CropAreaStat.getCropTypeIndex(key)] = sumMap.get(key);
+        }
+        sumStat.setList(Arrays.asList(sumArrays));
+        return townStatList;
     }
 
+
+
     public List<Land> list(String district, Integer townId, Integer limit, String cropType){
         limit = limit == null ? 50 : limit;
         String wkt = null;

+ 4 - 0
src/main/java/com/sysu/admin/controller/city/District.java

@@ -29,6 +29,10 @@ public class District{
         this.area = area;
         this.geom = (MultiPolygon)geom;
     }
+    public District(String name, String code) {
+        this.name = name;
+        this.code = code;
+    }
     public District(){}
 
     @Id

+ 3 - 0
src/main/java/com/sysu/admin/controller/city/DistrictRepository.java

@@ -16,6 +16,9 @@ public interface DistrictRepository extends JpaPlusRepository<District, Integer>
     @Query(value = "Select new District(p.name,p.code,p.area,p.geom) from District p where p.code like ?1 ")
     List<District> findList(String likeCode);
 
+    @Query(value = "Select new District(p.name,p.code) from District p where p.code like ?1 ")
+    List<District> findInfoList(String cityCodeLike);
+
     @Query(value = "select st_astext(geom) from p_district where code = ?1" ,nativeQuery = true)
     String findWktByCode(String code);
 

+ 30 - 0
src/main/java/com/sysu/admin/controller/city/DistrictService.java

@@ -0,0 +1,30 @@
+package com.sysu.admin.controller.city;
+
+import com.sysu.admin.support.base.BaseService;
+import com.xiesx.fastboot.core.jpa.JpaPlusRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+@Service
+public class DistrictService extends BaseService<District, Integer> {
+
+    @Autowired
+    DistrictRepository districtRepository;
+
+    public Map<Integer, String> getNames(String cityCode){
+        List<District> list = districtRepository.findInfoList(cityCode.substring(0,4) + "%");
+        Map<Integer, String> map = new HashMap<>();
+        list.stream().forEach(town -> {
+            map.put(Integer.valueOf(town.getCode()),town.getName());
+        });
+        return map;
+    }
+
+    @Override
+    public JpaPlusRepository<District, Integer> r() {
+        return districtRepository;
+    }
+}