/*******************************************************************************
//  版权所有  (C) 2015 刺客
//  文 件 名：MCPayment.cs
//  文件说明：会员消费支付方式记录数据操作类；
//  版    本：
//
//  创建标识：陈志豪 2016-01-07 10:40:24
//
//  修改标识：
//  修改说明：
*******************************************************************************/

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

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

namespace VA.Modules.JDingMember.Interface.MemberConsume.ClassLibrary.DataAccess
{
    /// <summary>
    /// 会员消费支付方式记录数据操作类；
    /// </summary>
    public class MCPayment : DAOBase
    {
        const string CODE_PATH = "DAL.MCPayment.";
        SqlParameter[] parms = null;

        static string Tb_Prefix = DBConfig.Tb_Prefix;
        string SQL_INSERT = "INSERT INTO va_Interface_MC_Payments(BizNo,PayType_Code,PayType_Name,PayAmount,Certificate_No)VALUES(@BizNo,@PayType_Code,@PayType_Name,@PayAmount,@Certificate_No)";
        string SQL_DELETE_BY_BIZNO = "DELETE FROM va_Interface_MC_Items WHERE BizNo=@BizNo";
        string SQL_GETLIST = "SELECT * FROM va_Interface_MC_Payments";
        string SQL_GETLIST_CNT = "SELECT COUNT(*) FROM va_Interface_MC_Payments";
        string PRO_GETLIST = "va_Pager201301";

        #region Insert 插入
        /// <summary>
        /// 插入；
        /// </summary>
        /// <param name="entity">会员消费支付方式记录实体对象</param>
        /// <returns>如果插入成功，则返回true，否则，返回false</returns>
        public bool Insert(MCPaymentInfo entity,SqlTransaction trans)
        {
            try
            {
                parms = GetParameters(entity);
                int affectedRows = SQLHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_INSERT, parms);

                return affectedRows > 0 ? true : false;
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.Insert(...):" + ex.Message);
            }
        }
        #endregion

        #region DeleteByBizNo 删除
        /// <summary>
        /// 删除；
        /// </summary>
        /// <param name="rID">记录ID</param>
        /// <returns>如果删除成功，则返回true，否则，返回false</returns>
        public bool DeleteByBizNo(string bizNo)
        {
            try
            {
                SqlParameter[] parms = new SqlParameter[]
				{
					SQLHelper.MakeInParam("@BizNo", SqlDbType.VarChar, bizNo)
				};
                int affectedRows = SQLHelper.ExecuteNonQuery(DBConfig.ConnectionString, CommandType.Text, SQL_DELETE_BY_BIZNO, parms);

                return affectedRows > 0 ? true : false;
            }
            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)
        {
            sqlFilter = sqlFilter == null ? String.Empty : sqlFilter;
            string tmpSqlFilter = sqlFilter.ToUpper().Trim();
            if (tmpSqlFilter.StartsWith("WHERE"))
            {
                throw new Exception("参数[sqlFilter]的值不能以\"where\"开头！");
            }
            if (tmpSqlFilter.StartsWith("AND"))
            {
                throw new Exception("参数[sqlFilter]的值不能以\"and\"开头！");
            }

            string cmdText = SQL_GETLIST;

            if (!String.IsNullOrEmpty(sqlFilter))
            {
                cmdText += " WHERE 1=1 AND " + sqlFilter;
            }

            if (!String.IsNullOrEmpty(orderBy))
            {
                cmdText += " ORDER BY " + orderBy;
            }

            try
            {
                DataSet ds = SQLHelper.ExecuteDataset(DBConfig.ConnectionString, CommandType.Text, cmdText, parms);
                return ds.Tables[0];
            }
            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)
        {
            sqlFilter = sqlFilter == null ? String.Empty : sqlFilter;
            string tmpSqlFilter = sqlFilter.ToUpper().Trim();
            if (tmpSqlFilter.StartsWith("WHERE"))
            {
                throw new Exception("参数[sqlFilter]的值不能以\"where\"开头！");
            }
            if (tmpSqlFilter.StartsWith("AND"))
            {
                throw new Exception("参数[sqlFilter]的值不能以\"and\"开头！");
            }

            SqlParameter[] parms = new SqlParameter[]
            {
                SQLHelper.MakeInParam("@TableName", SqlDbType.VarChar, "va_Interface_MC_Payments"),
                SQLHelper.MakeInParam("@SqlFilter", SqlDbType.VarChar, sqlFilter),                    
                SQLHelper.MakeInParam("@KeyFieldName", SqlDbType.VarChar, "RID"),                    
                SQLHelper.MakeInParam("@OrderBy", SqlDbType.VarChar, orderBy),
                SQLHelper.MakeInParam("@PageIndex", SqlDbType.Int, currPage),                      
                SQLHelper.MakeInParam("@PageSize", SqlDbType.Int, pageSize)       
            };

            try
            {
                DataSet ds = SQLHelper.ExecuteDataset(base.ConnectionString, CommandType.StoredProcedure, PRO_GETLIST, parms);
                return ds.Tables[0];
            }
            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
            {
                sqlFilter = sqlFilter == null ? String.Empty : sqlFilter;
                string tmpSqlFilter = sqlFilter.ToUpper().Trim();
                if (tmpSqlFilter.StartsWith("WHERE"))
                {
                    throw new Exception("参数[sqlFilter]的值不能以\"where\"开头！");
                }
                if (tmpSqlFilter.StartsWith("AND"))
                {
                    throw new Exception("参数[sqlFilter]的值不能以\"and\"开头！");
                }

                if (!String.IsNullOrEmpty(sqlFilter))
                {
                    sqlFilter = "WHERE " + sqlFilter;
                }

                string cmdText = SQL_GETLIST_CNT + " " + sqlFilter;
                return (int)SQLHelper.ExecuteScalar(base.ConnectionString, CommandType.Text, cmdText);
            }
            catch (Exception ex)
            {
                throw new Exception(CODE_PATH + "MCPayment.GetDataListCnt(...):" + ex.Message);
            }
        }
        #endregion

        #region GetParameters、GetKeyParameter 获取参数数组
        /// <summary>
        /// 获取参数数组；
        /// </summary>
        /// <param name=" entity">实体</param>
        /// <returns>SqlParameter</returns>
        SqlParameter[] GetParameters(MCPaymentInfo entity)
        {
            return new SqlParameter[]
				{
					SQLHelper.MakeInParam("@RID", SqlDbType.Int, entity.RID),
					SQLHelper.MakeInParam("@BizNo", SqlDbType.VarChar, entity.BizNo),
					SQLHelper.MakeInParam("@PayType_Code", SqlDbType.VarChar, entity.PayType_Code),
					SQLHelper.MakeInParam("@PayType_Name", SqlDbType.VarChar, entity.PayType_Name),
					SQLHelper.MakeInParam("@PayAmount", SqlDbType.Decimal, entity.PayAmount),
					SQLHelper.MakeInParam("@Certificate_No", SqlDbType.VarChar, entity.Certificate_No)
				};
        }

        /// <summary>
        /// 获取主键参数数组；
        /// </summary>
        /// <param name="rID">记录ID</param>
        /// <returns></returns>
        SqlParameter[] GetKeyParameter(int rID)
        {
            return new SqlParameter[]
				{
					SQLHelper.MakeInParam("@RID", SqlDbType.Int, rID)
				};
        }
        #endregion

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

