using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using GxPress.Common.Exceptions;
using GxPress.Common.Page;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.App.User;
using GxPress.Request.User;
using GxPress.Result.User;
using GxPress.Service.Interface;
using GxPress.Service.Interface.IM;
using Microsoft.AspNetCore.Hosting;
using OfficeOpenXml;
using Datory;
using GxPress.Common.Tools;
using GxPress.Result.App.User;
using GxPress.Repository.Interface.WorkFlow;
using GxPress.EnumConst;
using GxPress.Request.App.Flow;
using GxPress.Repository.Interface.WaitHandle;

namespace GxPress.Service.Implement
{
    public class UserService : IUserService
    {
        private readonly IUserRepository _userRepository;
        private readonly IDepartmentRepository _departmentRepository;
        private readonly IAddressBookGroupRepository _addressBookGroupRepository;
        private IWebHostEnvironment _webHostEnvironment;
        private readonly IBlacklistUserRepository _blacklistUserRepository;
        private readonly IRoleRepository _roleRepository;
        private readonly IIMService _imService;
        private readonly IMapper _mapper;
        private readonly IFlowRepository _flowRepository;
        private readonly IFlowTodoRepository _flowTodoRepository;
        private readonly IWaitHandleRepository waitHandleRepository;
        public UserService(IUserRepository userRepository, IDepartmentRepository departmentRepository,
            IAddressBookGroupRepository addressBookGroupRepository, IBlacklistUserRepository blacklistUserRepository,
            IMapper mapper, IWebHostEnvironment webHostEnvironment, IRoleRepository roleRepository, IIMService imService, IFlowRepository flowRepository, IFlowTodoRepository flowTodoRepository, IWaitHandleRepository waitHandleRepository)
        {

            _userRepository = userRepository;
            _departmentRepository = departmentRepository;
            _addressBookGroupRepository = addressBookGroupRepository;
            _blacklistUserRepository = blacklistUserRepository;
            _mapper = mapper;
            _webHostEnvironment = webHostEnvironment;
            _roleRepository = roleRepository;
            _imService = imService;
            _flowRepository = flowRepository;
            _flowTodoRepository = flowTodoRepository;
            this.waitHandleRepository = waitHandleRepository;
        }

        /// <summary>
        /// 根据部门ID获取自建ID获取用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<IEnumerable<UserInfoResult>> FindUser(FindUserRequest request)
        {
            var userInfoResults = new List<UserInfoResult>();
            if (request.TypeId == 1)
            {
                //获取部门
                List<Entity.Department> departments = new List<Entity.Department>();
                foreach (var item in request.ItemId)
                {
                    if (item > 0)
                    {
                        var users = await _userRepository.GetUserByDepartmentIdAsync(item);
                        userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
                    }
                    departments = await _departmentRepository.GetDepartmentByPid(item, departments);
                }
                foreach (var department in departments)
                {
                    //获取部门下面的员工
                    var users = await _userRepository.GetUserByDepartmentIdAsync(department.Id);
                    userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
                }
            }

            //查询自建用户
            if (request.TypeId == 2)
            {
                var users = new List<User>();
                foreach (var item in request.ItemId)
                {
                    if (item > 0)
                    {
                        users.AddRange(await _addressBookGroupRepository.GetUserByAddressBookGroupIdAsync(item));
                    }

                    users = await _addressBookGroupRepository.GetUserByAddressBookGroupPidAsync(users, item,
                        request.UserId);
                }

                userInfoResults.AddRange(users.Select(user => _mapper.Map<UserInfoResult>(user)).ToList());
            }

            foreach (var item in userInfoResults)
            {
                if (item == null)
                    continue;
                item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
            }

            return userInfoResults.Distinct();
        }

        /// <summary>
        /// 根据ID查询用户数据
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="blackUserId"></param>
        /// <returns></returns>
        public async Task<UserDetail> GetUserByIdAsync(int userId, int blackUserId)
        {
            var user = await _userRepository.GetAsync(blackUserId);
            if (user == null)
            {
                throw new BusinessException("用户id有误");
            }

            var userDetail = _mapper.Map<UserDetail>(user);
            if (user.DepartmentId > 0)
            {
                var department = await _departmentRepository.GetAsync(user.DepartmentId);
                userDetail.DepartmentName = department.Name;
            }
            userDetail.AvatarUrl = StringUtils.AddDomainMin(userDetail.AvatarUrl);
            userDetail.IsBlack = await _blacklistUserRepository.FindUserIsBlack(userId, blackUserId);
            return userDetail;
        }

