using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QMAPP.DAL;
using QMAPP.FJC.Entity.MesB9;
using QMAPP.FJC.Entity.ODS;
using QMAPP.FJC.Entity.WarnManage;
using QMFrameWork.Data;
using QMFrameWork.Log;

namespace QMAPP.FJC.DAL.ODS
{
    public class VideoInfoDAL : BaseDAL
    {
        #region 获取信息
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="">条件</param>
        /// <returns>*信息</returns>
        public VideoInfo Get(VideoInfo model)
        {
            try
            {
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    //获取信息
                    model = session.Get<VideoInfo>(model);
                }
                return model;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        public List<VideoInfo> GetAllList()
        {
            //DataParameter[] parameters;
            List<DataParameter> parameters = new List<DataParameter>();
            //string sql = this.GetQuerySql(condition, ref parameters);
            string sql = "SELECT * FROM T_ODS_VideoInfo ";
            using (IDataSession session = AppDataFactory.CreateMainSession())
            {
                return session.GetList<VideoInfo>(sql, parameters.ToArray()).ToList();
            }
        }

        protected string GetAllSql(VideoInfo condition,out DataParameter[] parameters)
        {
            List<DataParameter> parametersList = new List<DataParameter>();
            StringBuilder sql = new StringBuilder();
            sql.Append("SELECT * FROM T_ODS_VideoInfo ");
            parameters = parametersList.ToArray();
            return sql.ToString();
        }

        #region 获取页面
        /// <summary>
        /// 播放视频
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="page">数据页</param>
        /// <returns>数据页</returns>
        public DataPage GetDataPageList(VideoInfo condition, DataPage page)
        {
            string sql = null;
            List<DataParameter> parameters = new List<DataParameter>();
            try
            {
                sql = this.GetQuerySql(condition, ref parameters);

                //分页关键字段及排序
                page.KeyName = "PID";
                page.SortExpression = "UPDATEDATE DESC";

                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    page = session.GetDataPage<VideoInfo>(sql, parameters.ToArray(), page);
                }
                return page;
            }
            catch (Exception ex)
            {
                LogManager.LogHelper.Error(new LogInfo()
                {
                    ErrorInfo = ex,
                    Tag = ex.StackTrace,
                    Info = "视频播放数据层-视频播放页面"
                });
                throw;
            }
        }

        #endregion

        #region 播放视频查询语句
        /// <summary>
        /// 播放视频查询语句
        /// </summary>
        /// <param name="user">查询条件</param>
        /// <param name="parameters">参数</param>
        /// <returns>查询语句</returns>
        private string GetQuerySql(VideoInfo condition, ref List<DataParameter> parameters)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            StringBuilder whereBuilder = new StringBuilder();
            try
            {
                //构成查询语句
                sqlBuilder.Append(@"SELECT   PID
                                             ,VideoName
                                             ,VideoType
                                             ,VideoRule
                                             ,VideoUrl
                                             ,IsRepair
                                             ,MEMO
                                             ,CREATEDATE
                                             ,CREATEUSER
                                             ,UPDATEUSER
                                             ,UPDATEDATE 
                                            FROM T_ODS_VideoInfo T ");

                //查询条件
                //视频名称
                if (string.IsNullOrEmpty(condition.VideoName) == false)
                {
                    whereBuilder.Append(" AND VideoName LIKE  '%'+" + "@VideoName" + "+'%'");
                    parameters.Add(new DataParameter { ParameterName = "VideoName", DataType = DbType.String, Value = condition.VideoName });
                }
                //中心
                if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false)
                {
                    whereBuilder.Append(" AND WORKCENTER_CODE = @WORKCENTER_CODE ");
                    parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE });
                }
                //工序
                if (string.IsNullOrEmpty(condition.WORKCELL_CODE) == false)
                {
                    whereBuilder.Append(" AND WORKCELL_CODE = @WORKCELL_CODE ");
                    parameters.Add(new DataParameter { ParameterName = "WORKCELL_CODE", DataType = DbType.String, Value = condition.WORKCELL_CODE });
                }
                //工位
                if (string.IsNullOrEmpty(condition.WORKLOC_CODE) == false)
                {
                    whereBuilder.Append(" AND WORKLOC_CODE = @WORKLOC_CODE ");
                    parameters.Add(new DataParameter { ParameterName = "WORKLOC_CODE", DataType = DbType.String, Value = condition.WORKLOC_CODE });
                }
                //视频类型
                if (string.IsNullOrEmpty(condition.VideoType) == false)
                {
                    whereBuilder.Append(" AND VideoType = @VideoType ");
                    parameters.Add(new DataParameter { ParameterName = "VideoType", DataType = DbType.String, Value = condition.VideoType });
                }                

                if (condition.STARTCREATEDATE != DateTime.MinValue)
                {
                    whereBuilder.Append(" AND CREATEDATE >= @STARTCREATEDATE");
                    parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "STARTCREATEDATE", Value = condition.STARTCREATEDATE.ToString("yyyy-MM-dd HH:mm:ss") });
                }

                if (condition.ENDCREATEDATE != DateTime.MinValue)
                {
                    whereBuilder.Append(" AND CREATEDATE < @ENDCREATEDATE");
                    parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "ENDCREATEDATE", Value = condition.ENDCREATEDATE.ToString("yyyy-MM-dd HH:mm:ss") });
                }
                if (whereBuilder.Length > 0)
                {
                    sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
                }
                return sqlBuilder.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 插入信息
        public int Update(VideoInfo model)
        {
            int count = 0;
            try
            {
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    count = session.Update(model);
                }
                return count;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            
        }
        #endregion

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

        #region 删除
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name=""></param>
        /// <returns>删除个数</returns>
        public int Delete(VideoInfo model)
        {
            StringBuilder sqlBuilder = new StringBuilder();
            List<DataParameter> parameters = new List<DataParameter>();
            int count = 0;
            try
            {
                using (IDataSession session = AppDataFactory.CreateMainSession())
                {
                    //删除基本信息
                    sqlBuilder.Append("DELETE T_ODS_VideoInfo ");
                    sqlBuilder.Append("WHERE  PID = @PID ");
                    parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
                    count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray());

                }
                return count;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
        
    }
}