From 0118ba9749c850ba32b789011883c353a735f1fb Mon Sep 17 00:00:00 2001
From: wjc <1243177876@qq.com>
Date: 星期二, 22 四月 2025 19:25:33 +0800
Subject: [PATCH] 2025年04月22日19:25:13 优化功率单位转化

---
 app/src/main/java/com/hdl/photovoltaic/ui/home/HomePageFragment.java     |   22 ++++---
 app/src/main/java/com/hdl/photovoltaic/ui/adapter/DeviceInfoAdapter.java |    9 +-
 app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java         |   79 ++++++++++++--------------
 app/src/main/java/com/hdl/photovoltaic/ui/adapter/HouseInfoAdapter.java  |   19 +++--
 app/src/main/res/layout/item_plant_details.xml                           |    2 
 5 files changed, 65 insertions(+), 66 deletions(-)

diff --git a/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java b/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java
index 4720942..a935e16 100644
--- a/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java
+++ b/app/src/main/java/com/hdl/photovoltaic/other/HdlCommonLogic.java
@@ -62,14 +62,15 @@
 
 
     /**
-     * @param value 鍊�
+     * @param value    鍊�
+     * @param unitType 闇�瑕佽浆鐨勫崟浣嶏紙kW锛宬Wh锛�
      * @return 瀛楃涓插�艰繑鍥�
      */