        /// <summary>
        /// 生成excel
        /// </summary>
        /// <returns></returns>
        public async Task<string> ExcelUserAsync(List<int> userIds)
        {
            string sWebRootFolder = _webHostEnvironment.WebRootPath;
            string sFileName = $"{Guid.NewGuid()}.xlsx";
            FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
            using (ExcelPackage package = new ExcelPackage(file))
            {
                // 添加worksheet
                ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("员工信息"); //添加头
                worksheet.Cells[1, 1].Value = "姓名";
                worksheet.Cells[1, 2].Value = "手机号码";
                worksheet.Cells[1, 3].Value = "邮箱"; //添加值
                worksheet.Cells[1, 4].Value = "性别"; //添加值
                worksheet.Cells[1, 5].Value = "部门"; //添加值
                worksheet.Cells[1, 6].Value = "角色"; //添加值
                //获取用户
                var users = await _userRepository.FindAllUserAsync(Q.WhereIn(nameof(User.Id), userIds));
                var enumerable = users as User[] ?? users.ToArray();
                for (int i = 0; i < enumerable.Count(); i++)
                {
                    var index = i + 2;
                    worksheet.Cells["A" + index].Value = enumerable[i].Name;
                    worksheet.Cells["B" + index].Value = enumerable[i].Phone;
                    worksheet.Cells["C" + index].Value = enumerable[i].Email;
                    worksheet.Cells["D" + index].Value = enumerable[i].Gender == "Male" ? "男" : "女";
                    //获取部门
                    var department = await _departmentRepository.GetAsync(enumerable[i].DepartmentId);
                    worksheet.Cells["E" + index].Value = department == null ? "" : department.Name;
                    //获取角色
                    var role = await _roleRepository.GetAsync(enumerable[i].RoleId);
                    worksheet.Cells["F" + index].Value = role == null ? "" : role.Name;
                }

                package.Save();
            }

            return Path.Combine(sWebRootFolder, sFileName);

        }



        /// <summary>
        /// 用户分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PagedList<UserResult>> GetPagedListAsync(UserPageSearchRequest request)
        {
            var pagedList = new PagedList<UserResult>
            {
                Total = await GetCountAsync(request.DepartmentId, request.RoleId, request.Keyword)
            };
            var list = await GetPageListAsync(request.Page, request.PerPage, request.DepartmentId, request.RoleId,
                request.Keyword);
            var items = list.Select(user => _mapper.Map<UserResult>(user)).ToList();
            foreach (var item in items)
            {
                var departmentName = string.Empty;
                //获取部门
                departmentName =
                    await _departmentRepository.GetDepartmentFullPathAsync(item.DepartmentId, departmentName);
                item.DepartmentName = departmentName;
                var role = await _roleRepository.GetAsync(item.RoleId);
                if (role != null)
                    item.RoleName = role.Name;
            }

            pagedList.Items = items;
            return pagedList;
        }

        public async Task<int> GetCountAsync(int departmentId, int roleId, string keyword)
        {
            var query = Q.NewQuery();
            if (departmentId > 0)
            {
                query.Where(nameof(User.DepartmentId), departmentId);
            }

            if (roleId > 0)
            {
                query.Where(nameof(User.RoleId), roleId);
            }

            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = "%" + keyword + "%";
                query.Where(q =>
                    q.WhereLike(nameof(User.Name), keyword)
                        .OrWhereLike(nameof(User.Email), keyword)
                        .OrWhereLike(nameof(User.Phone), keyword)
                );
            }

            return await _userRepository.CountAsync(query);
        }

        /// <summary>
        /// Set the limit and offset for a given page.
        /// </summary>
        /// <param name="page"></param>
        /// <param name="perPage"></param>
        /// <param name="departmentId"></param>
        /// <param name="roleId"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<IEnumerable<User>> GetPageListAsync(int page, int perPage, int departmentId, int roleId,
            string keyword)
        {
            var query = Q.ForPage(page, perPage);
            if (departmentId > 0)
            {
                query.Where(nameof(User.DepartmentId), departmentId);
            }

            if (roleId > 0)
            {
                query.Where(nameof(User.RoleId), roleId);
            }

            if (!string.IsNullOrEmpty(keyword))
            {
                keyword = "%" + keyword + "%";
                query.Where(q =>
                    q.WhereLike(nameof(User.Name), keyword)
                        .OrWhereLike(nameof(User.Email), keyword)
                        .OrWhereLike(nameof(User.Phone), keyword)
                );
            }

            return await _userRepository.GetAllAsync(query);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public async Task<bool> DeleteUsersAsync(IEnumerable<int> userIds)
        {
            var enumerable = userIds as int[] ?? userIds.ToArray();
            foreach (var userId in enumerable)
            {
                var user = await _userRepository.GetAsync(userId);
                if (user != null)
                {
                    await _imService.DeleteUserAsync(user.ImId);
                }
            }

            return await _userRepository.DeleteUsersAsync(enumerable);
        }
        /// <summary>
        /// 获取用户未读数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<UserCountResult> GetUserCountAsync(int userId)
        {
            var result = await _userRepository.GetUserCountAsync(userId);
            //工作我的审批未操作数量
            var request = new FlowListRequest();
            result.FlowAuditUReadCount = await _flowTodoRepository.GetCountAsync(userId, nameof(TodoTypeConst.ApproverCheck), false,
                    request);
            // 工作我发起的已批复未读数量
            var flowState = new List<string> { FlowState.Checked.GetValue(), FlowState.Denied.GetValue() };
            result.FlowSendUReadCount = await _flowRepository.CountAsync(Q.Where(nameof(Entity.WorkFlow.Flow.UserId), userId).WhereIn(nameof(Entity.WorkFlow.Flow.State), flowState).Where(nameof(Entity.WorkFlow.Flow.IsRead), false).Where(nameof(Entity.WorkFlow.Flow.ProcessId), ">", 0));
            // 工作抄送我的未读数量
            result.FlowCcUReadCount = await _flowTodoRepository.FlowCcUReadCountAsync(userId);
            //待办事项未执行数量
            result.WaitHandleUFinishCount = await waitHandleRepository.GetUFinishAsync(userId);
            return result;
        }


    }
}
