From 14de918a79943e4961b09fa01ed320c6cad41f2e Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期三, 28 六月 2023 17:14:51 +0800
Subject: [PATCH] Revert "Revert "Merge branch 'hxb' into wjc""

---
 app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java |  385 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 321 insertions(+), 64 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
index ec5a1cf..fcdf6ec 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlResidenceLogic.java
@@ -1,5 +1,7 @@
 package com.hdl.photovoltaic.other;
 
+import android.text.TextUtils;
+
 import com.google.gson.Gson;
 import com.google.gson.JsonObject;
 import com.hdl.photovoltaic.bean.HttpResponsePack;
@@ -9,15 +11,26 @@
 import com.hdl.photovoltaic.listener.BaseSuccessFailureCallBeak;
 import com.hdl.photovoltaic.listener.CloudCallBeak;
 import com.hdl.photovoltaic.ui.bean.HouseInfoBean;
-import com.hdl.photovoltaic.ui.bean.HouseListBean;
+import com.hdl.photovoltaic.ui.bean.HouseIdBean;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
+/**
+ * 浣忓畢閫昏緫鐣岄潰
+ */
 public class HdlResidenceLogic {
+
+    public static final int pageSize = 50;
 
     private static volatile HdlResidenceLogic sHdlResidenceLogic;
 
+    /**
+     * 鑾峰彇褰撳墠瀵硅薄
+     *
+     * @return HdlResidenceLogic
+     */
     public static synchronized HdlResidenceLogic getInstance() {
         if (sHdlResidenceLogic == null) {
             synchronized (HdlResidenceLogic.class) {
@@ -30,114 +43,232 @@
         return sHdlResidenceLogic;
     }
 
+    /**
+     * 鑾峰彇浣忓畢鍒楄〃
+     *
+     * @return 浣忓畢鍒楄〃
+     */
     public List<HouseInfoBean> getHouseInfoList() {
         return houseInfoList;
     }
 
     public void setHouseInfoList(List<HouseInfoBean> houseInfoList) {
+        if (houseInfoList == null) {
+            houseInfoList = new ArrayList<>();
+        }
         this.houseInfoList = houseInfoList;
     }
 
-    public List<HouseListBean> getHouseInfoBeanList() {
-        return houseList;
+    /**
+     * 鑾峰彇浣忓畢ID鍒楄〃
+     *
+     * @return 浣忓畢ID鍒楄〃
+     */
+    public List<HouseIdBean> getHouseIdList() {
+        return houseIdList;
     }
 
-    public void setHouseInfoBeanList(List<HouseListBean> houseListBeanList) {
-        this.houseList = houseListBeanList;
+    public void setHouseIdList(List<HouseIdBean> houseIdList) {
+        if (houseIdList == null) {
+            houseIdList = new ArrayList<>();
+        }
+        this.houseIdList = houseIdList;
     }
 
-    //浣忓畢鍒楄〃
-    private List<HouseListBean> houseList = new ArrayList<>();
+    //浣忓畢ID鍒楄〃
+    private List<HouseIdBean> houseIdList = new ArrayList<>();
     //浣忓畢璇︽儏鍒楄〃
     private List<HouseInfoBean> houseInfoList = new ArrayList<>();
 
-    public HouseListBean getHouseByHouseId(String homeId) {
-        return new HouseListBean();
-    }
-
 
     /**
-     * 鑾峰彇浣忓畢(鐢电珯)鍒楄〃
+     * 鑾峰彇浣忓畢鍒楄〃
      *
-     * @param pageNo   褰撳墠椤垫暟
-     * @param pageSize 涓�椤靛ぇ灏�
+     * @param cloudCallBeak -
      */
-    public void getResidenceList(int pageNo, int pageSize, CloudCallBeak<Boolean> cloudCallBeak) {
-        if (pageNo == 1) {
-            //绗竴娆¤繘鏉ユ竻绌哄垪琛�
-            houseList.clear();
-        }
+    public void getHouseList(CloudCallBeak<Boolean> cloudCallBeak) {
+        //鑾峰彇浣忓畢ID鍒楄〃
+        getResidenceIdList("", "", new CloudCallBeak<List<HouseIdBean>>() {
+            @Override
+            public void onSuccess(List<HouseIdBean> houseListBeanIDList) {
+                if (houseListBeanIDList != null && houseListBeanIDList.size() > 0) {
+                    houseIdList.clear();
+                    houseIdList.addAll(houseListBeanIDList);
+                    //璁板綍璇锋眰鏉℃暟
+                    AtomicInteger atomicInteger = new AtomicInteger(0);
+                    for (int i = 0; i < houseIdList.size(); i++) {
+                        HouseIdBean houseIdBean = houseIdList.get(i);
+                        //鑾峰彇浣忓畢璇︽儏鍒楄〃
+                        getResidenceInfo(houseIdBean.getHomeId(), new CloudCallBeak<HouseInfoBean>() {
+                            @Override
+                            public void onSuccess(HouseInfoBean houseInfoBean) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                if (houseInfoBean != null) {
+                                    houseInfoBean.setHomeImage(houseIdBean.getHomeImage());//鐢电珯鍥剧墖
+                                    houseInfoBean.setInstalledCapacity(houseIdBean.getInstalledCapacity());//瑁呮満瀹归噺
+                                    houseInfoBean.setPower(houseIdBean.getPower());//鍙戠數鍔熺巼
+                                    houseInfoBean.setTodayElectricity(houseIdBean.getTodayElectricity());//浠婂ぉ鍙戠數閲�
+                                    houseInfoBean.setPowerStationStatus(houseIdBean.getPowerStationStatus());//鐢电珯鐘舵��(鐢电珯鐘舵��	1:姝e父(杩愯),2:绂荤嚎,3:杩炴帴涓�,4:鏁呴殰)
+                                    //娣诲姞鍒版湰鍦扮紦瀛�
+                                    addHouseToLocalMemory(houseInfoBean);
+                                }
+                                if (atomicInteger.get() == houseIdList.size()) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onSuccess(true);
+                                    }
+                                }
+
+                            }
+
+                            @Override
+                            public void onFailure(Exception exception) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                if (atomicInteger.get() == houseIdList.size()) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onFailure(exception);
+                                    }
+                                }
+                            }
+                        });
+
+                    }
+                } else {
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(true);
+                    }
+                }
+            }
+
+            @Override
+            public void onFailure(Exception exception) {
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
+            }
+        });
+
+    }
+
+    /**
+     * 鑾峰彇浣忓畢(鐢电珯)ID鍒楄〃
+     *
+     * @param key      鍙戠數鍔熺巼鎺掑簭(powerSort);浠婃棩鍙戠數閲忔帓搴�(todayElectricitySort);鍒涘缓鏃堕棿鎺掑簭(createTimeSort);
+     * @param keyValue (descending:闄嶅簭ascending:鍗囧簭),
+     */
+    public void getResidenceIdList(String key, String keyValue, CloudCallBeak<List<HouseIdBean>> cloudCallBeak) {
+
         String requestUrl = HttpApi.POST_PowerStation_List;
         JsonObject json = new JsonObject();
+        if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(keyValue)) {
+            json.addProperty(key, keyValue);//鍙戠數鍔熺巼鎺掑簭(descending:闄嶅簭ascending:鍗囧簭)
+        }
 //        json.addProperty("powerSort", "descending");//鍙戠數鍔熺巼鎺掑簭(descending:闄嶅簭ascending:鍗囧簭)
 //        json.addProperty("todayElectricitySort", "descending");//浠婃棩鍙戠數閲忔帓搴�
 //        json.addProperty("createTimeSort", "descending");//鍒涘缓鏃堕棿鎺掑簭
 //        json.addProperty("zoneType", "password");//鍖哄煙
