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

namespace Mall.Repository.Miai
{
    public class RB_MiAi_BaseInfoRepository : BaseRepository<RB_MiAi_BaseInfo>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_MiAi_BaseInfo); } }

        /// <summary>
        /// 用户所在分组仓储层对象
        /// </summary>
        private readonly RB_Member_GroupRepository member_GroupRepository = new RB_Member_GroupRepository();

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<RB_MiAi_BaseInfo_Extend> GetBaseInfoList(RB_MiAi_BaseInfo_Extend where)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@"SELECT * from RB_MiAi_BaseInfo  where Status=0");
            if (where != null)
            {
                if (where.TenantId > 0)
                {
                    sb.AppendFormat(" and TenantId={0}", where.TenantId);
                }
                if (where.MallBaseId > 0)
                {
                    sb.AppendFormat(" and MallBaseId={0}", where.MallBaseId);
                }
                if (where.Id > 0)
                {
                    sb.AppendFormat(" and Id={0}", where.Id);
                }
                if (where.UserId > 0)
                {
                    sb.AppendFormat(" and UserId={0}", where.UserId);
                }
            }
            return Get<RB_MiAi_BaseInfo_Extend>(sb.ToString()).ToList();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<RB_MiAi_BaseInfo_Extend> GetBaseInfoPageList(int pageIndex, int pageSize, out long rowsCount, RB_MiAi_BaseInfo_Extend where)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@"
SELECT a.*,b.`Name`,b.Photo 
FROM RB_MiAi_BaseInfo  as a LEFT JOIN rb_member_user as b on a.UserId=b.Id  
WHERE Status=0 and  b.Blacklist!=1
");
            if (where != null)
            {
                if (where.TenantId > 0)
                {
                    sb.AppendFormat(" and a.TenantId={0}", where.TenantId);
                }
                if (where.MallBaseId > 0)
                {
                    sb.AppendFormat(" and a.MallBaseId={0}", where.MallBaseId);
                }
                if (where.Id > 0)
                {
                    sb.AppendFormat(" and a.Id={0}", where.Id);
                }
                if (where.UserId > 0)
                {
                    sb.AppendFormat(" and a.UserId={0}", where.UserId);
                }

                if (!string.IsNullOrWhiteSpace(where.Name))
                {
                    sb.AppendFormat(" and b.Name like'%{0}%'", where.Name);
                }
                if (where.Sex > 0)
                {
                    sb.AppendFormat(" and a.Sex ={0}", where.Sex);
                }
            }
            return GetPage<RB_MiAi_BaseInfo_Extend>(pageIndex, pageSize, out rowsCount, sb.ToString()).ToList();
        }


        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<RB_MiAi_BaseInfo_Extend> GetFirstBaseInfoPageList_V2(int pageIndex, int pageSize, out long rowsCount, RB_MiaiMember_Extend where)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@"
