From 59237ecbcb9828b1dc762da07f83fa598bd9d6b2 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期二, 03 六月 2025 18:07:01 +0800
Subject: [PATCH] fix(NewCard2Activity): 优化读卡逻辑

---
 qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/card/UserCard.java |  246 ++++++++++++++++++++++++++++++++----------------
 1 files changed, 164 insertions(+), 82 deletions(-)

diff --git a/qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/card/UserCard.java b/qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/card/UserCard.java
index b26f931..310e0e5 100644
--- a/qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/card/UserCard.java
+++ b/qiheonlinelibrary/src/main/java/com/dayu/qiheonlinelibrary/card/UserCard.java
@@ -1,11 +1,13 @@
 package com.dayu.qiheonlinelibrary.card;
 
+import com.dayu.baselibrary.bean.BaseUserCardCard;
+import com.dayu.baselibrary.tools.BcdUtil;
 import com.dayu.baselibrary.tools.HexUtil;
-
 import com.dayu.qiheonlinelibrary.utils.CardCommon;
 import com.tencent.bugly.crashreport.CrashReport;
 
 import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Calendar;
 import java.util.List;
 
@@ -15,31 +17,68 @@
  * Date: 2023-11-07 9:37
  * Description: 榻愭渤椤圭洰鐢ㄦ埛鍗$粨鏋�
  */
