using Learun.Application.Base.AuthorizeModule;
using Learun.Application.Organization;
using Learun.Util;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Learun.Application.Web.Areas.LR_AuthorizeModule.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创建人：研发部
    /// 日 期：2017.04.17
    /// 描 述：用户对应对象
    /// </summary>
    public class UserRelationController : MvcControllerBase
    {
        private UserRelationIBLL userRelationIBLL = new UserRelationBLL();
        private UserIBLL userIBLL = new UserBLL();

        #region 获取视图
        /// <summary>
        /// 人员选择
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SelectForm()
        {
            return View();
        }
        /// <summary>
        /// 人员选择
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LookForm()
        {
            return View();
        }

        /// <summary>
        /// 人员选择
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult LookIndex()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取用户主键列表信息
        /// </summary>
        /// <param name="objectId">用户主键</param>
        /// <param name="ProjectId">项目id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetUserIdList(string objectId, string ProjectId)
        {
            var data = userRelationIBLL.GetUserIdList(objectId, ProjectId);
            string userIds = "";
            foreach (var item in data)
            {
                if (userIds != "")
                {
                    userIds += ",";
                }
                userIds += item.F_UserId;
            }
            var userList = userIBLL.GetListByUserIds(userIds);
            var datajson = new
            {
                userIds = userIds,
                userInfoList = userList
            };
            return Success(datajson);
        }

        /// <summary>
        /// 获取相关人员信息（项目角色 里 添加用户）
        /// </summary>
        /// <param name="keyWord">查询关键字</param>
        /// <param name="ProjectId">项目id</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetRelationUser(string KeyValue, string ProjectId)
        {
            var data = new List<UserRelationEntity>();
            if (string.IsNullOrEmpty(KeyValue))
            {
                data = userRelationIBLL.GetUserIdList("", ProjectId);
            }
            else
            {
                data =  userRelationIBLL.GetUserIdList(KeyValue, ProjectId);

            }
            string userIds = "";
            foreach (var item in data)
            {
                if (userIds != "")
                {
                    userIds += ",";
                }
                userIds += item.F_UserId;
            }
            var userList = userIBLL.GetListByUserIds(userIds);
            var datajson = new
            {
                userIds = userIds,
                userInfoList = userList
            };
            return Success(datajson);
        }

        /// <summary>
        /// 获取岗位人员信息
        /// </summary>
        /// <param name="KeyValue">岗位主键</param>
        /// <param name="ProjectId">项目</param>
        /// <param name="keyword">筛选关键字</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetRelationUserList(string KeyValue, string ProjectId, string keyword)
        {
            var data = new List<UserRelationEntity>();
            if (!string.IsNullOrEmpty(KeyValue))
            {
                data = userRelationIBLL.GetUserIdList(KeyValue, ProjectId);
            }
            string userIds = "";
            foreach (var item in data)
            {
                if (userIds != "")
                {
                    userIds += ",";
                }
                userIds += item.F_UserId;
            }
            var userList = userIBLL.GetListByUserIds(userIds);
            if (!string.IsNullOrEmpty(keyword) && userList != null)
            {
                userList = userList.FindAll(x => x.F_RealName.Contains(keyword));
            }
            return Success(userList);
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存表单数据（角色 关联 用户）
        /// </summary>
        /// <param name="objectId">对象主键</param>
        /// <param name="category">分类:1-角色 2-岗位（暂时应该没用到）</param>
        /// <param name="userIds">对用户主键列表</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]

        public ActionResult SaveForm(string objectId, int category, string userIds, string ProjectId)
        {
            userRelationIBLL.SaveEntityList(objectId, category, userIds, ProjectId);
            return Success("保存成功！");
        }

        /// <summary>
        /// 删除表单数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="objectId">objectId</param>
        /// <param name="ProjectId">项目id</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeletRelationUser(string keyValue, string objectId, string ProjectId)
        {
            UserRelationService UreService = new UserRelationService();
            UreService.DeleteUser(keyValue, objectId, ProjectId);
            return Success("删除成功！");
        }
        #endregion
    }
}