-        json.addProperty("pageNo", pageNo);//椤电爜
+        json.addProperty("pageNo", 1);//椤电爜
         json.addProperty("pageSize", pageSize);//椤垫暟
-
+        List<HouseIdBean> list = new ArrayList<>();
         HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
             @Override
             public void onSuccess(HttpResponsePack httpResponsePack) {
-                if (httpResponsePack != null) {
-                    if (httpResponsePack.getData() != null) {
-                        Gson gson = new Gson();
-                        String jsonStr = gson.toJson(httpResponsePack.getData().toString());
-                        HouseInfoBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseInfoBeanClass.class);
-                        houseList.addAll(houseInfoBeanClass.getList());
-                        //鎬诲叡鏈夊灏戦〉
-                        long totalPage = houseInfoBeanClass.totalPage;
-                        if (totalPage == houseInfoBeanClass.getPageNo()) {
-                            cloudCallBeak.onSuccess(true);
-                            return;
+                if (httpResponsePack != null && httpResponsePack.getData() != null) {
+                    Gson gson = new Gson();
+                    String jsonStr = gson.toJson(httpResponsePack.getData());
+                    HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
+                    list.addAll(houseInfoBeanClass.getList());
+                    //鎬诲叡鏈夊灏戦〉
+                    long totalPage = houseInfoBeanClass.totalPage;
+                    if (totalPage < 2) {
+                        if (cloudCallBeak != null) {
+                            cloudCallBeak.onSuccess(list);
                         }
-                        getResidenceList(pageNo + 1, 100, cloudCallBeak);
-                    } else {
-                        cloudCallBeak.onSuccess(false);
+                        return;
+                    }
+                    //璁板綍璇锋眰鏉℃暟
+                    AtomicInteger atomicInteger = new AtomicInteger(0);
+                    //浠庣浜岄〉鑾峰彇鏁版嵁
+                    for (int i = 2; i <= totalPage; i++) {
+                        json.addProperty("pageNo", i);//鏇存柊椤电爜
+                        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
+                            @Override
+                            public void onSuccess(HttpResponsePack httpResponsePack) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                Gson gson = new Gson();
+                                String jsonStr = gson.toJson(httpResponsePack.getData());
+                                HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
+                                list.addAll(houseInfoBeanClass.getList());
+                                if (atomicInteger.get() == totalPage - 1) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onSuccess(list);
+                                    }
+                                }
+                            }
+
+                            @Override
+                            public void onFailure(Exception exception) {
+                                atomicInteger.set(atomicInteger.get() + 1);
+                                if (atomicInteger.get() == totalPage - 1) {
+                                    //鏈�鍚庝竴鏉¢��鍑�
+                                    if (cloudCallBeak != null) {
+                                        cloudCallBeak.onSuccess(list);
+                                    }
+                                }
+                            }
+                        });
                     }
 
