using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.Course;
using Edu.Common.Enum.OKR;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.OKR;
using Edu.Module.OKR;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace Edu.WebApi.Controllers.OKR
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class OKRPeriodController : BaseController
    {
        /// <summary>
        /// OKR周期处理类对象
        /// </summary>
        private readonly OKRPeriodModule okrPeriodModule = new OKRPeriodModule();
        private readonly Module.User.DepartmentModule departmentModule = new Module.User.DepartmentModule();

        #region 基础配置
        /// <summary>
        /// 获取OKR周期配置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRPeriodConfigInfo()
        {
            var userInfo = base.UserInfo;
            var model = okrPeriodModule.GetOKRPeriodConfigInfo(userInfo.Group_Id);
            return ApiResult.Success("", new
            {
                model.Id,
                model.PeriodMonth,
                StartDate = model.StartDate.HasValue ? model.StartDate.Value.ToString("yyyy年MM月") : "",
                EndDate = model.EndDate.HasValue ? model.EndDate.Value.ToString("yyyy年MM月") : "",
                model.BeforeNum,
                model.BeforeType,
                model.YearOKR,
                model.DirectorAudit
            });
        }

        /// <summary>
        /// 保存OKR周期配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRPeriodConfigInfo()
        {
            var userInfo = base.UserInfo;
            RB_OKR_PeriodConfig_ViewModel dmodel = JsonHelper.DeserializeObject<RB_OKR_PeriodConfig_ViewModel>(RequestParm.Msg.ToString());
            if (dmodel.PeriodMonth <= 0)
            {
                return ApiResult.ParamIsNull("请选择周期长度");
            }
            if (!dmodel.StartDate.HasValue)
            {
                return ApiResult.ParamIsNull("请选择新周期生效时间");
            }
            if (!dmodel.EndDate.HasValue)
            {
                return ApiResult.ParamIsNull("请选择新周期生效时间");
            }
            if (dmodel.BeforeNum <= 0)
            {
                return ApiResult.ParamIsNull("请选择新周期出现时间");
            }
            if (dmodel.BeforeType <= 0)
            {
                return ApiResult.ParamIsNull("请选择类型");
            }
            if (dmodel.YearOKR <= 0)
            {
                return ApiResult.ParamIsNull("请选择年度是否开启");
            }
            if (dmodel.DirectorAudit <= 0)
            {
                return ApiResult.ParamIsNull("请选择主管审核是否开启");
            }
            if (dmodel.StartDate.Value.AddMonths((int)dmodel.PeriodMonth - 1).ToString("yyyy-MM") != dmodel.EndDate.Value.ToString("yyyy-MM"))
            {
                return ApiResult.ParamIsNull("时间不正确");
            }
            dmodel.StartDate = Convert.ToDateTime(dmodel.StartDate.Value.ToString("yyyy-MM") + "-01");
            dmodel.EndDate = Convert.ToDateTime(dmodel.EndDate.Value.ToString("yyyy-MM") + "-01 23:59:59").AddMonths(1).AddDays(-1);
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.School_Id = userInfo.School_Id;
            dmodel.CreateBy = userInfo.Id;
            dmodel.CreateTime = DateTime.Now;
            dmodel.UpdateBy = userInfo.Id;
            dmodel.UpdateTime = DateTime.Now;

            string msg = okrPeriodModule.SetOKRPeriodConfigInfo(dmodel, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取OKR提醒列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRRemindList()
        {
            var userInfo = base.UserInfo;
            var list = okrPeriodModule.GetOKRRemindList(userInfo.Group_Id);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Days,
                x.Time,
                x.Enable,
                x.Type
            }));
        }

        /// <summary>
        /// 设置OKR提醒
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRRemindInfo()
        {
            var userInfo = base.UserInfo;
            RB_OKR_Remind_ViewModel dmodel = JsonHelper.DeserializeObject<RB_OKR_Remind_ViewModel>(RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(dmodel.Days))
            {
                return ApiResult.ParamIsNull("请选择工作日");
            }
            if (string.IsNullOrEmpty(dmodel.Time))
            {
                return ApiResult.ParamIsNull("请选择时间");
            }
            if (dmodel.Type <= 0)
            {
                return ApiResult.ParamIsNull("请传递类型");
            }

            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.School_Id = userInfo.School_Id;
            dmodel.CreateBy = userInfo.Id;
            dmodel.CreateTime = DateTime.Now;
            dmodel.UpdateBy = userInfo.Id;
            dmodel.UpdateTime = DateTime.Now;
            dmodel.Enable = 1;
            string msg = okrPeriodModule.SetOKRRemindInfo(dmodel, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 谁知OKR提醒启用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRRemindEnable()
        {
            var userInfo = base.UserInfo;
            JObject prams = JObject.Parse(RequestParm.Msg.ToString());
            int Type = prams.GetInt("Type", 0);
            if (Type <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            bool flag = okrPeriodModule.SetOKRRemindEnable(Type, userInfo);
            if (flag)
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        /// <summary>
        /// 获取分数配置分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRScoreConfigPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_OKR_Score_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;

            var list = okrPeriodModule.GetOKRScoreConfigPageList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.Name,
                x.IsDefault,
                DetailList = x.DetailList.OrderBy(x => x.ScoreMin).Select(z => new
                {
                    z.Name,
                    z.ScoreMin,
                    z.ScoreMax,
                    z.Color
                })
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 设置分数规则
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRScoreConfigInfo()
        {
            var userInfo = base.UserInfo;
            RB_OKR_Score_ViewModel dmodel = JsonHelper.DeserializeObject<RB_OKR_Score_ViewModel>(RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(dmodel.Name))
            {
                return ApiResult.ParamIsNull("请传递规则名称");
            }
            if (dmodel.DetailList == null || !dmodel.DetailList.Any())
            {
                return ApiResult.ParamIsNull("请传递分数明细");
            }
            decimal min = 0;
            foreach (var item in dmodel.DetailList)
            {
                if (string.IsNullOrEmpty(item.Name))
                {
                    return ApiResult.ParamIsNull("请传递颜色名称");
                }
                if (item.ScoreMin < 0 || item.ScoreMin >= 1)
                {
                    return ApiResult.ParamIsNull("分数有误");
                }
                if (item.ScoreMax <= 0 || item.ScoreMax > 1)
                {
                    return ApiResult.ParamIsNull("分数有误");
                }
                if (item.ScoreMax < item.ScoreMin)
                {
                    return ApiResult.ParamIsNull("分数有误");
                }
                if (min == 0)
                {
                    if (item.ScoreMin < min)
                    {
                        return ApiResult.ParamIsNull("分数有交集");
                    }
                }
                else
                {
                    if (item.ScoreMin <= min)
                    {
                        return ApiResult.ParamIsNull("分数有交集");
                    }
                }
                min = item.ScoreMax;
            }

            dmodel.IsDefault = 2;
            dmodel.Status = 0;
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.School_Id = userInfo.School_Id;
            dmodel.CreateBy = userInfo.Id;
            dmodel.CreateTime = DateTime.Now;
            dmodel.UpdateBy = userInfo.Id;
            dmodel.UpdateTime = DateTime.Now;

            string msg = okrPeriodModule.SetOKRScoreConfigInfo(dmodel, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        /// <summary>
        /// 设置分数状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRScoreConfigState()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ScoreId = parms.GetInt("ScoreId", 0);
            int Type = parms.GetInt("Type", 1);// 类型  1设置默认   2删除
            if (ScoreId <= 0)
            {
                return ApiResult.ParamIsNull("请传递id");
            }

            string msg = okrPeriodModule.SetOKRScoreConfigState(ScoreId, Type, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPeriodMonthEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(PeriodMonthEnum));
            return ApiResult.Success("", list.Select(x => new
            {
                x.Name,
                x.Id
            }));
        }
        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetPermissionTypeEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(PermissionTypeEnum));
            return ApiResult.Success("", list.Select(x => new
            {
                x.Name,
                x.Id
            }));
        }
        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetProgressStateEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(ProgressStateEnum));
            return ApiResult.Success("", list.Select(x => new
            {
                x.Name,
                x.Id
            }));
        }
        #endregion

        #region 目标管理
        #region 基础目标
        /// <summary>
        /// 获取周期列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetOKRPeriodList()
        {
            var userInfo = base.UserInfo;
            var list = okrPeriodModule.GetOKRPeriodList(userInfo.Group_Id);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Name,
                StartDate = x.StartDate.ToString("yyyy-MM-dd HH:mm:ss"),
                EndDate = x.EndDate.ToString("yyyy-MM-dd HH:mm:ss"),
                x.IsCurrent,
                x.IsYear
            }));
        }

        /// <summary>
        /// 获取我的目标列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyObjectiveList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            var list = okrPeriodModule.GetOKRMyObjectiveList(PeriodId, userInfo);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 获取单个我的目标
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyObjectiveInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标id");
            }
            var obj = okrPeriodModule.GetOKRMyObjectiveInfo(ObjectiveId, userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 新增修改目标
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyObjectiveInfo()
        {
            var userInfo = base.UserInfo;
            RB_OKR_Objective_ViewModel dmodel = JsonHelper.DeserializeObject<RB_OKR_Objective_ViewModel>(RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(dmodel.Title))
            {
                return ApiResult.ParamIsNull("请输入Objective");
            }
            if (dmodel.PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            string msg = okrPeriodModule.SetOKRMyObjectiveInfo(dmodel, userInfo, out int ObjectiveId);
            if (msg == "")
            {
                return ApiResult.Success("", ObjectiveId);
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 新增修改关键结果
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyKeyResultInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            string Name = parms.GetStringValue("Name");
            if (string.IsNullOrEmpty(Name))
            {
                return ApiResult.ParamIsNull("请传递结果值");
            }
            if (KeyResultId <= 0 && ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("结果id不正确");
            }

            string msg = okrPeriodModule.SetOKRMyKeyResultInfo(KeyResultId, ObjectiveId, Name, userInfo, out int RKeyResultId);
            if (msg == "")
            {
                return ApiResult.Success("", RKeyResultId);
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 设置目标排序
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyObjectiveSort()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId1 = parms.GetInt("ObjectiveId1", 0);
            int ObjectiveId2 = parms.GetInt("ObjectiveId2", 0);//目标的后面
            if (ObjectiveId1 <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (ObjectiveId2 < 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (ObjectiveId1 == ObjectiveId2)
            {
                return ApiResult.Success();
            }

            string msg = okrPeriodModule.SetOKRMyObjectiveSort(ObjectiveId1, ObjectiveId2, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 设置目标关键结果排序
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyKeyResultSort()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResult1 = parms.GetInt("KeyResult1", 0);
            int KeyResult2 = parms.GetInt("KeyResult2", 0);//结果的后面
            if (KeyResult1 <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (KeyResult2 < 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (KeyResult1 == KeyResult2)
            {
                return ApiResult.Success();
            }

            string msg = okrPeriodModule.SetgetOKRMyKeyResultSort(KeyResult1, KeyResult2, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 设置目标状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyObjectiveState()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            int Type = parms.GetInt("Type", 1);// 类型 1设置默认状态  2设置自定义状态  3删除
            int ProgressState = parms.GetInt("ProgressState", 0);
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标id");
            }
            if (Type == 2 && ProgressState <= 0)
            {
                return ApiResult.ParamIsNull("请传递自定义状态");
            }
            string msg = okrPeriodModule.SetOKRMyObjectiveState(ObjectiveId, Type, ProgressState, out int OutProgressState, userInfo);
            if (msg == "")
            {
                return ApiResult.Success("", OutProgressState);
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 修改关键结果状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyKeyResultState()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);
            int Type = parms.GetInt("Type", 0);// 类型 1自定义状态   2删除（作废）  3切换为简单模式   4切换为高级模式
            string ProgressState = parms.GetStringValue("ProgressState");
            if (KeyResultId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            string msg = okrPeriodModule.SgetOKRMyKeyResultState(KeyResultId, Type, ProgressState, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 修改目标关键结果进度
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyKeyResultProgress()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);
            int Type = parms.GetInt("Type", 0);// 类型 1简单模式   2高级模式
            int Progress = parms.GetInt("Progress", 0);
            decimal StartValue = parms.GetDecimal("StartValue");
            decimal EndValue = parms.GetDecimal("EndValue");
            decimal CurrentValue = parms.GetDecimal("CurrentValue");
            if (KeyResultId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (Progress < 0 || StartValue < 0 || EndValue < 0 || CurrentValue < 0)
            {
                return ApiResult.ParamIsNull("不能小于0");
            }

            return okrPeriodModule.SgetOKRMyKeyResultProgress(KeyResultId, Type, Progress, StartValue, EndValue, CurrentValue, userInfo);
        }

        /// <summary>
        /// 修改目标关键结果权重
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyKeyResultWeight()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);
            decimal Weight = parms.GetDecimal("Weight");
            if (KeyResultId <= 0)
            {
                return ApiResult.ParamIsNull("请传递结果Id");
            }
            if (Weight < 0 || Weight > 100)
            {
                return ApiResult.ParamIsNull("权重值有误");
            }

            return okrPeriodModule.SgetOKRMyKeyResultWeight(KeyResultId, Weight, userInfo);
        }

        /// <summary>
        /// 修改目标关键结果分数
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyKeyResultScore()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);
            decimal Score = parms.GetDecimal("Score");
            if (KeyResultId <= 0)
            {
                return ApiResult.ParamIsNull("请传递结果Id");
            }
            if (Score < 0 || Score > 1)
            {
                return ApiResult.ParamIsNull("分值有误");
            }

            return okrPeriodModule.SgetOKRMyKeyResultScore(KeyResultId, Score, userInfo);
        }

        /// <summary>
        /// 删除关键结果状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyKeyResultDel()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);
            if (KeyResultId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            return okrPeriodModule.SgetOKRMyKeyResultDel(KeyResultId, userInfo);
        }

        /// <summary>
        /// 获取目标/关键结果  更新历史
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRObjectiveUpdateHistoryList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_OKR_UpdateHistory_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;

            var list = okrPeriodModule.GetOKRObjectiveUpdateHistoryList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.Content,
                CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
            });
            return ApiResult.Success("", pageModel);
        }

        #endregion

        //权限  添加对齐  审核  评论  左边人员列表   对齐视图  数据看板  （管理员版  对齐视图  数据看板）

        #region 添加对齐
        /// <summary>
        /// 添加对齐
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyObjectiveParent()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            int ParentId = parms.GetInt("ParentId", 0);
            if (ParentId <= 0)
            {
                return ApiResult.ParamIsNull("请传递对齐目标id");
            }
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递当前目标id");
            }
            if (ObjectiveId == ParentId)
            {
                return ApiResult.ParamIsNull();
            }

            string msg = okrPeriodModule.SetOKRMyObjectiveParent(ObjectiveId, ParentId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 取消对齐
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRMyObjectiveParentCancel()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            int ParentId = parms.GetInt("ParentId", 0);
            if (ParentId <= 0)
            {
                return ApiResult.ParamIsNull("请传递对齐目标id");
            }
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递当前目标id");
            }

            string msg = okrPeriodModule.SgetOKRMyObjectiveParentCancel(ObjectiveId, ParentId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取他人的目标列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKROthersObjectiveDropList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int UserId = parms.GetInt("UserId", 0);//他人id
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);//当前目标
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (UserId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递当前目标id");
            }
            if (UserId == userInfo.Id)
            {
                return ApiResult.Failed("只能对齐他人目标");
            }

            var list = okrPeriodModule.GetOKROthersObjectiveDropList(PeriodId, ObjectiveId, UserId, userInfo);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 获取他人的目标列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKROthersObjectiveDropListByName()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            string UserName = parms.GetStringValue("UserName");//用户名称
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);//当前目标
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (string.IsNullOrEmpty(UserName))
            {
                return ApiResult.ParamIsNull("请传递用户名称");
            }
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递当前目标id");
            }

            var list = okrPeriodModule.GetOKROthersObjectiveDropListByName(PeriodId, ObjectiveId, UserName, userInfo);
            return ApiResult.Success("", list);
        }
        #endregion

        #region 权限控制

        /// <summary>
        /// 获取权限控制列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRPermissionList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int TargetId = parms.GetInt("TargetId", 0);
            int Type = parms.GetInt("Type", 0);// 1周期  2目标  3关键结果
            if (TargetId <= 0)
            {
                return ApiResult.ParamIsNull("请传递当前目标id");
            }

            var list = okrPeriodModule.GetOKRPermissionList(TargetId, Type, userInfo);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.PermissionType,
                x.TargetId,
                x.Type,
                TypeName = x.Type.ToName(),
                x.SourceId,
                x.SourceName,
                x.State
            }));
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRPermissionInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PermissionType = parms.GetInt("PermissionType", 0);
            int TargetId = parms.GetInt("TargetId", 0);
            string List = parms.GetStringValue("List");
            if (PermissionType <= 0)
            {
                return ApiResult.ParamIsNull("请传递权限类型");
            }
            if (TargetId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标值");
            }
            if (string.IsNullOrEmpty(List))
            {
                return ApiResult.ParamIsNull("请传递列表数据");
            }
            List<RB_OKR_Permission_ViewModel> dmodel = JsonHelper.DeserializeObject<List<RB_OKR_Permission_ViewModel>>(List);
            foreach (var item in dmodel)
            {
                item.PermissionType = PermissionType;
                item.TargetId = TargetId;
                if (item.Type <= 0)
                {
                    return ApiResult.ParamIsNull("请传递类型");
                }
                if (item.SourceId <= 0)
                {
                    return ApiResult.ParamIsNull("请传递来源id");
                }
                if (item.State <= 0)
                {
                    return ApiResult.ParamIsNull("请传递查看状态");
                }

                item.Group_Id = userInfo.Group_Id;
                item.School_Id = userInfo.School_Id;
                item.CreateBy = userInfo.Id;
                item.CreateTime = DateTime.Now;
                item.UpdateBy = userInfo.Id;
                item.UpdateTime = DateTime.Now;
            }
            string msg = okrPeriodModule.SgetOKRPermissionInfo(PermissionType, TargetId, dmodel, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取默认所有人可见权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRDefaultPermissionInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PermissionType = parms.GetInt("PermissionType", 0);//	权限类型 1周期 2目标 3结果
            int TargetId = parms.GetInt("TargetId", 0);//周期/目标/结果 的id
            if (PermissionType <= 0 || TargetId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            var obj = okrPeriodModule.GetOKRDefaultPermissionInfo(PermissionType, TargetId, userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 设置默认所有人可见权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKRDefaultPermissionInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PermissionType = parms.GetInt("PermissionType", 0);//	权限类型 1周期 2目标 3结果
            int TargetId = parms.GetInt("TargetId", 0);//周期/目标/结果 的id
            int Permission = parms.GetInt("Permission", 1);// 权限   1包含所有人可见   2不包含所有人可见
            if (PermissionType <= 0 || TargetId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            bool flag = okrPeriodModule.SgetOKRDefaultPermissionInfo(PermissionType, TargetId, Permission, userInfo);
            if (flag)
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        #endregion

        #region 左边人员列表

        /// <summary>
        /// 获取我的人员列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyEmployeeList()
        {
            var userInfo = base.UserInfo;
            var obj = okrPeriodModule.GetOKRMyEmployeeList(userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取我的对齐人员列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyEmployeeAlignList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            var obj = okrPeriodModule.GetOKRMyEmployeeAlignList(PeriodId, userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取我的审核人员列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyEmployeeAuditList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            //if (PeriodId <= 0)
            //{
            //    return ApiResult.ParamIsNull();
            //}

            var obj = okrPeriodModule.GetOKRMyEmployeeAuditList(PeriodId, userInfo);
            return ApiResult.Success("", obj);
        }

        #endregion

        #region 他人目标

        /// <summary>
        /// 获取他人的目标列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKROthersObjectiveList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            int UserId = parms.GetInt("UserId", 0);
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (UserId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            if (UserId == userInfo.Id)
            {
                return ApiResult.Failed("只能查看他人目标");
            }

            var list = okrPeriodModule.GetOKROthersObjectiveList(PeriodId, UserId, userInfo);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 获取他人的单个目标
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKROthersObjectiveInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            int PeriodId = parms.GetInt("PeriodId", 0);
            int UserId = parms.GetInt("UserId", 0);
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标id");
            }
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (UserId <= 0)
            {
                return ApiResult.ParamIsNull("请传递当前用户id");
            }
            if (UserId == userInfo.Id)
            {
                return ApiResult.Failed("只能查看他人目标");
            }

            var obj = okrPeriodModule.GetOKROthersObjectiveInfo(PeriodId, UserId, ObjectiveId, userInfo);
            return ApiResult.Success("", obj);
        }

        #endregion

        #region 关注

        /// <summary>
        /// 获取该员工是否关注
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKREmployeeAttention()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int UserId = parms.GetInt("UserId", 0);
            if (UserId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }

            return okrPeriodModule.GetOKREmployeeAttention(UserId, userInfo);
        }

        /// <summary>
        /// 设置关注
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetOKREmployeeAttention()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int UserId = parms.GetInt("UserId", 0);
            int Type = parms.GetInt("Type", 0);//类型  1关注   2取消关注
            if (UserId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }

            string msg = okrPeriodModule.SgetOKREmployeeAttention(UserId, Type, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }
        #endregion

        #region 审核
        /// <summary>
        /// 获取待我审核的列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRObjectiveAuditList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            int UserId = parms.GetInt("UserId", 0);
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (UserId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            if (UserId == userInfo.Id)
            {
                return ApiResult.Failed("只能审核他人目标");
            }

            return okrPeriodModule.GetOKRObjectiveAuditList(PeriodId, UserId, userInfo);
        }

        /// <summary>
        /// 审核目标
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRObjectiveAudit()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            int Type = parms.GetInt("Type", 0);//类型  1通过  2不通过
            string Remark = parms.GetStringValue("Remark");
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标sid");
            }
            if (Type <= 0)
            {
                return ApiResult.ParamIsNull("请传递类型");
            }

            string msg = okrPeriodModule.SetOKRObjectiveAudit(ObjectiveId, Type, Remark, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 重新发起审核
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRObjectiveAuditAgain()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标sid");
            }

            string msg = okrPeriodModule.SetOKRObjectiveAuditAgain(ObjectiveId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        #endregion

        #region 更新提示

        /// <summary>
        /// 清除人员的更新提示
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult ClearMyEmployeeUpdateNotification()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int UserId = parms.GetInt("UserId", 0);
            if (UserId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }

            string msg = okrPeriodModule.ClearMyEmployeeUpdateNotification(UserId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 前端调用更新用户提示
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SgetMyUserUpdateNotification()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            string UserIds = parms.GetStringValue("UserIds");
            if (string.IsNullOrEmpty(UserIds))
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            List<int> UserIdList = JsonHelper.DeserializeObject<List<int>>(UserIds);

            bool flag = okrPeriodModule.SetMyUserUpdateNotification(UserIdList, userInfo);
            if (flag)
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }
        #endregion
        #endregion

        #region 规则管理

        /// <summary>
        /// 获取规则分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRRulePageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_OKR_Rule_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;

            var list = okrPeriodModule.GetOKRRulePageList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.RuleName,
                x.DataBase,
                x.Table,
                x.Field,
                x.Way,
                WayName = x.Way.ToName(),
                x.State,
                x.Identity,
                x.Time,
                x.Remark,
                x.Enable,
                CreateTime = x.CreateTime.ToString("yyyy-MM-dd")
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取规则列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRRuleDownLoadList()
        {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_OKR_Rule_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.Enable = 1;
            var list = okrPeriodModule.GetOKRRuleDownLoadList(dmodel);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.RuleName,
                x.Remark
            }));
        }

        /// <summary>
        /// 新增修改规则信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRRuleInfo()
        {
            var userInfo = base.UserInfo;
            RB_OKR_Rule_ViewModel dmodel = JsonHelper.DeserializeObject<RB_OKR_Rule_ViewModel>(RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(dmodel.RuleName))
            {
                return ApiResult.ParamIsNull("请输入规则名称");
            }
            if (string.IsNullOrEmpty(dmodel.DataBase))
            {
                return ApiResult.ParamIsNull("请输入数据库名称");
            }
            if (string.IsNullOrEmpty(dmodel.Table))
            {
                return ApiResult.ParamIsNull("请输入表名");
            }
            if (string.IsNullOrEmpty(dmodel.Field))
            {
                return ApiResult.ParamIsNull("请输入统计字段");
            }
            if (string.IsNullOrEmpty(dmodel.State))
            {
                return ApiResult.ParamIsNull("请输入状态字段");
            }
            if (string.IsNullOrEmpty(dmodel.Identity))
            {
                return ApiResult.ParamIsNull("请输入身份字段");
            }
            if (string.IsNullOrEmpty(dmodel.Time))
            {
                return ApiResult.ParamIsNull("请输入操作时间字段");
            }

            dmodel.Status = 0;
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.School_Id = userInfo.School_Id;
            dmodel.CreateBy = userInfo.Id;
            dmodel.CreateTime = DateTime.Now;
            dmodel.UpdateBy = userInfo.Id;
            dmodel.UpdateTime = DateTime.Now;
            bool flag = okrPeriodModule.SetOKRRuleInfo(dmodel, userInfo);
            if (flag)
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        /// <summary>
        /// 设置规则状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRRuleState()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int RuleId = parms.GetInt("RuleId", 0);
            int Type = parms.GetInt("Type", 0);//类型  1启用  2禁用  3删除

            string msg = okrPeriodModule.SetOKRRuleState(RuleId, Type, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetRuleTypeEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(RuleTypeEnum));
            return ApiResult.Success("", list.Select(x => new
            {
                x.Name,
                x.Id
            }));
        }

        /// <summary>
        /// 设置关键结果使用规则
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRKeyResultUseRule()
        {
            var userInfo = base.UserInfo;
            RB_OKR_KeyResult_ViewModel dmodel = JsonHelper.DeserializeObject<RB_OKR_KeyResult_ViewModel>(RequestParm.Msg.ToString());
            if (dmodel.Id <= 0)
            {
                return ApiResult.ParamIsNull("请传递关键结果id");
            }
            if (dmodel.RuleId <= 0)
            {
                return ApiResult.ParamIsNull("请传递规则Id");
            }
            if (!dmodel.RuleSTime.HasValue)
            {
                return ApiResult.ParamIsNull("请选择规则适配开始时间");
            }
            if (!dmodel.RuleETime.HasValue)
            {
                return ApiResult.ParamIsNull("请选择规则适配结束时间");
            }
            if (dmodel.StartValue < 0)
            {
                return ApiResult.ParamIsNull("请输入结果的起始值");
            }
            if (dmodel.EndValue <= 0)
            {
                return ApiResult.ParamIsNull("请输入结果的目标值");
            }
            if (dmodel.RelationList == null || !dmodel.RelationList.Any())
            {
                return ApiResult.ParamIsNull("请选择适配部门/直属人员");
            }
            foreach (var item in dmodel.RelationList)
            {
                if (item.RelationId <= 0)
                {
                    return ApiResult.ParamIsNull("关联部门/直属人员有误");
                }
            }

            dmodel.RuleSTime = Convert.ToDateTime(dmodel.RuleSTime.Value.ToString("yyyy-MM-dd"));
            dmodel.RuleETime = Convert.ToDateTime(dmodel.RuleETime.Value.ToString("yyyy-MM-dd") + " 23:59:59");
            string msg = okrPeriodModule.SetOKRKeyResultUseRule(dmodel, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 取消使用规则
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CancelOKRKeyResultUseRule()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);

            string msg = okrPeriodModule.CancelOKRKeyResultUseRule(KeyResultId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取结果  规则关联人员/部门列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRKeyResultRuleReList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int KeyResultId = parms.GetInt("KeyResultId", 0);
            if (KeyResultId <= 0)
            {
                return ApiResult.ParamIsNull("请传递关键结果id");
            }

            var list = okrPeriodModule.GetOKRKeyResultRuleReList(KeyResultId, userInfo);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Type,
                x.RelationId,
                x.RelationName,
                x.UserIcon,
                x.PostName,
                x.DeptName
            }));
        }

        /// <summary>
        /// 获取我负责的部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyManageDepartment()
        {
            var userInfo = base.UserInfo;

            var list = okrPeriodModule.GetOKRMyManageDepartment_V2(userInfo);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 获取我的直属下级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyChildEmployeeList()
        {
            var userInfo = base.UserInfo;

            var list = okrPeriodModule.GetOKRMyChildEmployeeList(userInfo);
            return ApiResult.Success("", list);
        }

        #endregion

        #region 统计模块

        /// <summary>
        /// 获取我的对齐视图
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyAlignView()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }

            var obj = okrPeriodModule.GetOKRMyAlignView(PeriodId, userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取我的对齐视图 再下一级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMyAlignViewNext()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);//目标id 
            int Type = parms.GetInt("Type", 1);//类型  1向上  2向下
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递关联目标id");
            }

            var obj = okrPeriodModule.GetOKRMyAlignViewNext(PeriodId, ObjectiveId, Type, userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取管理员对齐视图
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRAdminAlignView()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            int UserId = parms.GetInt("UserId", 0);
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }

            var obj = okrPeriodModule.GetOKRAdminAlignView(PeriodId, UserId, userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取管理员对齐视图  再下一级
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRAdminAlignViewNext()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);//目标id 
            int Type = parms.GetInt("Type", 1);//类型  1向上  2向下
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递关联目标id");
            }

            var obj = okrPeriodModule.GetOKRAdminAlignViewNext(PeriodId, ObjectiveId, Type, userInfo);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取管理员查看单个目标
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRAdminLookObjectiveInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int ObjectiveId = parms.GetInt("ObjectiveId", 0);
            if (ObjectiveId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标id");
            }

            var obj = okrPeriodModule.GetOKRAdminLookObjectiveInfo(ObjectiveId, userInfo);
            return ApiResult.Success("", obj);
        }
        #endregion

        #region okr评论管理
        /// <summary>
        /// 获取评论分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCommentPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_OKR_Comment_ViewModel()
            {

                School_Id = base.ParmJObj.GetInt("School_Id"),
                State = base.ParmJObj.GetInt("State", 0),
                PeriodId = base.ParmJObj.GetInt("PeriodId", 0),
                AccountId = base.ParmJObj.GetInt("AccountId", 0)
            };
            if (query.PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递期数");
            }
            if (query.AccountId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            query.Group_Id = base.UserInfo.Group_Id;
            query.Type = 4;
            var list = okrPeriodModule.GetCommentPageList(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                    item.CreateByIco = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.UserIcon ?? "";
                }
                if (item.UpdateBy > 0)
                {
                    item.UpdateByName = UserReidsCache.GetUserLoginInfo(item.UpdateBy)?.AccountName ?? "";
                }
                item.CreateTimeStr = StringHelper.DateFormatToString(item.CreateTime);
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list.Select(x => new { x.CreateByName, x.Id, x.CreateByIco, x.UpdateByName, x.Content, x.CreateTimeStr });
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 新增评论状态
        /// </summary>
        /// <returns></returns>
        public ApiResult SetComment()
        {
            var extModel = new RB_OKR_Comment_ViewModel()
            {
                School_Id = base.ParmJObj.GetInt("School_Id"),
                PeriodId = base.ParmJObj.GetInt("PeriodId"),
                Content = base.ParmJObj.GetStringValue("Content"),
                AccountId = base.ParmJObj.GetInt("AccountId", 0),
                Type = base.ParmJObj.GetInt("Type", 0),
                ParentId = base.ParmJObj.GetInt("ParentId", 0)
            };
            if (extModel.PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (extModel.AccountId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            if (string.IsNullOrEmpty(extModel.Content))
            {
                return ApiResult.ParamIsNull("请传递内容");
            }
            extModel.CreateBy = base.UserInfo.Id;
            extModel.State = 1;
            extModel.CreateTime = System.DateTime.Now;
            extModel.UpdateTime = System.DateTime.Now;
            extModel.UpdateBy = base.UserInfo.Id;
            extModel.Group_Id = base.UserInfo.Group_Id;
            bool flag = okrPeriodModule.SetOKRComment(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 修改评论状态
        /// </summary>
        /// <returns></returns>
        public ApiResult SetCommentStatus()
        {
            var extModel = new RB_OKR_Comment_ViewModel()
            {
                Id = base.ParmJObj.GetInt("Id"),
                State = base.ParmJObj.GetInt("State"),
            };
            if (extModel.Id <= 0)
            {
                return ApiResult.ParamIsNull(message: "未获取到评论编号,请刷新页面重试!");
            }
            if (extModel.State <= 0)
            {
                return ApiResult.ParamIsNull(message: "请传递评论状态!");
            }
            extModel.UpdateBy = base.UserInfo.Id;
            bool flag = okrPeriodModule.SetCommentStatus(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 新增评论状态
        /// </summary>
        /// <returns></returns>
        public ApiResult SetOKRCheckedComment()
        {
            var userInfo = base.UserInfo;
            var extModel = JsonHelper.DeserializeObject<RB_OKR_Comment_ViewModel>(RequestParm.Msg.ToString());
            if (extModel.PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期id");
            }
            if (extModel.AccountId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            if (string.IsNullOrEmpty(extModel.CheckedName))
            {
                return ApiResult.ParamIsNull("请传递选中内容");
            }
            if (string.IsNullOrEmpty(extModel.Content))
            {
                return ApiResult.ParamIsNull("请传递内容");
            }
            if (extModel.Type <= 0)
            {
                return ApiResult.ParamIsNull("请传递类型");
            }
            if (extModel.TargetId <= 0)
            {
                return ApiResult.ParamIsNull("请传递目标/结果id");
            }
            extModel.CreateBy = userInfo.Id;
            extModel.State = 1;
            extModel.CreateTime = System.DateTime.Now;
            extModel.UpdateTime = System.DateTime.Now;
            extModel.UpdateBy = userInfo.Id;
            extModel.Group_Id = userInfo.Group_Id;
            bool flag = okrPeriodModule.SetOKRCheckedComment(extModel, out int Id);
            return flag ? ApiResult.Success("", Id) : ApiResult.Failed();
        }

        /// <summary>
        /// 获取评论分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetCommentPageList_V2()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_OKR_Comment_ViewModel()
            {

                School_Id = base.ParmJObj.GetInt("School_Id"),
                State = base.ParmJObj.GetInt("State", 0),
                PeriodId = base.ParmJObj.GetInt("PeriodId", 0),
                AccountId = base.ParmJObj.GetInt("AccountId", 0),
                SelectType = base.ParmJObj.GetInt("SelectType", 0)
            };
            if (query.PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递期数");
            }
            if (query.AccountId <= 0)
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }
            query.Group_Id = base.UserInfo.Group_Id;
            var list = okrPeriodModule.GetCommentPageList_V2(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = rowsCount;
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.UpdateByName,
                x.Content,
                x.Type,
                x.TargetId,
                x.Identify,
                x.State,
                CreateTimeStr = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                ChildList = x.ChildList.Select(z => new
                {
                    z.Id,
                    z.CreateByName,
                    z.CreateByIco,
                    z.Content,
                    z.Type,
                    CreateTimeStr = z.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                })
            });
            return ApiResult.Success(data: pageModel);
        }

        #endregion


        #region 数据看板-上面统计

        /// <summary>
        /// 获取个人端数据分布
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRGRDateDistribution()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int RangeType = parms.GetInt("RangeType", 1);//范围类型  非管理端查询  1所有下级  2直属下级
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (IsAdmin == 1)
            {
                RangeType = 0;
            }
            var obj = okrPeriodModule.GetOKRGRDateDistribution(PeriodId, RangeType, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取个人端数据分布明细
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRGRDateDistributionDetail()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int Type = parms.GetInt("Type", 1);//1234  对应4项统计
            int StartValue = parms.GetInt("StartValue", 0);//开始值 
            int EndValue = parms.GetInt("EndValue", 0);//结束值
            int OrderBy = parms.GetInt("OrderBy", 2);// 1升序   2降序
            int RangeType = parms.GetInt("RangeType", 1);//范围类型  非管理端查询  1所有下级  2直属下级
            int DepartId = parms.GetInt("DepartId", 0);//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (IsAdmin == 1)
            {
                RangeType = 0;
            }
            var list = okrPeriodModule.GetOKRGRDateDistributionDetail(pageModel.PageIndex, pageModel.PageSize, out long count, PeriodId, Type, StartValue, EndValue, OrderBy, RangeType, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.CreateBy,
                x.EmName,
                x.EmPhoto,
                x.DeptName,
                x.PostName,
                x.ObjectiveNum
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取个人端数据分布明细 - 员工
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRGRDateDistributionDetail_V2()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int Type = parms.GetInt("Type", 1);//1234  对应4项统计
            string UserName = parms.GetStringValue("UserName");//用户名称
            int OrderBy = parms.GetInt("OrderBy", 2);// 1升序   2降序
            int OrderByNum = parms.GetInt("OrderByNum", 1);// 以第几个排序
            int RangeType = parms.GetInt("RangeType", 1);//范围类型  非管理端查询  1所有下级  2直属下级
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (IsAdmin == 1)
            {
                RangeType = 0;
            }
            OrderByNum = OrderByNum == 0 ? 1 : OrderByNum;
            object list = okrPeriodModule.GetOKRGRDateDistributionDetail_V2(pageModel.PageIndex, pageModel.PageSize, out long count, PeriodId, UserName, Type, OrderBy, OrderByNum, RangeType, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list;
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取个人端数据分布明细 - 部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRGRDateDistributionForDepartment()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int Type = parms.GetInt("Type", 1);//1234  对应4项统计
            int OrderBy = parms.GetInt("OrderBy", 2);// 1升序   2降序
            int OrderByNum = parms.GetInt("OrderByNum", 1);// 以第几个排序       
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            var list = okrPeriodModule.GetOKRGRDateDistributionForDepartment(PeriodId, Type, OrderBy, OrderByNum, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 获取事项统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMatterList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            object list = okrPeriodModule.GetOKRMatterList(PeriodId, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 获取事项明细列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRMatterDetailPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int Type = parms.GetInt("Type", 0);//类型
            int DepartId = parms.GetInt("DepartId", 0);//部门ID
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递周期");
            }
            if (Type <= 0)
            {
                return ApiResult.ParamIsNull("请传递类型");
            }
            var list = okrPeriodModule.GetOKRMatterDetailPageList(pageModel.PageIndex, pageModel.PageSize, out long count, PeriodId, Type, DepartId, PostId, LeaveStatus, userInfo);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.CreateBy,
                x.EmName,
                x.EmPhoto,
                x.DeptName,
                x.PostName
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 忽略注意事项
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetOKRMatterIgnore()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int Type = parms.GetInt("Type", 0);//类型
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (Type <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            bool flag = okrPeriodModule.SetOKRMatterIgnore(PeriodId, Type, userInfo);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取概率查询
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRProbabilityStatistics()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int RangeType = parms.GetInt("RangeType", 1);//范围类型  非管理端查询  1所有下级  2直属下级
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (IsAdmin == 1)
            {
                RangeType = 0;
            }
            var obj = okrPeriodModule.GetOKRProbabilityInfo(PeriodId, RangeType, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 获取概率明细 - 人员
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRProbabilityPageListForEmp() {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int SelectType = parms.GetInt("SelectType", 0);//类型  1填写率  2对齐率  3更新率  4打分率
            int OrderBy = parms.GetInt("OrderBy", 1);//排序  1已填写在前  2未填写在前
            string UserName = parms.GetStringValue("UserName");//用户名称
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int RangeType = parms.GetInt("RangeType", 1);//范围类型  非管理端查询  1所有下级  2直属下级
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (IsAdmin == 1)
            {
                RangeType = 0;
            }
            var list = okrPeriodModule.GetOKRProbabilityPageListForEmp(pageModel.PageIndex, pageModel.PageSize, out long count, PeriodId, SelectType, OrderBy, UserName, RangeType, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                EmpId = x.CreateBy,
                x.EmName,
                x.EmPhoto,
                x.DeptName,
                x.PostName,
                x.Status
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取概率明细 - 部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRProbabilityPageListForDept()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int SelectType = parms.GetInt("SelectType", 0);//类型  1填写率  2对齐率  3更新率  4打分率
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            var list = okrPeriodModule.GetOKRProbabilityPageListForDept(PeriodId, SelectType, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            return ApiResult.Success("", list);
        }
        #endregion



        #region 数据看板  最新

        /// <summary>
        /// 获取数据看板员工信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRGRDateDistributionPage()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int Type = parms.GetInt("Type", 1);//1234  对应4项统计
            int StartValue = parms.GetInt("StartValue", 0);//开始值 
            int EndValue = parms.GetInt("EndValue", 0);//结束值
            int OrderBy = parms.GetInt("OrderBy", 2);// 1升序   2降序
            int RangeType = parms.GetInt("RangeType", 1);//范围类型  非管理端查询  1所有下级  2直属下级
            string DepartIds = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            string EmployeeName = parms.GetStringValue("EmployeeName");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (IsAdmin == 1)
            {
                RangeType = 0;
            }
            var list = okrPeriodModule.GetOKRGRDateDistributionPage(pageModel.PageIndex, pageModel.PageSize, out long count, EmployeeName, PeriodId, Type, StartValue, EndValue, OrderBy, RangeType, DepartIds, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.DeptName,
                x.PostName,
                x.ObjectiveNCount,
                x.OBjectiveRelation,
                x.ScoreCount,
                x.DayNum,
                x.UserIcon,
                x.EmployeeName
            });
            return ApiResult.Success("", pageModel);
        }


        /// <summary>
        /// 获取个人端数据分布明细 - 部门
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRGRDateDistributionForDept()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//周期id
            int IsAdmin = parms.GetInt("IsAdmin", 2);//是否管理端查询  1是  2否
            int Type = parms.GetInt("Type", 1);//1234  对应4项统计
            int OrderBy = parms.GetInt("OrderBy", 2);// 1升序   2降序
            int OrderByNum = parms.GetInt("OrderByNum", 1);// 以第几个排序       
            string DepartId = parms.GetStringValue("DepartId");//部门ID   管理端可用查询
            int PostId = parms.GetInt("PostId", 0);//岗位ID
            int LeaveStatus = parms.GetInt("LeaveStatus", 0);//在职状态
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            object list = okrPeriodModule.GetOKRGRDateDistributionForDept(PeriodId, Type, OrderBy, OrderByNum, DepartId, PostId, LeaveStatus, userInfo, IsAdmin == 1);
            return ApiResult.Success("", list);
        }



        #endregion

        #region 个人端数据看板  作废
        /// <summary>
        /// 获取个人维度
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOKRPersonalDimensionList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);
            int DeptId = parms.GetInt("DeptId", 0);
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            List<OKRPersonalDimension> allList = okrPeriodModule.GetOKRPersonalDimensionList(userInfo.Group_Id, userInfo.Id, DeptId);


            if (allList == null || !allList.Any(x => x.Type == 2))
            {
                return ApiResult.Failed("您没有下级");
            }
            else
            {
                decimal objectiveRate = 0;
                decimal objectiveRelationRate = 0;
                decimal scoreRate = 0;
                string createBys = string.Join(",", allList.Where(x => x.Type == 2).Select(x => x.Id));
                var obj = okrPeriodModule.GetOKRPersonalAlignList(userInfo.Group_Id, PeriodId, createBys);
                if (obj != null && obj.Any())
                {
                    var objRelation = okrPeriodModule.GetOKRPersonalRelationAlignList(userInfo.Group_Id, PeriodId, createBys, userInfo.Id);
                    foreach (var item in allList.Where(x => x.Type == 2))
                    {
                        item.ObjectiveNCount = obj.Where(x => x.CreateBy == item.Id).Sum(x => x.ChildNum);
                        item.OBjectiveRelation = objRelation.Where(x => x.CreateBy == item.Id).Sum(x => x.ChildNum);
                        item.ScoreCount = obj.Where(x => x.CreateBy == item.Id).Sum(x => x.Score);
                    }
                    int allChildrenCount = allList.Where(x => x.Type == 2).Count();
                    objectiveRate = decimal.Round(obj.GroupBy(x => x.CreateBy).Count() / allChildrenCount, 2);
                    objectiveRelationRate = decimal.Round(objRelation.GroupBy(x => x.CreateBy).Count() / allChildrenCount, 2);
                    scoreRate = decimal.Round(obj.Where(x => x.Score > 0).GroupBy(x => x.CreateBy).Count() / allChildrenCount, 2);
                }
                var resutl = new
                {
                    MyChildrenList = allList.Where(x => x.Type == 2),
                    objectiveRate,
                    objectiveRelationRate,
                    scoreRate
                };
                return ApiResult.Success("", resutl);

            }
        }

        #endregion


        #region 管理端数据看板  作废
        /// <summary>
        /// 获取管理端数据看板员工分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAllEmployeeOKRPPageList()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new OKRPersonalDimension()
            {

                School_Id = base.ParmJObj.GetInt("School_Id"),

                PeriodId = base.ParmJObj.GetInt("PeriodId", 0),
                QDeptIds = base.ParmJObj.GetStringValue("QDeptIds"),
                Post_Id = base.ParmJObj.GetInt("PostId", 0),//岗位ID
                LeaveStatus = base.ParmJObj.GetInt("LeaveStatus", 0),//在职状态
            };

            query.Group_Id = base.UserInfo.Group_Id;
            var list = okrPeriodModule.GetAllEmployeeOKRPPageRepository(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            if (list != null && list.Any())
            {
                string ids = string.Join(",", list.Select(x => x.Id));
                var objRelation = okrPeriodModule.GetOKRPersonalRelationAlignList_V2(base.UserInfo.Group_Id, query.PeriodId, ids);

                foreach (var item in list)
                {
                    item.OBjectiveRelation = objRelation.Where(x => x.CreateBy == item.Id).Sum(x => x.ChildNum);
                }
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = list;
            return ApiResult.Success(data: pageModel);
        }




        /// <summary>
        /// 获取部门树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDeptOKR()
        {
            int PeriodId = base.ParmJObj.GetInt("PeriodId", 0);
            var query = new Model.ViewModel.User.RB_Department_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                QDeptIds = base.ParmJObj.GetStringValue("QDeptIds"),
            };
            var list = departmentModule.GetOrganizationOKRModule(query, true);
            var okrPersonalList = okrPeriodModule.GetAllEmployeeOKRPList(new OKRPersonalDimension { School_Id = query.School_Id, Group_Id = query.Group_Id, Dept_Ids = query.QDeptIds, PeriodId = PeriodId });

            int ChildrenNum = 0;

            List<OKRPersonalDimension> listResult = new List<OKRPersonalDimension>();
            foreach (var item in list.Where(x => x.DataType == 1))
            {
                OKRPersonalDimension model = new OKRPersonalDimension
                {
                    DeptName = item.DeptName,
                    DeptPersonName = string.IsNullOrWhiteSpace(item.ManagerIds) ? "" : string.Join(",", okrPersonalList.Where(x => item.ManagerIds.Contains(x.AccountId.ToString())).Select(x => x.EmployeeName)),
                    ChildrenNum = list.Where(x => x.DataType == 2 && x.DeptId == item.DeptId).Count()
                };
                ChildrenNum += model.ChildrenNum;
                if (model.ChildrenNum > 0)
                {
                    model.ObjectiveRate = decimal.Round((okrPersonalList.Where(x => x.Dept_Id == item.DeptId && x.ObjectiveNCount > 0).Count()) / model.ChildrenNum, 2);
                    model.ScoreRate = decimal.Round((okrPersonalList.Where(x => x.Dept_Id == item.DeptId && x.ScoreCount > 0).Count()) / model.ChildrenNum, 2);
                    model.OBjectiveRelation = 0;
                    foreach (var okrItem in okrPersonalList.Where(x => x.Dept_Id == item.DeptId))
                    {
                        model.OBjectiveRelation += okrPeriodModule.GetOKRPersonalRelationAlignList(query.Group_Id, PeriodId, okrItem.AccountId.ToString(), okrItem.AccountId).Where(x => x.ChildNum > 0).Count();
                    }
                    model.ObjectiveRelationRate = decimal.Round(model.OBjectiveRelation / model.ChildrenNum, 2);
                }
                listResult.Add(model);
            }
            return ApiResult.Success(data: listResult.Select(x => new { x.DeptName, x.Dept_Id, x.ObjectiveRate, x.ObjectiveRelationRate, x.ScoreRate, x.ChildrenNum }));
        }

        /// <summary>
        /// 获取部门树形列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetDeptOKR_V2()
        {
            int PeriodId = base.ParmJObj.GetInt("PeriodId", 0);
            var query = new Model.ViewModel.User.RB_Department_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                // School_Id = base.ParmJObj.GetInt("School_Id"),
                QDeptIds = base.ParmJObj.GetStringValue("QDeptIds"),
            };
            var list = departmentModule.GetOrganizationOKRModule(query, true);
            var okrPersonalList = okrPeriodModule.GetAllEmployeeOKRPList(new OKRPersonalDimension { School_Id = query.School_Id, Group_Id = query.Group_Id, Dept_Ids = query.QDeptIds, PeriodId = PeriodId });

            List<OKRPersonalDimension> listResult = new List<OKRPersonalDimension>();
            foreach (var item in list.Where(x => x.DataType == 1))
            {
                OKRPersonalDimension model = new OKRPersonalDimension
                {
                    DeptName = item.DeptName,
                    DeptPersonName = string.IsNullOrWhiteSpace(item.ManagerIds) ? "" : string.Join(",", okrPersonalList.Where(x => item.ManagerIds.Contains(x.AccountId.ToString())).Select(x => x.EmployeeName)),
                    ChildrenNum = list.Where(x => x.DataType == 2 && x.DeptId == item.DeptId).Count()
                };

                if (model.ChildrenNum > 0)
                {
                    string ids = string.Join(",", list.Where(x => x.DataType == 2 && x.DeptId == item.DeptId).Select(x => x.DeptId));
                    var objRelation = okrPeriodModule.GetOKRPersonalRelationAlignList_V2(base.UserInfo.Group_Id, PeriodId, ids);
                    model.ObjectiveRate = decimal.Round((okrPersonalList.Where(x => x.Dept_Id == item.DeptId && x.ObjectiveNCount > 0).Count()) / model.ChildrenNum, 2);
                    model.ScoreRate = decimal.Round((okrPersonalList.Where(x => x.Dept_Id == item.DeptId && x.ScoreCount > 0).Count()) / model.ChildrenNum, 2);
                    model.ObjectiveRelationRate = decimal.Round((objRelation.Where(x => x.ChildNum > 0).Count()) / model.ChildrenNum, 2);
                }
                listResult.Add(model);
                // model.OKRPersonalDimensionList.AddRange(GetChildDeptOKR(item.ChildList, PeriodId, okrPersonalList, listResult));
            }
            return ApiResult.Success(data: listResult.Select(x => new { x.DeptName, x.Dept_Id, x.ObjectiveRate, x.ObjectiveRelationRate, x.ScoreRate, x.ChildrenNum }));
        }
        #endregion

    }
}