SELECT * 
FROM 
(
    SELECT c.*,b.`Name`,b.Photo,b.BelongGroupIds,b.IsShow,(YEAR (curdate())- YEAR (c.Birthday )- 1+ (DATE_FORMAT(  c.Birthday, '%m%d' )<=(DATE_FORMAT( curdate(), '%m%d' )))) Age   
    FROM RB_MiAi_BaseInfo as c LEFT JOIN rb_member_user as b on c.UserId=b.Id  
    WHERE c.Status=0 and b.Blacklist!=1
) as a  
WHERE 1=1");
            //HK 2021-10-28 新增
            sb.Append(" and a.IsShow=1 ");
            List<int> groupIdList = new List<int>();
            if (!string.IsNullOrEmpty(where.UserGroupIds))
            {
                groupIdList = member_GroupRepository.GetLookGroupIdsRepository(new Model.Extend.User.RB_Member_Group_Extend()
                {
                    TenantId = where.TenantId ?? 0,
                    MallBaseId = where.MallBaseId ?? 0,
                    Q_GroupIds = where.UserGroupIds,
                });
            }
            if (where != null)
            {
                if (groupIdList != null && groupIdList.Count > 0)
                {
                    string tempStr = "";
                    for (var i = 0; i < groupIdList.Count; i++)
                    {
                        if (i > 0)
                        {
                            tempStr += " OR ";
                        }
                        tempStr += string.Format(" FIND_IN_SET('{0}',a.BelongGroupIds) ", groupIdList[i]);
                    }
                    if (!string.IsNullOrEmpty(tempStr))
                    {
                        sb.AppendFormat(" and ({0})", tempStr);
                    }
                }
                if (where.TenantId > 0)
                {
                    sb.AppendFormat(" and a.TenantId={0}", where.TenantId);
                }
                if (where.MallBaseId > 0)
                {
                    sb.AppendFormat(" and a.MallBaseId={0}", where.MallBaseId);
                }
                if (where.Id > 0)
                {
                    sb.AppendFormat(" and a.Id={0}", where.Id);
                }
                if (where.UserId > 0)
                {
                    sb.AppendFormat(" and a.UserId !={0}", where.UserId);
                }
                if (where.Sex > 0)
                {
                    sb.AppendFormat(" and a.Sex ={0}", where.Sex);
                }
                if (!string.IsNullOrWhiteSpace(where.UserIds))
                {
                    sb.AppendFormat(" and a.UserId in({0})", where.UserIds);
                }
                if (where.Age > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Age)} >={where.Age}");
                }
                if (where.EndAge > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Age)} <={where.EndAge}");
                }
                if (where.Height > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Height)} >={where.Height}");
                }
                if (where.EndHeight > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Height)} <={where.EndHeight}");
                }
                if (where.Weight > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Weight)} >={where.Weight}");
                }
                if (where.EndWeight > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Weight)} <={where.EndWeight}");
                }

                if (where.YearMoney > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.YearMoney)} >={where.YearMoney}");
                }
                if (where.EndYearMoney > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.YearMoney)} <={where.EndYearMoney}");
                }

                if (where.CarInfo.HasValue && where.CarInfo.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.CarInfo)} ={(int)where.CarInfo}");
                }

                if (where.HouseInfo.HasValue && where.HouseInfo.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.HouseInfo)} ={(int)where.HouseInfo}");
                }

                if (where.Marriage.HasValue && where.Marriage.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Marriage)} ={(int)where.Marriage}");
                }

                if (where.EducationType.HasValue && where.EducationType.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.EducationType)} ={(int)where.EducationType}");
                }

                sb.AppendFormat(" ORDER BY a.CreateDate desc ");

            }
            return GetPage<RB_MiAi_BaseInfo_Extend>(pageIndex, pageSize, out rowsCount, sb.ToString()).ToList();
        }


        

        /// <summary>
        /// 获取每日随机基础信息
        /// </summary>
        /// <param name="where"></param>
        /// <param name="Top"></param>
        /// <returns></returns>
        public List<RB_MiAi_BaseInfo_Extend> GetDailyRandomList(RB_MiaiMember_Extend where, int Top)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@" SELECT * FROM rb_miai_baseinfo  as a where a.`Status`=0  ");
            if (where != null)
            {
                if (where.TenantId > 0)
                {
                    sb.AppendFormat(" and a.TenantId={0}", where.TenantId);
                }
                if (where.MallBaseId > 0)
                {
                    sb.AppendFormat(" and a.MallBaseId={0}", where.MallBaseId);
                }
                if (where.Id > 0)
                {
                    sb.AppendFormat(" and a.Id={0}", where.Id);
                }
                if (where.UserId > 0)
                {
                    sb.AppendFormat(" and a.UserId!={0}", where.UserId);
                }
                if (where.Sex > 0)
                {
                    sb.AppendFormat(" and a.Sex ={0}", where.Sex);
                }
                if (!string.IsNullOrWhiteSpace(where.UserIds))
                {
                    sb.AppendFormat(" and a.UserId in({0})", where.UserIds);
                }
            }
            sb.AppendFormat($@" ORDER BY RAND()  LIMIT {Top}");

            return Get<RB_MiAi_BaseInfo_Extend>(sb.ToString()).ToList();
        }


        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<RB_MiAi_BaseInfo_Extend> GetFirstBaseInfoList(RB_MiAi_BaseInfo_Extend where)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@"
