/// <summary> /// 初始化写卡数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tbnWriteCard_Click(object sender, EventArgs e) { string d = Guid.NewGuid().ToString("N"); string password = CurrentUser.Current.PassWordKey; try { bool flag = false; string[] mary1 = StringUtil.readBlock(RFIDClass.ReadCardAndReturnStatus(password, Convert.ToInt32(1))); if (mary1[4] == "11" || mary1[4] == "12") { flag = true; password = SystemConstant.StringEmpty; mary1 = StringUtil.readBlock(RFIDClass.ReadCardAndReturnStatus(password, Convert.ToInt32(1))); } if (mary1[4] != "0") { throw new Exception(RFIDClass.ConvertMeassByStatus(Convert.ToInt32(mary1[4]))); } string cardStatus = string.IsNullOrEmpty(mary1[2])?"":mary1[2]; //判断如果长度是16 if (cardStatus.Length >= 16) { cardStatus = HelperClass.getCardStatus(cardStatus.Substring(13, 1)); } else { cardStatus = Bouwa.ITSP2V31.Model.CardTypeInfo.CardTypeInfoDefaultCardStatus.空白卡.ToString("D"); } //如果是从重新初始化的按钮进入,则跳过下面判断 if (!(Bouwa.ITSP2V31.Model.CardTypeInfo.CardTypeInfoDefaultCardStatus.空白卡.ToString("D").Equals(cardStatus) || (cardStatus.Equals(CardTypeInfo.CardTypeInfoDefaultCardStatus.已充值.ToString("D")) && string.IsNullOrEmpty(HelperClass.DecryptByString(mary1[3] == null ? "" : mary1[3].ToString()))) || flag)) { this.lblNum.Text = string.Empty; MessageBoxForm.Show("此卡非空卡,请更换卡!", MessageBoxButtons.OK); return; } if (!mary1[0].Equals(this.lblNum.Text)) { //MessageBoxForm.Show(_objSystemMessage.GetInfoByCode("ReadNoSameCard"), MessageBoxButtons.OK); MessageBoxForm.Show("读取停车卡和待写入卡不是同一张卡,不能被写入!", MessageBoxButtons.OK); return; } if (string.IsNullOrEmpty(this.lblNum.Text)) { this.lblNum.Text = string.Empty; MessageBoxForm.Show("未读取到卡内编号,请重试!", MessageBoxButtons.OK); return; } if (string.IsNullOrEmpty(this.tbxCardNum.Text)) { MessageBoxForm.Show("卡面编号不能为空,请重新输入!", MessageBoxButtons.OK); return; } //判断卡面编号输入是否合法 Regex rx = new Regex("^[\u4E00-\u9FA5]+$"); if (rx.IsMatch(this.tbxCardNum.Text.Trim())) { MessageBoxForm.Show("卡面编号必须是数字或者字母,请重新输入!", MessageBoxButtons.OK); return; } //判断输入的值是否超过预定的最大值 string maxValue = "0"; Hashtable table = new Hashtable(); CardTypeBLL _objCardTypeBLL = new CardTypeBLL(); table.Add("tcit_system", (int)CurrentUser.Current.PARKING_SYSTEM); table.Add("tcit_type", (int)CurrentUser.Current.PARK_BACKSTAGE); //判断上限分钟是否大于预设值 if (this.lblCostType.Text.Equals(Bouwa.ITSP2V31.Model.CardInfo.CardTypeInfoCostType.限时卡.ToString("D"))) { table.Add("tcit_code", CurrentUser.Current.MaxTime); maxValue = _objCardTypeBLL.GetRegisterType(table, null); int max = 0; Regex regex = new Regex("[^0-9]"); if (string.IsNullOrEmpty(this.tbxOverPlus.Text)) { MessageBoxForm.Show("剩余时间不能为空!", MessageBoxButtons.OK); return; } if (regex.IsMatch(maxValue)) { MessageBoxForm.Show("限时预设值格式不正确,无法继续充值,请联系管理员!", MessageBoxButtons.OK); return; } if (regex.IsMatch(this.tbxOverPlus.Text)) { MessageBoxForm.Show("剩余时间必须为数字,请重新输入!", MessageBoxButtons.OK); return; } max = Convert.ToInt32(this.tbxOverPlus.Text); if (Convert.ToInt32(this.tbxOverPlus.Text) > Convert.ToInt32(this.tbxMax.Text)) { MessageBoxForm.Show("剩余时间不能大于上限时间!", MessageBoxButtons.OK); return; } if (Convert.ToInt32(this.tbxOverPlus.Text) > max) { MessageBoxForm.Show("剩余时间不能大于预设最大" + maxValue + "分!", MessageBoxButtons.OK); return; } } //判断剩余次数是否大于预设值 else if (this.lblCostType.Text.Equals(Bouwa.ITSP2V31.Model.CardInfo.CardTypeInfoCostType.限次卡.ToString("D"))) { table.Add("tcit_code", CurrentUser.Current.MaxDegree); maxValue = _objCardTypeBLL.GetRegisterType(table, null); int max = 0; Regex regex = new Regex("[^0-9]"); if (string.IsNullOrEmpty(this.tbxOverPlus.Text)) { MessageBoxForm.Show("剩余次数不能为空!", MessageBoxButtons.OK); return; } if (regex.IsMatch(maxValue)) { MessageBoxForm.Show("次数预设值格式不正确,无法继续初始化,请联系管理员!", MessageBoxButtons.OK); return; } if (regex.IsMatch(this.tbxOverPlus.Text)) { MessageBoxForm.Show("剩余次数必须为数字,请重新输入!", MessageBoxButtons.OK); return; } max = Int32.Parse(this.tbxOverPlus.Text); if (Convert.ToInt32(this.tbxOverPlus.Text) > Convert.ToInt32(this.tbxMax.Text)) { MessageBoxForm.Show("剩余次数不能大于上限次数!", MessageBoxButtons.OK); return; } if (Convert.ToInt32(this.tbxOverPlus.Text) > Convert.ToInt32(maxValue)) { MessageBoxForm.Show("剩余次数不能大于预设最大" + maxValue + "次!", MessageBoxButtons.OK); return; } } //判断剩余金额是否大于预设值 else if (this.lblCostType.Text.Equals(Bouwa.ITSP2V31.Model.CardInfo.CardTypeInfoCostType.金额卡.ToString("D"))) { table.Add("tcit_code", CurrentUser.Current.MaxMoney); maxValue = _objCardTypeBLL.GetRegisterType(table, null); decimal max = new Decimal(0.00); Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); if (string.IsNullOrEmpty(this.tbxOverPlus.Text)) { MessageBoxForm.Show("剩余次数不能为空!", MessageBoxButtons.OK); return; } if (!RegDecimalSign.IsMatch(maxValue)) { MessageBoxForm.Show("次数预设值格式不正确,无法继续初始化,请联系管理员!", MessageBoxButtons.OK); return; } if (!RegDecimalSign.IsMatch(this.tbxOverPlus.Text)) { MessageBoxForm.Show("剩余次数必须为数字,请重新输入!", MessageBoxButtons.OK); return; } max = Convert.ToDecimal(this.tbxOverPlus.Text); if (Convert.ToDecimal(this.tbxOverPlus.Text) > Convert.ToDecimal(this.tbxMax.Text)) { MessageBoxForm.Show("剩余金额不能大于上限金额!", MessageBoxButtons.OK); return; } if (Convert.ToDecimal(this.tbxOverPlus.Text) > max) { MessageBoxForm.Show("剩余金额不能大于上限金额" + maxValue + "元!", MessageBoxButtons.OK); return; } } StringBuilder sb = new StringBuilder(); //初始化写入密码 RFIDClass.UpdateCardPassWordAndReturnStatus(this.lblNum.Text, 1, SystemConstant.StringEmpty, CurrentUser.Current.PassWordKey); //写入扩展的SAASID if (!"0".Equals(SystemConstant.saas_type.SAAS_TYPE.ToString("D"))) { sb.Append(StringUtil.formatDataString(Convert.ToInt32(lblExtSaasId.Text), 3)); } //写入系统编号 sb.Append(SystemConstant.system_type.CS_SYSTEM.ToString("D")); //写入剩余金额及剩余次数及剩余时间,限期卡为5个0,限时卡前面是小时数,最后一位是分钟数数 if (this.lblCostType.Text.Equals(Bouwa.ITSP2V31.Model.CardInfo.CardTypeInfoCostType.限期卡.ToString("D"))) { sb.Append(StringUtil.formatDataString(0, 5)); } else if (this.lblCostType.Text.Equals(Bouwa.ITSP2V31.Model.CardInfo.CardTypeInfoCostType.限时卡.ToString("D"))) { string hours = Convert.ToString(Convert.ToInt32(this.tbxOverPlus.Text) / 60); //根据输入分钟数,输入数字除60取余计算出小时数 string minutes = Convert.ToString(Convert.ToInt32(this.tbxOverPlus.Text) % 60 / 10); //根据输入数字除60取模,然后再除以10,得到分钟数,1代表10分钟,2代表20分钟,依次类推 sb.Append(StringUtil.formatDataString(Convert.ToInt32(hours + minutes), 5)); } else if (this.lblCostType.Text.Equals(Bouwa.ITSP2V31.Model.CardInfo.CardTypeInfoCostType.金额卡.ToString("D"))) { //格式化金额的存储,前4位保存整数,后1位保存小数点后面第一位 string money = Convert.ToDecimal(this.tbxOverPlus.Text).ToString("0.0"); sb.Append(StringUtil.formatDataString(Convert.ToInt32(money.Replace(".", "")), 5)); } else { sb.Append(StringUtil.formatDataString(Convert.ToInt32(this.tbxOverPlus.Text), 5)); } //写入卡面编号 sb.Append(StringUtil.formatDataString(this.tbxCardNum.Text, 7)); //写入第一扇区的第零块 writeBlock(1, 0, sb.ToString()); sb = new StringBuilder(); //写入生效日期 sb.Append(DateTime.Parse(this.lblEffectDate.Text).ToString("yyMMdd")); //写入最晚日期 sb.Append(DateTime.Parse(this.lblOutDate.Text).ToString("yyMMdd")); //写入扣费类型 sb.Append(this.lblCostType.Text.ToString()); //写入停车卡状态 sb.Append(Bouwa.ITSP2V31.Model.CardTypeInfo.CardTypeInfoDefaultCardStatus.已充值.ToString("D")); //写入卡类型 sb.Append(StringUtil.formatDataString(this.lblExtCardTypeId.Text.ToString(), 2)); //写入第一扇区的第一块 writeBlock(1, 1, sb.ToString()); //把最后操作时间写入第一扇区的第二块 sb = new StringBuilder(); sb.Append(DateTime.Now.ToString("yyyyMMddHHmmss")); writeBlock(1, 2, sb.ToString()); RFIDClass.IssueSound(50); //发出声音代表完成 //将该卡信息写入后台数据库 Hashtable cardInfo = new Hashtable(); //插入初始化记录 Hashtable htCardInfo = new Hashtable(); //主键 cardInfo.Add("id", Guid.NewGuid()); htCardInfo.Add("id", Guid.NewGuid().ToString()); //saasId if (!string.IsNullOrEmpty(this.lblSaasId.Text)) { cardInfo.Add("saas_id", new Guid(this.lblSaasId.Text)); htCardInfo.Add("saas_id", this.lblSaasId.Text); } //卡内编号 if (!string.IsNullOrEmpty(this.lblNum.Text)) { cardInfo.Add("card_id", this.lblNum.Text); htCardInfo.Add("card_id", Guid.Empty.ToString()); } else { MessageBoxForm.Show("未读取到卡内编号,请重试!", MessageBoxButtons.OK); return; } //卡面编号 if (!string.IsNullOrEmpty(this.tbxCardNum.Text.Trim())) { cardInfo.Add("no", StringUtil.formatDataString(this.tbxCardNum.Text, 7)); htCardInfo.Add("no", StringUtil.formatDataString(this.tbxCardNum.Text, 7)); } else { MessageBoxForm.Show("卡面编号不能为空,请重新输入!", MessageBoxButtons.OK); return; } //批次 if (!string.IsNullOrEmpty(this.tbxLotNum.Text)) { cardInfo.Add("batch", this.tbxLotNum.Text); } //停车卡类型 if (!string.IsNullOrEmpty(this.lblCardTypeId.Text)) { cardInfo.Add("card_type", _objId.ToString()); } cardInfo.Add("status", Bouwa.ITSP2V31.Model.CardTypeInfo.CardTypeInfoDefaultCardStatus.已充值.ToString("D")); //生效日期 if (!string.IsNullOrEmpty(this.lblEffectDate.Text)) { cardInfo.Add("efffect_date", Convert.ToDateTime(this.lblEffectDate.Text)); } //最晚到期 if (!string.IsNullOrEmpty(this.lblOutDate.Text)) { cardInfo.Add("end_date", Convert.ToDateTime(this.lblOutDate.Text)); } //停车卡用途 if (!string.IsNullOrEmpty(this.lblPurpose.Text)) { cardInfo.Add("purpose", Convert.ToInt32(this.lblPurpose.Text)); //cardInfo.purpose = 0; } //注册类型 if (!string.IsNullOrEmpty(this.lblSubmitType.Text)) { cardInfo.Add("submit_type", Convert.ToInt32(this.lblSubmitType.Text)); } //扣费类型 if (!string.IsNullOrEmpty(this.lblCostType.Text)) { cardInfo.Add("cost_type", Convert.ToInt32(this.lblCostType.Text)); htCardInfo.Add("cost_type", Convert.ToInt32(this.lblCostType.Text)); } //初始化操作日志,仅对非空白卡进行写数据 string message = SystemConstant.StringEmpty; //剩余操作次数 if (this.lblCostType.Text.Equals(CardTypeInfo.CardTypeInfoCostType.限次卡.ToString("D"))) { cardInfo.Add("times", Convert.ToInt32(this.tbxOverPlus.Text)); //备注信息 cardInfo.Add("memo", this.tbxCardNum.Text + "初始化成功!"); htCardInfo.Add("volume", Convert.ToInt32(this.tbxOverPlus.Text)); //htCardInfo.Add("operation_memo", String.Format(_objSystemMessage.GetInfoByCode("InitMemo").Content, this.tbxCardNum.Text, String.Format(_objSystemMessage.GetInfoByCode("InitTime").Content, this.tbxOverPlus.Text))); message = "InitTime"; } else { cardInfo.Add("times", 0); } //剩余操作金额 if (this.lblCostType.Text.Equals(CardTypeInfo.CardTypeInfoCostType.金额卡.ToString("D"))) { cardInfo.Add("money", Convert.ToDecimal(this.tbxOverPlus.Text)); //备注信息 cardInfo.Add("memo", this.tbxCardNum.Text + "初始化成功!"); htCardInfo.Add("volume", Convert.ToDecimal(this.tbxOverPlus.Text)); // htCardInfo.Add("operation_memo", String.Format(_objSystemMessage.GetInfoByCode("InitMemo").Content, this.tbxCardNum.Text, String.Format(_objSystemMessage.GetInfoByCode("InitMoney").Content, this.tbxOverPlus.Text))); message = "InitMoney"; } else { cardInfo.Add("money", 0); } //剩余计费时间 if (this.lblCostType.Text.Equals(CardTypeInfo.CardTypeInfoCostType.限时卡.ToString("D"))) { cardInfo.Add("charges_date", Convert.ToInt32(this.tbxOverPlus.Text)); //备注信息 cardInfo.Add("memo", this.tbxCardNum.Text + "初始化成功!"); htCardInfo.Add("volume", Convert.ToInt32(this.tbxOverPlus.Text)); //htCardInfo.Add("operation_memo", String.Format(_objSystemMessage.GetInfoByCode("InitMemo").Content, this.tbxCardNum.Text, String.Format(_objSystemMessage.GetInfoByCode("InitDegree").Content, this.tbxOverPlus.Text))); message = "InitDegree"; } else { cardInfo.Add("charges_date", 0); } //剩余计费时间 if (this.lblCostType.Text.Equals(CardTypeInfo.CardTypeInfoCostType.限期卡.ToString("D"))) { //备注信息 cardInfo.Add("memo", this.tbxCardNum.Text + "初始化成功!"); htCardInfo.Add("volume", Convert.ToDateTime(this.lblOutDate.Text).ToString("yyyyMMdd")); //htCardInfo.Add("operation_memo", "初始化成功!"); message = "InitDate"; } htCardInfo.Add("operation_memo_init", _objSystemMessage.GetInfoByCode("InitMemo").Content); htCardInfo.Add("operation_type_init", 1); if (this.ddlStatus.Text == CardTypeInfo.CardTypeInfoDefaultCardStatus.已充值.ToString()) { htCardInfo.Add("operation_type_payment", 5); htCardInfo.Add("operation_memo_payment", string.Format(_objSystemMessage.GetInfoByCode(message).Content, this.tbxOverPlus.Text)); } //初始化人员ID cardInfo.Add("create_user", CurrentUser.Current.UserId); htCardInfo.Add("operation_user", CurrentUser.Current.UserId); //初始化时间 cardInfo.Add("create_time", DateTime.Now); htCardInfo.Add("operation_time", DateTime.Now.ToString()); htCardInfo.Add("modity_user", CurrentUser.Current.UserId); htCardInfo.Add("modity_time", DateTime.Now.ToString()); // htCardInfo.Add("operation_type","1"); htCardInfo.Add("address_type", "1"); htCardInfo.Add("network_id", CurrentUser.Current.NetWorkID); string mes = _objCardInfoBLL.ChangeCardInitAndBack(cardInfo, htCardInfo, null, ref _objSystemMessageInfo); if ("换卡操作成功!".Equals(mes)) { MessageBoxForm.Show("停车卡[" + this.tbxCardNum.Text + "]换卡成功!", MessageBoxButtons.OK); this.Close(); } else if ("换卡操作失败!".Equals(mes)) { //写入停车卡状态 RFIDClass.UpdateCardPassWordAndReturnStatus(this.lblNum.Text, 1, password, SystemConstant.StringEmpty); writeBlock(1, 0, ""); writeBlock(1, 1, ""); writeBlock(1, 2, ""); MessageBoxForm.Show("停车卡[" + this.tbxCardNum.Text + "]换卡失败!", MessageBoxButtons.OK); } } catch (Exception ex) { //写入停车卡状态 RFIDClass.UpdateCardPassWordAndReturnStatus(this.lblNum.Text, 1, password, SystemConstant.StringEmpty); writeBlock(1, 0, ""); writeBlock(1, 1, ""); writeBlock(1, 2, ""); MessageBoxForm.Show(ex.Message, MessageBoxButtons.OK); } }
/// <summary> /// 执行卡重置 /// </summary> /// <param name="theCardTypeInfo">实体层对象</param> /// <param name="key">密码</param> /// <returns>0所有操作成功,1网络连接不成功,99是远程服务器方法存储失败,其他状态</returns> public int ResetCarInforAndCarHistoryByID(CardInfo _cardInfo, string key) { try { int cSector = 1; //保存要操作的扇区下标 //先执行密码修改 string[] mary = RFIDClass.UpdateCardPassWordAndReturnStatus(_cardInfo.card_id, cSector, key); if (mary[0] != "0") //修改密码错误就直接返回错误状态 { return(Convert.ToInt32(mary[0])); } else //成功继续往下走 { //先读卡 string[] mary1 = ConvertToBytes(RFIDClass.ReadCardAndReturnStatus(key, cSector)); //保存一份副本 string[] maryback = new string[3]; for (int i = 1; i < mary1.Length - 1; i++) { maryback[i - 1] = mary1[i]; } //传入默认密码,执行初始化写入第一扇区 RFIDClass.WriterCardAndReturnStatus(_cardInfo.card_id, "", cSector, 0, ""); RFIDClass.WriterCardAndReturnStatus(_cardInfo.card_id, "", cSector, 1, ""); int p = RFIDClass.WriterCardAndReturnStatus(_cardInfo.card_id, "", cSector, 2, ""); if (p != 0) //如果出现异常就执行返回 { return(p); } else //如果初始化卡成功,则执行下一步数据库录入 { Dictionary <string, object> arr = new Dictionary <string, object>(); arr["ICode"] = "ITSP2_RFID_CS_ResetCarInforAndCarHistory"; arr["IVer"] = "1.0.0.1"; arr["card_id"] = _cardInfo.card_id; arr["no"] = _cardInfo.no; arr["modify_user"] = _cardInfo.modify_user; DataTable table = ConvertMethod.getDateTable("msgTable", arr); string Parameter = ConvertMethod.CDataToXml(table); //加密xml进行返回 Parameter = HelperClass.EncryptDESByKey8(Parameter); string resoue = _objICardInfoDAL.ResetCarInforAndCarHistoryByID(Parameter); //先解密成xml格式 string str = HelperClass.DecryptDESByKey8(resoue); //把字符串转换成表对象 DataSet parameterTable = ConvertMethod.CXmlToDataSet(str); if (parameterTable.Tables[0].Rows[0]["msgCode"].ToString() == "卡重置成功!") { return(0); //未实现,默认全部成功 } else { //否则就是失败,需要执行还原 RFIDClass.WriterCardAndReturnStatus(_cardInfo.card_id, "", cSector, 0, HelperClass.EncryptByString(maryback[0])); RFIDClass.WriterCardAndReturnStatus(_cardInfo.card_id, "", cSector, 1, HelperClass.EncryptByString(maryback[1])); RFIDClass.WriterCardAndReturnStatus(_cardInfo.card_id, "", cSector, 2, HelperClass.EncryptByString(maryback[2])); RFIDClass.UpdateCardPassWordAndReturnStatus(_cardInfo.card_id, cSector, "", key); //修改密码 return(99); } } //结束 } } catch (Exception ex) { Bouwa.Helper.Class.Log.WriterLine(Bouwa.Helper.Class.ELevel.error, "方法名:ResetCarInforAndCarHistoryByID(CardInfo _cardInfo, string key),卡信息BLL层对数据操作时出现异常!", ex.Message); return(1); } }