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

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 MCMaster
    {
        const string CODE_PATH = "Core.MCMaster.";
        DataAccess.MCMaster mCMasterObj = null;
        DataAccess.MCItem mCItemObj = null;
        DataAccess.MCPayment mCPaymentObj = null;

        public MCMaster()
        {
            mCMasterObj = new DataAccess.MCMaster();
            mCItemObj = new DataAccess.MCItem();
            mCPaymentObj = new DataAccess.MCPayment();
        }

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

            try
            {
                return this.mCMasterObj.Insert(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCMaster.Add(...):" + 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 mCMasterObj.DeleteByBizNo(bizNo);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCMaster.Delete(...):" + ex.Message);
            }
        }
        #endregion

        #region GetEntityByBizNo 得到实体
        /// <summary>
        /// 得到实体；
        /// </summary>
        /// <param name="rID">记录ID</param>
        /// <returns>会员消费实体</returns>
        public MCMasterInfo GetEntityByBizNo(string bizNo)
        {
            try
            {
                return mCMasterObj.GetEntityByBizNo(bizNo);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCMaster.GetEntity(...):" + 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 mCMasterObj.GetDataList(sqlFilter, orderBy);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCMaster.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 mCMasterObj.GetDataList(currPage, pageSize, sqlFilter, orderBy);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCMaster.GetDataList(...):" + ex.Message);
            }
        }
        #endregion

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

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

                return mCMasterEntities;
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCMaster.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<MCMasterInfo> GetList(int currPage, int pageSize, string sqlFilter, string orderBy)
        {
            try
            {
                DataTable dtb = mCMasterObj.GetDataList(currPage, pageSize, sqlFilter, orderBy);
                List<MCMasterInfo> mCMasterEntities = ConvertDataTableToEntityList(dtb);

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

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

            MCMasterInfo mCMasterInfo = null;

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

                DataRow row = dtb.Rows[i];

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

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

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

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

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

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

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

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

                if (row["Operate_Date"] != DBNull.Value)
                {
                    mCMasterInfo.Operate_Date = Convert.ToDateTime(row["Operate_Date"]);
                }

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

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

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

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

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

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

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

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

                mCMasterEntities.Add(mCMasterInfo);
            }

            return mCMasterEntities;
        }
        #endregion

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

