using System.Collections.Generic;
using System.Linq;
using Edu.Model.Entity.Advertising;
using Edu.Model.ViewModel.Advertising;

namespace Edu.Repository.Advertising
{
    /// <summary>
    /// 广告仓储层
    /// </summary>
    public class Rb_AdvertisingRepository : BaseRepository<Rb_Advertising>
    {
        /// <summary>
        /// 广告列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="lineId"></param>
        /// <param name="seriesId"></param>
        /// <param name="countryId"></param>
        /// <param name="advName"></param>
        /// <param name="IsFee"></param>
        /// <param name="RB_Group_id"></param>
        /// <returns></returns>
        public List<Rb_Advertising_Extend> GetAdvertisingList(int pageIndex, int pageSize, out long count,   string advName, int IsFee, int RB_Group_id)
        {
            string where = $@" where 1=1 and A.{nameof(Rb_Advertising.Group_Id)}={RB_Group_id} and A.{nameof(Rb_Advertising.Status)}=0";
            if (IsFee > 0)
            {
                where += $@" and A.{nameof(Rb_Advertising.IsFee)}={IsFee}";
            }
            if (!string.IsNullOrWhiteSpace(advName))
            {
                where += $@" and A.{nameof(Rb_Advertising.Title)} like '%{advName}%'";
            }
            string sql = $@"select *,(SELECT count(*) from Rb_Advertising_Make where `Status`=0 and AdvertisingID=A.ID) as AdvertisingCount from {nameof(Rb_Advertising)}  AS A {where} order by A.{nameof(Rb_Advertising.CreateDate)} desc";
            return GetPage<Rb_Advertising_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 根据where 查询列表
        /// </summary>
        /// <param name="lineId"></param>
        /// <param name="seriesId"></param>
        /// <param name="countryId"></param>
        /// <param name="advName"></param>
        /// <param name="RB_Group_id"></param>
        /// <returns></returns>
        public List<Rb_Advertising_Extend> GetAdvertsingListForComboBox( string advName, int RB_Group_id)
        {
            string where = $@" where 1=1 and {nameof(Rb_Advertising.Group_Id)}={RB_Group_id} and {nameof(Rb_Advertising.Status)}=0";
            if (!string.IsNullOrWhiteSpace(advName))
            {
                where += $@" and {nameof(Rb_Advertising.Title)} like '%{advName}%'";
            }
            return Get<Rb_Advertising_Extend>($@"select * from {nameof(Rb_Advertising)} {where} order by {nameof(Rb_Advertising.CreateDate)} desc").ToList();
        }
    }
}