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_PeriodRepository : BaseRepository<RB_OKR_Period>
    {

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_Period_ViewModel> GetPageList(int pageIndex, int pageSize, out long rowsCount, RB_OKR_Period_ViewModel demodel)
        {
            string where = $@" 1=1 and Status =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_OKR_Period_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.IsYear > 0)
            {
                where += $@" and {nameof(RB_OKR_Period_ViewModel.IsYear)} ={demodel.IsYear}";
            }

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

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="demodel"></param>
        /// <returns></returns>
        public List<RB_OKR_Period_ViewModel> GetList(RB_OKR_Period_ViewModel demodel)
        {
            string where = $@" 1=1 and Status =0";
            if (demodel.Group_Id > 0)
            {
                where += $@" and {nameof(RB_OKR_Period_ViewModel.Group_Id)} ={demodel.Group_Id}";
            }
            if (demodel.IsYear > 0)
            {
                where += $@" and {nameof(RB_OKR_Period_ViewModel.IsYear)} ={demodel.IsYear}";
            }
            if (demodel.IsNormal > 0)
            {
                where += $@" and {nameof(RB_OKR_Period_ViewModel.IsNormal)} ={demodel.IsNormal}";
            }
            if (demodel.IsCurrent == 1)
            {
                where += $@" and {nameof(RB_OKR_Period_ViewModel.StartDate)} <='{DateTime.Now.ToString("yyyy-MM-dd")}'";
                where += $@" and {nameof(RB_OKR_Period_ViewModel.EndDate)} >='{DateTime.Now.ToString("yyyy-MM-dd")}'";
            }
            if (!string.IsNullOrEmpty(demodel.Name))
            {
                where += $@" and {nameof(RB_OKR_Period_ViewModel.Name)} ='{demodel.Name}'";
            }

            string sql = $@" select * from RB_OKR_Period where {where} order by StartDate desc,IsYear asc";
            return Get<RB_OKR_Period_ViewModel>(sql).ToList();
        }

        /// <summary>
        /// 更新周期全部未历史周期
        /// </summary>
        /// <param name="GroupId"></param>
        /// <returns></returns>
        public bool SetPeriodHistory(int GroupId) {
            string sql = $" UPDATE rb_okr_period SET IsNormal =2 WHERE Group_Id={GroupId} AND IsNormal =1 AND IsYear =2";
            return Execute(sql)> 0;
        }

        /// <summary>
        /// 获取上一周期id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isYear"></param>
        /// <param name="group_Id"></param>
        /// <returns></returns>
        public int GetLastPeriodId(int id, int isYear, int group_Id)
        {
            string sql = $@"SELECT Id FROM rb_okr_period WHERE Id <{id} and IsNormal = 1 and IsYear = {isYear} and Group_Id ={group_Id} ORDER BY Id DESC LIMIT 1";
            var obj = ExecuteScalar(sql);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }
    }
}
