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

namespace Edu.Repository.Web
{
    /// <summary>
    /// 咨询管理仓储层
    /// </summary>
    public class RB_Web_AdvisoryRepository : BaseRepository<RB_Web_Advisory>
    {
        /// <summary>
        /// 获取咨询管理分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示调试</param>
        /// <param name="rowsCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Web_Advisory_ViewModel> GetWebAdvisoryPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Web_Advisory_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT A.* FROM
(
		SELECT A.*,IFNULL(B.CourseName,'') AS SourceName
		FROM rb_web_advisory AS A INNER JOIN rb_course AS B ON (A.SourceId=B.CourseId AND A.AdvType=1)
		UNION ALL 
		SELECT A.*,IFNULL(B.ClassName,'') AS SourceName
		FROM rb_web_advisory AS A INNER JOIN rb_class AS B ON  (A.SourceId=B.ClassId AND A.AdvType=2)
		UNION ALL 
		SELECT A.*,IFNULL(B.`Name`,'') AS SourceName
		FROM rb_web_advisory AS A INNER JOIN rb_studyabroad AS B ON  (A.SourceId=B.Id AND A.AdvType=3)
) AS A
WHERE 1=1
");
            if (query == null)
            {
                return new List<RB_Web_Advisory_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Advisory_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.LinkMan))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @LinkMan ", nameof(RB_Web_Advisory_ViewModel.LinkMan));
                    parameters.Add("LinkMan", "%" + query.LinkMan.Trim() + "%");
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Advisory_ViewModel.Status), (int)query.Status);
                }
                if (query.DealStatus >= 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Advisory_ViewModel.DealStatus), query.DealStatus);
                }
                if (query.Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Advisory_ViewModel.Id), query.Id);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Web_Advisory_ViewModel.Id));
            return GetPage<RB_Web_Advisory_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 获取咨询管理实体
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public RB_Web_Advisory_ViewModel GetWebAdvisoryEntityRepository(RB_Web_Advisory_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT A.* FROM
(
		SELECT A.*,IFNULL(B.CourseName,'') AS SourceName
		FROM rb_web_advisory AS A INNER JOIN rb_course AS B ON (A.SourceId=B.CourseId AND A.AdvType=1)
		UNION ALL 
		SELECT A.*,IFNULL(B.ClassName,'') AS SourceName
		FROM rb_web_advisory AS A INNER JOIN rb_class AS B ON  (A.SourceId=B.ClassId AND A.AdvType=2)
		UNION ALL 
		SELECT A.*,IFNULL(B.`Name`,'') AS SourceName
		FROM rb_web_advisory AS A INNER JOIN rb_studyabroad AS B ON  (A.SourceId=B.Id AND A.AdvType=3)
) AS A
WHERE 1=1
");
            if (query == null)
            {
                return new RB_Web_Advisory_ViewModel();
            }
            else
            {
                if (query.Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Advisory_ViewModel.Id), query.Id);
                }
            }
            return Get<RB_Web_Advisory_ViewModel>(builder.ToString(), parameters)?.FirstOrDefault();
        }

    }
}