using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.MD;
using QMFrameWork.Data;
using System.Data;
using QMAPP.Entity;
using QMAPP.MD.Entity.Bucket;

namespace QMAPP.FJC.DAL.Bucket
{
    /// <summary>
    /// 模块名称:搪塑称重记录
    /// 作    者:张松男
    /// 编写日期:2021年08月23日
    /// </summary>
    public class TSWeighRecoreDAL
    {

        #region 获取信息
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="">条件</param>
        /// <returns>*信息</returns>
        public TSWeighRecore Get(TSWeighRecore info)
        {
            try
            {
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    //获取信息
                    info = session.Get<TSWeighRecore>(info);
                }
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="">条件</param>
        /// <returns>*信息</returns>
        public TSWeighRecore Get(string materialcode)
        {
            try
            {
                string sql = "SELECT * FROM [T_AW_TSWeighRecore] WHERE [MATERIAL_CODE]=@materialcode";
                List<DataParameter> parameters = new List<DataParameter>();
                parameters.Add(new DataParameter("materialcode", materialcode));
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    //获取信息
                    var info = session.Get<TSWeighRecore>(sql, parameters.ToArray());
                    return info;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region 获取列表
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="page">数据页</param>
        /// <returns>数据页</returns>
        public DataPage GetList(TSWeighRecore condition, DataPage page)
        {
            string sql = null;
            List<DataParameter> parameters = new List<DataParameter>();
            try
            {
                sql = this.GetQuerySql(condition, ref parameters);
                //分页关键字段及排序
                page.KeyName = "PID";
                if (string.IsNullOrEmpty(page.SortExpression))
                    page.SortExpression = "UPDATEDATE DESC";
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    page = session.GetDataPage<TSWeighRecore>(sql, parameters.ToArray(), page);
                }
                return page;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="page">数据页</param>
        /// <returns>数据页</returns>
        public List<TSWeighRecore> GetALL()
        {
            string sql = null;
            List<DataParameter> parameters = new List<DataParameter>();
            try
            {
                sql = "SELECT * FROM T_AW_TSWeighRecore ";
                //分页关键字段及排序
                var LIST = new List<TSWeighRecore>();
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    LIST = session.GetList<TSWeighRecore>(sql, parameters.ToArray()).ToList();
                }
                return LIST;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取查询语句
        /// <summary>
        /// 获取查询语句
        /// </summary>
        /// <param name="user">查询条件</param>
        /// <param name="parameters">参数</param>
        /// <returns>查询语句</returns>
        private string GetQuerySql(TSWeighRecore condition, ref List<DataParameter> parameters)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            StringBuilder whereBuilder = new StringBuilder();
            try
            {
                
                //构成查询语句
                sqlBuilder.Append("SELECT PID,ProductCode,PColour,MATERIAL_CODE,Weigh,CREATEUSER,CREATEDATE,UPDATEUSER,UPDATEDATE ");
                sqlBuilder.Append("FROM T_AW_TSWeighRecore ");

                if (string.IsNullOrEmpty(condition.ProductCode) == false)
                {
                    whereBuilder.Append(" AND ProductCode = @ProductCode");
                    parameters.Add(new DataParameter { ParameterName = "ProductCode", DataType = DbType.String, Value = condition.ProductCode });
                }
                //查询条件
                if (whereBuilder.Length > 0)
                {
                    sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
                }
                return sqlBuilder.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        
        #region 信息是否重复
        /// <summary>
        /// 判断名称是否存在
        /// </summary>
        /// <param name="info"></param>
        /// <returns>true:已存在;fasel:不存在。</returns>
        public bool Exists(TSWeighRecore info)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            StringBuilder whereBuilder = new StringBuilder();
            List<DataParameter> parameters = new List<DataParameter>();
            int count = 0;
            try
            {
                sqlBuilder.Append("SELECT COUNT(0) FROM T_AW_TSWeighRecore");
                if (info.PID == null)
                {
                    info.PID = "";
                }
                whereBuilder.Append(" AND PID <> @PID ");
                parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = info.PID });

                //添加进行无重复字段判断代码

                if (whereBuilder.Length > 0)
                {
                    sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
                }
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray()));
                }
                return count > 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 插入信息
        /// <summary>
        /// 插入信息(单表)
        /// </summary>
        /// <param name="">信息</param>
        /// <returns>插入行数</returns>
        public int Insert(TSWeighRecore info)
        {
            int count = 0;
            try
            {
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    //插入基本信息
                    count = session.Insert<TSWeighRecore>(info);
                }
                return count;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 更新信息
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name=""></param>
        /// <returns>更新行数</returns>
        public int Update(TSWeighRecore info)
        {
            int count = 0;
            try
            {
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    //更新基本信息
                    count = session.Update<TSWeighRecore>(info);
                }
                return count;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

       
        

    }
}