using System.Collections.Generic;
using GxPress.Repository.Interface.UserMiddle;
using GxPress.Request.UserMiddle;
using GxPress.Service.Interface.UserMiddle;
using GxPress.Common.Exceptions;
using System.Threading.Tasks;
using GxPress.Repository.Interface;
using Datory;
using System.Linq;
namespace GxPress.Service.Implement.UserMiddle
{
    public class UserMiddleService : IUserMiddleService
    {
        private readonly IUserMiddleRepository _userMiddleRepoitory;
        private readonly IUserRepository _userRepoitory;
        private readonly IAddressBookGroupUserRepository _addressBookGroupUserRepository;
        private readonly IAddressBookGroupRepository _addressBookGroupRepository;
        private readonly IDepartmentRepository _departmentRepository;
        public UserMiddleService(IUserMiddleRepository userMiddleRepository, IUserRepository userRepository, IAddressBookGroupUserRepository addressBookGroupUserRepository, IDepartmentRepository departmentRepository, IAddressBookGroupRepository addressBookGroupRepository)
        {
            _userMiddleRepoitory = userMiddleRepository;
            _userRepoitory = userRepository;
            _addressBookGroupUserRepository = addressBookGroupUserRepository;
            _departmentRepository = departmentRepository;
            _addressBookGroupRepository = addressBookGroupRepository;
        }
        /// <summary>
        /// 添加用户中间表
        /// </summary>
        /// <param name="userMiddles"></param>
        /// <returns></returns>
        public async Task<bool> AddUserMiddleAsync(List<UserMiddleInRequest> request)
        {
            try
            {
                foreach (var userMiddle in request)
                {
                    //添加用户中间件
                    var userMiddleDto = new Entity.UserMiddle.UserMiddle();
                    userMiddleDto.SourceId = userMiddle.SourceId;
                    userMiddleDto.SourceType = userMiddle.SourceType;
                    userMiddleDto.MiddleType = userMiddle.MiddleType;
                    userMiddleDto.SourceName = userMiddle.SourceName;
                    userMiddleDto.DataSourceId = userMiddle.DataSourceId;
                    await _userMiddleRepoitory.InsertAsync(userMiddleDto);
                }
            }
            catch (System.Exception ex)
            {
                throw new BusinessException(ex.Message);
            }
            return true;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(SqlKata.Query query)
        {
            return await _userMiddleRepoitory.DeleteAsync(query);
        }

       /// <summary>
       ///  删除收件人
       /// </summary>
       /// <param name="dataSourceId"></param>
       /// <param name="middleType">类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组  10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要</param>
       /// <returns></returns>
        public async Task<bool> DeleteAsync(int dataSourceId, int middleType)
        {
            return await _userMiddleRepoitory.DeleteAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), dataSourceId).Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), middleType));
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<bool> ExistsAsync(SqlKata.Query query)
        {
            return await _userMiddleRepoitory.ExistsAsync(query);
        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="middleType">来源类型 1:通知收件人 2:通知抄送人 3:站内信收集人 4:站内信抄送人 5:话题  6:笔记共享文件夹 7:收藏共享文件夹 </param>
        /// <param name="sourceId"></param>
        /// <returns></returns>
        public async Task<List<Entity.User>> FindUsersAsync(List<Request.UserMiddle.UserMiddleInRequest> userMiddles, int userId)
        {
            List<Entity.User> users = new List<Entity.User>();
            var userIds = new List<int>();
            userIds = userMiddles.Where(n => n.SourceType == 0).Select(n => n.SourceId).ToList();
            if (userIds.Count() > 0)
                //获取用户
                users.AddRange(await _userRepoitory.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), userIds)));
            foreach (var userMiddle in userMiddles)
            {
                if (userMiddle.SourceType != 0)
                {
                    if (userMiddle.SourceType == 1)
                    {
                        if (userMiddle.SourceId == 0)
                            //获取所有用户
                            users.AddRange(await _userRepoitory.GetAllAsync(Q.Where(nameof(Entity.User.Id), ">", 0)));
                        else
                        {
                            //递归获取部门
                            var departments = new List<Entity.Department>();
                            departments = await _departmentRepository.GetDepartmentById(userMiddle.SourceId, departments);
                            users.AddRange(await _userRepoitory.GetAllAsync(Q.WhereIn(nameof(Entity.User.DepartmentId), departments.Select(n => n.Id))));
                        }
                    }
                    if (userMiddle.SourceType == 3)
                    {
                        if (userMiddle.SourceId == 0)
                        {
                            var addressBookGroups = await _addressBookGroupRepository.GetAllAsync(Q.Where(nameof(Entity.AddressBookGroup.UserId), userId));
                            var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), addressBookGroups.Select(n => n.Id)));
                            userIds = addressBookGroupUsers.Select(n => n.UserId).ToList();
                            //获取用户
                            users.AddRange(await _userRepoitory.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), userIds)));
                        }
                        else
                        {
                            //获取自建分组
                            var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(Q.Where(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), userMiddle.SourceId));
                            userIds = addressBookGroupUsers.Select(n => n.UserId).ToList();
                            //获取用户
                            users.AddRange(await _userRepoitory.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), userIds)));
                        }
                    }
                }
            }
            users = users.Distinct().ToList();
            return users;
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="middleType">类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组  10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要 </param>
        /// <param name="sourceId"></param>
        /// <returns></returns>
        public async Task<IEnumerable<Entity.UserMiddle.UserMiddle>> FindUserMiddlesAsync(int middleType, int dataSourceId)
        {
            var userMiddles = await _userMiddleRepoitory.GetAllAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), middleType).Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), dataSourceId));
            return userMiddles;
        }
    }
}