| 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; | 
|   | 
| /** | 
|  * Copyright (C), 2023, | 
|  * Author: zuo | 
|  * Date: 2023-11-07 9:37 | 
|  * Description: 齐河项目用户卡结构 | 
|  */ | 
| public class UserCard extends BaseUserCardCard implements Serializable { | 
|     public String cardType = CardCommon.USER_CARD_TYPE_1;//写卡标志 A1刷卡开泵前 A8刷卡开泵后  A2叠加充值 | 
|   | 
|     public int arerNumber;//区域号(底位在前高位在后) | 
|   | 
|     public String userCode;//用户编号BCD省市县乡村 | 
|   | 
|     public int userCodeNumber;//用户编号(高位在前低位在后) | 
|   | 
|     public int cardWriteState;//管理卡返写机制 00:中心写01:控制器返写 | 
|   | 
|     public int cardState;//现场卡状态  00:旧卡 01:新卡 | 
|   | 
|     public int balance;//剩余金额 底位在前  2位小数点,单位分 | 
|   | 
|     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转bean | 
|      * | 
|      * @param data | 
|      */ | 
|     @Override | 
|     public UserCard getBean(List<byte[]> data) { | 
|         try { | 
|             UserCard userCard = new UserCard(); | 
|             //第0块解析 | 
|             byte[] zero = data.get(0); | 
|   | 
|             userCard.cardType = HexUtil.byteToHex(zero[0]); | 
|   | 
|             byte[] arerNumberByte = new byte[4]; | 
|             System.arraycopy(zero, 1, arerNumberByte, 0, arerNumberByte.length); | 
|             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); | 
|   | 
|             byte[] surplusWaterByte = new byte[4]; | 
|             System.arraycopy(one, 4, surplusWaterByte, 0, surplusWaterByte.length); | 
|             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; | 
|   | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|             return null; | 
|         } | 
|   | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 用户卡0块 | 
|      */ | 
|     public class Zero extends BaseCard { | 
|         public byte[] toByte() { | 
|             try { | 
|                 byte[] data = new byte[16]; | 
|                 data[0] = HexUtil.hexToByte(cardType); | 
|                 //区域号 | 
|                 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[] 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[] 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) { | 
|                 e.printStackTrace(); | 
|             } | 
|   | 
|             return null; | 
|         } | 
|   | 
|     } | 
|   | 
|     /** | 
|      * 用户卡1块 | 
|      */ | 
|     public class One extends BaseCard { | 
|   | 
|         public byte[] toBytes() { | 
|             byte[] data = new byte[16]; | 
|   | 
|   | 
|             try { | 
|                 byte[] balanceBytes = new byte[4]; | 
|                 byte[] balanceDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance)); | 
|                 System.arraycopy(balanceDatas, 0, balanceBytes, 0, balanceDatas.length); | 
|                 if (balanceBytes != null) { | 
|                     System.arraycopy(balanceBytes, 0, data, 0, balanceBytes.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) { | 
|                     // 获取年、月、日、时、分、秒 | 
|                     int year = (rechargeDate.get(Calendar.YEAR)) % 1000; | 
|                     int month = rechargeDate.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1 | 
|                     int day = rechargeDate.get(Calendar.DAY_OF_MONTH); | 
|                     byte bcdYear = HexUtil.getIntToBCD(year)[0]; | 
|                     byte bcdMonth = HexUtil.getIntToBCD(month)[0]; | 
|                     byte bcdDay = HexUtil.getIntToBCD(day)[0]; | 
|                     data[12] = bcdYear; | 
|                     data[13] = bcdMonth; | 
|                     data[14] = bcdDay; | 
|                 } | 
|   | 
|                 data[15] = getByteSum(data); | 
|             } catch (Exception e) { | 
|                 e.printStackTrace(); | 
|                 CrashReport.postCatchedException(e); | 
|             } | 
|             return data; | 
|         } | 
|   | 
|   | 
|     } | 
|   | 
|     /** | 
|      * 用户卡2块 | 
|      */ | 
|     public class Two extends BaseCard { | 
|         public byte[] toBytes() { | 
|             byte[] data = new byte[16]; | 
|   | 
|   | 
|             try { | 
|                 byte[] balanceBytes = new byte[4]; | 
|                 byte[] balanceDatas = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance)); | 
|                 System.arraycopy(balanceDatas, 0, balanceBytes, 0, balanceDatas.length); | 
|                 if (balanceBytes != null) { | 
|                     System.arraycopy(balanceBytes, 0, data, 0, balanceBytes.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) { | 
|                     // 获取年、月、日、时、分、秒 | 
|                     int year = (rechargeDate.get(Calendar.YEAR)) % 1000; | 
|                     int month = rechargeDate.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1 | 
|                     int day = rechargeDate.get(Calendar.DAY_OF_MONTH); | 
|                     byte bcdYear = HexUtil.getIntToBCD(year)[0]; | 
|                     byte bcdMonth = HexUtil.getIntToBCD(month)[0]; | 
|                     byte bcdDay = HexUtil.getIntToBCD(day)[0]; | 
|                     data[12] = bcdYear; | 
|                     data[13] = bcdMonth; | 
|                     data[14] = bcdDay; | 
|                 } | 
|   | 
|   | 
|                 data[15] = getByteSum(data); | 
|             } catch (Exception e) { | 
|                 e.printStackTrace(); | 
|                 CrashReport.postCatchedException(e); | 
|             } | 
|             return data; | 
|         } | 
|     } | 
|   | 
|     public byte[] getZeroBytes() { | 
|         Zero zero = new Zero(); | 
|         return zero.toByte(); | 
|     } | 
|   | 
|     public byte[] getOneBytes() { | 
|         One zero = new One(); | 
|         return zero.toBytes(); | 
|     } | 
|   | 
|     public byte[] getTwoBytes() { | 
|         Two zero = new Two(); | 
|         return zero.toBytes(); | 
|     } | 
|   | 
|   | 
|     public String getCardType() { | 
|         return cardType; | 
|     } | 
|   | 
|     public void setCardType(String cardType) { | 
|         this.cardType = cardType; | 
|     } | 
|   | 
|     public int getArerNumber() { | 
|         return arerNumber; | 
|     } | 
|   | 
|     public void setArerNumber(int arerNumber) { | 
|         this.arerNumber = arerNumber; | 
|     } | 
|   | 
|     public String getUserCode() { | 
|         return userCode; | 
|     } | 
|   | 
|     public void setUserCode(String userCode) { | 
|         this.userCode = userCode; | 
|     } | 
|   | 
|     public int getUserCodeNumber() { | 
|         return userCodeNumber; | 
|     } | 
|   | 
|     public void setUserCodeNumber(int userCodeNumber) { | 
|         this.userCodeNumber = userCodeNumber; | 
|     } | 
|   | 
|     public int getCardWriteState() { | 
|         return cardWriteState; | 
|     } | 
|   | 
|     public void setCardWriteState(int cardWriteState) { | 
|         this.cardWriteState = cardWriteState; | 
|     } | 
|   | 
|     public int getCardState() { | 
|         return cardState; | 
|     } | 
|   | 
|     public void setCardState(int cardState) { | 
|         this.cardState = cardState; | 
|     } | 
|   | 
|     public int getBalance() { | 
|         return balance; | 
|     } | 
|   | 
|     public void setBalance(int balance) { | 
|         this.balance = balance; | 
|     } | 
|   | 
|     public int getSurplusElecticity() { | 
|         return surplusElecticity; | 
|     } | 
|   | 
|     public void setSurplusElecticity(int surplusElecticity) { | 
|         this.surplusElecticity = surplusElecticity; | 
|     } | 
|   | 
|     public int getTotalMorny() { | 
|         return totalMorny; | 
|     } | 
|   | 
|     public void setTotalMorny(int totalMorny) { | 
|         this.totalMorny = totalMorny; | 
|     } | 
|   | 
|     public Calendar getRechargeDate() { | 
|         return rechargeDate; | 
|     } | 
|   | 
|     public void setRechargeDate(Calendar rechargeDate) { | 
|         this.rechargeDate = rechargeDate; | 
|     } | 
|   | 
|     public Float getElectricPrice() { | 
|         return electricPrice; | 
|     } | 
|   | 
|     public void setElectricPrice(Float electricPrice) { | 
|         this.electricPrice = electricPrice; | 
|     } | 
|   | 
|   | 
| } |