-    public static String convertDoubleValue(String value) {
+    public static String convertDoubleValue(String value, String unitType) {
         if (TextUtils.isEmpty(value)) {
             return UnitType.noValue;
         }
-        return divideByOneThousandAndFormat(value).toString();
+        return divideByOneThousandAndFormat(value,unitType).toString();
     }
 
     /**
@@ -122,10 +123,10 @@
      * @param value 鍊�
      * @return BigDecimal
      */
-    public static BigDecimal divideByOneThousandAndFormat(String value) {
+    public static BigDecimal divideByOneThousandAndFormat(String value, String unitType) {
         try {
             double doubleValue = Double.parseDouble(value);
-            int mDoubleValue = doubleValue(value);
+            long mDoubleValue = doubleValue(value,unitType);
             if (mDoubleValue == 0) {
                 return getBigDecimal(value);
             } else {
@@ -143,17 +144,29 @@
      *
      * @param value 鍊�
      */
-    public static int doubleValue(String value) {
+    public static long doubleValue(String value, String unitType) {
         try {
             double doubleValue = Double.parseDouble(value);
-            if (doubleValue > 1000 && doubleValue < 1000 * 1000) {
-                return 1000;
-            } else if (doubleValue > 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
-                return 1000 * 1000;
-            } else if (doubleValue > 1000 * 1000 * 1000) {
-                return 1000 * 1000 * 1000;
+            if (unitType.equals(UnitType.kW)) {
+                if (doubleValue > 1000 && doubleValue < 1000 * 1000) {
+                    return 1000 ;
+                } else if (doubleValue >= 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
+                    return 1000 * 1000 ;
+                } else if (doubleValue >= 1000 * 1000 * 1000) {
+                    return 1000L * 1000 * 1000 ;
+                } else {
+                    return 1000;
+                }
             } else {
-                return 0;
+                if (doubleValue >= 1000 && doubleValue < 1000 * 1000) {
+                    return 1000;
+                } else if (doubleValue >= 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
+                    return 1000 * 1000;
+                } else if (doubleValue >= 1000 * 1000 * 1000) {
+                    return 1000 * 1000 * 1000;
+                } else {
+                    return 0;
+                }
             }
         } catch (Exception e) {
             return 0;
@@ -161,29 +174,7 @@
 
     }
 
-    /**
-     * 杞崲鍗曚綅
-     *
-     * @param value    鍊�
-     * @param unitType 琛ㄧず鏄崟浣嶏紙鍔熺巼锛岀數閲忥級
-     */
-    public static String convertUnit(String value, String unitType) {
-        try {
-            double doubleValue = Double.parseDouble(value);
-            if (doubleValue > 1000 && doubleValue < 1000 * 1000) {
-                return unitType.equals(UnitType.kW) ? UnitType.mW : UnitType.mWh;
-            } else if (doubleValue > 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
-                return unitType.equals(UnitType.kW) ? UnitType.gW : UnitType.gWh;
-            } else if (doubleValue > 1000 * 1000 * 1000) {
-                return unitType.equals(UnitType.kW) ? UnitType.tW : UnitType.tWh;
-            } else {
-                return unitType.equals(UnitType.kW) ? UnitType.kW : UnitType.kWh;
-            }
-        } catch (Exception e) {
-            return unitType.equals(UnitType.kW) ? UnitType.kW : UnitType.kWh;
-        }
 
-    }
 
     /**
      * 杞崲鐢甸噺(kWh)鍗曚綅
@@ -193,11 +184,11 @@
     public static String convertKWHUnit(String value) {
         try {
             double doubleValue = Double.parseDouble(value);
-            if (doubleValue > 1000 && doubleValue < 1000 * 1000) {
+            if (doubleValue >= 1000 && doubleValue < 1000 * 1000) {
                 return UnitType.mWh;
-            } else if (doubleValue > 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
+            } else if (doubleValue >= 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
                 return UnitType.gWh;
-            } else if (doubleValue > 1000 * 1000 * 1000) {
+            } else if (doubleValue >= 1000 * 1000 * 1000) {
                 return UnitType.tWh;
             } else {
                 return UnitType.kWh;
@@ -217,10 +208,12 @@
         try {
             double doubleValue = Double.parseDouble(value);
             if (doubleValue > 1000 && doubleValue < 1000 * 1000) {
+                return UnitType.kW;
+            } else if (doubleValue >= 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
                 return UnitType.mW;
-            } else if (doubleValue > 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
+            } else if (doubleValue >= 1000 * 1000 * 1000&&doubleValue < 1000 * 1000 * 1000*1000L) {
                 return UnitType.gW;
-            } else if (doubleValue > 1000 * 1000 * 1000) {
+            } else if (doubleValue >= 1000 * 1000 * 1000*1000L) {
                 return UnitType.tW;
             } else {
                 return UnitType.kW;
@@ -239,11 +232,11 @@
     public static String convertKWPUnit(String value) {
         try {
             double doubleValue = Double.parseDouble(value);
-            if (doubleValue > 1000 && doubleValue < 1000 * 1000) {
+            if (doubleValue >= 1000 && doubleValue < 1000 * 1000) {
                 return UnitType.MWp;
-            } else if (doubleValue > 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
+            } else if (doubleValue >= 1000 * 1000 && doubleValue < 1000 * 1000 * 1000) {
                 return UnitType.GWp;
-            } else if (doubleValue > 1000 * 1000 * 1000) {
+            } else if (doubleValue >= 1000 * 1000 * 1000) {
                 return UnitType.TWp;
             } else {
                 return UnitType.kWp;
diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/adapter/DeviceInfoAdapter.java b/app/src/main/java/com/hdl/photovoltaic/ui/adapter/DeviceInfoAdapter.java
index 611574c..4b36cf4 100644
--- a/app/src/main/java/com/hdl/photovoltaic/ui/adapter/DeviceInfoAdapter.java
+++ b/app/src/main/java/com/hdl/photovoltaic/ui/adapter/DeviceInfoAdapter.java
@@ -44,13 +44,14 @@
     public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
         CloudInverterDeviceBean deviceBean = this.mList.get(position);
 
+
         holder.homeNameTv.setText(deviceBean.getHomeNameAndDeviceName());
         holder.device_details_sn_tv.setText(deviceBean.getOsn());
         holder.device_label_run_state_value_tv.setText(deviceBean.getSystemStatusDesc());
-        holder.device_label_power_value_tv.setText(HdlCommonLogic.convertDoubleValue(deviceBean.getOutputActivePower(), 1000));
-        holder.device_label_power_unit.setText(HdlCommonLogic.convertUnit(deviceBean.getOutputActivePower(), UnitType.kW));
-        holder.device_label_day_value_tv.setText(HdlCommonLogic.convertDoubleValue(deviceBean.getTotalElectricityPvToday()));
-        holder.device_label_day_unit.setText(HdlCommonLogic.convertUnit(deviceBean.getOutputActivePower(), UnitType.kWh));
+        holder.device_label_power_value_tv.setText(HdlCommonLogic.convertDoubleValue(deviceBean.getOutputActivePower(), UnitType.kW));
+        holder.device_label_power_unit.setText(HdlCommonLogic.convertKWUnit(deviceBean.getOutputActivePower()));
+        holder.device_label_day_value_tv.setText(HdlCommonLogic.convertDoubleValue(deviceBean.getTotalElectricityPvToday(), UnitType.kWh));
+        holder.device_label_day_unit.setText(HdlCommonLogic.convertKWHUnit(deviceBean.getTotalElectricityPvToday()));
         holder.device_label_location_tv.setText(deviceBean.getHomeAddress());
         setTextViewStyle(holder.device_label_state_tv, deviceBean.getDeviceStatus());
         holder.itemView.setTag(position);
diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/adapter/HouseInfoAdapter.java b/app/src/main/java/com/hdl/photovoltaic/ui/adapter/HouseInfoAdapter.java
index a445f25..d2d9071 100644
--- a/app/src/main/java/com/hdl/photovoltaic/ui/adapter/HouseInfoAdapter.java
+++ b/app/src/main/java/com/hdl/photovoltaic/ui/adapter/HouseInfoAdapter.java
@@ -61,17 +61,18 @@
         HouseIdBean houseIdBean = this.mList.get(position);
         holder.homeNameTv.setText(houseIdBean.getHomeName().trim());
         //瑁呮満瀹归噺
-        String capacity = HdlCommonLogic.getBigDecimal(houseIdBean.getInstalledCapacity()).toString();
-        holder.capacityTv.setText(capacity);
+        holder.capacityTv.setText(HdlCommonLogic.convertDoubleValue(houseIdBean.getInstalledCapacity(), UnitType.kWp));
+        holder.capacity_kwp_unit.setText(HdlCommonLogic.convertKWPUnit(houseIdBean.getInstalledCapacity()));
+
         //鍙戠數鍔熺巼
-        holder.power_value_tv.setText(HdlCommonLogic.convertDoubleValue(houseIdBean.getPower()));
-        holder.power_kw_unit.setText(HdlCommonLogic.convertUnit(houseIdBean.getPower(), UnitType.kW));
+        holder.power_value_tv.setText(HdlCommonLogic.convertDoubleValue(houseIdBean.getPower(), UnitType.kW));
+        holder.power_kw_unit.setText(HdlCommonLogic.convertKWUnit(houseIdBean.getPower()));
         //褰撴棩鍙戠數閲�
-        holder.day_value_tv.setText(HdlCommonLogic.convertDoubleValue(houseIdBean.getTodayElectricity()));
-        holder.day_kwh_unit.setText(HdlCommonLogic.convertUnit(houseIdBean.getTodayElectricity(), UnitType.kWh));
+        holder.day_value_tv.setText(HdlCommonLogic.convertDoubleValue(houseIdBean.getTodayElectricity(), UnitType.kWh));
+        holder.day_kwh_unit.setText(HdlCommonLogic.convertKWHUnit(houseIdBean.getTodayElectricity()));
         //鐢垫睜瀹归噺
-        holder.month_value_tv.setText(HdlCommonLogic.convertDoubleValue(houseIdBean.getBatteryCapacity()));
-        holder.month_kwh_unit.setText(HdlCommonLogic.convertUnit(houseIdBean.getBatteryCapacity(), UnitType.kWh));
+        holder.month_value_tv.setText(HdlCommonLogic.convertDoubleValue(houseIdBean.getBatteryCapacity(), UnitType.kWh));
+        holder.month_kwh_unit.setText(HdlCommonLogic.convertKWHUnit(houseIdBean.getBatteryCapacity()));
 
         holder.plant_details_location_tv.setText(houseIdBean.getHomeAddress());
         holder.item_parent_rl.setTag(position);
@@ -219,6 +220,7 @@
         public ImageView homeIconIv;//浣忓畢鍥剧墖
         public TextView homeNameTv;//浣忓畢鍚嶇О
         public TextView capacityTv;//瑁呮満瀹归噺
+        public TextView capacity_kwp_unit;//瑁呮満瀹归噺鍗曚綅
         public TextView power_value_tv;//鍙戠數鍔熺巼
         public TextView power_kw_unit;//鍙戠數鍔熺巼鍗曚綅
         public TextView day_value_tv;//褰撴棩鍙戠數閲�
@@ -237,6 +239,7 @@
             homeIconIv = itemView.findViewById(R.id.device_details_image_iv);
             homeNameTv = itemView.findViewById(R.id.device_details_name_tv);
             capacityTv = itemView.findViewById(R.id.device_details_value_tv);
+            capacity_kwp_unit = itemView.findViewById(R.id.capacity_kwp_unit);
             power_value_tv = itemView.findViewById(R.id.power_value_tv);
             power_kw_unit = itemView.findViewById(R.id.power_kw_unit);
             day_value_tv = itemView.findViewById(R.id.day_value_tv);
diff --git a/app/src/main/java/com/hdl/photovoltaic/ui/home/HomePageFragment.java b/app/src/main/java/com/hdl/photovoltaic/ui/home/HomePageFragment.java
index d576529..17e4a36 100644
--- a/app/src/main/java/com/hdl/photovoltaic/ui/home/HomePageFragment.java
+++ b/app/src/main/java/com/hdl/photovoltaic/ui/home/HomePageFragment.java
@@ -535,11 +535,13 @@
                         if (dataOverBean == null) {
                             return;
                         }
-                        String capacityValue = HdlCommonLogic.divideByOneThousandAndFormat(dataOverBean.getInstalledCapacity()).toString();
+
+                        String capacityValue = HdlCommonLogic.convertDoubleValue(dataOverBean.getInstalledCapacity(), UnitType.kWp);
                         viewBinding.infoRl1Text1Tv.setText(capacityValue);
                         viewBinding.kwpUnit.setText(HdlCommonLogic.convertKWPUnit(dataOverBean.getInstalledCapacity()));
-                        viewBinding.infoRl1Text3Tv.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getPower()));
 
+                        viewBinding.infoRl1Text3Tv.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getPower(), UnitType.kW));
+                        viewBinding.kwUnit.setText(HdlCommonLogic.convertKWUnit(dataOverBean.getPower()));
 //                        String str = dataOverBean.getPowerRatio() + "%";
 //                        int startIndex = str.lastIndexOf("%");
 //                        int endIndex = str.lastIndexOf("%") + 1;
@@ -567,14 +569,14 @@
                         } catch (Exception ignored) {
                         }
 
-                        viewBinding.day.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getTodayElectricity()));
-                        viewBinding.dayUnit.setText(HdlCommonLogic.convertUnit(dataOverBean.getTodayElectricity(), UnitType.kWh));
-                        viewBinding.month.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getMonthElectricity()));
-                        viewBinding.monthUnit.setText(HdlCommonLogic.convertUnit(dataOverBean.getMonthElectricity(), UnitType.kWh));
-                        viewBinding.year.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getYearElectricity()));
-                        viewBinding.yearUnit.setText(HdlCommonLogic.convertUnit(dataOverBean.getYearElectricity(), UnitType.kWh));
-                        viewBinding.Total.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getTotalElectricity()));
-                        viewBinding.TotalUnit.setText(HdlCommonLogic.convertUnit(dataOverBean.getTotalElectricity(), UnitType.kWh));
+                        viewBinding.day.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getTodayElectricity(), UnitType.kWh));
+                        viewBinding.dayUnit.setText(HdlCommonLogic.convertKWHUnit(dataOverBean.getTodayElectricity()));
+                        viewBinding.month.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getMonthElectricity(), UnitType.kWh));
+                        viewBinding.monthUnit.setText(HdlCommonLogic.convertKWHUnit(dataOverBean.getMonthElectricity()));
+                        viewBinding.year.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getYearElectricity(), UnitType.kWh));
+                        viewBinding.yearUnit.setText(HdlCommonLogic.convertKWHUnit(dataOverBean.getYearElectricity()));
+                        viewBinding.Total.setText(HdlCommonLogic.convertDoubleValue(dataOverBean.getTotalElectricity(), UnitType.kWh));
+                        viewBinding.TotalUnit.setText(HdlCommonLogic.convertKWHUnit(dataOverBean.getTotalElectricity()));
 
                     }
                 });
diff --git a/app/src/main/res/layout/item_plant_details.xml b/app/src/main/res/layout/item_plant_details.xml
index 2c0de7f..e20fc6c 100644
--- a/app/src/main/res/layout/item_plant_details.xml
+++ b/app/src/main/res/layout/item_plant_details.xml
@@ -86,7 +86,7 @@
                 android:textSize="@dimen/text_14" />
 
             <TextView
-                android:id="@+id/kwp_unit"
+                android:id="@+id/capacity_kwp_unit"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignBottom="@+id/device_details_value_tv"

--
Gitblit v1.8.0