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

namespace Mall.Repository.User
{
    /// <summary>
    /// 商户会员等级仓储层
    /// </summary>
    public class RB_Member_GradeRepository : BaseRepository<RB_Member_Grade>
    {

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="dmodel">查询条件</param>
        /// <returns></returns>
        public List<RB_Member_Grade_Extend> GetPageList(int pageIndex, int pageSize, out long rowCount, RB_Member_Grade_Extend dmodel)
        {
            string where = $" 1=1 and {nameof(RB_Member_Grade.Status)}=0 ";
            if (dmodel.TenantId > 0) {
                where += $@" and {nameof(RB_Member_Grade.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.Grade > 0) {
                where += $@" and {nameof(RB_Member_Grade.Grade)} ={(int)dmodel.Grade}";
            }
            if (!string.IsNullOrEmpty(dmodel.Name)) {
                where += $@" and {nameof(RB_Member_Grade.Name)} like '%{dmodel.Name}%'";
            }
            if (dmodel.Enabled > 0) {
                where += $@" and {nameof(RB_Member_Grade.Enabled)} ={(int)dmodel.Enabled}";
            }
            string sql = $@"select * from RB_Member_Grade where {where} order by Id desc";
            return GetPage<RB_Member_Grade_Extend>(pageIndex, pageSize, out rowCount, sql).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Member_Grade_Extend> GetList(RB_Member_Grade_Extend dmodel)
        {
            string where = $" 1=1 and {nameof(RB_Member_Grade.Status)}=0 ";
            if (dmodel.TenantId > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.Id > 0) {
                where += $@" and {nameof(RB_Member_Grade.Id)} ={(int)dmodel.Id}";
            }
            if (dmodel.Grade > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.Grade)} ={(int)dmodel.Grade}";
            }
            if (!string.IsNullOrEmpty(dmodel.Name))
            {
                where += $@" and {nameof(RB_Member_Grade.Name)} like '%{dmodel.Name}%'";
            }
            if (dmodel.Enabled > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.Enabled)} ={(int)dmodel.Enabled}";
            }
            if (dmodel.IsMemberBuy > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.IsMemberBuy)} ={dmodel.IsMemberBuy}";
            }
            string sql = $@"select * from RB_Member_Grade where {where} order by Grade asc";
            return Get<RB_Member_Grade_Extend>(sql).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Member_Grade_Extend> GetGrade(RB_Member_Grade_Extend dmodel)
        {
            string where = $" 1=1 and {nameof(RB_Member_Grade.Status)}=0 ";
            if (dmodel.TenantId > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.Grade > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.Grade)} ={(int)dmodel.Grade}";
            }
            if (!string.IsNullOrEmpty(dmodel.Name))
            {
                where += $@" and {nameof(RB_Member_Grade.Name)} like '%{dmodel.Name}%'";
            }
            if (dmodel.Enabled > 0)
            {
                where += $@" and {nameof(RB_Member_Grade.Enabled)} ={(int)dmodel.Enabled}";
            }
            string sql = $@"select Grade from RB_Member_Grade where {where} order by Grade asc";
            return Get<RB_Member_Grade_Extend>(sql).ToList();
        }


        /// <summary>
        /// 获取下一等级
        /// </summary>
        /// <param name="grade"></param>
        /// <param name="tenantId"></param>
        /// <param name="mallBaseId"></param>
        /// <returns></returns>
        public RB_Member_Grade_Extend GetMemberUpgrade(int? grade, int tenantId, int mallBaseId)
        {
            string where = $@" 1=1 and {nameof(RB_Member_Grade.Status)}=0";

            where += $@" and {nameof(RB_Member_Grade.TenantId)}={tenantId}";
            where += $@" and {nameof(RB_Member_Grade.MallBaseId)}={mallBaseId}";
            where += $@" and {nameof(RB_Member_Grade.Grade)}>{grade}";
            where += $@" and {nameof(RB_Member_Grade.Enabled)}=1";

            string sql = $@"select * from RB_Member_Grade where {where} order by Grade asc limit 1";
            return Get<RB_Member_Grade_Extend>(sql).FirstOrDefault();
        }
    }
}
