using Edu.Common.Enum;
using Edu.Model.Entity.System;
using Edu.Model.ViewModel.System;
using System;
using System.Collections.Generic;
using System.Text;
using VT.FW.DB.Dapper;
using System.Linq;

namespace Edu.Repository.System
{
    /// <summary>
    /// 渠道仓储层
    /// </summary>
    public class RB_ChannelRepository: BaseRepository <RB_Channel>
    {
        /// <summary>
        /// 获取渠道分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Channel_Extend> GetChannelPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Channel_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Channel AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Channel_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Channel_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Channel_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
            }
            return GetPage<RB_Channel_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取渠道列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Channel_Extend> GetChannelListRepository(RB_Channel_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            var parameters = new DynamicParameters();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Channel AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Channel_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Channel_Extend.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.Name))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @Name ", nameof(RB_Channel_Extend.Name));
                    parameters.Add("Name", "%" + query.Name.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.QChannelIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Channel_Extend.Id), query.QChannelIds);
                }
            }
            return Get<RB_Channel_Extend>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 获取学习目的实体类
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Channel_Extend GetChannelExtEntityRepository(int Id)
        {
            var extModel = base.GetEntity<RB_Channel_Extend>(Id);
            return extModel;
        }
    }
}
