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

namespace Mall.Module.User
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class MemberUserModule
    {
        /// <summary>
        /// 用户管理
        /// </summary>
        private readonly RB_Member_UserRepository member_UserRepository = new RB_Member_UserRepository();

        /// <summary>
        /// 根据用户Id更新用户头像和名称
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public bool SetMemberUserNameAndPhoto(RB_Member_User_Extend extModel)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Member_User_Extend.Name),extModel.AliasName},
                { nameof(RB_Member_User_Extend.Photo),extModel.Photo},
            };
            return member_UserRepository.Update(fileds, new WhereHelper(nameof(RB_Member_User_Extend.Id), extModel.Id));
            //bool falg= member_UserRepository.UpdateMemberUserRepository(new RB_Member_User_Extend()
            //{
            //     Name=extModel.AliasName,
            //     Photo=extModel.Photo,
            //     Id=extModel.Id
            //});
            //return falg;
        }

        /// <summary>
        /// 获取用户佣金
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public RB_Member_User_Extend GetUserCommissionModule(int UserId)
        {
            RB_Member_User_Extend model = new RB_Member_User_Extend() { Id = UserId };
            //总佣金
            var totalList = member_UserRepository.GetUserCommissionRepository(new RB_Member_User_Extend() { UserIds=UserId.ToString()});
            //本月佣金
            var currentMonthList = member_UserRepository.GetUserCommissionRepository(new RB_Member_User_Extend() { UserIds = UserId.ToString() }, IsGetCurrentMonth: true);
            if (totalList != null && totalList.Count > 0)
            {
                model.TotalCommission = totalList.Sum(qitem => qitem.Commission);
            }
            if (currentMonthList != null && currentMonthList.Count > 0)
            {
                model.Commission = currentMonthList.Sum(qitem => qitem.Commission);
            }
            return model;
        }
    }
}
