using Mall.Model.Entity.TradePavilion;
using Mall.Model.Extend.TradePavilion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Mall.Repository.TradePavilion
{
    /// <summary>
    /// 【商载通】 品牌、企业服务仓储层
    /// </summary>
    public class RB_Brand_EnterpriseRepository:BaseRepository<RB_Brand_Enterprise>
    {
        /// <summary>
        /// 【商载通】品牌、企业服务仓储层
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Brand_Enterprise_Extend> GetBrandEnterprisePageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Brand_Enterprise_Extend query)
        {
            DynamicParameters parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();

            builder.AppendFormat(@"
SELECT A.*,IFNULL(B.ClassName,'') AS CategoryName 
FROM rb_brand_Enterprise AS A LEFT JOIN rb_brandclass AS B ON A.CategoryId=B.ID
WHERE 1=1
");

            if (query != null)
            {
                if (query.TenantId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Brand_Enterprise_Extend.TenantId), query.TenantId);
                }
                if (query.MallBaseId > 0)
                {
                    builder.AppendFormat(" AND A.{0}={1} ", nameof(RB_Brand_Enterprise_Extend.MallBaseId), query.MallBaseId);
                }
                if (!string.IsNullOrEmpty(query.BrandName))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @BrandName ", nameof(RB_Brand_Enterprise_Extend.BrandName));
                    parameters.Add("@BrandName", "%" + query.BrandName + "%");
                }
            }
            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Brand_Enterprise_Extend.ID));
            return GetPage<RB_Brand_Enterprise_Extend>(pageIndex, pageSize, out rowsCount, builder.ToString(),parameters).ToList();
        }
    }
}
