/*******************************************************************************
//  版权所有  (C) 2015 刺客
//  文 件 名：MCPayment.cs
//  文件说明：无；
//  版    本：
//
//  创建标识：陈志豪 2016-01-07 10:58:19
//
//  修改标识：
//  修改说明：
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;

using VA.Modules.JDingMember.Interface.MemberConsume.ClassLibrary.Model;

namespace VA.Modules.JDingMember.Interface.MemberConsume.ClassLibrary.Business
{
    /// <summary>
    /// 会员消费支付方式记录业务处理类；
    /// </summary>
    public class MCPayment
    {
        const string CODE_PATH = "Core.MCPayment.";
        DataAccess.MCPayment mCPaymentObj = null;

        public MCPayment()
        {
            mCPaymentObj = new DataAccess.MCPayment();
        }

        #region AddBatchlly 添加
        /// <summary>
        /// 添加；
        /// </summary>
        /// <param name="entity">会员消费支付方式记录实体对象</param>
        /// <param name="errorMsg">添加失败的原因（仅当添加失败时有效）；</param>
        /// <param name="args">扩展参数（不对业务处理构成影响）；</param>
        /// <returns>如果插入成功，则返回true，否则，返回false</returns>
        public bool AddBatchlly(List<MCPaymentInfo> paymentList, out string errorMsg, params object[] args)
        {
            errorMsg = null;

            try
            {
                //开始事务
                using (TransactionWrapper transScope = new TransactionWrapper())
                {
                    try
                    {
                        transScope.BeginTrans(); //开始事务；

                        foreach (MCPaymentInfo item in paymentList)
                        {
                            if (!this.mCPaymentObj.Insert(item, transScope.Transaction))
                            {
                                throw new Exception("添加失败！");
                            }
                        }

                        transScope.Complete();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        transScope.Rollback(); //如果发生异常，务必回滚事务；
                        throw; //向上抛出异常；
                    }
                    finally
                    {
                        transScope.Dispose(); //销毁事务；
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.AddBatchlly(...):" + ex.Message);
            }
        }
        #endregion

        #region DeleteByBizNo 删除
        /// <summary>
        /// 删除；
        /// </summary>
        /// <param name="rID">记录ID</param>
        /// <param name="errorMsg">删除失败的原因（仅当删除失败时有效）；</param>
        /// <param name="args">扩展参数（不对业务处理构成影响）；</param>
        /// <returns>如果删除成功，则返回会员消费支付方式记录代码，否则，返回空字符串</returns>
        public bool DeleteByBizNo(string bizNo, out string errorMsg, params object[] args)
        {
            errorMsg = null;

            try
            {
                return mCPaymentObj.DeleteByBizNo(bizNo);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.DeleteByBizNo(...):" + ex.Message);
            }
        }
        #endregion
      
        #region GetDataList 得到数据集
        /// <summary>
        /// 得到数据集；
        /// </summary>		
        /// <param name="sqlFilter">SQL筛选器。例如："NAME LIKE'%张三%' AND SEX='1'"</param>
        /// <param name="orderBy">排序依据。例如："ID DESC"</param>
        /// <returns>数据集（DataTable）</returns>
        public DataTable GetDataList(string sqlFilter, string orderBy)
        {
            try
            {
                return mCPaymentObj.GetDataList(sqlFilter, orderBy);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.GetDataList(...):" + ex.Message);
            }
        }

        /// <summary>
        /// 得到数据集；
        /// </summary>
        /// <param name="currPage">当前页码</param>
        /// <param name="pageSize">每页显示的记录条数</param>
        /// <param name="sqlFilter">SQL筛选器。例如："NAME LIKE'%张三%' AND SEX='1'"</param>
        /// <param name="orderBy">如：SaleState ASC,CarID DESC</param>        
        /// <returns>数据集（DataTable）</returns>
        public DataTable GetDataList(int currPage, int pageSize, string sqlFilter, string orderBy)
        {
            try
            {
                return mCPaymentObj.GetDataList(currPage, pageSize, sqlFilter, orderBy);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.GetDataList(...):" + ex.Message);
            }
        }
        #endregion

        #region GetDataListCnt
        /// <summary>
        /// 得到记录数量
        /// </summary>
        /// <param name="sqlCondition">查询条件(含WHERE)</param>
        /// <returns>int</returns>
        public int GetDataListCnt(string sqlFilter)
        {
            try
            {
                return mCPaymentObj.GetDataListCnt(sqlFilter);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.GetDataListCnt(...):" + ex.Message);
            }
        }
        #endregion

        #region GetList 得到会员消费支付方式记录实体集
        /// <summary>
        /// 得到会员消费支付方式记录实体集；
        /// </summary>
        /// <param name="sqlCondition">查询条件</param>
        /// <param name="orderBy">排序依据（例如：ORDER BY ID DESC）</param>
        /// <returns>会员消费支付方式记录实体集</returns>
        public List<MCPaymentInfo> GetList(string sqlCondition, string orderBy)
        {
            try
            {
                DataTable dtb = mCPaymentObj.GetDataList(sqlCondition, orderBy);
                List<MCPaymentInfo> mCPaymentEntities = ConvertDataTableToEntityList(dtb);

                return mCPaymentEntities;
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.GetList(...):" + ex.Message);
            }
        }

        /// <summary>
        /// 得到会员消费支付方式记录实体集；
        /// </summary>
        /// <param name="currPage">当前页码</param>
        /// <param name="pageSize">每页显示的记录条数</param>
        /// <param name="sqlFilter">SQL筛选器。例如："NAME LIKE'%张三%' AND SEX='1'"</param>
        /// <param name="orderBy">排序依据，例如："ID DESC"</param>
        /// <returns>会员消费支付方式记录实体集</returns>
        public List<MCPaymentInfo> GetList(int currPage, int pageSize, string sqlFilter, string orderBy)
        {
            try
            {
                DataTable dtb = mCPaymentObj.GetDataList(currPage, pageSize, sqlFilter, orderBy);
                List<MCPaymentInfo> mCPaymentEntities = ConvertDataTableToEntityList(dtb);

                return mCPaymentEntities;
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.GetList(...):" + ex.Message);
            }
        }
        #endregion

        #region ConvertDataTableToEntityList 将DataTable转化为MCPaymentInfo集合
        /// <summary>
        /// 将DataTable转化为ArticleEntity集合
        /// </summary>
        /// <param name="dtb"></param>
        /// <returns></returns>
        List<MCPaymentInfo> ConvertDataTableToEntityList(DataTable dtb)
        {
            List<MCPaymentInfo> mCPaymentEntities = new List<MCPaymentInfo>();

            MCPaymentInfo mCPaymentInfo = null;

            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                mCPaymentInfo = new MCPaymentInfo();

                DataRow row = dtb.Rows[i];

                if (row["RID"] != DBNull.Value)
                {
                    mCPaymentInfo.RID = Convert.ToInt32(row["RID"]);
                }

                if (row["BizNo"] != DBNull.Value)
                {
                    mCPaymentInfo.BizNo = row["BizNo"].ToString();
                }

                if (row["PayType_Code"] != DBNull.Value)
                {
                    mCPaymentInfo.PayType_Code = row["PayType_Code"].ToString();
                }

                if (row["PayType_Name"] != DBNull.Value)
                {
                    mCPaymentInfo.PayType_Name = row["PayType_Name"].ToString();
                }

                if (row["PayAmount"] != DBNull.Value)
                {
                    mCPaymentInfo.PayAmount = Convert.ToDecimal(row["PayAmount"]);
                }

                if (row["Certificate_No"] != DBNull.Value)
                {
                    mCPaymentInfo.Certificate_No = row["Certificate_No"].ToString();
                }


                mCPaymentEntities.Add(mCPaymentInfo);
            }

            return mCPaymentEntities;
        }
        #endregion

    } //class end.
} //namespace end.

