using CrazyStudio.Core.Common.Eitities.Page;
using CrazyStudio.Core.Common.Tools.Extensions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XPrintServer.Business.Services.Interface;
using XPrintServer.DataModel;
using XPrintServer.DataModel.Models;

namespace XPrintServer.Business.Services
{
    public class RoleService : SqlSugarRepository<Role>, IBusinessService
    {
        public RoleService(ConnectionConfig config) : base(config)
        {
        }

        public async Task AddOrUpdateRole(Role role)
        {
            if (role.Id.IsNullOrGuidEmpty())
            {
                await Context.Insertable(role).ExecuteCommandAsync().ConfigureAwait(false);
            }
            else
            {
                await Context.Updateable(role).ExecuteCommandAsync().ConfigureAwait(false);
            }
        }

        public async Task<PageResult<Role>> GetRoleList(PageData pageData)
        {
            var roleList = await Context.Queryable<Role>()
                .ToPageListAsync(pageData.Page, pageData.PageSize).ConfigureAwait(false);
            return new PageResult<Role>
            {
                Data = roleList,
                PageSize = pageData.PageSize,
                Total = await Context.Queryable<Role>().CountAsync().ConfigureAwait(false)
            };
        }

        public async Task<List<Role>> AllRoleList()
        {
            var roleList = await Context.Queryable<Role>()
                .ToListAsync().ConfigureAwait(false);
            return roleList;
        }

        public async Task<Role> GetRole(Guid Id)
        {
            var role = await Context.Queryable<Role>().Where(m => m.Id == Id).FirstAsync().ConfigureAwait(false);
            return role;
        }
    }
}
