using Edu.Model.Entity.Bonus;
using Edu.Model.ViewModel.Bonus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.Bonus
{
    /// <summary>
    /// 员工人头奖励仓储层
    /// </summary>
    public class RB_Personnel_BonusRepository : BaseRepository<RB_Personnel_Bonus>
    {
        /// <summary>
        /// 员工人头奖励详情仓储层对象
        /// </summary>
        private readonly RB_Personnel_BonusDetailRepository personnel_BonusDetailRepository = new RB_Personnel_BonusDetailRepository();

        /// <summary>
        /// 获取员工人头奖励分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Personnel_Bonus_Extend> GetPersonnelBonusPageRepository(int pageIndex,int pageSize,out long rowsCount, RB_Personnel_Bonus_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Personnel_Bonus AS A
WHERE 1=1
");
            if (query != null)
            {
          
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Personnel_Bonus_Extend.Group_Id), query.Group_Id);
                }
                if (query.YearNum>0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Personnel_Bonus_Extend.YearNum), query.YearNum);
                }
                if (query.MonthNum > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Personnel_Bonus_Extend.MonthNum), query.MonthNum);
                }
                if (query.PeriodId > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Personnel_Bonus_Extend.PeriodId), query.PeriodId);
                }
            }
            return GetPage<RB_Personnel_Bonus_Extend>(pageIndex,pageSize,out rowsCount,builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取员工人头奖励列表
        /// </summary>
        /// <param name="query"></param>
        /// <param name="isGetDetail">true-查询详情</param>
        /// <returns></returns>
        public List<RB_Personnel_Bonus_Extend> GetPersonnelBonusListRepository(RB_Personnel_Bonus_Extend query,bool isGetDetail=false)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Personnel_Bonus AS A
WHERE 1=1
");
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Personnel_Bonus_Extend.Group_Id), query.Group_Id);
                }
                if (query.YearNum > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Personnel_Bonus_Extend.YearNum), query.YearNum);
                }
                if (query.MonthNum > 0)
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Personnel_Bonus_Extend.MonthNum), query.MonthNum);
                }
                if (query.EmployeeId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Personnel_Bonus_Extend.EmployeeId), query.EmployeeId);
                }
                if (!string.IsNullOrEmpty(query.QEmployeeIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Personnel_Bonus_Extend.EmployeeId), query.QEmployeeIds);
                }
                if (query.PlanType > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Personnel_Bonus_Extend.PlanType), query.PlanType);
                }
                if (query.IsNoBalance == 1)
                {
                    builder.AppendFormat(" AND A.DeductionMoney<>A.FinishDeductionMoney ");
                }
                if (query.PeriodId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Personnel_Bonus_Extend.PeriodId), query.PeriodId);
                }
                if (!string.IsNullOrEmpty(query.QPeriodIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Personnel_Bonus_Extend.PeriodId), query.QPeriodIds);
                }
            }
            var list= Get<RB_Personnel_Bonus_Extend>(builder.ToString()).ToList();
            if (list != null && list.Count > 0)
            {
                string ids = string.Join(",", list.Select(qitem => qitem.Id));
                List<RB_Personnel_BonusDetail_Extend> detailsList = personnel_BonusDetailRepository.GetPersonnelBonusDetailListRepository(new RB_Personnel_BonusDetail_Extend()
                {
                    QBonusIds = ids
                });
                foreach (var item in list)
                {
                    item.DetailList = detailsList?.Where(qitem => qitem.BonusId == item.Id)?.ToList();
                }
            }
            return list;
        }

        /// <summary>
        /// 设置员工人头奖励未已发放
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public bool SetEmployeeBounsIsIssueRepository(string Ids)
        {
            string sql = string.Format(@" UPDATE RB_Personnel_Bonus SET IsIssue=1 WHERE Id IN({0})", Ids);
            return base.Execute(sql) > 0;   
        }
    }
}