+                } else {
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(list);
+                    }
                 }
             }
 
-
             @Override
             public void onFailure(Exception exception) {
-                cloudCallBeak.onFailure(exception);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
             }
         });
+
+
     }
 
-
     /**
-     * 鑾峰彇浣忓畢(鐢电珯)璇︽儏
+     * 鑾峰彇浣忓畢(鐢电珯)ID璇︽儏
      *
      * @param homeId        浣忓畢id
      * @param cloudCallBeak 鍥炶皟
      */
     public void getResidenceInfo(String homeId, CloudCallBeak<HouseInfoBean> cloudCallBeak) {
-        String requestUrl = HttpApi.POST_GET_MEMBER_INFO;
+        String requestUrl = HttpApi.POST_PowerStation_Info;
         JsonObject json = new JsonObject();
         json.addProperty("homeId", homeId);//鐢电珯id
         //json.addProperty("zoneType", "password");//鍖哄煙
         HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
             @Override
             public void onSuccess(HttpResponsePack httpResponsePack) {
-                if (httpResponsePack != null) {
-                    if (httpResponsePack.getData() != null) {
-                        Gson gson = new Gson();
-                        String jsonStr = gson.toJson(httpResponsePack.getData().toString());
-                        HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
-                        cloudCallBeak.onSuccess(houseInfoBean);
-                    } else {
-                        cloudCallBeak.onSuccess(null);
-                    }
+                if (httpResponsePack != null && httpResponsePack.getData() != null) {
+                    Gson gson = new Gson();
+                    String jsonStr = gson.toJson(httpResponsePack.getData());
+                    HouseInfoBean houseInfoBean = gson.fromJson(jsonStr, HouseInfoBean.class);
 
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(houseInfoBean);
+                    }
                 }
             }
 
             @Override
             public void onFailure(Exception exception) {
-                cloudCallBeak.onFailure(exception);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
             }
         });
 
