using Mall.Model.Extend.User;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Mall.Model.Entity.User;
using VT.FW.DB.Dapper;

namespace Mall.Repository.User
{
    /// <summary>
    /// 商户用户仓储层
    /// </summary>
    public partial  class RB_Member_UserRepository2 : BaseRepository<RB_Member_User>
    {
        /// <summary>
        /// 获取商户用户列表
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Member_User_Extend> GetMemberUserListExtRepository(RB_Member_User_Extend dmodel)
        {
            DynamicParameters dynamicParameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT  IFNULL(B.Id,0) AS ShopId,IFNULL(B.Name,'') AS ShopName,A.* 
FROM rb_member_user AS A LEFT JOIN rb_smallshops_info AS B ON A.Id=B.UserId
WHERE 1=1
"); 
            if (dmodel.TenantId > 0)
            {
                builder.AppendFormat($@" and A.{nameof(RB_Member_User.TenantId)}={dmodel.TenantId}");
            }
            if (dmodel.MallBaseId > 0)
            {
                builder.AppendFormat($@" and A.{nameof(RB_Member_User.MallBaseId)}={dmodel.MallBaseId}");
            }
            if (!string.IsNullOrEmpty(dmodel.Name.Trim()))
            {
                builder.AppendFormat($@" and A.Name like @Name ");
                dynamicParameters.Add("Name", "%" + dmodel.Name.Trim() + "%");
            }
            return Get<RB_Member_User_Extend>(builder.ToString(), dynamicParameters).ToList();
        }

        /// <summary>
        /// 统计今日登录【总人数，内部员工人数，领队导游人数】
        /// </summary>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public object GetMemberUserStatisticsRepository(RB_Member_User_Extend dmodel)
        {
            StringBuilder where = new StringBuilder();
            if (dmodel.TenantId > 0)
            {
                where.AppendFormat($@" and A.{nameof(RB_Member_User.TenantId)}={dmodel.TenantId}");
            }
            if (dmodel.MallBaseId > 0)
            {
                where.AppendFormat($@" and A.{nameof(RB_Member_User.MallBaseId)}={dmodel.MallBaseId}");
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT * FROM rb_member_user AS A
INNER JOIN
(
		SELECT DISTINCT UserId 
		FROM rb_login_log
		WHERE DATE_FORMAT(LoginTime,'%y/%m/%d')=DATE_FORMAT('{1}','%y/%m/%d')
) AS B ON A.Id=B.UserId
WHERE A.SuperiorId>0 AND A.SuperiorId<20 AND A.PostId=0 {0}
UNION ALL 

SELECT * FROM rb_member_user AS A
INNER JOIN
(
		SELECT DISTINCT UserId 
		FROM rb_login_log
		WHERE DATE_FORMAT(LoginTime,'%y/%m/%d')=DATE_FORMAT('{1}','%y/%m/%d')
) AS B ON A.Id=B.UserId
WHERE   A.PostId=1 {0}
", where.ToString(), DateTime.Now.ToString("yyyy-MM-dd"));
            var list = Get<RB_Member_User_Extend>(builder.ToString()).ToList();
            var obj = new
            {
                LoginTotal = list?.Count ?? 0,
                LoginEmployee = list?.Where(qitem => qitem.PostId == 0)?.Count() ?? 0,
                LoginLeader = list?.Where(qitem => qitem.PostId == 1)?.Count() ?? 0,
            };
            return obj;
        }
    }
}
