using Edu.Common.Enum.User;
using Edu.Model.ViewModel.Log;
using Edu.Repository.Log;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Edu.Repository.User;
using Edu.Model.ViewModel.User;

namespace Edu.Module.Log
{
    /// <summary>
    /// 用户信息改变日志处理类
    /// </summary>
    public class UserChangeLogModule
    {
        /// <summary>
        /// 用户信息修改日志仓储层对象
        /// </summary>
        private readonly RB_User_ChangeLogRepository changeLogRepository = new RB_User_ChangeLogRepository();

        /// <summary>
        /// 账号仓储层对象
        /// </summary>
        private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();

        /// <summary>
        /// 获取用户信息改变日志列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public object GetUserChangeLogListModule(RB_User_ChangeLog_ViewModel query)
        {
            List<object> list = new List<object>();
            var logList= changeLogRepository.GetUserChangeLogListRepository(query);
            if (logList != null && logList.Count > 0)
            {
                var empList = new List<Employee_ViewModel>();
                string uids = string.Join(",", logList.Select(qitem => qitem.CreateBy));
                if (!string.IsNullOrEmpty(uids))
                {
                    empList= accountRepository.GetEmployeeListRepository(new Model.ViewModel.User.Employee_ViewModel()
                    {
                        QIds = uids
                    });
                }
                var dayList = logList.GroupBy(qitem => new { NewTime = Common.ConvertHelper.FormatDate(qitem.CreateTime) }).OrderByDescending(qitem => qitem.Key.NewTime).Select(qitem => new { qitem.Key.NewTime });
                if (dayList != null && dayList.Count() > 0)
                {
                    foreach (var item in dayList)
                    {
                        var subList = logList.Where(qitem => Common.ConvertHelper.FormatDate(qitem.CreateTime) == item.NewTime).ToList();
                        if (subList != null && subList.Count > 0)
                        {
                            list.Add(new
                            {
                                item.NewTime,
                                WeekStr = Common.ConvertHelper.GetWeekDay(Convert.ToDateTime(item.NewTime)),
                                LogList = subList.Select(qitem => new
                                {
                                    TimeStr = Common.ConvertHelper.FormatTimeStr(qitem.CreateTime),
                                    UserIcon = empList?.Where(eItem=> eItem.Id==qitem.CreateBy)?.FirstOrDefault()?.UserIcon??"",
                                    CreateName = empList?.Where(eItem => eItem.Id == qitem.CreateBy)?.FirstOrDefault()?.EmployeeName ?? "",
                                    qitem.LogContent
                                })
                            });
                        }
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 新增日志
        /// </summary>
        /// <param name="extModel"></param>
        /// <returns></returns>
        public bool SetUserChangeLogModule(RB_User_ChangeLog_ViewModel extModel)
        {
           return changeLogRepository.Insert(extModel) > 0;
        }

        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_User_ChangeLog_ViewModel> GetUserChangeLogList(int pageIndex, int pageSize, out long count, RB_User_ChangeLog_ViewModel dmodel)
        {
            return changeLogRepository.GetPageList(pageIndex, pageSize, out count, dmodel);
        }

        /// <summary>
        /// 新增日志
        /// </summary>
        /// <param name="CreateBy">创建人</param>
        /// <param name="GroupId">集团编号</param>
        /// <param name="SchoolId">校区编号</param>
        /// <param name="content">日志内容</param>
        /// <returns></returns>
        public bool SetUserChangeLogModule(int CreateBy, int GroupId,int SchoolId,string content, int Id, AccountTypeEnum accountType)
        {
            RB_User_ChangeLog_ViewModel model = new RB_User_ChangeLog_ViewModel()
            {
                Id = 0,
                CreateBy = CreateBy,
                CreateTime = DateTime.Now,
                Group_Id = GroupId,
                School_Id = SchoolId,
                LogContent = content.TrimStart(','),
                AccountType= accountType,
                SourceId= Id
            };
            return changeLogRepository.Insert(model) > 0; 
        }
    }
}
