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 ModelService : SqlSugarRepository<Model>, IBusinessService
    {
        public ModelService(ConnectionConfig config) : base(config)
        {
        }

        public async Task AddOrUpdateModel(Model model)
        {
            model.CreateTime = DateTime.Now;
            if (model.Id.IsNullOrGuidEmpty())
            {
                await Context.Insertable(model).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
            }
            else
            {
                await Context.Updateable(model).SplitTable().ExecuteCommandAsync().ConfigureAwait(false);
            }
        }

        public async Task<PageResult<Model>> GetModelList(PageData pageData)
        {
            var modelList = await Context.Queryable<Model>().SplitTable()
                //.OrderBy(m => m.Id)
                .ToPageListAsync(pageData.Page, pageData.PageSize).ConfigureAwait(false);
            return new PageResult<Model>
            {
                Data = modelList,
                PageSize = pageData.PageSize,
                Total = await Context.Queryable<Model>().SplitTable().CountAsync().ConfigureAwait(false)
            };
        }

        public async Task<Model> GetModel(Guid Id)
        {
            var model = await Context.Queryable<Model>().Where(m => m.Id == Id).SplitTable().FirstAsync().ConfigureAwait(false);
            return model;
        }
    }
}
