using Edu.Common.Enum;
using Edu.Model.Entity.OKR;
using Edu.Model.ViewModel.OKR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.OKR
{
    /// <summary>
    /// OKR对齐仓储层
    /// </summary>
    public class RB_OKR_ObjectiveRelationRepository : BaseRepository<RB_OKR_ObjectiveRelation>
    {
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_ObjectiveRelation> GetList(RB_OKR_ObjectiveRelation demodel)
        {
            string where = $@" 1=1 ";
            if (demodel.ObjectiveId > 0)
            {
                where += $@" and {nameof(RB_OKR_ObjectiveRelation.ObjectiveId)} ={demodel.ObjectiveId}";
            }
            if (demodel.ParentId > 0)
            {
                where += $@" and {nameof(RB_OKR_ObjectiveRelation.ParentId)} ={demodel.ParentId}";
            }

            string sql = $@" select * from RB_OKR_ObjectiveRelation where {where} order by Id desc";
            return Get<RB_OKR_ObjectiveRelation>(sql).ToList();
        }

        /// <summary>
        /// 获取上对齐数量
        /// </summary>
        /// <param name="ObjectiveIds"></param>
        /// <returns></returns>
        public List<RB_OKR_ObjectiveRelation> GetParentAlignNum(string ObjectiveIds)
        {
            string sql = $@" select ObjectiveId,COUNT(0) AS ParentId from RB_OKR_ObjectiveRelation WHERE ObjectiveId in({ObjectiveIds}) GROUP BY ObjectiveId";
            return Get<RB_OKR_ObjectiveRelation>(sql).ToList();
        }

        /// <summary>
        /// 获取下对齐数量
        /// </summary>
        /// <param name="ObjectiveIds"></param>
        /// <returns></returns>
        public List<RB_OKR_ObjectiveRelation> GetChildAlignNum(string ObjectiveIds)
        {
            string sql = $@" select ParentId,COUNT(0) AS ObjectiveId from RB_OKR_ObjectiveRelation WHERE ParentId in({ObjectiveIds}) GROUP BY ParentId";
            return Get<RB_OKR_ObjectiveRelation>(sql).ToList();
        }

        /// <summary>
        /// 获取目标的上对齐 所有的目标
        /// </summary>
        /// <param name="objectiveId"></param>
        /// <returns></returns>
        public string GetObjectiveParentIds(int objectiveId)
        {
            string sql = $" SELECT func_okr_getobjectiveparentids({objectiveId});";
            var obj = ExecuteScalar(sql);
            return obj == null ? "" : obj.ToString();
        }

        /// <summary>
        /// 获取目标的下对齐 所有的目标
        /// </summary>
        /// <param name="objectiveId"></param>
        /// <returns></returns>
        public string GetObjectiveChildIds(int objectiveId)
        {
            string sql = $" SELECT func_okr_getobjectivechildids({objectiveId});";
            var obj = ExecuteScalar(sql);
            return obj == null ? "" : obj.ToString();
        }
    }
}
