using Edu.Common.Enum;
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_NavRepository : BaseRepository<RB_Web_Nav>
    {
        /// <summary>
        /// 获取网站导航分页列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示调试</param>
        /// <param name="rowsCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Web_Nav_ViewModel> GetWebNavPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Web_Nav_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT A.* 
FROM rb_web_nav AS A
WHERE 1=1
");
            if (query == null)
            {
                return new List<RB_Web_Nav_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Nav_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.NavTitle))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @NavTitle ", nameof(RB_Web_Nav_ViewModel.NavTitle));
                    parameters.Add("NavTitle", "%" + query.NavTitle.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.SubTitle))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @NavTitle ", nameof(RB_Web_Nav_ViewModel.SubTitle));
                    parameters.Add("SubTitle", "%" + query.SubTitle.Trim() + "%");
                }
                if (query.NavType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Nav_ViewModel.NavType), (int)query.NavType);
                }
                if (query.Status >= 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Nav_ViewModel.Status), (int)query.Status);
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Web_Nav_ViewModel.Id));
            return GetPage<RB_Web_Nav_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(),parameters).ToList();
        }

        /// <summary>
        /// 获取网站导航列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Web_Nav_ViewModel> GetWebNavListRepository(RB_Web_Nav_ViewModel query)
        {
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT A.* 
FROM rb_web_nav AS A
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Nav_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_Web_Nav_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Nav_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.NavTitle))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @NavTitle ", nameof(RB_Web_Nav_ViewModel.NavTitle));
                    parameters.Add("NavTitle", "%" + query.NavTitle.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.SubTitle))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @NavTitle ", nameof(RB_Web_Nav_ViewModel.SubTitle));
                    parameters.Add("SubTitle", "%" + query.SubTitle.Trim() + "%");
                }
                if (query.NavType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_Nav_ViewModel.NavType), (int)query.NavType);
                }
            }
            return Get<RB_Web_Nav_ViewModel>(builder.ToString()).ToList();
        }

    }
}
