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

namespace Hncore.Pass.OSS.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/oss/v{version:apiVersion}/asset/[action]")]
    public class AssetController : HncoreControllerBase
    {
        AssetGroupService m_AssetGroupService;
        AssetService m_AssetService;
        public AssetController(AssetGroupService assetGroupService, AssetService _AssetService)
        {
            m_AssetGroupService = assetGroupService;
            m_AssetService = _AssetService;
        }

        #region group
        [HttpPost]
        public async Task<ApiResult> AddGroup([FromBody] PostGroupRequest request)
        {
            var entity = request.MapTo<AssetGroup>();
            entity.TenantId = this.Request.GetManageUserInfo().TenantId;
            await m_AssetGroupService.Add(entity);
            return Success();
        }
        [HttpPost]
        public async Task<ApiResult> UpdateGroup([FromBody] PostGroupRequest request)
        {
            var enitty = await m_AssetGroupService.GetById(request.Id);
            enitty.Name = request.Name;
            enitty.TenantId = this.Request.GetManageUserInfo().TenantId;
            await m_AssetGroupService.Update(enitty);
            return Success();
        }
        [HttpPost]
        public async Task<ApiResult> DeleteGroup([FromQuery] int id)
        {
            await m_AssetGroupService.DeleteById(id);
            return Success();
        }

        [HttpGet]
        public async Task<ApiResult> GetGroups([FromQuery] int groupType)
        {
            var enitty = await m_AssetGroupService.Query(m => m.GroupType == groupType, true).ToListAsync();
            return Success(enitty);
        }

        #endregion

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

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Put([FromBody]PostAssetRequest request)
        {
            var entity = await m_AssetService.GetById(request.Id);
            if (entity == null)
                return Error("资源不存在");
            if (request.Name.Has())
            {
                entity.Name = request.Name;
            }
            if (request.GroupId > 0)
            {
                entity.GroupId = request.GroupId;
            }
            await m_AssetService.Update(entity);
            return Success();
        }

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


        [HttpGet]
        public async Task<ApiResult> Page([FromQuery]int pageIndex,[FromQuery] int assetType,int groupId, [FromQuery] string name)
        {
            Expression<Func<Asset, bool>> expr = m => m.AssetType == assetType;
            if (groupId>0)
            {
                expr = expr.And(m => m.GroupId== groupId);
            }
            if (name.Has())
            {
                expr = expr.And(m => m.Name.Contains(name));
            }
            var enitty = await m_AssetService.Page(pageIndex,15,expr, true);
            return Success(enitty);
        }
    }
}
