using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.BaseApp.SysGroupManager;
using OpenAuth.App.BaseApp.SysGroupManager.Request;
using OpenAuth.App.ServiceApp.InsTaskHallManager;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers.BaseControllers
{
    /// <summary>
    /// 分组
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SysGroupController : ControllerBase
    {
        private readonly SysGroupApp _app;
        public SysGroupController(SysGroupApp app)
        {
            _app = app;
        }


        /// <summary>
        /// 分页获取列表数据
        /// </summary>
        /// <param name="keyword">筛选条件</param>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<SysGroup>>>> LoadGroupList(string keyword, int page = 1, int limit = 15)
        {
            return await _app.LoadGroupList(keyword, page, limit);
        }

        /// <summary>
        /// 获取单个分组信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<SysGroup> GetEntityById(string id)
        {
            return await _app.GetEntityById(id);
        }

        /// <summary>
        /// 新增分组
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="group">数据源实体</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Response<bool>> SaveEntity(string id, SysGroup group)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.SaveEntity(id, group);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 删除分组
        /// </summary>
        /// <param name="id">主键</param>
        [HttpPost]
        public async Task<Response<bool>> DeleteEntity(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteEntity(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 给分组分配用户
        /// </summary>
        /// <param name="model">数据</param>
        [HttpPost]
        public async Task<Response<bool>> AssignGroupUser(GroupUserReq model)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AssignGroupUser(model);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        /// <summary>
        /// 根据小组id，获取成员信息
        /// </summary>
        /// <param name="groupid">小组id</param>
        /// <param name="keyword">用户名筛选</param>
        /// <param name="page">当前页</param>
        /// <param name="limit">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Response<PageInfo<List<SysUser>>>> LoadUserListByGroupid(string groupid, string keyword, int page, int limit)
        {
            return await _app.LoadUserListByGroupid(groupid,keyword, page, limit);
        }
    }
}
