using Autofac.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Ropin.Inspection.Api.Common;
using Ropin.Inspection.Model.ViewModel;
using Ropin.Inspection.Service;
using Ropin.Inspection.Service.SYS.Interface;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using Ropin.Inspection.Model.ViewModel.SYS;

namespace Ropin.Inspection.Api.Controllers.SYS
{
    [AllowAnonymous]
    public class TsysPrivController : BaseController
    {
        public ILogger<TsysPrivController> _logger { get; }
        private readonly ITsysPrivService _service;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="service"></param>
        /// <param name="logger"></param>
        public TsysPrivController(ITsysPrivService service, ILogger<TsysPrivController> logger)
        {
            _service = service;
            _logger = logger;
        }

        /// <summary>
        /// 获取树型列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetPrivTree")]
        public async Task<ApiResult> GetListTreeAsync(TsysPrivSearch model)
        {
            try
            {
                if (model==null)
                {
                    model = new TsysPrivSearch();
                }
                var list = await _service.GetPrivTree(model);
                return new ApiResult<List<TsysPrivTreeModel>>(list);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Route("CreatePrivAsync")]
        [HttpPost]
        public async Task<ApiResult> CreatePrivAsync(TsysPrivViewModel entity)
        {
            if (entity == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _service.CreateOneAsync(entity);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        [HttpPut("UpdatePriv/{id}")]
        public async Task<ApiResult> UpdatePrivAsync(string id, TsysPrivViewModel updateModel)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _service.UpdateOneAsync(id, updateModel);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("DeletePriv/{id}")]
        public async Task<ApiResult> DeletePriv(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _service.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }


    }
}