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

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

        public MCItem()
        {
            mCItemObj = new DataAccess.MCItem();
        }

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

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

                        if (!this.mCItemObj.Insert(entity, 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 + "MCItem.Add(...):" + ex.Message);
            }
        }
        #endregion

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

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

                        foreach (MCItemInfo item in itemList)
                        {
                            if (!this.mCItemObj.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 + "MCItem.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 mCItemObj.DeleteByBizNo(bizNo);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCItem.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 mCItemObj.GetDataList(sqlFilter, orderBy);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCItem.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 mCItemObj.GetDataList(currPage, pageSize, sqlFilter, orderBy);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCItem.GetDataList(...):" + ex.Message);
            }
        }
        #endregion

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

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

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

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

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

            MCItemInfo mCItemInfo = null;

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

                DataRow row = dtb.Rows[i];

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


                mCItemEntities.Add(mCItemInfo);
            }

            return mCItemEntities;
        }
        #endregion

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