-public class UserCard implements Serializable {
+public class UserCard extends BaseUserCardCard implements Serializable {
     public String cardType = CardCommon.USER_CARD_TYPE_1;//鍐欏崱鏍囧織 A1鍒峰崱寮�娉靛墠 A8鍒峰崱寮�娉靛悗  A2鍙犲姞鍏呭��
 
     public int arerNumber;//鍖哄煙鍙�(搴曚綅鍦ㄥ墠楂樹綅鍦ㄥ悗)
 
-    public int deviceNumberl;//璁惧缂栧彿(搴曚綅鍦ㄥ墠楂樹綅鍦ㄥ悗)
-    public short rechargeTimes;//鍏呭�兼鏁�
+    public String userCode;//鐢ㄦ埛缂栧彿BCD鐪佸競鍘夸埂鏉�
 
-    public int totalWater;//鐢ㄦ埛鎬荤敤姘撮噺  搴曚綅鍦ㄥ墠锛岄珮浣嶅湪鍚�2浣嶅皬鏁扮偣  鍚袱涓皬鏁扮偣鐨勬暣鏁�
+    public int userCodeNumber;//鐢ㄦ埛缂栧彿(楂樹綅鍦ㄥ墠浣庝綅鍦ㄥ悗锛�
 
-    public int totalElectric;//鎬荤敤鐢甸噺浣� 搴曚綅鍦ㄥ墠锛岄珮浣嶅湪鍚�  1浣嶅皬鏁扮偣  鍚�1浣嶅皬鏁扮偣鐨勬暣鏁�
+    public int cardWriteState;//绠$悊鍗¤繑鍐欐満鍒� 00:涓績鍐�01:鎺у埗鍣ㄨ繑鍐�
+
+    public int cardState;//鐜板満鍗$姸鎬�  00:鏃у崱 01:鏂板崱
 
     public int balance;//鍓╀綑閲戦 搴曚綅鍦ㄥ墠  2浣嶅皬鏁扮偣锛屽崟浣嶅垎
 
-    public int surplusWater;//鍓╀綑姘撮噺 搴曚綅鍦ㄥ墠  2浣嶅皬鏁扮偣銆�鍗曚綅绔嬫柟绫� (璺熶环鏍间竴鏍峰厛涔樹互100)
+    public int surplusElecticity;//鍓╀綑鐢甸噺 搴曚綅鍦ㄥ墠  2浣嶅皬鏁扮偣銆�鍗曚綅绔嬫柟绫� (璺熶环鏍间竴鏍峰厛涔樹互100)
+
+    public int totalMorny;//绱鍏呭�奸噾棰�
 
     public Calendar rechargeDate;// 璐按鏃堕棿 BCD
 
+    public Float electricPrice;//鐢靛崟浠凤紝淇濈暀涓変綅灏忔暟
+
+    /**
+     * 鍐欏崱瀹屾垚鍚庢牎楠屾槸鍚﹀啓鍗℃垚鍔�
+     *
+     * @param data
+     * @return
+     */
+    public boolean equlsUserCard(List<byte[]> data) {
+        // 鍙傛暟楠岃瘉
+        if (data == null || data.size() < 3) {
+            return false;
+        }
+        // 瀹氫箟瑕佹瘮杈冪殑瀛楄妭鏁扮粍
+        byte[][] expectedBytes = {getZeroBytes(), getOneBytes(), getTwoBytes()};
+        // 閫愪釜姣旇緝瀛楄妭鏁扮粍
+        for (int i = 0; i < 3; i++) {
+            if (!Arrays.equals(data.get(i), expectedBytes[i])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * 杩斿洖瀹屾暣鐨勭敤鎴风紪鍙�
+     *
+     * @return
+     */
+    public String getMyUserCode() {
+        return userCode + String.format("%04d", userCodeNumber);
+//        return "";
+    }
 
     /**
      * 閫氳繃byte杞琤ean
      *
      * @param data
      */
-    public static UserCard getBean(List<byte[]> data) {
+    @Override
+    public UserCard getBean(List<byte[]> data) {
         try {
             UserCard userCard = new UserCard();
             //绗�0鍧楄В鏋�
@@ -47,44 +86,48 @@
 
             userCard.cardType = HexUtil.byteToHex(zero[0]);
 
-            byte[] arerNumberByte = new byte[2];
+            byte[] arerNumberByte = new byte[4];
             System.arraycopy(zero, 1, arerNumberByte, 0, arerNumberByte.length);
-            userCard.arerNumber =  HexUtil.get16To10LowHightByBytes(arerNumberByte);
-
-            byte[] deviceNumberlByte = new byte[2];
-            System.arraycopy(zero, 3, deviceNumberlByte, 0, deviceNumberlByte.length);
-            userCard.deviceNumberl =  HexUtil.get16To10LowHightByBytes(deviceNumberlByte);
-
-            userCard.rechargeTimes = HexUtil.hexToByte(HexUtil.byteToHex(zero[5]));
-
-            byte[] totalWaterByte = new byte[4];
-            System.arraycopy(zero, 6, totalWaterByte, 0, totalWaterByte.length);
-            userCard.totalWater = HexUtil.get16To10LowHightByBytes(totalWaterByte);
-
-            byte[] totalElectricByte = new byte[4];
-            System.arraycopy(zero, 10, totalElectricByte, 0, totalElectricByte.length);
-            userCard.totalElectric = HexUtil.get16To10LowHightByBytes(totalElectricByte);
-
+            userCard.arerNumber = HexUtil.get16To10LowHightByBytes(arerNumberByte);
+            byte[] userCodeByte = new byte[6];
+            System.arraycopy(zero, 5, userCodeByte, 0, userCodeByte.length);
+            userCard.userCode = BcdUtil.bcdToStr(userCodeByte);
+            byte[] userCodeNumber = new byte[2];
+            System.arraycopy(zero, 11, userCodeNumber, 0, userCodeNumber.length);
+            userCard.userCodeNumber = HexUtil.get16To10LowHightByBytes(userCodeNumber);
+            byte[] cardWriteState = new byte[1];
+            System.arraycopy(zero, 13, cardWriteState, 0, cardWriteState.length);
+            userCard.cardWriteState = HexUtil.get16To10LowHightByBytes(cardWriteState);
+            byte[] cardState = new byte[1];
+            System.arraycopy(zero, 14, cardState, 0, cardState.length);
+            userCard.cardState = HexUtil.get16To10LowHightByBytes(cardState);
             //绗�1鍧楄В鏋�
             byte[] one = data.get(1);
             byte[] balanceByte = new byte[4];
             System.arraycopy(one, 0, balanceByte, 0, balanceByte.length);
-            userCard.balance =HexUtil.get16To10LowHightByBytes(balanceByte);
+            userCard.balance = HexUtil.get16To10LowHightByBytes(balanceByte);
 
             byte[] surplusWaterByte = new byte[4];
             System.arraycopy(one, 4, surplusWaterByte, 0, surplusWaterByte.length);
-            userCard.surplusWater = HexUtil.get16To10LowHightByBytes(surplusWaterByte);
+            userCard.surplusElecticity = HexUtil.get16To10LowHightByBytes(surplusWaterByte);
+
+            byte[] totalMorny = new byte[4];
+            System.arraycopy(one, 8, totalMorny, 0, totalMorny.length);
+            userCard.totalMorny = HexUtil.get16To10LowHightByBytes(totalMorny);
 
             byte[] rechargeDateByte = new byte[3];
             System.arraycopy(one, 12, rechargeDateByte, 0, rechargeDateByte.length);
-
             int year = HexUtil.getBcdToInt(one[12]);
             int month = HexUtil.getBcdToInt(one[13]);
             int day = HexUtil.getBcdToInt(one[14]);
-
             Calendar calendar = Calendar.getInstance();
             calendar.set(2000 + year, month, day, 0, 0, 0);
             userCard.rechargeDate = calendar;
+
+            byte[] two = data.get(2);
+            byte[] electricPriceByte = new byte[4];
+            System.arraycopy(two, 8, electricPriceByte, 0, electricPriceByte.length);
+            userCard.electricPrice = HexUtil.hexToFloatLowHigh(electricPriceByte);
 
             return userCard;
 
@@ -105,37 +148,45 @@
                 byte[] data = new byte[16];
                 data[0] = HexUtil.hexToByte(cardType);
                 //鍖哄煙鍙�
-                byte[] arerNumberBytes = new byte[2];
+                byte[] arerNumberBytes = new byte[4];
                 byte[] arerNumberDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(arerNumber));
                 System.arraycopy(arerNumberDatas, 0, arerNumberBytes, 0, arerNumberDatas.length);
                 if (arerNumberBytes != null) {
                     System.arraycopy(arerNumberBytes, 0, data, 1, arerNumberBytes.length);
                 }
-                //璁惧缂栧彿
-                byte[] deviceNumberlBytes = new byte[2];
-                byte[] deviceNumberlDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(deviceNumberl));
-                System.arraycopy(deviceNumberlDatas, 0, deviceNumberlBytes, 0, deviceNumberlDatas.length);
-                if (deviceNumberlBytes != null) {
-                    System.arraycopy(deviceNumberlBytes, 0, data, 3, deviceNumberlBytes.length);
-                }
-                //鍏呭�兼鏁�
-                byte rechargeTimesByte = HexUtil.hexToByte(HexUtil.get10to16(rechargeTimes));
-                data[5] = rechargeTimesByte;
 
-                //鐢ㄦ埛鎬荤敤姘撮噺
-                byte[] totalWaterBytes = new byte[4];
-                byte[] totalWaterDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(totalWater));
-                System.arraycopy(totalWaterDatas, 0, totalWaterBytes, 0, totalWaterDatas.length);
-                if (totalWaterBytes != null) {
-                    System.arraycopy(totalWaterBytes, 0, data, 6, totalWaterBytes.length);
+
+                //鐢ㄦ埛缂栧彿(鍦板潃)
+                byte[] userCodeBytes = new byte[6];
+                byte[] userCodeDatas = BcdUtil.strToBcd(userCode);
+                System.arraycopy(userCodeDatas, 0, userCodeBytes, 0, userCodeDatas.length);
+                if (userCodeBytes != null) {
+                    System.arraycopy(userCodeBytes, 0, data, 5, userCodeBytes.length);
                 }
-                //鎬荤敤鐢甸噺浣�
-                byte[] totalElectricBytes = new byte[4];
-                byte[] totalElectricDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(totalElectric));
-                System.arraycopy(totalElectricDatas, 0, totalElectricBytes, 0, totalElectricDatas.length);
-                if (totalElectricBytes != null) {
-                    System.arraycopy(totalElectricBytes, 0, data, 10, totalElectricBytes.length);
+                //鐢ㄦ埛缂栧彿(鑷)
+                byte[] userCodeNumberBytes = new byte[2];
+                byte[] userCodeNumberDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(userCodeNumber));
+                System.arraycopy(userCodeNumberDatas, 0, userCodeNumberBytes, 0, userCodeNumberDatas.length);
+                if (userCodeNumberBytes != null) {
+                    System.arraycopy(userCodeNumberBytes, 0, data, 11, userCodeNumberBytes.length);
                 }
+                //绠$悊鍗¤繑鍐欐満鍒� 00:涓績鍐�01:鎺у埗鍣ㄨ繑鍐�
+                byte[] cardWriteStateBytes = new byte[1];
+                byte[] cardWriteStateDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(cardWriteState));
+                System.arraycopy(cardWriteStateDatas, 0, cardWriteStateBytes, 0, cardWriteStateDatas.length);
+                if (cardWriteStateBytes != null) {
+                    System.arraycopy(cardWriteStateBytes, 0, data, 13, cardWriteStateBytes.length);
+                }
+
+                //绠$悊鍗¤繑鍐欐満鍒� 00:涓績鍐�01:鎺у埗鍣ㄨ繑鍐�
+                byte[] cardStateBytes = new byte[1];
+                byte[] cardStateDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(cardState));
+                System.arraycopy(cardStateDatas, 0, cardStateBytes, 0, cardStateDatas.length);
+                if (cardStateBytes != null) {
+                    System.arraycopy(cardStateBytes, 0, data, 14, cardStateBytes.length);
+                }
+
+
                 data[15] = getByteSum(data);
                 return data;
             } catch (Exception e) {
@@ -164,11 +215,17 @@
                     System.arraycopy(balanceBytes, 0, data, 0, balanceBytes.length);
                 }
 
-                byte[] surplusWaterBytes = new byte[4];
-                byte[] surplusWaterDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater));
-                System.arraycopy(surplusWaterDatas, 0, surplusWaterBytes, 0, surplusWaterDatas.length);
-                if (surplusWaterBytes != null) {
-                    System.arraycopy(surplusWaterBytes, 0, data, 4, surplusWaterBytes.length);
+                byte[] surplusElecticityBytes = new byte[4];
+                byte[] surplusElecticityDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusElecticity));
+                System.arraycopy(surplusElecticityDatas, 0, surplusElecticityBytes, 0, surplusElecticityDatas.length);
+                if (surplusElecticityBytes != null) {
+                    System.arraycopy(surplusElecticityBytes, 0, data, 4, surplusElecticityBytes.length);
+                }
+                byte[] totalMornyBytes = new byte[4];
+                byte[] totalMornyDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(totalMorny));
+                System.arraycopy(totalMornyDatas, 0, totalMornyBytes, 0, totalMornyDatas.length);
+                if (totalMornyBytes != null) {
+                    System.arraycopy(totalMornyBytes, 0, data, 8, totalMornyBytes.length);
                 }
 
                 if (rechargeDate != null) {
@@ -202,6 +259,7 @@
         public byte[] toBytes() {
             byte[] data = new byte[16];
 
+
             try {
                 byte[] balanceBytes = new byte[4];
                 byte[] balanceDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance));
@@ -210,11 +268,18 @@
                     System.arraycopy(balanceBytes, 0, data, 0, balanceBytes.length);
                 }
 
