using Hncore.Infrastructure.EntitiesExtension;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.Vpn.Domain;
using Hncore.Pass.Vpn.Service;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Hncore.Pass.Vpn.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/course/v{version:apiVersion}/article/[action]")]
    public class ArticleController : HncoreControllerBase
    {
        private ArticleService m_ArticleService;

        public ArticleController(ArticleService _ArticleService)
        {
            m_ArticleService = _ArticleService;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Post([FromBody]ArticleEntity request)
        {
            request.TenantId = this.Request.GetManageUserInfo().TenantId;
            await m_ArticleService.Add(request);
            return Success();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Put([FromBody]ArticleEntity request)
        {
             await m_ArticleService.Update(request);
            return Success();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Delete([FromQuery]int id)
        {
            var flag = await m_ArticleService.DeleteById(id);
            if (flag)
                return Success();
            else
                return Error("删除失败");
        }


        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Get([FromQuery]int id)
        {
            var data = await m_ArticleService.GetById(id);
            return Success(data);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Page([FromQuery]PageRequestBase request)
        {
            Expression<Func<ArticleEntity, bool>> expr = m => 1 == 1;
            if (request.KeyWord.Has())
            {
                expr = expr.And(m => m.Title.Contains(request.KeyWord)
                 || m.SubTitle.Contains(request.KeyWord)
                  || m.Keyword.Contains(request.KeyWord));
            }
            var ret = await m_ArticleService.Page(request.PageIndex, request.PageSize, expr,true);
            var data = ret.ToApiResult();
            return data;
        }

        [HttpPost]
        public async Task<ApiResult> Audit([FromQuery] int id)
        {
            var entity = await m_ArticleService.GetById(id);

            entity.Publish = entity.Publish == 1 ? 0 : 1;

            await m_ArticleService.Update(entity);
            return Success(entity.Publish);
        }
    }
}
