using OpenAuth.App.Base;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using OpenAuth.App.Interface;
using Infrastructure;
using OpenAuth.App;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Request;

namespace OpenAuth.App
{
    public class SysPosGroupApp : SqlSugarBaseApp<SysPosGroup, SugarDbContext>
    {
        public SysPosGroupApp(ISugarUnitOfWork<SugarDbContext> unitWork, ISimpleClient<SysPosGroup> repository, IAuth auth) : base(unitWork, repository, auth)
        {
        }


        public async Task<Response<List<SysPosGroup>>> Load(string name)
        {
            var groups = await base.Repository.AsQueryable()
                     .WhereIF(!string.IsNullOrEmpty(name), g => g.Name.Contains(name))
                     .ToListAsync();

            return new Response<List<SysPosGroup>>
            {
                Result = groups
            };
        }

        public async Task<Response<bool>> Add(PosGroupForm pos)
        {
            var userId = base._auth.GetCurrentUser().User.Id;

            var model = pos.MapTo<SysPosGroup>();
            model.Id = Yitter.IdGenerator.YitIdHelper.NextId();
            model.CreateId = userId;
            model.CreateTime = DateTime.Now;

            var flag = await base.Repository.InsertAsync(model);
            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }
    }
}
