using Arch.EntityFrameworkCore.UnitOfWork;
using WorkStation.Server.Context.Entity;
using WorkStation.Share;

namespace WorkStation.Server.Services
{
    public class UserService(IUnitOfWork unitOfWork) : IUserService
    {
        private readonly IUnitOfWork _unitOfWork = unitOfWork;
        private readonly int _pageList = 100;
        public async Task<ApiResponse> AddAsync(User entity)
        {
            try
            {
                if (string.IsNullOrEmpty(entity.Account))
                {
                    return new ApiResponse(false, "添加用户失败，账户无效", entity);
                }
                if (string.IsNullOrEmpty(entity.Password))
                {
                    return new ApiResponse(false, "添加用户失败，密码无效", entity);
                }
                entity.CreateTime = DateTime.Now;
                entity.UpdateTime = DateTime.Now;
                entity.Id = 0;
                var repository = _unitOfWork.GetRepository<User>();
                await repository.InsertAsync(entity);
                var count = await _unitOfWork.SaveChangesAsync();
                if (count > 0)
                {
                    return new ApiResponse(true, "添加用户成功", entity);
                }
                else
                {
                    return new ApiResponse(false, "添加用户失败，unitOfWork保存失败", entity);
                }
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, $"添加用户异常：{ex.Message}", null);
            }
        }

        public async Task<ApiResponse> DelateAsync(int id)
        {
            try
            {
                if (id <= 0)
                {
                    return new ApiResponse(false, "id = 无效", $"id = {id}");
                }
                var repository = _unitOfWork.GetRepository<User>();
                var user = await repository.GetFirstOrDefaultAsync(predicate: item => item.Id == id);
                if (user == null)
                {
                    return new ApiResponse(false, "删除用户失败，没有查询到该用户", id);
                }
                repository.Delete(user);
                var count = await _unitOfWork.SaveChangesAsync();
                if (count > 0)
                {
                    return new ApiResponse(true, "删除用户成功", $"id = {id}");
                }
                else
                {
                    return new ApiResponse(false, "删除用户失败，unitOfWork保存失败", $"id = {id}");
                }
            }
            catch (Exception ex)
            {
                return new ApiResponse(false, $"删除用户异常：{ex.Message}", $"id = {id}");
            }
        }

        public async Task<ApiRespon<List<User>>> GetAllAsync()
        {
            try
            {
                var repository = _unitOfWork.GetRepository<User>();
                var pageIndex = 0;
                var data = new List<User>();
                while (true)
                {
                    var users = await repository.GetPagedListAsync(pageIndex: pageIndex, 
                        pageSize: _pageList,
                        orderBy: source => source.OrderByDescending(item => item.Id));
                    if (users == null)
                    {
                        return new ApiRespon<List<User>>(false, "获取失败，GetPagedListAsync 结果为 null", null);
                    }
                    foreach(var item in users.Items)
                    {
                        data.Add(item);
                    }
                    if (!users.HasNextPage)
                    {
                        return new ApiRespon<List<User>>(true, "获取用户成功", data);
                    }
                    pageIndex++;
                }
            }
            catch(Exception ex) 
            {
                return new ApiRespon<List<User>>(false, $"获取用户异常：{ex.Message}", null);
            }
        }

        public Task<ApiResponse> GetByIdAsync(int id)
        {
            throw new NotImplementedException();
        }

        public Task<ApiResponse> UpdateAsync(User entity)
        {
            throw new NotImplementedException();
        }

    }
}
