using System;
using System.Collections.Generic;
using System.Text;
using Mall.Model.Entity.Product;
using Mall.Model.Extend.Product;
using System.Linq;

namespace Mall.Repository.Product
{
    /// <summary>
    /// 商品关联课程仓储层
    /// </summary>
    public class RB_Goods_WK_CourseRepository : BaseRepository<RB_Goods_WK_Course>
    {

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="dmodel">查询条件</param>
        /// <returns></returns>
        public List<RB_Goods_WK_Course_Extend> GetList(RB_Goods_WK_Course_Extend dmodel)
        {
            string where = $" 1=1 and gc.{nameof(RB_Goods_WK_Course.Status)}=0";
            if (dmodel.TenantId > 0) {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0) {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.Id > 0) {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.Id)}={dmodel.Id}";
            }
            if (dmodel.GoodsId > 0) {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.GoodsId)}={dmodel.GoodsId}";
            }
            if (!string.IsNullOrEmpty(dmodel.GoodsIds)) {
                where += $@" and gc.{nameof(RB_Goods_Category.GoodsId)} in({dmodel.GoodsIds})";
            }
            
            string sql = $@"
SELECT gc.* FROM RB_Goods_WK_Course gc 
WHERE {where} ORDER BY gc.Sort asc ";
            return Get<RB_Goods_WK_Course_Extend>(sql).ToList();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_WK_Course_Extend> GetPageList(int pageIndex, int pageSize, out long count, RB_Goods_WK_Course_Extend dmodel)
        {
            string where = $" 1=1 and gc.{nameof(RB_Goods_WK_Course.Status)}=0";
            if (dmodel.TenantId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.Id > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.Id)}={dmodel.Id}";
            }
            if (dmodel.GoodsId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.GoodsId)}={dmodel.GoodsId}";
            }
            if (!string.IsNullOrEmpty(dmodel.GoodsIds))
            {
                where += $@" and gc.{nameof(RB_Goods_Category.GoodsId)} in({dmodel.GoodsIds})";
            }

            string sql = $@"
SELECT gc.* FROM RB_Goods_WK_Course gc 
WHERE {where} ORDER BY gc.Sort asc ";
            return GetPage<RB_Goods_WK_Course_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }


        /// <summary>
        /// 未缓存列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_WK_Course_Extend> GetMyCourseNoCachePageList(int pageIndex, int pageSize, out long count, RB_Goods_WK_Course_Extend dmodel)
        {
            string where = $" 1=1 and gc.{nameof(RB_Goods_WK_Course.Status)}=0 and gc.IsCache =1 and d.Id is null";
            if (dmodel.TenantId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.Id > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.Id)}={dmodel.Id}";
            }
            if (dmodel.GoodsId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.GoodsId)}={dmodel.GoodsId}";
            }
            if (!string.IsNullOrEmpty(dmodel.GoodsIds))
            {
                where += $@" and gc.{nameof(RB_Goods_Category.GoodsId)} in({dmodel.GoodsIds})";
            }

            string sql = $@"
SELECT gc.* FROM RB_Goods_WK_Course gc 
left join rb_goods_wk_download d on gc.Id=d.CourseId and d.FileState <> 3 and d.UserId={dmodel.UserId}
WHERE {where} ORDER BY gc.Sort asc ";
            return GetPage<RB_Goods_WK_Course_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 已缓存列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_WK_Course_Extend> GetMyCourseCachedPageList(int pageIndex, int pageSize, out long count, RB_Goods_WK_Course_Extend dmodel)
        {
            string where = $" 1=1 and gc.{nameof(RB_Goods_WK_Course.Status)}=0 and gc.IsCache =1 and d.UserId={dmodel.UserId}";
            if (dmodel.TenantId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.Id > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.Id)}={dmodel.Id}";
            }
            if (dmodel.GoodsId > 0)
            {
                where += $@" and gc.{nameof(RB_Goods_WK_Course.GoodsId)}={dmodel.GoodsId}";
            }
            if (!string.IsNullOrEmpty(dmodel.GoodsIds))
            {
                where += $@" and gc.{nameof(RB_Goods_Category.GoodsId)} in({dmodel.GoodsIds})";
            }

            string sql = $@"
SELECT gc.Id,gc.Sort,gc.SortName,gc.Name,gc.GoodsId,d.FileState,d.FileType as Type,d.FilePath,d.Size,d.Unit FROM RB_Goods_WK_Course gc 
inner join rb_goods_wk_download d on gc.Id=d.CourseId 
WHERE {where} ORDER BY gc.Sort asc ";
            return GetPage<RB_Goods_WK_Course_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }
    }
}
