using Edu.Common.Enum;
using Edu.Model.Entity.Sell;
using Edu.Model.ViewModel.Sell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.Sell
{
    /// <summary>
    /// 课程顾问、市场达标奖金配置阶梯仓储层
    /// </summary>
    public class RB_Bouns_PlanDetailRepository : BaseRepository<RB_Bouns_PlanDetail>
    {
        /// <summary>
        /// 获取课程顾问、市场达标奖金配置阶梯列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Bouns_PlanDetail_Extend> GetBounsPlanDetailListRepository(RB_Bouns_PlanDetail_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.*
FROM RB_Bouns_PlanDetail AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bouns_PlanDetail_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.PlanId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Bouns_PlanDetail_Extend.PlanId), query.PlanId);
                }
                if (!string.IsNullOrEmpty(query.QPlanIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Bouns_PlanDetail_Extend.PlanId), query.QPlanIds);
                }
            }
            return Get<RB_Bouns_PlanDetail_Extend>(builder.ToString()).ToList();
        }


        /// <summary>
        /// 根据计划编号删除阶梯信息
        /// </summary>
        /// <param name="PlanId"></param>
        /// <param name="DetailId"></param>
        /// <returns></returns>
        public bool RemoveBounsPlanDetailByPlanIdRepository(int PlanId,int DetailId=0)
        {
            string sql = string.Format(@" UPDATE RB_Bouns_PlanDetail SET Status=1 WHERE PlanId={0} ", PlanId);
            if (DetailId > 0)
            {
                sql += string.Format(" AND DetailId={0} ", DetailId);
            }
            return base.Execute(sql) > 0;
        }
    }
}
