using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edu.Model.Entity.Mall;
using Edu.Model.ViewModel.Mall;

namespace Edu.Repository.Mall
{
   public class RB_Member_UserRepository:BaseRepository<RB_Member_User>
    {
        /// <summary>
        /// 根据查询条件获取用户实体类
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RB_Member_User_Extend GetMemberUserEntityRepository(RB_Member_User_Extend query)
        {
            string where = " 1=1 ";
            if (query.TenantId > 0)
            {
                where += $@" and {nameof(RB_Member_User.TenantId)}={query.TenantId}";
            }
            if (query.MallBaseId > 0)
            {
                where += $@" and {nameof(RB_Member_User.MallBaseId)}={query.MallBaseId}";
            }
            if (query.Id > 0)
            {
                where += $@" and {nameof(RB_Member_User.Id)}={query.Id}";
            }
            if (!string.IsNullOrEmpty(query.Name))
            {
                where += $@" and {nameof(RB_Member_User.Name)} like '%{query.Name}%'";
            }
            if (!string.IsNullOrEmpty(query.AliasName))
            {
                where += $@" and {nameof(RB_Member_User.AliasName)} like '%{query.AliasName}%'";
            }
            if (query.Source > 0)
            {
                where += $@" and {nameof(RB_Member_User.Source)} ={(int)query.Source}";
            }
            if (!string.IsNullOrEmpty(query.Moblie))
            {
                where += $@" and {nameof(RB_Member_User.Moblie)} like '%{query.Moblie}%'";
            }
            if (query.MemberGrade > 0)
            {
                where += $@" and {nameof(RB_Member_User.MemberGrade)}={query.MemberGrade}";
            }
            if (query.SuperiorId > 0)
            {
                where += $@" and {nameof(RB_Member_User.SuperiorId)}={query.SuperiorId}";
            }
            if (query.IsEmptyUserPhoto == 1)
            {
                where += $@" and IFNULL({nameof(RB_Member_User.Photo)},'')=''";
            }
            //HK06-01新增查询条件
            if (query.OpenId != null && !string.IsNullOrWhiteSpace(query.OpenId))
            {
                where += $@" and {nameof(RB_Member_User.OpenId)} LIKE '{query.OpenId}%'";
            }
            string sql = $@"select * from RB_Member_User where {where} ";
            return Get<RB_Member_User_Extend>(sql).FirstOrDefault();
        }
    }
}