@@ -146,13 +277,13 @@
 
 
     /**
-     * 缂栬緫浣忓畢(鐢电珯)
+     * 缂栬緫浣忓畢(鐢电珯),
      *
      * @param houseInfoBean -
      * @param cloudCallBeak -
      */
     public void editResidence(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
-        String requestUrl = HttpApi.POST_PowerStation_Create;
+        String requestUrl = HttpApi.POST_PowerStation_Edit;
         JsonObject json = new JsonObject();
         JsonObject location = new JsonObject();
         json.addProperty("homeId", houseInfoBean.getHomeId());
@@ -179,13 +310,17 @@
         HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
             @Override
             public void onSuccess(HttpResponsePack httpResponsePack) {
-                cloudCallBeak.onSuccess(true);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onSuccess(true);
+                }
 
             }
 
             @Override
             public void onFailure(Exception exception) {
-                cloudCallBeak.onFailure(exception);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
             }
         });
     }
@@ -204,25 +339,29 @@
         HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
             @Override
             public void onSuccess(HttpResponsePack httpResponsePack) {
-                cloudCallBeak.onSuccess(true);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onSuccess(true);
+                }
 
             }
 
             @Override
             public void onFailure(Exception exception) {
-                cloudCallBeak.onFailure(exception);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
             }
         });
 
     }
 
     /**
-     * 娣诲姞浣忓畢(鐢电珯)
+     * 娣诲姞浣忓畢(鐢电珯)鍒颁簯绔�
      *
      * @param houseInfoBean -
      * @param cloudCallBeak -
      */
-    public void addResidence(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
+    public void addHouseToCloud(HouseInfoBean houseInfoBean, CloudCallBeak<Boolean> cloudCallBeak) {
         String requestUrl = HttpApi.POST_PowerStation_Create;
         JsonObject json = new JsonObject();
         JsonObject location = new JsonObject();
@@ -250,20 +389,138 @@
         HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
             @Override
             public void onSuccess(HttpResponsePack httpResponsePack) {
-                cloudCallBeak.onSuccess(true);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onSuccess(true);
+                }
 
             }
 
             @Override
             public void onFailure(Exception exception) {
-                cloudCallBeak.onFailure(exception);
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
             }
         });
 
 
     }
 
