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_KeyResultRepository : BaseRepository<RB_OKR_KeyResult>
    {
        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_KeyResult_ViewModel> GetPageList(int pageIndex, int pageSize, out long rowsCount, RB_OKR_KeyResult_ViewModel demodel)
        {
            string where = $@" 1=1 and Status =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.ObjectiveId > 0)
            {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.ObjectiveId)} ={demodel.ObjectiveId}";
            }
            if (demodel.ProgressState > 0)
            {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.ProgressState)} ={(int)demodel.ProgressState}";
            }

            string sql = $@" select * from RB_OKR_KeyResult where {where} order by Id desc";
            return GetPage<RB_OKR_KeyResult_ViewModel>(pageIndex, pageSize, out rowsCount, sql).ToList();
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_KeyResult_ViewModel> GetList(RB_OKR_KeyResult_ViewModel demodel)
        {
            string where = $@" 1=1 and Status =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.ObjectiveId > 0)
            {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.ObjectiveId)} ={demodel.ObjectiveId}";
            }
            if (!string.IsNullOrEmpty(demodel.ObjectiveIds))
            {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.ObjectiveId)} in({demodel.ObjectiveIds})";
            }
            if (demodel.ProgressState > 0)
            {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.ProgressState)} ={(int)demodel.ProgressState}";
            }
            if (demodel.IsUseRule == 1) {
                where += $@" and {nameof(RB_OKR_KeyResult_ViewModel.RuleId)} >0";
            }

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

        /// <summary>
        /// 获取最大状态
        /// </summary>
        /// <param name="objectiveId"></param>
        /// <returns></returns>
        public int GetKeyResultMaxProgressState(int objectiveId)
        {
            string sql = $@"SELECT MAX(ProgressState) as ProgressState FROM rb_okr_keyresult WHERE Status =0 and ObjectiveId ={objectiveId}";
            var obj = ExecuteScalar(sql);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 获取最大sort
        /// </summary>
        /// <param name="objectiveId"></param>
        /// <returns></returns>
        public int GetKeyResultMaxSort(int objectiveId)
        {
            string sql = $@"SELECT MAX(Sort) Sort FROM rb_okr_keyresult WHERE Status =0 and ObjectiveId ={objectiveId}";
            var obj = ExecuteScalar(sql);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 设置排序
        /// </summary>
        /// <param name="objectiveId"></param>
        /// <param name="id"></param>
        /// <param name="startSort"></param>
        /// <param name="endSort"></param>
        /// <returns></returns>
        public bool UpdateOtherSort(int objectiveId, int id, int startSort, int endSort)
        {
            string sql = $@"UPDATE rb_okr_keyresult SET Sort =Sort +1 WHERE Status =0 and ObjectiveId={objectiveId} AND Id <>{id} ";
            if (startSort > 0)
            {
                sql += $@" AND Sort > {startSort} ";
            }
            if (endSort > 0)
            {
                sql += $@" and Sort < {endSort} ";
            }
            return Execute(sql) > 0;
        }

        /// <summary>
        /// 设置排序
        /// </summary>
        /// <param name="objectiveId"></param>
        /// <param name="id"></param>
        /// <param name="startSort"></param>
        /// <param name="endSort"></param>
        /// <returns></returns>
        public bool UpdateOtherSortDesc(int objectiveId, int id, int startSort, int endSort)
        {
            string sql = $@"UPDATE rb_okr_keyresult SET Sort =Sort -1 WHERE Status =0 and ObjectiveId={objectiveId} AND Id <>{id} ";
            if (startSort > 0 && endSort > 0)
            {
                sql += $@" AND Sort > {startSort} and Sort <= {endSort}";
            }
            return Execute(sql) > 0;
        }
    }
}