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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 课程优惠仓储层
    /// </summary>
    public class RB_Course_PreferentialRepository : BaseRepository<RB_Course_Preferential>
    {
        /// <summary>
        /// 获取课程优惠列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_Preferential_Extend> GetCoursePreferentialListRepostory(RB_Course_Preferential_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT * 
FROM RB_Course_Preferential 
WHERE 1=1 ");
            builder.AppendFormat(" AND {0}={1} ", nameof(RB_Course_Preferential_Extend.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_Course_Preferential_Extend>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Course_Preferential_Extend.Group_Id), query.Group_Id);
                }
                if (query.CourseId > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Course_Preferential_Extend.CourseId), query.CourseId);
                }
                if (!string.IsNullOrEmpty(query.QCourseIds))
                {
                    builder.AppendFormat(" AND {0} IN({1}) ", nameof(RB_Course_Preferential_Extend.CourseId), query.QCourseIds);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(" AND {0}={1} ", nameof(RB_Course_Preferential_Extend.Id), query.Id);
                }
            }
            return Get<RB_Course_Preferential_Extend>(builder.ToString()).ToList();
        }


        /// <summary>
        /// 根据课程编号删除优惠信息
        /// </summary>
        /// <param name="CourseId">课程编号</param>
        /// <returns></returns>
        public bool DeleteCoursePreferentialRepostory(int CourseId)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" DELETE FROM RB_Course_Preferential WHERE CourseId={0} ", CourseId);
            return base.Execute(builder.ToString()) > 0;
        }
    }
}
