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

namespace Edu.Repository.Course
{
    /// <summary>
    /// 课程报价详情仓储层
    /// </summary>
    public class RB_Course_OfferDetailsRepository : BaseRepository<RB_Course_OfferDetails>
    {
        /// <summary>
        /// 获取报价单详情列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Course_OfferDetails_ViewModel> GetCourseOfferDetailsListRepository(RB_Course_OfferDetails_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@" 
SELECT * FROM
(
		SELECT A.*,IFNULL(C.CourseName,'') AS CourseName,IFNULL(C.SellPrice,0) AS SellPrice
		FROM RB_Course_OfferDetails AS A INNER JOIN rb_course_offer AS B ON A.OfferId=B.Id  
				 INNER JOIN rb_course AS C ON (A.CourseId=C.CourseId AND B.OfferType=1 )
		UNION ALL 
		SELECT A.*,IFNULL(C.`Name`,'') AS CourseName,IFNULL(C.SellPrice,0) AS SellPrice
		FROM RB_Course_OfferDetails AS A INNER JOIN rb_course_offer AS B ON A.OfferId=B.Id  
				 INNER JOIN rb_studyabroad AS C ON (A.CourseId=C.Id AND B.OfferType<>1) 
) AS A
WHERE 1=1
");   
            if (query == null)
            {
                return new List<RB_Course_OfferDetails_ViewModel>();
            }
            else
            {
                if (query.OfferId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Course_OfferDetails_ViewModel.OfferId), query.OfferId);
                }
                if (!string.IsNullOrEmpty(query.QOfferIds))
                {
                    builder.AppendFormat(" AND A.{0} IN({1}) ", nameof(RB_Course_OfferDetails_ViewModel.OfferId), query.QOfferIds);
                }
            }
            return Get<RB_Course_OfferDetails_ViewModel>(builder.ToString(), parameters).ToList();
        }

        /// <summary>
        /// 根据报价单编号删除报价单详情
        /// </summary>
        /// <param name="OfferId"></param>
        /// <returns></returns>
        public bool DeleteCourseOfferDetailsRepository(int OfferId)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(" DELETE FROM RB_Course_OfferDetails WHERE OfferId IN({0}) ", OfferId);
            return base.Execute(builder.ToString()) > 0;
        }
    }
}
