package com.dayu.general.bean.card
|
|
import com.dayu.baselibrary.tools.BcdUtil
|
import com.dayu.baselibrary.tools.HexUtil
|
import java.io.Serializable
|
|
/**
|
* 取数卡
|
* 用于在用户卡丢失后,读取当前用户或挂起用户的卡内信息,作为补卡依据
|
*/
|
class FetchDataCard : BaseCard(), Serializable {
|
companion object {
|
const val CARD_TYPE_NEED_FETCH = "B1" // 需要刷卡取数
|
const val CARD_TYPE_FETCH_SUCCESS = "B2" // 刷卡取数返写成功
|
const val IDENTIFY_CODE_A0 = 0xA0.toByte() // 识别码A0
|
const val IDENTIFY_CODE_B1 = 0xB1.toByte() // 识别码B1
|
const val IDENTIFY_CODE_C2 = 0xC2.toByte() // 识别码C2
|
const val IDENTIFY_CODE_89 = 0x89.toByte() // 识别码89
|
}
|
|
var areaNumber: Int = 0 // 国家行政区域号(12位BCD,精确到村)
|
var userNumber: Int = 0 // 用户编号(HEX)
|
var cardType: String = CARD_TYPE_NEED_FETCH // 卡类型:B1需要刷卡取数,B2刷卡取数返写成功
|
var projectCode: Int = 0 // 项目编码(HEX 1-255)
|
|
/**
|
* 写卡完成后校验是否写卡成功
|
*/
|
fun equalsFetchDataCard(data: List<ByteArray>?): Boolean {
|
if (data == null || data.isEmpty()) {
|
return false
|
}
|
return data[0].contentEquals(getZeroBytes())
|
}
|
|
/**
|
* 通过byte转bean
|
*/
|
fun getBean(data: List<ByteArray>): FetchDataCard? {
|
try {
|
val fetchDataCard = FetchDataCard()
|
// 解析第0块
|
val zero = data[0]
|
|
// 解析国家行政区域号(0-5位)
|
val areaCodeBytes = zero.copyOfRange(0, 6)
|
fetchDataCard.areaNumber = BcdUtil.bcdToStr(areaCodeBytes).toInt()
|
|
// 解析用户编号(6-7位)
|
val userNumberBytes = zero.copyOfRange(6, 8)
|
fetchDataCard.userNumber = HexUtil.get16To10LowHightByBytes(userNumberBytes)
|
|
// 解析卡类型(8位)
|
val cardType = HexUtil.byteToHex(zero[8])
|
if (cardType != CARD_TYPE_NEED_FETCH && cardType != CARD_TYPE_FETCH_SUCCESS) {
|
return null
|
}
|
fetchDataCard.cardType = cardType
|
|
// 验证识别码(9-12位)
|
if (zero[9] != IDENTIFY_CODE_A0 ||
|
zero[10] != IDENTIFY_CODE_B1 ||
|
zero[11] != IDENTIFY_CODE_C2 ||
|
zero[12] != IDENTIFY_CODE_89) {
|
return null
|
}
|
|
// 解析项目编码(13位)
|
fetchDataCard.projectCode = HexUtil.get16To10LowHightByBytes(byteArrayOf(zero[13]))
|
|
return fetchDataCard
|
} catch (e: Exception) {
|
e.printStackTrace()
|
return null
|
}
|
}
|
|
/**
|
* 生成第0块数据
|
*/
|
inner class Zero : BaseCard() {
|
fun toBytes(): ByteArray {
|
val data = ByteArray(16)
|
try {
|
// 设置国家行政区域号(BCD格式,6字节,0-5位)
|
val areaCodeBytes = BcdUtil.strToBcd(String.format("%012d", areaNumber))
|
System.arraycopy(areaCodeBytes, 0, data, 0, 6)
|
|
// 设置用户编号(6-7位)
|
val userNumberBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(userNumber))
|
System.arraycopy(userNumberBytes, 0, data, 6, 2)
|
|
// 设置卡类型(8位)
|
data[8] = HexUtil.hexToByte(cardType)
|
|
// 设置识别码(9-12位)
|
data[9] = IDENTIFY_CODE_A0
|
data[10] = IDENTIFY_CODE_B1
|
data[11] = IDENTIFY_CODE_C2
|
data[12] = IDENTIFY_CODE_89
|
|
// 设置项目编码(13位)
|
data[13] = projectCode.toByte()
|
|
// 设置备用位(14位)
|
data[14] = 0x00
|
|
// 设置校验和(15位)
|
data[15] = getByteSum(data)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
}
|
return data
|
}
|
}
|
|
/**
|
* 将卡片状态更新为取数成功
|
*/
|
fun markAsFetchSuccess() {
|
cardType = CARD_TYPE_FETCH_SUCCESS
|
}
|
|
/**
|
* 检查是否已经取数成功
|
*/
|
fun isFetchSuccess(): Boolean {
|
return cardType == CARD_TYPE_FETCH_SUCCESS
|
}
|
|
fun getZeroBytes(): ByteArray = Zero().toBytes()
|
}
|