SELECT a.*,b.`Name`,b.Photo,(YEAR (curdate())- YEAR (a.Birthday)- 1+ (DATE_FORMAT(a.Birthday,'%m%d')<=(DATE_FORMAT(curdate(),'%m%d')))) Age    
FROM RB_MiAi_BaseInfo as a LEFT JOIN rb_member_user as b on a.UserId=b.Id  
WHERE a.Status=0 and b.Blacklist!=1 ");
            //HK 2021-10-28 新增
            sb.Append(" and b.IsShow=1 ");
            List<int> groupIdList = new List<int>();
            if (!string.IsNullOrEmpty(where.UserGroupIds))
            {
                groupIdList = member_GroupRepository.GetLookGroupIdsRepository(new Model.Extend.User.RB_Member_Group_Extend()
                {
                    TenantId = where.TenantId ?? 0,
                    MallBaseId = where.MallBaseId ?? 0,
                    Q_GroupIds = where.UserGroupIds,
                });
            }
            if (where != null)
            {
                if (groupIdList != null && groupIdList.Count > 0)
                {
                    string tempStr = "";
                    for (var i = 0; i < groupIdList.Count; i++)
                    {
                        if (i > 0)
                        {
                            tempStr += " OR ";
                        }
                        tempStr += string.Format("  FIND_IN_SET('{0}',b.BelongGroupIds) ", groupIdList[i]);
                    }
                    if (!string.IsNullOrEmpty(tempStr))
                    {
                        sb.AppendFormat(" and ({0})", tempStr);
                    }
                }
                if (where.TenantId > 0)
                {
                    sb.AppendFormat(" and a.TenantId={0}", where.TenantId);
                }
                if (where.MallBaseId > 0)
                {
                    sb.AppendFormat(" and a.MallBaseId={0}", where.MallBaseId);
                }
                if (where.Id > 0)
                {
                    sb.AppendFormat(" and a.Id={0}", where.Id);
                }
                if (where.UserId > 0)
                {
                    sb.AppendFormat(" and a.UserId !={0}", where.UserId);
                }
                if (where.Sex > 0)
                {
                    sb.AppendFormat(" and a.Sex !={0}", where.Sex);
                }

                if (!string.IsNullOrWhiteSpace(where.UserIds))
                {
                    sb.AppendFormat(" and a.UserId in({0})", where.UserIds);
                }

                if (where.Sort == 1)
                {
                    // sb.AppendFormat("  order by ");
                }
                else if (where.Sort == 2)
                {
                    sb.AppendFormat($@" ORDER BY (((a.FollowNum* {where.FollowRate})+(a.BrowseNum*{where.BrowseRate}))) desc");
                }
                else if (where.Sort == 3)
                {
                    sb.AppendFormat(" ORDER BY b.CreateDate desc ");
                }
            }
            return Get<RB_MiAi_BaseInfo_Extend>(sb.ToString()).ToList();
        }

        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<RB_MiAi_BaseInfo_Extend> GetFirstBaseInfoPageList(int pageIndex, int pageSize, out long rowsCount, RB_MiAi_BaseInfo_Extend where)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@"
SELECT a.*,b.`Name`,b.Photo,(YEAR (curdate())- YEAR (a.Birthday )- 1+ (DATE_FORMAT(a.Birthday, '%m%d' )<=(DATE_FORMAT( curdate(), '%m%d' )))) Age   
FROM RB_MiAi_BaseInfo as a LEFT JOIN rb_member_user as b on a.UserId=b.Id   
WHERE a.Status=0 AND b.Blacklist!=1 ");
            //HK 2021-10-28 新增
            sb.Append(" AND b.IsShow=1 ");
            List<int> groupIdList = new List<int>();
            if (!string.IsNullOrEmpty(where.UserGroupIds))
            {
                groupIdList = member_GroupRepository.GetLookGroupIdsRepository(new Model.Extend.User.RB_Member_Group_Extend()
                {
                    TenantId = where.TenantId ?? 0,
                    MallBaseId = where.MallBaseId ?? 0,
                    Q_GroupIds = where.UserGroupIds,
                });
            }
            if (where != null)
            {
                if (groupIdList != null && groupIdList.Count > 0)
                {
                    string tempStr = "";
                    for (var i = 0; i < groupIdList.Count; i++)
                    {
                        if (i > 0)
                        {
                            tempStr += " OR ";
                        }
                        tempStr += string.Format("  FIND_IN_SET('{0}',b.BelongGroupIds) ", groupIdList[i]);
                    }
                    if (!string.IsNullOrEmpty(tempStr))
                    {
                        sb.AppendFormat(" and ({0})", tempStr);
                    }
                }
                if (!string.IsNullOrEmpty(where.LookGroupIds))
                {
                    sb.AppendFormat(" AND FIND_IN_SET('{0}',b.BelongGroupIds) ", where.LookGroupIds);
                }

                if (where.TenantId > 0)
                {
                    sb.AppendFormat(" and a.TenantId={0}", where.TenantId);
                }
                if (where.MallBaseId > 0)
                {
                    sb.AppendFormat(" and a.MallBaseId={0}", where.MallBaseId);
                }
                if (where.Id > 0)
                {
                    sb.AppendFormat(" and a.Id={0}", where.Id);
                }
                if (where.UserId > 0)
                {
                    sb.AppendFormat(" and a.UserId !={0}", where.UserId);
                }
                if (where.Sex > 0)
                {
                    sb.AppendFormat(" and a.Sex !={0}", where.Sex);
                }
                if (!string.IsNullOrWhiteSpace(where.UserIds))
                {
                    sb.AppendFormat(" and a.UserId in({0})", where.UserIds);
                }

                if (where.Sort == 1)
                {
                    // sb.AppendFormat("  order by ");
                }
                else if (where.Sort == 2)
                {

                    sb.AppendFormat($@" ORDER BY (((a.FollowNum* {where.FollowRate})+(a.BrowseNum*{where.BrowseRate}))) desc");
                }
                else if (where.Sort == 3)
                {
                    sb.AppendFormat(" ORDER BY b.CreateDate desc ");
                }
            }
            return GetPage<RB_MiAi_BaseInfo_Extend>(pageIndex, pageSize, out rowsCount, sb.ToString()).ToList();
        }


        /// <summary>
        /// 首页插件匹配分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<RB_MiAi_BaseInfo_Extend> GetMatchingBaseInfoList(RB_MiaiMember_Extend where, int Top)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($@"
