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

namespace Edu.Repository.Sell
{
    /// <summary>
    /// 课程顾问、市场达标奖金配置仓储层
    /// </summary>
    public class RB_Bonus_PlanRepository : BaseRepository<RB_Bonus_Plan>
    {

        /// <summary>
        /// 课程顾问、市场达标奖金配置阶梯仓储层对象
        /// </summary>
        private readonly RB_Bouns_PlanDetailRepository bouns_PlanDetailRepository = new RB_Bouns_PlanDetailRepository();

        /// <summary>
        ///  获取课程顾问、市场达标奖金配置分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Bonus_PlanExtend> GetBounsPlanPageRepository(int pageIndex,int pageSize,out long rowsCount,RB_Bonus_PlanExtend query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Bonus_Plan AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bonus_PlanExtend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bonus_PlanExtend.Group_Id), query.Group_Id);
                }
                if (query.PlanType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bonus_PlanExtend.PlanType), query.PlanType);
                }
                if (!string.IsNullOrEmpty(query.PlanName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @PlanName ", nameof(RB_Bonus_PlanExtend.PlanName));
                    parameters.Add("PlanName", "%" + query.PlanName.Trim() + "%");
                }
            }
            return GetPage<RB_Bonus_PlanExtend>(pageIndex,pageSize,out rowsCount,builder.ToString()).ToList();
        }

        /// <summary>
        /// 获取课程顾问、市场达标奖金配置列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Bonus_PlanExtend> GetBounsPlanListRepository(RB_Bonus_PlanExtend query, bool isGetDetail = false)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Bonus_Plan AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bonus_PlanExtend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bonus_PlanExtend.Group_Id), query.Group_Id);
                }
                if (query.PlanType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bonus_PlanExtend.PlanType), query.PlanType);
                }
            }
            var list= Get<RB_Bonus_PlanExtend>(builder.ToString()).ToList();
            if (list != null && list.Count > 0 && isGetDetail)
            {
                string planIds = string.Join(",", list.Select(qitem => qitem.Id));
                List<RB_Bouns_PlanDetail_Extend> detailList = bouns_PlanDetailRepository.GetBounsPlanDetailListRepository(new RB_Bouns_PlanDetail_Extend()
                {
                    QPlanIds = planIds
                });
                foreach (var item in list)
                {
                    item.DetailList = detailList?.Where(qitem => qitem.PlanId == item.Id)?.ToList();
                }
            }
            return list;
        }
    }
}