-                byte[] surplusWaterBytes = new byte[4];
-                byte[] surplusWaterDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater));
-                System.arraycopy(surplusWaterDatas, 0, surplusWaterBytes, 0, surplusWaterDatas.length);
-                if (surplusWaterBytes != null) {
-                    System.arraycopy(surplusWaterBytes, 0, data, 4, surplusWaterBytes.length);
+                byte[] surplusElecticityBytes = new byte[4];
+                byte[] surplusElecticityDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusElecticity));
+                System.arraycopy(surplusElecticityDatas, 0, surplusElecticityBytes, 0, surplusElecticityDatas.length);
+                if (surplusElecticityBytes != null) {
+                    System.arraycopy(surplusElecticityBytes, 0, data, 4, surplusElecticityBytes.length);
+                }
+
+                byte[] electricPriceBytes = new byte[4];
+                byte[] electricPriceDatas = HexUtil.hexToByteArray(HexUtil.floatToHexLowHigh(electricPrice));
+                System.arraycopy(electricPriceDatas, 0, electricPriceBytes, 0, electricPriceDatas.length);
+                if (electricPriceBytes != null) {
+                    System.arraycopy(electricPriceBytes, 0, data, 8, electricPriceBytes.length);
                 }
 
                 if (rechargeDate != null) {
@@ -229,6 +294,7 @@
                     data[13] = bcdMonth;
                     data[14] = bcdDay;
                 }
