
using Infrastructure;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;

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

        }

        #region 查询
        /// <summary>
        /// 分页
        /// </summary>
        public async Task<Response<PageInfo<List<MiVehicle>>>> LoadAllPage(PageReq request)
        {         
            RefAsync<int> totalCount = 0;
            var result = new PageInfo<SysRole>();
            var list = await base.Repository.AsQueryable()
                .ToPageListAsync(request.page, request.limit, totalCount);

            return new Response<PageInfo<List<MiVehicle>>>
            {
                Result = new PageInfo<List<MiVehicle>>
                {
                    Items = list,
                    Total = totalCount
                }
            };
        }
        #endregion

        public async Task<MiVehicle> Get(object id)
        {
            return await Repository.GetByIdAsync(id);
        }


        /// <summary>
        /// 添加
        /// </summary>
        public async Task<Response<bool>> Add(MiVehicle model)
        {          
            var flag = await Repository.InsertAsync(model);

            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }



        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        public async Task<Response<bool>> Delete(List<MiVehicle> models)
        {
             var flag = await Repository.DeleteAsync(models);

             return new Response<bool>
             {
                 Result = flag,
                 Message = flag == true ? "success" : "error"
             };
            
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="obj"></param>
        public async Task<Response<bool>> Update(MiVehicle model)
        {
            bool flag = await base.Repository.UpdateAsync(model);

            return new Response<bool>
            {
                Result = flag,
                Message = flag == true ? "success" : "error"
            };
        }

        /// <summary>
        /// 事务示例
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Response<bool>> AssignModule()
        {
            using (var uwo = UnitWork.CreateContext())
            {                
                //await uwo.SysRoleElement.InsertRangeAsync(model.ElementIds.Select(a => new SysRoleElement { RoleId = model.RoleId, ElementId = a }).ToList());

                var flag = uwo.Commit();
                return new Response<bool>
                {
                    Result = flag,
                    Message = flag == true ? "success" : "error"
                };
            }
        }

    }
}