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

namespace Ropin.Inspection.Api.Controllers
{
    [AllowAnonymous]
    /// <summary>
    /// 角色
    /// </summary>
    public class TsysRoleController : BaseController
    {
        private readonly ILogger<TsysRoleController> _logger;
        private readonly ITsysRoleService _tsysRoleService;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tsysRoleService"></param>
        /// <param name="logger"></param>
        public TsysRoleController(ITsysRoleService tsysRoleService, ILogger<TsysRoleController> logger)
        {
            _tsysRoleService = tsysRoleService;
            _logger = logger;
        }
        /// <summary>
        /// 通过角色ID获取角色信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [HttpGet("GetRoleAsync/{RoleId}")]
        public async Task<ApiResult> GetRoleAsync(Guid RoleId)
        {
            if (Guid.Empty == RoleId)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var Role = await _tsysRoleService.GetByIdAsync(RoleId);
                return new ApiResult<TsysRoleViewModel>(Role);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetRolesAsync")]
        public async Task<ApiResult> GetRolesAsync()
        {
            try
            {
                var RoleList = await _tsysRoleService.GetAllAsync();
                return new ApiResult<IEnumerable<TsysRoleViewModel>>(RoleList.OrderBy(i=>i.C_LicenseCode));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 根据LicenseCode获取所有角色
        /// </summary>
        /// <param name="LicenseCode"></param>
        /// <returns></returns>
        [HttpGet("GetBylicenseCodeAsync/{LicenseCode}/{status=1}")]
        public async Task<ApiResult> GetBylicenseCodeAsync(string LicenseCode, string status)
        {
            if (string.IsNullOrEmpty(LicenseCode))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var RoleList = await _tsysRoleService.GetBylicenseCodeAsync(LicenseCode, status);
                return new ApiResult<IEnumerable<TsysRoleOrgLicenseViewModel>>(RoleList);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }
        /// <summary>
        /// 根据LicenseCode和名称获取所有角色
        /// </summary>
        /// <param name="LicenseCode"></param>
        /// <param name="Name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet("GetBylicenseCodeByNameAsync/{LicenseCode}/{Name}/{status=1}")]
        public async Task<ApiResult> GetBylicenseCodeByNameAsync(string LicenseCode, string Name, string status)
        {
            if (string.IsNullOrEmpty(LicenseCode))
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                var RoleList = await _tsysRoleService.GetBylicenseCodeAsync(LicenseCode, status);
                if(string.IsNullOrWhiteSpace(Name))
                return new ApiResult<IEnumerable<TsysRoleOrgLicenseViewModel>>(RoleList);
                else
                    return new ApiResult<IEnumerable<TsysRoleOrgLicenseViewModel>>(RoleList.Where(x=>x.C_Name.Contains(Name)));
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="Role"></param>
        /// <returns></returns>
        [Route("CreateRoleAsync")]
        [HttpPost]
        public async Task<ApiResult> CreateRoleAsync([FromBody] TsysRoleCreateViewModel Role)
        {
            if (Role == null)
            {
                return new ApiResult(ReturnCode.ArgsError);
            }
            try
            {
                await _tsysRoleService.CreateAsync(Role);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        [HttpDelete("DeleteRoleAsync/{RoleId}")]
        public async Task<ApiResult> DeleteRoleAsync(Guid RoleId)
        {
            if (Guid.Empty == RoleId)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tsysRoleService.DeleteAsync(RoleId);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="RoleId"></param>
        /// <param name="updateRole"></param>
        /// <returns></returns>
        [HttpPut("UpdateRoleAsync/{RoleId}")]
        public async Task<ApiResult> UpdateRoleAsync(Guid RoleId, TsysRoleUpdateViewModel updateRole)
        {
            if (Guid.Empty == RoleId)
            {
                return new ApiResult(ReturnCode.GeneralError);
            }
            try
            {
                await _tsysRoleService.UpdateAsync(RoleId, updateRole);
            }
            catch (Exception ex)
            {
                return new ApiResult(ReturnCode.GeneralError, ex.Message);
            }
            return new ApiResult(ReturnCode.Success);
        }
     
        ///// <summary>
        ///// 条件查询角色
        ///// </summary>
        ///// <param name="searchViewModel"></param>
        ///// <returns></returns>
        //[Route("GetRolesByAsync")]
        //[HttpPost]
        //public async Task<ApiResult> GetByAsync(TsysRoleSearchModel searchViewModel)
        //{
        //    try
        //    {
        //        var list = await _tsysRoleService.GetByAsync(searchViewModel);
        //        return new ApiResult<PagesModel<TsysRoleViewModel>>(new PagesModel<TsysRoleViewModel>(list, searchViewModel));
        //    }
        //    catch (Exception ex)
        //    {
        //        return new ApiResult(ReturnCode.GeneralError, ex.Message);
        //    }
        //}

    }
}