Пример #1
0
        /*
         * // parameters:
         * //      strRecord   [in] 读者XML记录,或读者证条码号。
         * //                  如果是读者 XML 记录,第一字符是 '<',函数内代码可以据此进行判断。读者 XML 记录里面包含足够的标识字段即可,不要求包含所有字段
         * //      strPriceString  [out] 金额字符串。一般为类似“CNY12.00”这样的形式
         * //      strRest    [out] 扣款后的余额
         * */
        /// <summary>
        /// 从卡中心扣款
        /// </summary>
        /// <param name="strRecord">账户标识,一般为读者 barcode </param>
        /// <param name="strPriceString">扣款金额字符串,单位:元。一般为类似“CNY12.00”这样的形式</param>
        /// <param name="strPassword">账户密码</param>
        /// <param name="strRest">扣款后的余额,单位:元,格式为 货币符号+金额数字部分,形如“CNY100.00”表示人民币 100元</param>
        /// <param name="strError">错误信息</param>
        /// <returns>
        /// <para>-2  密码不正确</para>
        /// <para>-1  出错(调用出错等特殊原因)</para>
        /// <para>0   扣款不成功(因为余额不足等普通原因)。注意 strError 中应当返回不成功的原因</para>
        /// <para>1   扣款成功</para>
        /// </returns>
        public int Deduct(string strRecord,
                          string strPriceString,
                          string strPassword,
                          out string strRest,
                          out string strError)
        {
            strRest  = "";
            strError = "";

            if (strRecord.Trim().Substring(0, 1) == "<")
            {
                strError = "参数'strRecord'值不正确,不是正确的卡号";
                return(-1);
            }

            // 第一个流程 扣款金额字符串 为空,需返回 账户余额
            if (String.IsNullOrEmpty(strPriceString) == true)
            {
                strRest = "";
                return(1);
            }

            // 处理带前缀的价格字符串
            string strPrefix  = ""; // 金额字符串前缀
            string strValue   = ""; // 金额字符串数字部分
            string strPostfix = ""; // 金额字符串后缀
            int    nRet       = PriceUtil.ParsePriceUnit(strPriceString,
                                                         out strPrefix,
                                                         out strValue,
                                                         out strPostfix,
                                                         out strError);

            if (nRet == -1)
            {
                return(-1);
            }

            // TODO: 这里应该加入真实的扣款代码。
            // 一般是要访问卡中心的 SQL 数据库,找到该读者的记录,对金额字段进行扣款修改,然后返回扣款后账户余额
            // 可用的参数:经过前面的准备,这里 strPrefix 内容是货币代码;strValue 内容是要扣款的数字金额值。strRecord 内容是读者证条码号;strPassword 是读者(在前端扣款界面)输入的密码

            // 扣款成功,返回扣款后的余额。注意这只是一行简单的示范代码
            strRest = strPrefix + "0.00";
            return(1);
        }
Пример #2
0
        public static DoubleCurrencyItem Parse(string strText)
        {
            string strError   = "";
            string strPrefix  = "";
            string strValue   = "";
            string strPostfix = "";
            int    nRet       = PriceUtil.ParsePriceUnit(strText,
                                                         out strPrefix,
                                                         out strValue,
                                                         out strPostfix,
                                                         out strError);

            if (nRet == -1)
            {
                throw new Exception(strError);
            }
            double value = 0;

            try
            {
                value = Convert.ToDouble(strValue);
            }
            catch
            {
                strError = "数字 '" + strValue + "' 格式不正确";
                throw new Exception(strError);
            }

            DoubleCurrencyItem item = new DoubleCurrencyItem();

            item.Prefix  = strPrefix;
            item.Postfix = strPostfix;
            item.Value   = value;

            return(item);
        }
Пример #3
0
        internal static int ParsePriceString(string strPrice,
                                             out long value,
                                             out string strUnit,
                                             out string strError)
        {
            value    = 0;
            strUnit  = "";
            strError = "";

            if (string.IsNullOrEmpty(strPrice) == true)
            {
                return(0);
            }

#if NO
            string strPrefix  = "";
            string strValue   = "";
            string strPostfix = "";

            // 分析价格参数
            // 允许前面出现+ -号
            // return:
            //      -1  出错
            //      0   成功
            int nRet = PriceUtil.ParsePriceUnit(strPrice,
                                                out strPrefix,
                                                out strValue,
                                                out strPostfix,
                                                out strError);
            if (nRet == -1)
            {
                return(-1);
            }
            strUnit = strPrefix + strPostfix;
            decimal v = 0;
            if (decimal.TryParse(strValue, out v) == false)
            {
                strError = "金额字符串 '" + strPrice + "' 中数字部分 '" + strValue + "' 格式不正确";
                return(-1);
            }
#endif
            CurrencyItem item = null;
            // 解析单个金额字符串。例如 CNY10.00 或 -CNY100.00/7
            int nRet = PriceUtil.ParseSinglePrice(strPrice,
                                                  out item,
                                                  out strError);
            if (nRet == -1)
            {
                return(-1);
            }

            strUnit = item.Prefix + item.Postfix;
            try
            {
                value = (long)(item.Value * 100);
            }
            catch (Exception ex)
            {
                // 2016/3/31
                strError = "元值 '" + item.Value.ToString() + "' 折算为分值的时候出现异常:" + ex.Message;
                return(-1);
            }
            return(0);
        }