-    static class HouseInfoBeanClass {
+    /**
+     * 鑾峰彇浣忓畢鍥剧墖
+     */
+    public void getResidenceImage(String imageUrl, CloudCallBeak<List<HouseIdBean>> cloudCallBeak) {
+        String requestUrl = imageUrl;
+        JsonObject json = new JsonObject();
+        List<HouseIdBean> list = new ArrayList<>();
+        HttpClient.getInstance().requestHttp(requestUrl, json.toString(), true, true, new BaseSuccessFailureCallBeak() {
+            @Override
+            public void onSuccess(HttpResponsePack httpResponsePack) {
+                if (httpResponsePack != null && httpResponsePack.getData() != null) {
+                    Gson gson = new Gson();
+                    String jsonStr = gson.toJson(httpResponsePack.getData());
+                    HouseBeanClass houseInfoBeanClass = gson.fromJson(jsonStr, HouseBeanClass.class);
+                } else {
+                    if (cloudCallBeak != null) {
+                        cloudCallBeak.onSuccess(list);
+                    }
+                }
+            }
+
+            @Override
+            public void onFailure(Exception exception) {
+                if (cloudCallBeak != null) {
+                    cloudCallBeak.onFailure(exception);
+                }
+            }
+        });
+
+
+    }
+
+    /**
+     * 娣诲姞銆愪綇瀹呰鎯呫�戝埌鏈湴缂撳瓨
+     *
+     * @param houseInfoBean 浣忓畢璇︽儏
+     */
+    public void addHouseToLocalMemory(HouseInfoBean houseInfoBean) {
+        if (houseInfoBean == null) {
+            return;
+        }
+        int index = isExistHouse(houseInfoBean.getHomeId());
+        if (index > -1) {
+            houseInfoList.remove(index);
+            houseInfoList.add(index, houseInfoBean);
+        } else {
+            houseInfoList.add(houseInfoBean);
+        }
+    }
+
+    /**
+     * 鏌ヨ鏈湴缂撳瓨浣忓畢鍒楄〃鏄惁瀛樺湪浣忓畢
+     *
+     * @param homeId 浣忓畢id
+     * @return 鍒楄〃绱㈠紩
+     */
+    public int isExistHouse(String homeId) {
+        int index = -1;
+        for (int i = 0; i < houseInfoList.size(); i++) {
+            if (houseInfoList.get(i).getHomeId().equals(homeId)) {
+                index = i;
+                break;
+            }
+        }
+        return index;
+    }
+
+    /**
+     * 鑾峰彇浣忓畢璇︽儏
+     *
+     * @param homeId 浣忓畢id
+     * @return 浣忓畢鎵句笉鍒拌繑鍥瀗ull
+     */
+    public HouseInfoBean getHouse(String homeId) {
+        int position = isExistHouse(homeId);
+        if (position > -1) {
+            return houseInfoList.get(position);
+        }
+        return null;
+    }
+
+    /**
+     * 鍒犻櫎浣忓畢
+     *
+     * @param homeId 浣忓畢id
+     */
+    public void delHouse(String homeId) {
+        int position = isExistHouse(homeId);
+        if (position > -1) {
+            houseInfoList.remove(position);
+        }
+    }
+
+    /**
+     * 鍒囨崲浣忓畢
+     *
+     * @param homeId 浣忓畢id
+     */
+    public Boolean switchHouse(String homeId) {
+        String oidHomeId = UserConfigManage.getInstance().getHomeId();
+        HdlFileLogic.getInstance().deleteDirectory(HdlFileLogic.getInstance().getCurrentHomeRootPath());
+        UserConfigManage.getInstance().setHomeId(homeId);
+        HdlFileLogic.getInstance().createDirectory();
+
+        HdlThreadLogic.runThread(new Runnable() {
+            @Override
+            public void run() {
+                //鎼滅储涓�涓嬩綇瀹呰澶�
+            }
+        }, null, null);
+        return true;
+    }
+
+
+    static class HouseBeanClass {
 
         //鎬绘潯鏁�
         private long totalCount;
@@ -274,7 +531,7 @@
         //椤垫暟
         private long pageSize;
         //鐢电珯鍒楄〃
-        private List<HouseListBean> list;
+        private List<HouseIdBean> list;
 
         public long getTotalCount() {
             return totalCount;
@@ -308,11 +565,11 @@
             this.pageSize = pageSize;
         }
 
-        public List<HouseListBean> getList() {
+        public List<HouseIdBean> getList() {
             return list == null ? new ArrayList<>() : list;
         }
 
-        public void setList(List<HouseListBean> list) {
+        public void setList(List<HouseIdBean> list) {
             this.list = list;
         }
     }

--
Gitblit v1.8.0