左晓为主开发手持机充值管理机
zuoxiao
2023-11-14 5a843ba11b991722f8f2af386f0e546e2769f7c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.dayu.recharge.card;
 
import com.dayu.recharge.tools.BcdUtil;
import com.dayu.recharge.tools.HexUtil;
import com.dayu.recharge.utils.MornyUtil;
 
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
 
/**
 * Copyright (C), 2023,
 * Author: zuo
 * Date: 2023-11-07 9:37
 * Description: 用户卡结构
 */
public class UserCard implements Serializable {
    public String cardType = "A1";//写卡标志 A1刷卡开泵前 A8刷卡开泵后  A2叠加充值
 
    public int rechargeTimes;//充值次数
 
    public short swipeNumber;//刷卡次数 一般不用精细管控才用
 
    public float electricityPrice;//电量单价 管理元可以修改
 
    public String state = "00";//卡状态 返写查询充值时卡状态位
 
    public int balance;//余额(单位为分)
 
    public String addressCode;//行政区域
 
    public Calendar rechargeDate;//购水时间
 
    public String initPeasantCode;//用户注册编号
 
 
    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();
    }
 
    /**
     * 通过byte转bean
     *
     * @param data
     */
    public static UserCard getBean(List<byte[]> data) {
        UserCard userCard = new UserCard();
        if (data != null) {
            byte[] zero = data.get(0);
            byte[] one = data.get(1);
            byte[] two = data.get(2);
            userCard.cardType = HexUtil.byteToHex(zero[0]);
            userCard.rechargeTimes = HexUtil.get16to10(HexUtil.byteToHex(zero[1]));
            byte[] swipeNumberBytes = new byte[2];
            swipeNumberBytes[0] = zero[2];
            swipeNumberBytes[1] = zero[3];
            String swipeNumberHex = HexUtil.bytesToHex(swipeNumberBytes);
            userCard.swipeNumber = (short) HexUtil.get16to10(swipeNumberHex);
            byte[] electricityPriceBytes = new byte[4];
            for (int i = 0; i < 4; i++) {
                electricityPriceBytes[i] = zero[i + 4];
            }
            userCard.electricityPrice = HexUtil.hexToFloat(HexUtil.bytesToHex(electricityPriceBytes));
            userCard.state = BcdUtil.bcdToStr(zero[8]);
            byte[] balanceBytes = new byte[4];
            for (int i = 0; i < 4; i++) {
                balanceBytes[i] = zero[i + 9];
            }
            userCard.balance = HexUtil.get16to10(HexUtil.bytesToHex(balanceBytes));
 
            byte[] addressCodeBytes = new byte[6];
            addressCodeBytes[0] = zero[13];
            addressCodeBytes[1] = zero[14];
            addressCodeBytes[2] = two[11];
            addressCodeBytes[3] = two[12];
            addressCodeBytes[4] = two[13];
            addressCodeBytes[5] = two[14];
            userCard.addressCode = BcdUtil.bcdToStr(addressCodeBytes);
 
            int year = 0;
            int month = 0;
            int day = 0;
            int hour = 0;
            int minute = 0;
            int second = 0;
            for (int i = 0; i < 6; i++) {
                byte byteData = one[i + 4];
                switch (i) {
                    case 0:
                        year = getBcdToInt(byteData);
                        break;
                    case 1:
                        month = getBcdToInt(byteData);
                        break;
                    case 2:
                        day = getBcdToInt(byteData);
                        break;
                    case 3:
                        hour = getBcdToInt(byteData);
                        break;
                    case 4:
                        minute = getBcdToInt(byteData);
                        break;
                    case 5:
                        second = getBcdToInt(byteData);
                        break;
                }
            }
            Calendar calendar = Calendar.getInstance();
            calendar.set(2000 + year, month, day, hour, minute, second);
            byte[] initPeasantCodeBytes = new byte[16];
            initPeasantCodeBytes[0] = one[10];
            initPeasantCodeBytes[1] = one[11];
            initPeasantCodeBytes[2] = one[12];
            initPeasantCodeBytes[3] = one[13];
            initPeasantCodeBytes[4] = one[14];
            initPeasantCodeBytes[5] = two[0];
            initPeasantCodeBytes[6] = two[1];
            initPeasantCodeBytes[7] = two[2];
            initPeasantCodeBytes[8] = two[3];
            initPeasantCodeBytes[9] = two[4];
            initPeasantCodeBytes[10] = two[5];
            initPeasantCodeBytes[11] = two[6];
            initPeasantCodeBytes[12] = two[7];
            initPeasantCodeBytes[13] = two[8];
            initPeasantCodeBytes[14] = two[9];
            initPeasantCodeBytes[15] = two[10];
            userCard.initPeasantCode = HexUtil.bytesToHex(initPeasantCodeBytes);
            return userCard;
        }
        return null;
    }
 
 
    private static int getBcdToInt(byte data) {
        return ((data & 0xF0) >> 4) * 10 + ((data & 0x0F));
    }
 
 
    /**
     * 用户卡0块
     */
    public class Zero extends BaseCard {
        public byte[] toByte() {
            byte[] data = new byte[16];
            data[0] = HexUtil.hexToByte(cardType);
            try {
                data[1] = HexUtil.hexToByte(HexUtil.get10to16(rechargeTimes));
            } catch (Exception e) {
                e.printStackTrace();
            }
            byte[] swipeNumbers = HexUtil.hexToByteArray(HexUtil.get10to16(swipeNumber));
            for (int i = 0; i < swipeNumbers.length; i++) {
                data[i + 2] = swipeNumbers[i];
            }
            byte[] electricityPrices = HexUtil.hexToByteArray(HexUtil.folatToHexString(electricityPrice));
            for (int i = 0; i < electricityPrices.length; i++) {
                data[i + 4] = electricityPrices[i];
            }
            try {
                data[8] = BcdUtil.strToBcd(state)[0];
            } catch (Exception e) {
                e.printStackTrace();
            }
            byte[] balances = HexUtil.hexToByteArray(HexUtil.get10to16LowHigh(balance, 8));
            for (int i = 0; i < balances.length; i++) {
                data[i + 9] = balances[i];
            }
            byte[] addresscodes = BcdUtil.strToBcd(addressCode);
            data[13] = addresscodes[0];
            data[14] = addresscodes[1];
            data[15] = getByteSum(data);
            return data;
        }
 
    }
 
    /**
     * 用户卡1块
     */
    public class One extends BaseCard {
 
        public byte[] toBytes() {
            byte[] data = new byte[16];
            byte[] balances = HexUtil.hexToByteArray(HexUtil.get10to16LowHigh(balance, 8));
            for (int i = 0; i < balances.length; i++) {
                data[i] = balances[i];
            }
            if (rechargeDate != null) {
                // 获取年、月、日、时、分、秒
 
//                bcdBytes[0] = (byte) ((year / 1000) << 4) | (byte) (year % 1000 / 100); // 年份的十位和个位(高字节)
//                bcdBytes[1] = (byte) ((year / 10 % 10) << 4) | (byte) (year % 10); // 年份的十位和个位(低字节)
                int year = (rechargeDate.get(Calendar.YEAR)) % 1000;
                int month = rechargeDate.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1
                int day = rechargeDate.get(Calendar.DAY_OF_MONTH);
                int hour = rechargeDate.get(Calendar.HOUR_OF_DAY);
                int minute = rechargeDate.get(Calendar.MINUTE);
                int second = rechargeDate.get(Calendar.SECOND);
 
                byte bcdYear = (byte) ((year / 10) << 4 | ((year % 10) / 100));
                byte bcdMonth = (byte) ((month / 10) << 4 | (month % 10));
                byte bcdDay = (byte) ((day / 10) << 4 | (day % 10));
                byte bcdHour = (byte) ((hour / 10) << 4 | (hour % 10));
                byte bcdMinute = (byte) ((minute / 10) << 4 | (minute % 10));
                byte bcdSecond = (byte) ((second / 10) << 4 | (second % 10));
                data[4] = bcdYear;
                data[5] = bcdMonth;
                data[6] = bcdDay;
                data[7] = bcdHour;
                data[8] = bcdMinute;
                data[9] = bcdSecond;
            }
            byte[] initPeasantCodes = HexUtil.hexToByteArray(initPeasantCode);
            for (int i = 0; i < 5; i++) {
                data[i + 10] = initPeasantCodes[i];
            }
            data[15] = getByteSum(data);
            return data;
        }
 
 
    }
 
    /**
     * 用户卡2块
     */
    public class Two extends BaseCard {
        public byte[] toBytes() {
            byte[] data = new byte[16];
            byte[] initPeasantCodes = HexUtil.hexToByteArray(initPeasantCode);
            for (int i = 5; i < 16; i++) {
                data[i - 5] = initPeasantCodes[i];
            }
            byte[] addresscodes = BcdUtil.strToBcd(addressCode);
            data[11] = addresscodes[2];
            data[12] = addresscodes[3];
            data[13] = addresscodes[4];
            data[14] = addresscodes[5];
            data[15] = getByteSum(data);
            return data;
        }
    }
 
 
    public String getCardType() {
        return cardType;
    }
 
    public void setCardType(String cardType) {
        this.cardType = cardType;
    }
 
    public int getRechargeTimes() {
        return rechargeTimes;
    }
 
    public void setRechargeTimes(int rechargeTimes) {
        this.rechargeTimes = rechargeTimes;
    }
 
 
    public short getSwipeNumber() {
        return swipeNumber;
    }
 
    public void setSwipeNumber(short swipeNumber) {
        this.swipeNumber = swipeNumber;
    }
 
    public float getElectricityPrice() {
        return electricityPrice;
    }
 
    public void setElectricityPrice(float electricityPrice) {
        this.electricityPrice = electricityPrice;
    }
 
    public String getState() {
        return state;
    }
 
    public void setState(String state) {
        this.state = state;
    }
 
    public int getBalance() {
        return balance;
    }
 
    public void setBalance(int balance) {
        this.balance = balance;
    }
 
    public String getAddressCode() {
        return addressCode;
    }
 
    public void setAddressCode(String addressCode) {
        this.addressCode = addressCode;
    }
 
    public Calendar getRechargeDate() {
        return rechargeDate;
    }
 
    public void setRechargeDate(Calendar rechargeDate) {
        this.rechargeDate = rechargeDate;
    }
 
    public String getInitPeasantCode() {
        return initPeasantCode;
    }
 
    public void setInitPeasantCode(String initPeasantCode) {
        this.initPeasantCode = initPeasantCode;
    }
 
 
}