+
 
                 data[15] = getByteSum(data);
             } catch (Exception e) {
@@ -271,36 +337,36 @@
         this.arerNumber = arerNumber;
     }
 
-    public int getDeviceNumberl() {
-        return deviceNumberl;
+    public String getUserCode() {
+        return userCode;
     }
 
-    public void setDeviceNumberl(int deviceNumberl) {
-        this.deviceNumberl = deviceNumberl;
+    public void setUserCode(String userCode) {
+        this.userCode = userCode;
     }
 
-    public short getRechargeTimes() {
-        return rechargeTimes;
+    public int getUserCodeNumber() {
+        return userCodeNumber;
     }
 
-    public void setRechargeTimes(short rechargeTimes) {
-        this.rechargeTimes = rechargeTimes;
+    public void setUserCodeNumber(int userCodeNumber) {
+        this.userCodeNumber = userCodeNumber;
     }
 
-    public int getTotalWater() {
-        return totalWater;
+    public int getCardWriteState() {
+        return cardWriteState;
     }
 
-    public void setTotalWater(int totalWater) {
-        this.totalWater = totalWater;
+    public void setCardWriteState(int cardWriteState) {
+        this.cardWriteState = cardWriteState;
     }
 
-    public int getTotalElectric() {
-        return totalElectric;
+    public int getCardState() {
+        return cardState;
     }
 
-    public void setTotalElectric(int totalElectric) {
-        this.totalElectric = totalElectric;
+    public void setCardState(int cardState) {
+        this.cardState = cardState;
     }
 
     public int getBalance() {
@@ -311,12 +377,20 @@
         this.balance = balance;
     }
 
-    public int getSurplusWater() {
-        return surplusWater;
+    public int getSurplusElecticity() {
+        return surplusElecticity;
     }
 
-    public void setSurplusWater(int surplusWater) {
-        this.surplusWater = surplusWater;
+    public void setSurplusElecticity(int surplusElecticity) {
+        this.surplusElecticity = surplusElecticity;
+    }
+
+    public int getTotalMorny() {
+        return totalMorny;
+    }
+
+    public void setTotalMorny(int totalMorny) {
+        this.totalMorny = totalMorny;
     }
 
     public Calendar getRechargeDate() {
@@ -327,5 +401,13 @@
         this.rechargeDate = rechargeDate;
     }
 
+    public Float getElectricPrice() {
+        return electricPrice;
+    }
+
+    public void setElectricPrice(Float electricPrice) {
+        this.electricPrice = electricPrice;
+    }
+
 
 }

--
Gitblit v1.8.0