SELECT * 
FROM 
    (
        SELECT a.*,b.`Name`,b.Photo,b.BelongGroupIds,b.IsShow,(YEAR (curdate())- YEAR ( a.Birthday )- 1+ (DATE_FORMAT(  a.Birthday, '%m%d' )<=(DATE_FORMAT( curdate(), '%m%d' )))) Age 
        FROM `RB_MiAi_BaseInfo` AS a LEFT JOIN rb_member_user as b on a.UserId=b.Id  
        WHERE a.Status=0 and b.Blacklist!=1
    ) as a 
WHERE 1=1");

            //HK 2021-10-28 新增
            sb.Append(" and a.IsShow=1 ");
            List<int> groupIdList = new List<int>();
            if (!string.IsNullOrEmpty(where.UserGroupIds))
            {
                groupIdList = member_GroupRepository.GetLookGroupIdsRepository(new Model.Extend.User.RB_Member_Group_Extend()
                {
                    TenantId = where.TenantId ?? 0,
                    MallBaseId = where.MallBaseId ?? 0,
                    Q_GroupIds = where.UserGroupIds,
                });
            }
            if (where != null)
            {
                if (groupIdList != null && groupIdList.Count > 0)
                {
                    string tempStr = "";
                    for (var i = 0; i < groupIdList.Count; i++)
                    {
                        if (i > 0)
                        {
                            tempStr += " OR ";
                        }
                        tempStr += string.Format(" FIND_IN_SET('{0}',a.BelongGroupIds) ", groupIdList[i]);
                    }
                    if (!string.IsNullOrEmpty(tempStr))
                    {
                        sb.AppendFormat(" and ({0})", tempStr);
                    }
                }
                if (where.TenantId > 0)
                {
                    sb.AppendFormat(" and a.TenantId={0}", where.TenantId);
                }
                if (where.MallBaseId > 0)
                {
                    sb.AppendFormat(" and a.MallBaseId={0}", where.MallBaseId);
                }
                if (where.Id > 0)
                {
                    sb.AppendFormat(" and a.Id={0}", where.Id);
                }
                if (where.UserId > 0)
                {
                    sb.AppendFormat(" and a.UserId!={0}", where.UserId);
                }
                if (where.Sex > 0)
                {
                    sb.AppendFormat(" and a.Sex!={0}", where.Sex);
                }
                if (!string.IsNullOrWhiteSpace(where.UserIds))
                {
                    sb.AppendFormat(" and a.UserId in({0})", where.UserIds);
                }
                if (where.Age > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Age)} >={where.Age}");
                }
                if (where.EndAge > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Age)} <={where.EndAge}");
                }
                if (where.Height > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Height)} >={where.Height}");
                }
                if (where.EndHeight > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Height)} <={where.EndHeight}");
                }
                if (where.Weight > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Weight)} >={where.Weight}");
                }
                if (where.EndWeight > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Weight)} <={where.EndWeight}");
                }

                if (where.YearMoney > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.YearMoney)} >={where.YearMoney}");
                }
                if (where.EndYearMoney > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.YearMoney)} <={where.EndYearMoney}");
                }

                if (where.CarInfo.HasValue && where.CarInfo.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.CarInfo)} ={(int)where.CarInfo}");
                }

                if (where.HouseInfo.HasValue && where.HouseInfo.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.HouseInfo)} ={(int)where.HouseInfo}");
                }

                if (where.Marriage.HasValue && where.Marriage.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.Marriage)} ={(int)where.Marriage}");
                }

                if (where.EducationType.HasValue && where.EducationType.Value > 0)
                {
                    sb.AppendFormat($@" and a.{nameof(RB_MiaiMember_Extend.EducationType)} ={(int)where.EducationType}");
                }
            }


            sb.AppendFormat($@" ORDER BY a.id LIMIT {Top}");

            return Get<RB_MiAi_BaseInfo_Extend>(sb.ToString()).ToList();
        }
    }
}
