using LangGuan.Command.Model;
using LangGuan.Command.Model.EntityModel;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace LangGuan.Services
{
    /// 
    /// 
    ///  
    public class VideoService : BaseService
    {
        /// 
        /// 
        ///  
        ///  
        public VideoService(IConfiguration config) : base(config, nameof(Video))
        {
        }
        /// 
        /// 首页取全部
        ///  
        ///  
        public async Task GetAll()
        {
            Expression> exp = num => true;
            var query = await base.GetListWithExp(exp);
            var reslut = query.OrderByDescending(s => s.CreateDateTime)
                .Select(s => new { s.GroupName, s.Url, }).ToList();
            if (reslut.Count > 0)
            {
                return reslut;
            }
            return Task.FromResult(new { });
        }
        /// 
        /// 分页数据
        ///  
        ///  
        ///  
        public async Task GetList(RqeustPaging request)
        {
            request.pageSize = request.pageSize == 0 ? 10 : request.pageSize;
            Expression> exp = num => true;
            var query = await base.GetListWithExp(exp);
            var total = query.Count();
            var items = query.OrderByDescending(s => s.CreateDateTime)
                .Skip(request.pageSize * (request.current - 1)).Take(request.pageSize)
                .Select(s => new { s.Id, s.GroupName, s.Url, }).ToList();
            return new ApiResult()
            {
                code = 0,
                data = new { total = total, items = items }
            };
        }
        /// 
        /// 
        ///  
        ///  
        ///  
        public async Task Add(Video video)
        {
            await base.CreateAsync(video);
            return new ApiResult() { code = 0, msg = "" };
        }
        /// 
        /// 更新
        ///  
        ///  
        ///  
        public async Task Update(Video video)
        {
            var entity = await base.GetAsync(video.Id);
            if (entity != null)
            {
                entity.GroupName = video.GroupName;
                entity.Url = video.Url;
                await base.UpdateAsync(video.Id, entity);
                return new ApiResult() { code = 0, msg = "" };
            }
            return new ApiResult() { code = 1, msg = "修改失败" };
        }
    }
}