using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using ConfigurationApi.IRepositories;
using MadRunFabric.Common;
using MadRunFabric.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace ConfigurationApi.Controllers
{
    /// <summary>
    /// 岗位系统
    /// </summary>
    [Authorize]
    [ApiVersion("6.0")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    public class PostSystemController : BaseController
    {
        private readonly ILogger<PostSystemController> _logger;
        private readonly ISys_Post_SystemRepository _sys_post_systemrepository;
        public PostSystemController(ILogger<PostSystemController> logger, ISys_Post_SystemRepository sys_post_systemrepository)
        {
            _logger = logger;
            _sys_post_systemrepository = sys_post_systemrepository;
        }

        /// <summary>
        /// 保存当前选中的系统
        /// </summary>
        /// <param name="functionIds"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpPost("savepostsystem")]
        public async Task<IActionResult> SavePostSystemAsync(string[] ids, string postid)
        {
            if (ids == null)
                return Error("参数错误");
            var list_model = new List<Sys_Post_System>();
            await _sys_post_systemrepository.Remove(x => x.postid == postid);
            foreach (var funcId in ids)
            {
                var model = new Sys_Post_System();
                model.postid = postid;
                model.systemid = funcId;
                model.createby= User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Sid).Value;
                model.isdelete = 0;
                list_model.Add(model);
            }

            if (await _sys_post_systemrepository.AddRange(list_model))
            {
                return Success("权限设置成功!");
            }
            else
            {
                return Error("权限设置失败!");
            }
        }

        /// <summary>
        /// 获取岗位系统树形结构
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("getpostsystem")]
        public IActionResult GetPostSystemAsync(string postid)
        {
            var result = _sys_post_systemrepository.GetPostSystemAsync(postid);

            return Success("成功", result);
        }
    }
}