/// <summary>
        /// 讀卡
        /// </summary>
        /// <returns></returns>
        private bool ReadCard()
        {
            try
            {
                if (_IsConnected)
                {
                    CardInfo = _Reader.ReadCardInfo(_CardInfoSection, _SectionPwd);
                    if (CardInfo != null)
                    {
                        this.lblMessage.Text = "已读卡,请稍后。";
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {

                }
            }
            catch
            {

            }

            return false;
        }
        private void ReadCard()
        {
            ClearControl();

            WaitForCardReader wfcrFrm = new WaitForCardReader(this._strPwd);
            if (wfcrFrm.ShowDialog() == DialogResult.OK)
            {
                _cardInfo = wfcrFrm.CardInfo;
                ShowCardMessage();
            }
            else
            {
                this.btnSearch.Enabled = false;
            }
        }
        /// <summary>
        /// 確定換卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                bool resUkey = base.CheckUKey();
                if (!resUkey)
                {
                    return;
                }

                #region 添加换卡前对收数情况的判断,若收数未成功,则不能进行换卡操作

                ConsumeMachineMaster_cmm_Info searchMacInfo = new ConsumeMachineMaster_cmm_Info();
                searchMacInfo.cmm_cStatus = Common.DefineConstantValue.ConsumeMachineStatus.Using.ToString();
                List<ConsumeMachineMaster_cmm_Info> listMacInfos = this._IConsumeMachineBL.SearchRecords(searchMacInfo);
                listMacInfos = listMacInfos.Where(x => x.cmm_lIsActive).ToList();
                if (listMacInfos == null && listMacInfos.Count > 0)
                {
                    base.ShowErrorMessage("获取消费数据同步信息时出现异常,请重试。");
                    return;
                }
                //消费机最后同步时间
                DateTime dtMacSync = listMacInfos[0].cmm_dLastAccessTime;
                //List<ConsumeMachineMaster_cmm_Info> listUnSyncMachineInfos = listMacInfos.Where(x =>
                //          x.cmm_dLastAccessTime.Hour != dtMacSync.Hour).ToList();
                List<ConsumeMachineMaster_cmm_Info> listUnSyncMachineInfos = new List<ConsumeMachineMaster_cmm_Info>();
                foreach (ConsumeMachineMaster_cmm_Info macItem in listMacInfos)
                {
                    if (macItem != null)
                    {
                        DateTime dtAccess = macItem.cmm_dLastAccessTime;
                        if (Math.Abs((dtAccess - dtMacSync).TotalMinutes) > 30)
                        {
                            listUnSyncMachineInfos.Add(macItem);
                        }
                    }
                    else
                        continue;
                }
                List<ConsumeMachineMaster_cmm_Info> listUnConnMachineInfos = listMacInfos.Where(x =>
                        !x.cmm_lLastAccessRes).ToList();
                if ((listUnSyncMachineInfos != null && listUnSyncMachineInfos.Count > 0) || (listUnConnMachineInfos != null && listUnConnMachineInfos.Count > 0))
                {
                    if (base.ShowQuestionMessage("暂时不能进行换卡操作,因检测到有断线现象的消费机,请在恢复收集数据正常后重试。" + Environment.NewLine + "需要立即查看【消费数据收集情况】吗?"))
                    {
                        MenuItem menuClick = new MenuItem();
                        Sys_FormMaster_fom_Info formClick = new Sys_FormMaster_fom_Info();
                        formClick.fom_cExePath = "WindowUI.HHZX.ConsumerDevice.frmConsumptionRecordCollect";
                        menuClick.Tag = formClick;
                        base.ShowSubForm(menuClick, base.BaseDockPanel);
                    }
                    this.btnSave.Enabled = false;
                    this.lblReadNo.Text = "请读卡";
                    return;
                }

                #endregion

                this.btnSave.Enabled = false;
                this.lblReadNo.Text = "请读卡";

                UserCardPair_ucp_Info userPairInfo = new UserCardPair_ucp_Info();
                userPairInfo.ucp_cCardID = _CardInfo.CardSourceID;
                userPairInfo.ucp_iCardNo = int.Parse(_CardInfo.CardNo);
                //查询用户是否已拥有正常的消费卡
                List<UserCardPair_ucp_Info> listResUserPair = _IUserCardPairBL.SearchRecords(userPairInfo);
                if (userPairInfo != null)
                {
                    listResUserPair = listResUserPair.Where(x => x.ucp_cUseStatus != Common.DefineConstantValue.ConsumeCardStatus.Returned.ToString()).ToList();
                }

                if (listResUserPair != null && listResUserPair.Count > 0)
                {
                    string strMessage = string.Empty;

                    userPairInfo = listResUserPair[0] as UserCardPair_ucp_Info;

                    userPairInfo = _IUserCardPairBL.DisplayRecord(userPairInfo);

                    if (userPairInfo.CardOwner != null)
                    {
                        strMessage += userPairInfo.CardOwner.cus_cChaName + " " + userPairInfo.CardOwner.ClassName + " 卡号:" + userPairInfo.ucp_iCardNo;
                    }

                    base.ShowErrorMessage("该卡已在使用中。使用者:" + strMessage);
                    return;
                }

                if (!ShowQuestionMessage("是否确认换卡?"))
                {
                    return;
                }
                if (ShowQuestionMessage("是否需要重新设置【换卡工本费】?" + Environment.NewLine + "当前【换卡工本费】为:" + _ReplaceCost.ToString()))
                {
                    btnSetCost_Click(null, null);
                    if (!ShowQuestionMessage("【换卡工本费】更新完毕,是否需要继续进行换卡操作?"))
                    {
                        return;
                    }
                }

                userPairInfo = _CurrentUserInfo.PairInfo;
                int iOldCardNo = userPairInfo.ucp_iCardNo;
                userPairInfo.ucp_cUseStatus = DefineConstantValue.ConsumeCardStatus.Returned.ToString();
                userPairInfo.ucp_dReturnTime = System.DateTime.Now;

                if (!_IUserCardPairBL.Save(userPairInfo, DefineConstantValue.EditStateEnum.OE_Update).isError)
                {
                    ConsumeCardMaster_ccm_Info ccmInfo = new ConsumeCardMaster_ccm_Info();
                    ccmInfo.ccm_cCardID = _CardInfo.CardSourceID;

                    ccmInfo.ccm_cCardState = DefineConstantValue.CardUseState.InUse.ToString();
                    ccmInfo.ccm_lIsActive = true;
                    ccmInfo.ccm_cAdd = this.UserInformation.usm_cUserLoginID;
                    ccmInfo.ccm_dAddDate = System.DateTime.Now;
                    ccmInfo.ccm_cLast = this.UserInformation.usm_cUserLoginID;
                    ccmInfo.ccm_dLastDate = System.DateTime.Now;
                    ///如果新卡沒錄入系統,則先錄入新卡
                    if (_IConsumeCardMasterBL.DisplayRecord(ccmInfo) != null)
                    {
                        //如果新卡已在系統,則設為已使用
                        if (_IConsumeCardMasterBL.Save(ccmInfo, DefineConstantValue.EditStateEnum.OE_Update).isError)
                        {

                        }
                    }
                    else
                    {
                        //添加新卡信息
                        if (_IConsumeCardMasterBL.Save(ccmInfo, DefineConstantValue.EditStateEnum.OE_Insert).isError)
                        {
                            base.ShowErrorMessage("卡信息录入时发生错误,请再次尝试。");
                        }
                    }

                    //如果存在舊卡,將舊卡設為未使用
                    if (_CurrentUserInfo.PairInfo != null)
                    {
                        ConsumeCardMaster_ccm_Info oldCardInfo = new ConsumeCardMaster_ccm_Info();
                        oldCardInfo.ccm_cCardID = _CurrentUserInfo.PairInfo.ucp_cCardID;
                        oldCardInfo = _IConsumeCardMasterBL.DisplayRecord(oldCardInfo) as ConsumeCardMaster_ccm_Info;
                        if (oldCardInfo != null)
                        {
                            oldCardInfo.ccm_cCardState = DefineConstantValue.CardUseState.NotUsed.ToString();
                            _IConsumeCardMasterBL.Save(oldCardInfo, DefineConstantValue.EditStateEnum.OE_Update);
                        }
                    }

                    userPairInfo.ucp_cRecordID = Guid.NewGuid();
                    userPairInfo.ucp_cCardID = ccmInfo.ccm_cCardID;
                    userPairInfo.ucp_cAdd = this.UserInformation.usm_cUserLoginID;
                    userPairInfo.ucp_cLast = this.UserInformation.usm_cUserLoginID;
                    userPairInfo.ucp_dPairTime = DateTime.Now;
                    userPairInfo.ucp_dAddDate = DateTime.Now;
                    userPairInfo.ucp_dLastDate = DateTime.Now;
                    userPairInfo.ucp_cUseStatus = DefineConstantValue.ConsumeCardStatus.Normal.ToString();
                    userPairInfo.ucp_lIsActive = true;
                    userPairInfo.ucp_dReturnTime = null;

                    ReturnValueInfo returnInfo = _IUserCardPairBL.InsertExchargeCard(userPairInfo, _ReplaceCost);

                    userPairInfo = returnInfo.ValueObject as UserCardPair_ucp_Info;

                    if (returnInfo.isError)
                    {
                        base.ShowErrorMessage("换卡失败,请再次尝试。");
                        return;
                    }
                    else
                    {
                        //換卡成功,將新卡設為已使用
                        initValue();

                        UserCardPair_ucp_Info currentPair = this._IUserCardPairBL.DisplayRecord(new UserCardPair_ucp_Info() { ucp_cRecordID = userPairInfo.ucp_cRecordID }) as UserCardPair_ucp_Info;

                        string strCardName = string.Empty;

                        //抽取用户信息写入卡显示名称
                        if (currentPair.CardOwner != null)
                        {
                            CardUserMaster_cus_Info userSearch = new CardUserMaster_cus_Info() { cus_cRecordID = currentPair.CardOwner.cus_cRecordID };
                            CardUserMaster_cus_Info userInfo = this._ICardUserMasterBL.DisplayRecord(userSearch) as CardUserMaster_cus_Info;
                            if (userInfo != null)
                            {
                                if (userInfo.cus_cIdentityNum == DefineConstantValue.CodeMasterDefine.KEY2_SIOT_CardUserIdentity_Student)//学生
                                {
                                    if (userInfo.ClassInfo != null)
                                    {
                                        if (userInfo.ClassInfo.GradeInfo == null)
                                        {
                                            strCardName += userInfo.ClassInfo.csm_cClassName.Substring(0, 1) == "高" ? "G" : "C";
                                            strCardName += userInfo.ClassInfo.csm_cClassName.Substring(1, 1);
                                        }
                                        else
                                        {
                                            strCardName += userInfo.ClassInfo.GradeInfo.gdm_cAbbreviation;
                                        }

                                        string strTmpName = userInfo.cus_cChaName;
                                        if (userInfo.cus_cChaName.Length > 3)
                                        {
                                            strTmpName = userInfo.cus_cChaName.Substring(1, 3);
                                        }
                                        strCardName += strTmpName;
                                    }
                                    else
                                    {
                                        this.Cursor = Cursors.Default;
                                        base.ShowWarningMessage("用户班级信息不全,请重试。");
                                        return;
                                    }
                                }
                                else//老师
                                {
                                    string strTmpName = userInfo.cus_cChaName;
                                    if (userInfo.cus_cChaName.Length > 6)
                                    {
                                        strTmpName = strTmpName.Substring(0, 6);
                                    }
                                    strCardName = strTmpName;
                                }
                            }
                        }

                        CardUserAccount_cua_Info cuaInfo = new CardUserAccount_cua_Info();
                        cuaInfo.cua_cCUSID = userPairInfo.ucp_cCUSID;

                        cuaInfo = _ICardUserAccountBL.SearchRecords(cuaInfo).FirstOrDefault() as CardUserAccount_cua_Info;
                        _CardInfo.CardNo = userPairInfo.ucp_iCardNo.ToString();
                        _CardInfo.Name = strCardName;
                        _CardInfo.CardPwd = this._PayPwd;
                        //_cardInfo.CardBalance = cuaInfo.cua_fCurrentBalance;

                        decimal Balance = cuaInfo.cua_fCurrentBalance;

                        if (this._CurrentUserInfo != null)
                        {
                            if (this._CurrentUserInfo.cus_cIdentityNum == Common.DefineConstantValue.CodeMasterDefine.KEY2_SIOT_CardUserIdentity_Student)
                            {
                                CodeMaster_cmt_Info codeInfo = this._ICodeMasterBL.SearchRecords(new CodeMaster_cmt_Info() { cmt_cKey1 = Common.DefineConstantValue.CodeMasterDefine.KEY1_ConstantExpenses, cmt_cKey2 = Common.DefineConstantValue.CodeMasterDefine.KEY2_AdvanceCost }).FirstOrDefault() as CodeMaster_cmt_Info;
                                if (codeInfo != null)
                                {
                                    Balance = Balance + codeInfo.cmt_fNumber;
                                }
                            }
                        }

                        if (Balance < 0)
                        {
                            Balance = 0;
                        }

                        _CardInfo.CardBalance = Balance;

                        this._Reader.WriteCardInfo(this._CardInfoSection, this._SectionPwd, _CardInfo);

                        this.lblReadNo.Text = "已换卡";

                        userPairInfo = returnInfo.ValueObject as UserCardPair_ucp_Info;

                        _CurrentUserInfo.PairInfo = userPairInfo;

                        _CardInfo = null;

                        initValue();

                        ShowInformationMessage("换卡成功。");

                        ReturnValueInfo rvRemoveOld = RemoveOldCardFromWList(iOldCardNo);
                        ReturnValueInfo rvAddNew = AddNewCardToWList(userPairInfo.ucp_iCardNo);
                        if (rvRemoveOld.boolValue && rvAddNew.boolValue)
                        {
                            ShowInformationMessage("旧卡自动添加到黑名单列表成功,原卡已不能继续使用。");
                        }
                        else
                        {
                            ShowErrorMessage("旧卡自动添加到黑名单列表失败,可等待停餐服务收集名单添加或联系管理员手动添加。");
                        }
                    }
                }
            }
            catch
            {

            }
        }
        /// <summary>
        ///  读卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRead_Click(object sender, EventArgs e)
        {
            try
            {
                bool resUkey = base.CheckUKey();
                if (!resUkey)
                {
                    return;
                }

                this.btnSave.Enabled = false;
                this.lblReadNo.Text = "未读卡";
                _CardInfo = null;

                btnReaderState_Click(this.btnReaderCon, null);
                if (_IsConnected)
                {
                    this.Cursor = Cursors.WaitCursor;

                    _CardInfo = _Reader.ReadCardInfo(_CardInfoSection, _SectionPwd);
                    if (_CardInfo != null)
                    {
                        this.lblReadNo.Text = "已读卡";
                        this.btnSave.Enabled = true;
                    }
                    else
                    {
                        _CardInfo = _Reader.ReadCardInfo(_CardInfoSection, this._OrganizeSectionPwd);
                        if (_CardInfo != null)
                        {
                            ReturnValueInfo rvInfo = this._Reader.ModifySectionPwd(base._CardInfoSection, base._OrganizeSectionPwd, base._SectionPwd);
                            this.lblReadNo.Text = "已读卡";
                            this.btnSave.Enabled = true;
                        }
                        else
                        {
                            base.ShowErrorMessage("找不到卡片,请确认读卡器是否已连接或是否将卡片放在读卡器的感应区内。");
                        }

                    }
                }
                else
                {
                    base.ShowErrorMessage("找不到读卡器,请确认读卡器是否已连接。");
                }
            }
            catch
            {
                base.ShowErrorMessage("找不到卡片,请确认读卡器是否已连接或是否将卡片放在读卡器的感应区内。");

            }

            this.Cursor = Cursors.Default;
        }
        private void btnConfirmRefund_Click(object sender, EventArgs e)
        {
            bool resUkey = base.CheckUKey();
            if (!resUkey)
            {
                return;
            }

            if (string.IsNullOrEmpty(tbxRefundMoney.Text.Trim()))
            {
                this.ShowWarningMessage("请输入退款金额。");
                tbxRefundMoney.Focus();
                return;
            }
            decimal fRefund;
            bool res = decimal.TryParse(tbxRefundMoney.Text.Trim(), out fRefund);
            if (!res)
            {
                this.ShowWarningMessage("请检查退款金额的格式。");
                tbxRefundMoney.Focus();
                return;
            }
            if (fRefund <= 0)
            {
                this.ShowWarningMessage("退款金额需大于0,请重新输入。");
                tbxRefundMoney.Focus();
                return;
            }
            else if (fRefund > Common.DefineConstantValue.MaxRechargeVal)
            {
                this.ShowWarningMessage("退款金额不能大于" + Common.DefineConstantValue.MaxRechargeVal.ToString() + ",请重新输入。");
                tbxRefundMoney.Focus();
                tbxRefundMoney.SelectAll();
                return;
            }

            //帐号余额
            ConsumeCardInfo ccInfo = new ConsumeCardInfo();
            ccInfo.CardBalance = decimal.Parse(this.labAccountBalance.Text);

            //查卡号
            UserCardPair_ucp_Info ucpInfo = new UserCardPair_ucp_Info();
            ucpInfo.ucp_cCUSID = this._CurrentUser.cus_cRecordID;
            List<UserCardPair_ucp_Info> ucpList = _IUserCardPairBL.SearchRecords(ucpInfo);
            if (ucpList != null && ucpList.Count > 0)
            {
                ucpList = ucpList.OrderBy(p => p.ucp_dAddDate).ToList();
                ccInfo.CardNo = ucpList[0].ucp_iCardNo.ToString();
            }
            else
            {
                if (MessageBox.Show("该用户未发卡,确认继续进行操作?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {

                }
                else
                {
                    return;
                }
            }

            dlgConfirmInfo dlg = new dlgConfirmInfo();
            dlg.RefundMoney = fRefund;
            dlg.UserInfo = this._CurrentUser;
            dlg.PreCostMoney = computePreCost(this._CurrentUser.cus_cRecordID);
            dlg.CardInfo = ccInfo;
            dlg.IsPrinted = cbxPrint.Checked;
            dlg.IsSkipPreCost = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    PreRechargeRecord_prr_Info preRechargeInfo = new PreRechargeRecord_prr_Info();
                    preRechargeInfo.prr_cAdd = base.UserInformation.usm_cUserLoginID;
                    preRechargeInfo.prr_cLast = base.UserInformation.usm_cUserLoginID;
                    preRechargeInfo.prr_cRechargeType = Common.DefineConstantValue.ConsumeMoneyFlowType.Refund_PersonalTransfer.ToString();
                    preRechargeInfo.prr_cRecordID = Guid.NewGuid();
                    preRechargeInfo.prr_cStatus = Common.DefineConstantValue.ConsumeMoneyFlowStatus.WaitForAcceptTransfer.ToString();
                    preRechargeInfo.prr_cUserID = this._CurrentUser.cus_cRecordID;
                    preRechargeInfo.prr_dRechargeTime = DateTime.Now;
                    preRechargeInfo.prr_fRechargeMoney = fRefund;

                    ReturnValueInfo rvInfo = this._IPreRechargeRecordBL.Save(preRechargeInfo, Common.DefineConstantValue.EditStateEnum.OE_Insert);
                    if (rvInfo.boolValue && !rvInfo.isError)
                    {
                        base.ShowInformationMessage("转账成功,请通知卡用户进行退款确认。");

                        if (cbxPrint.Checked)
                        {
                            RechargeDetail rdInfo = new RechargeDetail();
                            //rdInfo.CardNo = Int32.Parse(this.labCardNo.Text);
                            rdInfo.OperationTime = System.DateTime.Now;
                            rdInfo.Operator = this.UserInformation.usm_cUserLoginID;
                            rdInfo.RechargeValue = fRefund;
                            rdInfo.UserName = this.labUserName.Text;
                            rdInfo.ClassName = this.labClassName.Text;
                            //打印小票
                            PrintTicket(rdInfo);
                        }

                    }
                    else
                    {
                        base.ShowErrorMessage("转账失败。" + rvInfo.messageText);
                    }
                }
                catch (Exception ex)
                {
                    this.Cursor = Cursors.Default;
                    this.ShowErrorMessage(ex);
                }

                ResetAllControls();
                this.Cursor = Cursors.Default;
            }
        }
        private void btnConfirmRecharge_Click(object sender, EventArgs e)
        {
            bool resUkey = base.CheckUKey();
            if (!resUkey)
            {
                return;
            }
            if (base._CurrentCardInfo == null)
            {
                this.ShowWarningMessage("请重新读卡。");
                btnRead.Focus();
                return;
            }
            if (string.IsNullOrEmpty(tbxRechargeMoney.Text.Trim()))
            {
                this.ShowWarningMessage("请输入充值金额。");
                tbxRechargeMoney.Focus();
                tbxRechargeMoney.SelectAll();
                return;
            }

            decimal fRecharge;//实际充值金额
            #region 检查实际充值金额

            bool res = decimal.TryParse(tbxRechargeMoney.Text.Trim(), out fRecharge);
            if (!res)
            {
                this.ShowWarningMessage("请检查充值金额的格式。");
                tbxRechargeMoney.Focus();
                tbxRechargeMoney.SelectAll();
                return;
            }
            if (fRecharge <= 0)
            {
                this.ShowWarningMessage("充值金额需大于0,请重新输入。");
                tbxRechargeMoney.Focus();
                tbxRechargeMoney.SelectAll();
                return;
            }
            else if (fRecharge > Common.DefineConstantValue.MaxRechargeVal)
            {
                this.ShowWarningMessage("充值金额不能大于" + Common.DefineConstantValue.MaxRechargeVal.ToString() + ",请重新输入。");
                tbxRechargeMoney.Focus();
                tbxRechargeMoney.SelectAll();
                return;
            }

            #endregion

            decimal fPreCost;//当次欠费,显示为负数
            #region 检查当次欠费

            bool resPreCost = decimal.TryParse(labPreCost.Text, out fPreCost);
            if (!resPreCost)
            {
                this.ShowWarningMessage("充值金额需大于0,请重新输入。");
                btnRead.Focus();
                return;
            }

            #endregion

            if (base._CurrentCardInfo == null)
            {
                ShowWarningMessage("原卡信息异常,请重新读卡确认。");
                btnRead.Focus();
                return;
            }
            if (this._CurrentPairInfo == null)
            {
                ShowWarningMessage("卡信息异常,请重新读卡确认。");
                btnRead.Focus();
                return;
            }
            if (base._CurrentCardUserInfo == null)
            {
                ShowWarningMessage("卡用户信息异常,请重新读卡确认");
                btnRead.Focus();
                return;
            }

            decimal fAdvanceMoney = decimal.Zero;
            RechargeRecord_rcr_Info searchAdvance = new RechargeRecord_rcr_Info();
            //searchAdvance.rcr_cCardID = this._CurrentCardInfo.CardSourceID;
            searchAdvance.rcr_cUserID = this._CurrentCardUserInfo.cus_cRecordID;
            searchAdvance.rcr_cRechargeType = Common.DefineConstantValue.ConsumeMoneyFlowType.Recharge_AdvanceMoney.ToString();
            RechargeRecord_rcr_Info advanceRecharge = this._IRechargeRecordBL.SearchRecords(searchAdvance).FirstOrDefault();
            if (advanceRecharge != null)
            {
                fAdvanceMoney = advanceRecharge.rcr_fRechargeMoney;
            }

            //确认页面
            dlgConfirmInfo dlg = new dlgConfirmInfo();
            dlg.CardInfo = base._CurrentCardInfo;
            dlg.UserInfo = base._CurrentCardUserInfo;
            dlg.IsPrinted = cbxPrint.Checked;
            dlg.RechargeMoney = fRecharge;
            dlg.AdvanceMoney = fAdvanceMoney;
            if (ckbIsSyncUnPay.Checked)
            {
                dlg.PreCostMoney = fPreCost;
            }
            else
            {
                dlg.PreCostMoney = decimal.Zero;
            }

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;

                decimal fRechargeDeduct = decimal.Zero;
                if (ckbIsSyncUnPay.Checked)
                {
                    fRechargeDeduct = fRecharge + fPreCost;//已经扣除的预付款数据
                    if (fRechargeDeduct < fRecharge)
                        fPreCost = Math.Abs(fPreCost);
                    else
                        fPreCost = 0;
                }
                else
                {
                    fRechargeDeduct = fRecharge;
                    fPreCost = 0;
                }

                //卡信息再次验证
                ConsumeCardInfo cardInfo = this._Reader.ReadCardInfo(base._CardInfoSection, base._SectionPwd);
                #region 卡片信息再次验证

                if (cardInfo == null)
                {
                    ShowWarningMessage("充值失败。" + Environment.NewLine + "无法确认充值卡信息,请重新读卡再试。");
                    this.Cursor = Cursors.Default;
                    return;
                }
                if (cardInfo.CardSourceID != dlg.CardInfo.CardSourceID)
                {
                    ShowWarningMessage("充值失败。" + Environment.NewLine + "充值卡信息已变更,请重新读卡再试。");
                    this.Cursor = Cursors.Default;
                    return;
                }

                #endregion

                //修正卡显示资料
                #region 修正卡显示资料
                try
                {
                    string strOldCardName = cardInfo.Name.Trim();
                    string strCurrentCardName = (this._CurrentPairInfo.CardOwner.ClassInfo.GradeInfo.gdm_cAbbreviation + this._CurrentPairInfo.CardOwner.cus_cChaName).Trim();
                    if (strOldCardName != strCurrentCardName)
                    {
                        ConsumeCardInfo changeCardInfo = new ConsumeCardInfo();
                        changeCardInfo.CardBalance = this._CurrentCardInfo.CardBalance;
                        changeCardInfo.CardNo = this._CurrentCardInfo.CardNo;
                        changeCardInfo.CardPwd = this._CurrentCardInfo.CardPwd;
                        changeCardInfo.CardSourceID = this._CurrentCardInfo.CardSourceID;
                        changeCardInfo.CardBalance = this._CurrentCardInfo.CardBalance;
                        changeCardInfo.Name = strCurrentCardName;

                        //年级信息发生变更,修改对应信息
                        ReturnValueInfo rvChangeCardInfo = this._Reader.WriteCardInfo(base._CardInfoSection, base._SectionPwd, changeCardInfo);
                    }
                }
                catch (Exception)
                { }
                #endregion

                //先进行【卡物理充值】
                ReturnValueInfo rvInfo = this._Reader.Recharge(base._CardInfoSection, base._SectionPwd, fRechargeDeduct);
                if (rvInfo.boolValue && !rvInfo.isError)
                {
                    //卡物理充值成功,继续进行充值记录上传操作
                    RechargeRecord_rcr_Info rechargeRecord = new RechargeRecord_rcr_Info();
                    #region 实体赋值

                    rechargeRecord.rcr_cRecordID = Guid.NewGuid();
                    rechargeRecord.rcr_cCardID = cardInfo.CardSourceID;
                    rechargeRecord.rcr_cRechargeType = DefineConstantValue.ConsumeMoneyFlowType.Recharge_PersonalRealTime.ToString();
                    rechargeRecord.rcr_cStatus = DefineConstantValue.ConsumeMoneyFlowStatus.Finished.ToString();
                    rechargeRecord.rcr_cUserID = this._CurrentCardUserInfo.cus_cRecordID;
                    rechargeRecord.rcr_dRechargeTime = DateTime.Now;//TODO:应该修改为跟服务器时间
                    rechargeRecord.rcr_fRechargeMoney = fRechargeDeduct;//扣减欠费得到的当次实际充值金额
                    rechargeRecord.PreCostMoney = fPreCost;//需要缴付的欠款金额
                    //<--卡实际充值后余额=卡当前余额+实际充值金额
                    rechargeRecord.rcr_fBalance = base._CurrentCardInfo.CardBalance + fRechargeDeduct;
                    //--->
                    rechargeRecord.IsNeedSyncAccount = ckbIsSyncUnPay.Checked;//是否需要同步账户,主要用以分别透支金额这些系统额外金额的记录登记
                    rechargeRecord.rcr_cAdd = this.UserInformation.usm_cUserLoginID;
                    rechargeRecord.rcr_cLast = rechargeRecord.rcr_cAdd;
                    rechargeRecord.rcr_dLastDate = rechargeRecord.rcr_dRechargeTime;

                    #endregion

                    //卡充值成功后,将充值信息写入充值记录表
                    rvInfo = this._IRechargeRecordBL.InsertRechargeRecord(rechargeRecord);
                    ComputePreCost(this._CurrentPairInfo);

                    if (rvInfo.boolValue && !rvInfo.isError)
                    {
                        //成功录入充值记录后提示成功
                        this.Cursor = Cursors.Default;
                        this.ShowInformationMessage("充值成功。");

                        if (cbxPrint.Checked)
                        {
                            RechargeDetail rdInfo = new RechargeDetail();
                            rdInfo.CardNo = Int32.Parse(this.labCardNo.Text);
                            rdInfo.OperationTime = System.DateTime.Now;
                            rdInfo.Operator = this.UserInformation.usm_cUserLoginID;
                            rdInfo.RechargeValue = fRecharge;
                            rdInfo.UserName = this.labUserName.Text;
                            rdInfo.ClassName = this.labClassName.Text;
                            //打印小票
                            PrintTicket(rdInfo);
                        }

                        resetAllControls();
                        this.Invoke(new DLG_GetRechargeHistory(BindRechargeList));

                        DateTime? dtSysNow = DateTime.Now;//使用系统时间作准,避免因客户机时间不同步造成操作异常
                        if (rvInfo.ValueObject != null)
                        {
                            dtSysNow = Convert.ToDateTime(rvInfo.ValueObject);
                        }
                        if (dtSysNow == null)
                        {
                            dtSysNow = DateTime.Now;
                        }
                        ResetMealPlanning(base._CurrentCardUserInfo, dtSysNow);

                        btnRead.Focus();
                        return;
                    }
                    else
                    {
                        ShowErrorMessage("上传充值记录失败,需要进行回滚扣费,请摆放好卡片后,点击确定。");
                        ConsumeCardInfo cardInfoRollback = this._Reader.ReadCardInfo(base._CardInfoSection, base._SectionPwd);
                        if (cardInfoRollback.CardSourceID != cardInfo.CardSourceID)
                        {
                            ShowWarningMessage("卡片信息不符合,请核对后重新放卡,原卡片卡号为:" + cardInfo.CardNo.ToString() + ",卡信息为:" + cardInfo.Name);
                            cardInfoRollback = this._Reader.ReadCardInfo(base._CardInfoSection, base._SectionPwd);
                            if (cardInfoRollback.CardSourceID == cardInfo.CardSourceID)
                            {
                                //录入充值记录失败,则需要将原先充入的卡金额扣除,如果卡已被取走,则需等待后台自动同步金额
                                rvInfo = this._Reader.Recharge(base._CardInfoSection, base._SectionPwd, fRechargeDeduct * -1);
                                if (rvInfo.boolValue && !rvInfo.isError)
                                {
                                    ShowInformationMessage("回滚扣费成功,可重新进行充值。");
                                    return;
                                }
                                else
                                {
                                    ShowWarningMessage("回滚扣费失败,请联系系统管理员处理问题卡片。");
                                }
                            }
                            else
                            {
                                ShowWarningMessage("核对信息失败,请联系系统管理员处理问题卡片。");
                            }
                        }
                        else
                        {
                            rvInfo = this._Reader.Recharge(base._CardInfoSection, base._SectionPwd, fRechargeDeduct * -1);
                            if (rvInfo.boolValue && !rvInfo.isError)
                            {
                                ShowInformationMessage("回滚扣费成功,可重新进行充值。");
                                return;
                            }
                            else
                            {
                                ShowWarningMessage("回滚扣费失败,请联系系统管理员处理问题卡片。");
                            }
                        }

                        resetAllControls();
                        this.Cursor = Cursors.Default;
                        this.ShowInformationMessage("充值失败。" + rvInfo.messageText);
                        btnRead.Focus();
                        return;
                    }
                }
                else
                {
                    resetAllControls();
                    this.Cursor = Cursors.Default;
                    this.ShowInformationMessage("充值失败。" + rvInfo.messageText);
                    btnRead.Focus();
                    return;
                }
            }
        }
        /// <summary>
        /// 初始化读卡信息
        /// </summary>
        void InitReadCard()
        {
            labCardNo.Text = string.Empty.PadLeft(8, '0');
            labName.Text = string.Empty;
            labPreCost.Text = "0.00";
            labRecharge.Text = "0.00";
            labTotalRecharge.Text = "0.00";
            labCardBalance.Text = "0.00";
            lvRechargeList.SetDataSource<RechargeInfo>(new List<RechargeInfo>());
            ListViewSorter sorter = new ListViewSorter(1, SortOrder.Ascending);
            lvRechargeList.ListViewItemSorter = sorter;
            lvRechargeList.Sorting = SortOrder.Ascending;

            WaitForCardReader wfcrFrm = new WaitForCardReader(this._strPwd);

            GlobalVar.OpenFormList.Add(wfcrFrm);

            if (wfcrFrm.ShowDialog() == DialogResult.OK)
            {
                this._CardInfo = wfcrFrm.CardInfo;
                if (this._CardInfo != null)
                {
                    string strCardID = this._CardInfo.CardSourceID;
                    labCardBalance.Text = this._CardInfo.CardBalance.ToString();

                    UserCardPair_ucp_Info cardPair = this._IUserCardPairBL.SearchRecords(new UserCardPair_ucp_Info()
                    {
                        ucp_iCardNo = Convert.ToInt16(this._CardInfo.CardNo),
                        ucp_cCardID = strCardID,
                        ucp_cUseStatus = Common.DefineConstantValue.ConsumeCardStatus.Normal.ToString()
                    }).FirstOrDefault();

                    if (cardPair != null)
                    {
                        if (cardPair.CardOwner != null && cardPair.CardOwner.AccountInfo != null)
                        {
                            ucMealBookingDetail.ShowData(cardPair.CardOwner);
                            labName.Text = cardPair.CardOwner.cus_cChaName;
                            labCardNo.Text = this._CardInfo.CardNo.PadLeft(8, '0');
                            this._UserInfo = cardPair.CardOwner;

                            //预充值表
                            List<PreRechargeRecord_prr_Info> listPreRechargeRecord = this._IPreRechargeRecordBL.SearchRecords(new PreRechargeRecord_prr_Info()
                            {
                                prr_cUserID = cardPair.ucp_cCUSID,
                                prr_cStatus = Common.DefineConstantValue.ConsumeMoneyFlowStatus.WaitForAcceptTransfer.ToString()
                            });
                            if (listPreRechargeRecord == null && (listPreRechargeRecord != null && listPreRechargeRecord.Count > 0))
                            {
                                base.MessageDialog("提示", "没有可用的预充值的记录。");
                                return;
                            }

                            //转账列表
                            List<RechargeRecord_rcr_Info> listRechargeRecord = new List<RechargeRecord_rcr_Info>();
                            foreach (PreRechargeRecord_prr_Info item in listPreRechargeRecord)
                            {
                                RechargeRecord_rcr_Info rechrageInfo = new RechargeRecord_rcr_Info();
                                rechrageInfo.rcr_cAdd = "tqs";
                                rechrageInfo.rcr_cCardID = this._CardInfo.CardSourceID;
                                rechrageInfo.rcr_cLast = "tqs";
                                rechrageInfo.rcr_cRechargeType = item.prr_cRechargeType;
                                rechrageInfo.rcr_cRecordID = Guid.NewGuid();
                                rechrageInfo.rcr_cStatus = Common.DefineConstantValue.ConsumeMoneyFlowStatus.Finished.ToString();
                                rechrageInfo.rcr_cUserID = item.prr_cUserID;
                                rechrageInfo.rcr_dLastDate = DateTime.Now;
                                rechrageInfo.rcr_dRechargeTime = item.prr_dRechargeTime;
                                rechrageInfo.rcr_fBalance = 0;
                                rechrageInfo.rcr_fRechargeMoney = item.prr_fRechargeMoney;

                                item.prr_cRCRID = rechrageInfo.rcr_cRecordID;

                                listRechargeRecord.Add(rechrageInfo);
                            }
                            this._ListPreRechargeRecord = listPreRechargeRecord;
                            this._ListRechargeRecord = listRechargeRecord;

                            List<RechargeInfo> listRechargeInfo = getRechargeInfo(listRechargeRecord);
                            if (listRechargeInfo != null)
                            {
                                listRechargeInfo = listRechargeInfo.OrderByDescending(x => x.RechargeTime).ToList();
                                lvRechargeList.SetDataSource<RechargeInfo>(listRechargeInfo);
                                decimal dRecharge = listRechargeInfo.Sum(x => x.RechargeMoney);
                                if (dRecharge != 0)
                                {
                                    labRecharge.Text = dRecharge.ToString();
                                }
                            }

                            //未结算的预消费列表
                            List<PreConsumeRecord_pcs_Info> listPreCost = this._IPreConsumeRecordBL.SearchRecords(new PreConsumeRecord_pcs_Info() { pcs_cAccountID = cardPair.CardOwner.AccountInfo.cua_cRecordID });
                            if (listPreCost != null)
                            {
                                listPreCost = listPreCost.Where(x =>
                                    x.pcs_cConsumeType != Common.DefineConstantValue.ConsumeMoneyFlowType.IndeterminateCost.ToString()
                                    && x.pcs_cConsumeType != Common.DefineConstantValue.ConsumeMoneyFlowType.AdvanceMealCost.ToString()
                                    && x.pcs_cConsumeType != Common.DefineConstantValue.ConsumeMoneyFlowType.HedgeFund.ToString()
                                    && x.pcs_lIsSettled == false
                                    && x.pcs_cUserID == cardPair.CardOwner.cus_cRecordID
                                    ).ToList();
                                labPreCost.Text = Math.Round(listPreCost.Sum(x => x.pcs_fCost), 2).ToString();
                            }
                            else
                            {
                                labPreCost.Text = "0.00";
                            }

                            decimal fTotalRecharge = decimal.Parse(labRecharge.Text) + decimal.Parse(labPreCost.Text);
                            decimal fActualRecharge = decimal.Parse(labRecharge.Text) + decimal.Parse(labPreCost.Text) + decimal.Parse(labCardBalance.Text);
                            labTotalRecharge.Text = fTotalRecharge.ToString();
                            if (fActualRecharge >= decimal.Zero)
                                btnRecharge.Enabled = true;
                            else
                                btnRecharge.Enabled = false;
                        }
                        else
                        {
                            base.MessageDialog("提示", "本卡持卡人信息异常!");
                            return;
                        }
                    }
                    else
                    {
                        base.MessageDialog("提示", "未找到此卡或此卡不能進行充值!");
                        return;
                    }
                }
                else
                {
                    base.MessageDialog("提示", "读卡失败,请重试。" + Environment.NewLine + "(如多次失败,卡片有可能已损坏,请及时申请更换)");
                    return;
                }
            }
        }
        private void btnRecharge_Click(object sender, EventArgs e)
        {
            dlgConfirmInfo dlg = new dlgConfirmInfo();
            dlg.CardInfo = this._CardInfo;
            dlg.UserInfo = this._UserInfo;
            dlg.RechargeMoney = decimal.Parse(labTotalRecharge.Text);
            dlg.PreCostMoney = decimal.Parse(labPreCost.Text);

            GlobalVar.OpenFormList.Add(dlg);

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.Cursor = Cursors.WaitCursor;
                this._Reader = PaymentReaderFactory.CreateWriter(PaymentReaderFactory.EastRiverReader906);
                this._Reader.Conn();

                //卡信息再次验证
                ConsumeCardInfo cardInfo = this._Reader.ReadCardInfo(base._CardInfoSection, base._SectionPwd);
                if (cardInfo == null)
                {
                    base.MessageDialog("提示", "转账失败。" + Environment.NewLine + "无法确认卡信息,请重新读卡再试。");
                    this.Cursor = Cursors.Default;
                    return;
                }
                if (cardInfo.CardSourceID != dlg.CardInfo.CardSourceID)
                {
                    base.MessageDialog("提示", "转账失败。" + Environment.NewLine + "卡信息已变更,请重新读卡再试。");
                    this.Cursor = Cursors.Default;
                    return;
                }

                decimal fTotalRecharge = decimal.Parse(labTotalRecharge.Text);
                decimal fPreCost = dlg.PreCostMoney;

                try
                {
                    string strOldCardName = cardInfo.Name.Trim();
                    string strCurrentCardName = (this._UserInfo.ClassInfo.GradeInfo.gdm_cAbbreviation + this._UserInfo.cus_cChaName).Trim();
                    if (strOldCardName != strCurrentCardName)
                    {
                        ConsumeCardInfo changeCardInfo = new ConsumeCardInfo();
                        changeCardInfo.CardBalance = this._CardInfo.CardBalance;
                        changeCardInfo.CardNo = this._CardInfo.CardNo;
                        changeCardInfo.CardPwd = this._CardInfo.CardPwd;
                        changeCardInfo.CardSourceID = this._CardInfo.CardSourceID;
                        changeCardInfo.CardBalance = this._CardInfo.CardBalance;
                        changeCardInfo.Name = strCurrentCardName;

                        //年级信息发生变更,修改对应信息
                        ReturnValueInfo rvChangeCardInfo = this._Reader.WriteCardInfo(base._CardInfoSection, base._SectionPwd, changeCardInfo);
                    }
                }
                catch (Exception)
                { }

                //先进行物理卡充值
                ReturnValueInfo rvInfo = this._Reader.Recharge(base._CardInfoSection, base._SectionPwd, dlg.RechargeMoney);
                if (rvInfo.boolValue && !rvInfo.isError)
                {
                    //卡充值成功后,将原充值记录的状态更新为已完成
                    if (this._ListRechargeRecord != null)
                    {
                        for (int i = 0; i < this._ListRechargeRecord.Count; i++)
                        {
                            if (this._ListRechargeRecord[i] != null)
                            {
                                this._ListRechargeRecord[i].rcr_cStatus = Common.DefineConstantValue.ConsumeMoneyFlowStatus.Finished.ToString();

                                fPreCost += this._ListRechargeRecord[i].rcr_fRechargeMoney;
                                if (fPreCost < 0)
                                {
                                    //未将未结算预付款扣除完毕,余额不变
                                    if (i != this._ListRechargeRecord.Count - 1)
                                    {
                                        this._ListRechargeRecord[i].rcr_fBalance = this._CardInfo.CardBalance;
                                    }
                                    else
                                    {
                                        this._ListRechargeRecord[i].rcr_fBalance = this._CardInfo.CardBalance + fPreCost;
                                    }
                                }
                                else
                                {
                                    this._ListRechargeRecord[i].rcr_fBalance = this._CardInfo.CardBalance + Math.Abs(fPreCost);
                                }

                                this._ListRechargeRecord[i].rcr_dRechargeTime = DateTime.Now.AddSeconds(i);
                            }
                        }

                        decimal fPreCostRecharge = dlg.PreCostMoney;
                        if (fPreCostRecharge != 0)
                        {
                            fPreCostRecharge = Math.Abs(fPreCostRecharge);
                        }
                        rvInfo = this._IRechargeRecordBL.UpdateRechargeRecord(this._ListPreRechargeRecord, this._ListRechargeRecord, fPreCostRecharge);
                    }
                    else
                    {
                        rvInfo.messageText = "转账缓存信息异常,请重新打开本窗口。";
                        rvInfo.isError = true;
                    }

                    if (rvInfo.boolValue && !rvInfo.isError)
                    {
                        try
                        {
                            IMealBookingHistoryBL mealBl = MasterBLLFactory.GetBLL<IMealBookingHistoryBL>(MasterBLLFactory.MealBookingHistory);
                            MealBookingHistory_mbh_Info history = new MealBookingHistory_mbh_Info();
                            history.mbh_cTargetType = Common.DefineConstantValue.MealType.DebtAuto.ToString();
                            history.mbh_cTargetID = this._UserInfo.cus_cRecordID;
                            history.mbh_dMealDate = DateTime.Now.Date;
                            List<MealBookingHistory_mbh_Info> listHistory = mealBl.SearchRecords(history);
                            if (listHistory != null && listHistory.Count > 0)
                            {
                                DateTime? dtSysNow = DateTime.Now;
                                if (rvInfo.ValueObject != null)
                                {
                                    dtSysNow = Convert.ToDateTime(rvInfo.ValueObject);
                                }
                                if (dtSysNow == null)
                                {
                                    dtSysNow = DateTime.Now;
                                }
                                localLog.WriteLog(this._UserInfo.cus_cChaName + "转账完毕。时间:" + dtSysNow.ToString(), string.Empty, SystemLog.SystemLog.LogType.Trace);

                                ResetMealPlanning(this._UserInfo, dtSysNow.Value, listHistory);

                            }
                        }
                        catch (Exception exSub)
                        {
                            localLog.WriteLog(exSub.Message, string.Empty, SystemLog.SystemLog.LogType.Error);
                        }

                        //成功录入充值记录后提示成功
                        this.Cursor = Cursors.Default;
                        base.MessageDialog("提示", "充值成功。");

                        InitReadCard();
                    }
                    else
                    {
                        //录入充值记录失败,则需要将原先充入的卡金额扣除,如果卡已被取走,则需等待后台自动同步金额
                        rvInfo = this._Reader.Recharge(base._CardInfoSection, base._SectionPwd, dlg.RechargeMoney * -1);

                        this.Cursor = Cursors.Default;
                        base.MessageDialog("提示", "充值失败。" + Environment.NewLine + rvInfo.messageText);
                        return;
                    }
                }
                else
                {
                    this.Cursor = Cursors.Default;
                    base.MessageDialog("提示", "充值失败。" + Environment.NewLine + rvInfo.messageText);

                    return;
                }
            }
        }
 public override ReturnValueInfo WriteCardInfo(short iCardSection, string strSectionPwd, ConsumeCardInfo cardInfo)
 {
     ReturnValueInfo rvInfo = new ReturnValueInfo();
     try
     {
         rvInfo.boolValue = this._Reader.ER_WriteCardInfo(iCardSection, strSectionPwd, cardInfo);
     }
     catch (Exception ex)
     {
         rvInfo.messageText = ex.Message;
         rvInfo.isError = true;
     }
     return rvInfo;
 }
        /// <summary>
        /// 写卡信息
        /// </summary>
        /// <param name="iSectionNo">资料扇区号</param>
        /// <param name="strCardBlockPwd">扇区密码</param>
        /// <param name="cardInfo">写入的卡资料</param>
        /// <returns></returns>
        public bool ER_WriteCardInfo(short iSectionNo, string strCardBlockPwd, ConsumeCardInfo cardInfo)
        {
            bool res = false;
            try
            {
                if (cardInfo != null)
                {
                    string strXML = string.Empty;
                    strXML = @"<EastRiver><V1CardInfo>" +
                                     "<AsectorNo>" + iSectionNo.ToString() + "</AsectorNo>" +
                                     "<PwdA>" + strCardBlockPwd + "</PwdA>" +
                                     "<CardNo>" + cardInfo.CardNo + "</CardNo>" +
                                     "<CardName>" + cardInfo.Name + "</CardName>" +
                                     "<CardBalance>" + ((int)(cardInfo.CardBalance * 10)).ToString() + "</CardBalance>" +
                                     "<ChargeTimes>" + cardInfo.ConsumeTimes.ToString() + "</ChargeTimes>";
                    res = WriteER690CardXML(strXML);
                    return res;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return res;
        }
        /// <summary>
        /// 读取卡片信息
        /// </summary>
        /// <param name="iSectionNo">资料扇区号</param>
        /// <param name="strCardBlockPwd">卡片扇区密码</param>
        /// <returns></returns>
        public ConsumeCardInfo ER_ReadCardInfo(short iSectionNo, string strCardBlockPwd)
        {
            ConsumeCardInfo cardInfo = null;
            try
            {
                StringBuilder sbReadXML = new StringBuilder(5120);
                bool res = ReadER690CardXML(iSectionNo, strCardBlockPwd, sbReadXML);

                if (res)
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(sbReadXML.ToString());

                    cardInfo = new ConsumeCardInfo();
                    cardInfo.CardNo = xmlDoc.DocumentElement.GetElementsByTagName("CardNo")[0].InnerText;
                    cardInfo.CardBalance = Convert.ToDecimal(xmlDoc.DocumentElement.GetElementsByTagName("CardBalance")[0].InnerText) / 10;
                    cardInfo.Name = xmlDoc.DocumentElement.GetElementsByTagName("CardName")[0].InnerText;
                    cardInfo.ConsumeTimes = Convert.ToInt32(xmlDoc.DocumentElement.GetElementsByTagName("ChargeTimes")[0].InnerText);
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return cardInfo;
        }
        /// <summary>
        /// 单卡发卡逻辑
        /// </summary>
        /// <param name="userID">用户信息记录ID</param>
        /// <param name="fNewCardCost">新卡工本费</param>
        /// <param name="fAdvanceCost">可透支额</param>
        /// <returns></returns>
        ReturnValueInfo pairCardLogic(Guid userID, decimal fNewCardCost, decimal fAdvanceCost)
        {
            ReturnValueInfo rvInfo = new ReturnValueInfo();
            try
            {
                ConsumeCardInfo sourceCardInfo = this._Reader.ReadCardInfo(base._CardInfoSection, base._SectionPwd);
                if (sourceCardInfo == null)
                {
                    sourceCardInfo = this._Reader.ReadCardInfo(base._CardInfoSection, base._OrganizeSectionPwd);
                    if (sourceCardInfo != null)
                    {
                        rvInfo = this._Reader.ModifySectionPwd(base._CardInfoSection, base._OrganizeSectionPwd, base._SectionPwd);
                    }
                }

                if (sourceCardInfo == null)
                {
                    rvInfo.messageText = "卡片已损坏,请重试。";
                    rvInfo.isError = true;
                    return rvInfo;
                }

                string strCardID = sourceCardInfo.CardSourceID;
                if (strCardID == null)
                {
                    rvInfo.messageText = "卡片已损坏或摆放位置不正确,请重试。";
                    rvInfo.isError = true;
                    return rvInfo;
                }

                ConsumeCardMaster_ccm_Info cardSearch = null;
                ConsumeCardMaster_ccm_Info cardInfo = new ConsumeCardMaster_ccm_Info();
                cardInfo.ccm_cCardID = strCardID;

                // *********1、搜索卡主档,检查是否存在此卡资料*********
                #region 卡主档信息检查
                cardSearch = this._IConsumeCardMasterBL.DisplayRecord(cardInfo) as ConsumeCardMaster_ccm_Info;
                if (cardSearch == null)
                {
                    //无相应卡主档记录则先添加此卡
                    cardInfo.ccm_cCardState = DefineConstantValue.CardUseState.NotUsed.ToString();
                    cardInfo.ccm_cAdd = base.UserInformation.usm_cUserLoginID;
                    cardInfo.ccm_dAddDate = DateTime.Now;
                    cardInfo.ccm_cLast = cardInfo.ccm_cAdd;
                    cardInfo.ccm_dLastDate = cardInfo.ccm_dAddDate;

                    rvInfo = this._IConsumeCardMasterBL.Save(cardInfo, DefineConstantValue.EditStateEnum.OE_Insert);
                    if (!rvInfo.boolValue || rvInfo.isError)
                    {
                        rvInfo.messageText = "录入本卡信息失败,请重试。";
                        rvInfo.isError = true;
                        return rvInfo;
                    }
                }

                #endregion

                try
                {
                    //*********2、插入发卡信息************
                    UserCardPair_ucp_Info pairInfo = new UserCardPair_ucp_Info();

                    #region 检查是否已发卡

                    pairInfo = new UserCardPair_ucp_Info();
                    pairInfo.ucp_cCUSID = userID;
                    List<UserCardPair_ucp_Info> listSearch = this._IUserCardPairBL.SearchRecords(pairInfo);
                    listSearch = listSearch.Where(x => x.ucp_cUseStatus != Common.DefineConstantValue.ConsumeCardStatus.Returned.ToString()).ToList();
                    if (listSearch.Count > 0)
                    {
                        //此人已发卡
                        rvInfo.messageText = "此用户已被发卡。" + Environment.NewLine + "拥有卡号:" + listSearch[0].ucp_iCardNo.ToString();
                        return rvInfo;
                    }

                    pairInfo = new UserCardPair_ucp_Info();
                    pairInfo.ucp_iCardNo = int.Parse(sourceCardInfo.CardNo);
                    pairInfo.ucp_cCardID = cardInfo.ccm_cCardID;
                    listSearch = this._IUserCardPairBL.SearchRecords(pairInfo);
                    listSearch = listSearch.Where(x => x.ucp_cUseStatus != Common.DefineConstantValue.ConsumeCardStatus.Returned.ToString()).ToList();
                    if (listSearch.Count > 0)
                    {
                        //此卡已发卡
                        rvInfo.messageText = "此卡已被发卡。" + Environment.NewLine + "卡拥有人:" + listSearch[0].CardOwner.cus_cStudentID + " " + listSearch[0].CardOwner.cus_cChaName;
                        return rvInfo;
                    }

                    #endregion

                    pairInfo = new UserCardPair_ucp_Info();
                    pairInfo.ucp_cRecordID = Guid.NewGuid();
                    pairInfo.ucp_cCardID = cardInfo.ccm_cCardID;
                    pairInfo.ucp_cCUSID = userID;
                    pairInfo.ucp_dPairTime = DateTime.Now;
                    pairInfo.ucp_cUseStatus = Common.DefineConstantValue.ConsumeCardStatus.Normal.ToString();
                    pairInfo.ucp_cAdd = base.UserInformation.usm_cUserLoginID;
                    pairInfo.ucp_dAddDate = pairInfo.ucp_dPairTime;
                    pairInfo.ucp_cLast = pairInfo.ucp_cAdd;
                    pairInfo.ucp_dLastDate = pairInfo.ucp_dAddDate;

                    rvInfo = this._IUserCardPairBL.InsertNewCard(pairInfo, fNewCardCost);

                    if (rvInfo.boolValue && !rvInfo.isError)
                    {
                        // *********3、写入卡物理信息*********
                        #region 发卡

                        UserCardPair_ucp_Info currentPair = this._IUserCardPairBL.DisplayRecord(new UserCardPair_ucp_Info()
                        {
                            ucp_cRecordID = pairInfo.ucp_cRecordID
                        });

                        CardUserMaster_cus_Info userInfo = null;

                        //抽取用户资料【写入卡显示名称】
                        string strCardName = string.Empty;//卡片显示信息
                        #region 写入卡显示信息

                        if (currentPair.CardOwner != null)
                        {
                            CardUserMaster_cus_Info userSearch = new CardUserMaster_cus_Info()
                            {
                                cus_cRecordID = currentPair.CardOwner.cus_cRecordID
                            };
                            userInfo = this._ICardUserMasterBL.DisplayRecord(userSearch);

                            if (userInfo != null)
                            {
                                #region 判断身份

                                if (userInfo.cus_cIdentityNum == DefineConstantValue.CodeMasterDefine.KEY2_SIOT_CardUserIdentity_Student)//学生
                                {
                                    if (userInfo.ClassInfo != null)
                                    {
                                        if (userInfo.ClassInfo.GradeInfo == null)
                                        {
                                            strCardName += userInfo.ClassInfo.csm_cClassName.Substring(0, 1) == "高" ? "G" : "C";
                                            strCardName += userInfo.ClassInfo.csm_cClassName.Substring(1, 1);
                                        }
                                        else
                                        {
                                            strCardName += userInfo.ClassInfo.GradeInfo.gdm_cAbbreviation;
                                        }

                                        string strTmpName = userInfo.cus_cChaName;
                                        if (userInfo.cus_cChaName.Length > 3)
                                        {
                                            strTmpName = userInfo.cus_cChaName.Substring(1, 3);
                                        }
                                        strCardName += strTmpName;
                                    }
                                    else
                                    {
                                        rvInfo.messageText = "用户班级信息不全,请重试。";
                                        rvInfo.isError = true;
                                        return rvInfo;
                                    }
                                }
                                else if (userInfo.cus_cIdentityNum == DefineConstantValue.CodeMasterDefine.KEY2_SIOT_CardUserIdentity_Staff)//老师
                                {
                                    string strTmpName = userInfo.cus_cChaName;
                                    if (userInfo.cus_cChaName.Length > 6)
                                    {
                                        strTmpName = strTmpName.Substring(0, 6);
                                    }
                                    strCardName = strTmpName;
                                }
                                else
                                {
                                    rvInfo.messageText = "有未知身份的用户。" + Environment.NewLine + userInfo.cus_cStudentID + " " + userInfo.cus_cChaName;
                                    rvInfo.isError = true;
                                    return rvInfo;
                                }

                                #endregion

                                ConsumeCardInfo writeCardInfo = new ConsumeCardInfo() { CardBalance = 0, CardNo = currentPair.ucp_iCardNo.ToString(), CardPwd = base._PayPwd, ConsumeTimes = 0, Name = strCardName };
                                //写入卡显示信息
                                rvInfo = this._Reader.WriteCardInfo(base._CardInfoSection, base._SectionPwd, writeCardInfo);
                                if (!rvInfo.boolValue || rvInfo.isError)
                                {
                                    rvInfo = this._IUserCardPairBL.Save(pairInfo, DefineConstantValue.EditStateEnum.OE_Delete);
                                    if (rvInfo.boolValue && rvInfo.isError)
                                    {
                                        rvInfo.messageText = "写入用户卡信息时出现异常,当前发卡记录已重置,请重新进行发卡操作。";
                                        rvInfo.isError = true;
                                        return rvInfo;
                                    }
                                    else
                                    {
                                        rvInfo.messageText = "写入用户卡信息时出现异常,当前发卡记录未能重置,请联系系统管理员重置发卡记录后重新发卡。";
                                        rvInfo.isError = true;
                                        return rvInfo;
                                    }
                                }
                            }
                            else
                            {
                                rvInfo.messageText = "用户信息异常,请检查该用户信息后重试。";
                                rvInfo.isError = true;
                                return rvInfo;
                            }
                        }

                        #endregion

                        if (userInfo.cus_cIdentityNum == DefineConstantValue.CodeMasterDefine.KEY2_SIOT_CardUserIdentity_Student)
                        {
                            #region 学生发卡,需预充透支金额

                            //插入发卡信息后,充入透支金额
                            rvInfo = this._Reader.Recharge((short)base._CardInfoSection, base._SectionPwd, fAdvanceCost);

                            if (rvInfo.boolValue && !rvInfo.isError)
                            {
                                RechargeRecord_rcr_Info record = new RechargeRecord_rcr_Info();

                                record.rcr_cRecordID = Guid.NewGuid();
                                record.rcr_cCardID = cardInfo.ccm_cCardID;
                                record.rcr_cRechargeType = DefineConstantValue.ConsumeMoneyFlowType.Recharge_AdvanceMoney.ToString();
                                record.rcr_cStatus = DefineConstantValue.ConsumeMoneyFlowStatus.Finished.ToString();
                                record.rcr_cUserID = userID;
                                record.rcr_dRechargeTime = DateTime.Now;
                                record.rcr_cAdd = base.UserInformation.usm_cUserLoginID;
                                record.rcr_cLast = record.rcr_cAdd;
                                record.rcr_dLastDate = record.rcr_dRechargeTime;
                                record.rcr_fRechargeMoney = fAdvanceCost;

                                //成功充值后,将充值信息计入相应记录表
                                rvInfo = this._IRechargeRecordBL.Save(record, DefineConstantValue.EditStateEnum.OE_Insert);
                                if (!rvInfo.boolValue || rvInfo.isError)
                                {
                                    //卡充值失败,将原金额扣除
                                    rvInfo = this._Reader.Recharge((short)base._CardInfoSection, base._SectionPwd, fAdvanceCost * -1);

                                    rvInfo.messageText = "发卡成功,保存预充透支金额失败。";
                                    rvInfo.isError = true;
                                    return rvInfo;
                                }

                                ConsumeCardInfo cardCurrentInfo = this._Reader.ReadCardInfo(base._CardInfoSection, base._SectionPwd);
                                string strContent = "发卡成功。" + Environment.NewLine + "学生姓名:" + userInfo.cus_cChaName;
                                if (cardCurrentInfo != null)
                                {
                                    strContent += Environment.NewLine + "卡可用余额:¥" + cardCurrentInfo.CardBalance.ToString() + "元。";
                                    strContent += Environment.NewLine + "(包含可透支金额:¥" + fAdvanceCost.ToString() + "元)";
                                    strContent += Environment.NewLine + "新卡工本费:¥" + fNewCardCost.ToString() + "元。";
                                }

                                rvInfo.messageText = strContent;
                            }//CardRecharge
                            else
                            {
                                rvInfo.messageText = "发卡成功,消费卡充入预支款失败。";
                                rvInfo.isError = true;
                                return rvInfo;
                            }

                            #endregion
                        }
                        else if (userInfo.cus_cIdentityNum == DefineConstantValue.CodeMasterDefine.KEY2_SIOT_CardUserIdentity_Staff)
                        {
                            #region 老师卡发卡

                            rvInfo.messageText = "发卡成功。" + Environment.NewLine
                                + "老师姓名:" + userInfo.cus_cChaName + "。" + Environment.NewLine
                                + "新卡工本费:" + fNewCardCost.ToString() + "。";

                            #endregion
                        }

                        #endregion
                        rvInfo.ValueObject = currentPair;

                        //*********4、查询是否有预充值款项,有则进行预充值*********
                        //查找该用户是否有【转账充值】款项
                        List<PreRechargeRecord_prr_Info> listPreRecharge = this._IPreRechargeRecordBL.SearchRecords(new PreRechargeRecord_prr_Info()
                        {
                            prr_cUserID = userID,
                            prr_cStatus = Common.DefineConstantValue.ConsumeMoneyFlowStatus.WaitForAcceptTransfer.ToString()
                        });
                        if (listPreRecharge != null && listPreRecharge.Count > 0)
                        {
                            #region 预充值

                            //查找该用户是否有未付的【预消费】款项
                            decimal fSumPreCost = decimal.Zero;//未结算消费总额
                            List<PreConsumeRecord_pcs_Info> listPreCost = this._IPreConsumeRecordBL.SearchRecords(new PreConsumeRecord_pcs_Info()
                            {
                                pcs_cUserID = userID,
                            });
                            if (listPreCost != null)
                            {
                                listPreCost = listPreCost.Where(x =>
                                    x.pcs_lIsSettled == false
                                    && x.pcs_cConsumeType != Common.DefineConstantValue.ConsumeMoneyFlowType.IndeterminateCost.ToString()
                                    && x.pcs_cConsumeType != Common.DefineConstantValue.ConsumeMoneyFlowType.AdvanceMealCost.ToString()
                                    && x.pcs_cConsumeType != Common.DefineConstantValue.ConsumeMoneyFlowType.HedgeFund.ToString()
                                    ).ToList();
                                if (listPreCost.Count > 0)
                                {
                                    fSumPreCost = listPreCost.Sum(x => x.pcs_fCost);
                                }
                            }

                            ConsumeCardInfo CardInfo = this._Reader.ReadCardInfo(base._CardInfoSection, base._SectionPwd);
                            if (CardInfo == null)
                            {
                                rvInfo.messageText += Environment.NewLine + "确认卡片信息失败,无法继续进行预充值操作。";
                                rvInfo.boolValue = false;
                                return rvInfo;
                            }
                            decimal fCardBalance = CardInfo.CardBalance;//卡片现可用余额
                            decimal fPreRecharge = listPreRecharge.Sum(x => x.prr_fRechargeMoney);//预充值累积款
                            decimal fCardRecharge = fPreRecharge + fSumPreCost;//预充值款 + 未结算预付款  = 卡片应充值金额

                            if (fPreRecharge > 0 && fCardRecharge >= 0)
                            {
                                #region 存在预充值记录,并且足以扣除未结算款项

                                ReturnValueInfo rvInfoPreRecharge = this._Reader.Recharge(base._CardInfoSection, base._SectionPwd, fCardRecharge);
                                if (rvInfoPreRecharge.boolValue && !rvInfoPreRecharge.isError)
                                {
                                    List<RechargeRecord_rcr_Info> listRechargeInsert = new List<RechargeRecord_rcr_Info>();
                                    decimal fPerTimeRecharge = fSumPreCost;

                                    //foreach (PreRechargeRecord_prr_Info preRecItem in listPreRecharge)
                                    //{
                                    //    RechargeRecord_rcr_Info rechargeRecord = new RechargeRecord_rcr_Info();
                                    //    rechargeRecord.rcr_cAdd = base.UserInformation.usm_cUserLoginID;
                                    //    rechargeRecord.rcr_cCardID = pairInfo.ucp_cCardID;
                                    //    rechargeRecord.rcr_cLast = base.UserInformation.usm_cUserLoginID;
                                    //    rechargeRecord.rcr_cRechargeType = preRecItem.prr_cRechargeType;
                                    //    rechargeRecord.rcr_cRecordID = Guid.NewGuid();
                                    //    rechargeRecord.rcr_cStatus = DefineConstantValue.ConsumeMoneyFlowStatus.Finished.ToString();
                                    //    rechargeRecord.rcr_cUserID = pairInfo.ucp_cCUSID;
                                    //    rechargeRecord.rcr_dLastDate = DateTime.Now;
                                    //    rechargeRecord.rcr_dRechargeTime = DateTime.Now;
                                    //    rechargeRecord.rcr_fRechargeMoney = preRecItem.prr_fRechargeMoney;

                                    //    fPerTimeRecharge += preRecItem.prr_fRechargeMoney;
                                    //    if (fPerTimeRecharge < 0)
                                    //    {
                                    //        rechargeRecord.rcr_fBalance = fCardBalance;
                                    //    }
                                    //    else
                                    //    {
                                    //        rechargeRecord.rcr_fBalance = fCardBalance + fPerTimeRecharge;
                                    //    }

                                    //    preRecItem.prr_cRCRID = rechargeRecord.rcr_cRecordID;
                                    //    listRechargeInsert.Add(rechargeRecord);
                                    //}

                                    for (int i = 0; i < listPreRecharge.Count; i++)
                                    {
                                        RechargeRecord_rcr_Info rechargeRecord = new RechargeRecord_rcr_Info();
                                        rechargeRecord.rcr_cAdd = base.UserInformation.usm_cUserLoginID;
                                        rechargeRecord.rcr_cCardID = pairInfo.ucp_cCardID;
                                        rechargeRecord.rcr_cLast = base.UserInformation.usm_cUserLoginID;
                                        rechargeRecord.rcr_cRechargeType = listPreRecharge[i].prr_cRechargeType;
                                        rechargeRecord.rcr_cRecordID = Guid.NewGuid();
                                        rechargeRecord.rcr_cStatus = DefineConstantValue.ConsumeMoneyFlowStatus.Finished.ToString();
                                        rechargeRecord.rcr_cUserID = pairInfo.ucp_cCUSID;
                                        rechargeRecord.rcr_dLastDate = DateTime.Now;
                                        rechargeRecord.rcr_dRechargeTime = DateTime.Now;
                                        rechargeRecord.rcr_fRechargeMoney = listPreRecharge[i].prr_fRechargeMoney;

                                        fPerTimeRecharge += listPreRecharge[i].prr_fRechargeMoney;
                                        if (fPerTimeRecharge < 0)
                                        {
                                            //rechargeRecord.rcr_fBalance = fCardBalance;

                                            if (i != listPreRecharge.Count - 1)
                                            {
                                                rechargeRecord.rcr_fBalance = fCardBalance;
                                            }
                                            else
                                            {
                                                rechargeRecord.rcr_fBalance = fCardBalance + fPerTimeRecharge;
                                            }
                                        }
                                        else
                                        {
                                            rechargeRecord.rcr_fBalance = fCardBalance + fPerTimeRecharge;
                                        }

                                        listPreRecharge[i].prr_cRCRID = rechargeRecord.rcr_cRecordID;
                                        listRechargeInsert.Add(rechargeRecord);
                                    }

                                    rvInfoPreRecharge = this._IRechargeRecordBL.UpdateRechargeRecord(listPreRecharge, listRechargeInsert, fSumPreCost);

                                    if (rvInfoPreRecharge.boolValue && !rvInfoPreRecharge.isError)
                                    {
                                        rvInfo.messageText += Environment.NewLine + "存在预转账记录" + listPreRecharge.Count.ToString() + "条,款项合共:¥" + fPreRecharge.ToString() + "元,已成功充入。";
                                        rvInfo.boolValue = true;
                                        return rvInfo;
                                    }
                                    else
                                    {
                                        rvInfoPreRecharge = this._Reader.Recharge(base._CardInfoSection, base._SectionPwd, fPreRecharge);
                                        if (rvInfoPreRecharge.isError || !rvInfoPreRecharge.boolValue)
                                        {
                                            rvInfo.messageText += Environment.NewLine + "保存预充值记录失败,卡片金额已更新,请联系管理员扣减卡金额。";
                                        }
                                    }
                                }
                                else
                                {
                                    rvInfo.messageText += Environment.NewLine + "预充值操作失败,无法将该用户的待转账款项充入卡内,请稍后手动转账充值。" + Environment.NewLine + rvInfoPreRecharge.messageText;
                                    rvInfo.boolValue = false;
                                }

                                #endregion
                            }
                            else
                            {
                                if (fCardRecharge < 0)
                                {
                                    rvInfo.messageText += Environment.NewLine + "本用户有转账充值记录" + listPreRecharge.Count.ToString() + "条,但不足以抵扣未结算预付款,请充值。";
                                    rvInfo.boolValue = true;
                                    return rvInfo;
                                }
                            }

                            #endregion
                        }
                        else
                        {
                            rvInfo.boolValue = true;
                            return rvInfo;
                        }
                    }
                    else
                    {
                        rvInfo.messageText = "保存新发卡资料失败。" + rvInfo.messageText;
                        rvInfo.isError = true;
                        return rvInfo;
                    }
                }
                catch (Exception ex)
                {
                    rvInfo.messageText = ex.Message;
                    rvInfo.isError = true;
                    return rvInfo;
                }
            }
            catch (Exception exAll)
            {
                rvInfo.messageText = exAll.Message;
                rvInfo.isError = true;
            }
            return rvInfo;
        }