using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Module.User;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Linq;

namespace Edu.WebApi.Controllers.Third
{
    /// <summary>
    /// 第三方API
    /// </summary>

    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class ThirdController : BaseController
    {
        /// <summary>
        /// 员工处理类
        /// </summary>
        private readonly EmployeeModule employeeModule = new EmployeeModule();

        /// <summary>
        /// 根部部门层级获取部门主管
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetEmploeeByTier()
        {
            JObject jobj = JObject.Parse(RequestParm.Msg.ToString());
            int EmployeeId = jobj.GetInt("EmployeeId", 0);
            int TierId = jobj.GetInt("TierId", 0);
            if (EmployeeId <= 0)
            {
                return ApiResult.ParamIsNull(message: "请传递员工编号!");
            }
            var empModel = employeeModule.GetEmployeeListModule(new Model.ViewModel.User.Employee_ViewModel() { Id = EmployeeId })?.FirstOrDefault();
            if (empModel == null)
            {
                return ApiResult.ParamIsNull(message: "当前员工编号不存在!");
            }
            if (TierId <= 0)
            {
                return ApiResult.ParamIsNull(message: "请选择部门层级!");
            }
            var empList = employeeModule.GetEmployeeByDeptTierModule(new Model.ViewModel.User.Employee_ViewModel()
            {
                Dept_Id = empModel.Dept_Id,
                DeptTier = TierId,
                Group_Id = empModel.Group_Id
            });
            var result = empList?.Select(qitem => new
            {
                qitem.Id,
                qitem.EmployeeName,
                qitem.Dept_Id,
                qitem.DeptName,
                qitem.Post_Id,
                qitem.PostName,
                qitem.School_Id,
                qitem.SchoolName,
                qitem.Group_Id,
                qitem.GroupName
            });
            return ApiResult.Success(data: result);
        }

        /// <summary>
        /// 根据岗位编号获取岗位员工列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HttpPost]
        [AllowAnonymous]
        public ApiResult GetEmployeeByPost()
        {
            JObject jobj = JObject.Parse(RequestParm.Msg.ToString());
            int EmployeeId = jobj.GetInt("EmployeeId", 0);
            if (EmployeeId <= 0)
            {
                return ApiResult.ParamIsNull(message: "请传递员工编号!");
            }
            var empModel = employeeModule.GetEmployeeListModule(new Model.ViewModel.User.Employee_ViewModel() { Id = EmployeeId })?.FirstOrDefault();
            if (empModel == null)
            {
                return ApiResult.ParamIsNull(message: "当前员工编号不存在!");
            }
            int postId = jobj.GetInt("PostId");
            if (postId <= 0)
            {
                return ApiResult.ParamIsNull(message: "请选择岗位!");
            }
            var list = employeeModule.GetEmployeeListModule(new Model.ViewModel.User.Employee_ViewModel()
            {
                QPostIds = postId.ToString(),
                Group_Id = empModel.Group_Id
            });
            return ApiResult.Success(data:
                list?.Select(qitem => new
                {
                    qitem.Id,
                    qitem.EmployeeName,
                    qitem.Dept_Id,
                    qitem.DeptName,
                    qitem.Post_Id,
                    qitem.PostName,
                    qitem.School_Id,
                    qitem.SchoolName,
                    qitem.Group_Id,
                    qitem.GroupName
                }));
        }
    }
}
