using Mall.Common.Enum;
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_BrandEnterpriseApplyForRepository : BaseRepository<RB_BrandEnterpriseApplyFor>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_BrandEnterpriseApplyFor); } }


        /// <summary>
        /// 品牌/企业申请信息列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_BrandEnterpriseApplyFor_Extend> GetBrandEnterpriseApplyForPageList(int pageIndex, int pageSize, out long rowCount, RB_BrandEnterpriseApplyFor_Extend query)
        {
            DynamicParameters parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT a.*,b.ClassName from {TableName} as a LEFT JOIN rb_brandclass as b on a.BrandClassId=b.ID   WHERE 1=1 ");

            builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_BrandEnterpriseApplyFor_Extend.Status), (int)DateStateEnum.Normal);
            builder.AppendFormat(" AND b.{0}={1} ", nameof(RB_BrandEnterpriseApplyFor_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.TenantId > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.TenantId)}={query.TenantId}");
                }
                if (query.MallBaseId > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (query.ID > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.ID)}={query.ID}");
                }
                if (query.BrandEnterpriseType > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.BrandEnterpriseType)}={query.BrandEnterpriseType}");
                }
                if (query.ProjectType.HasValue && query.ProjectType > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.ProjectType)}={(int)query.ProjectType}");
                }
                if (!string.IsNullOrEmpty(query.BrandName))
                {
                    builder.AppendFormat(" AND a.{0} LIKE @Name ", nameof(RB_BrandEnterpriseApplyFor_Extend.BrandName));
                    parameters.Add("Name", "%" + query.BrandName.Trim() + "%");
                }
                if (query.BrandClassId > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_BrandEnterpriseApplyFor_Extend.BrandClassId), query.BrandClassId);
                }
                if (query.ApplyForStatus > -1)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_BrandEnterpriseApplyFor_Extend.ApplyForStatus), query.ApplyForStatus);
                }
                if (!string.IsNullOrEmpty(query.StartTime))
                {
                    builder.Append($" AND a.{nameof(RB_BrandEnterpriseApplyFor_Extend.CreateDate)} >= '{query.StartTime}'");
                }
                if (!string.IsNullOrEmpty(query.EndTime))
                {
                    builder.Append($" AND a.{nameof(RB_BrandEnterpriseApplyFor_Extend.CreateDate)} <= '{query.EndTime} 23:59:59'");
                }
            }
            return GetPage<RB_BrandEnterpriseApplyFor_Extend>(pageIndex, pageSize, out rowCount, builder.ToString(), parameters).ToList();
        }


        /// <summary>
        /// 品牌/企业申请信息列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_BrandEnterpriseApplyFor_Extend> GetCarrierEnterpriseApplyForRepository(RB_BrandEnterpriseApplyFor_Extend query)
        {
            DynamicParameters parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT a.*,b.ClassName from {TableName} as a LEFT JOIN rb_brandclass as b on a.BrandClassId=b.ID   WHERE 1=1 ");

            builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_BrandEnterpriseApplyFor_Extend.Status), (int)DateStateEnum.Normal);
            builder.AppendFormat(" AND b.{0}={1} ", nameof(RB_BrandEnterpriseApplyFor_Extend.Status), (int)DateStateEnum.Normal);
            if (query != null)
            {
                if (query.TenantId > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.TenantId)}={query.TenantId}");
                }
                if (query.MallBaseId > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (query.ID > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.ID)}={query.ID}");
                }
                if (query.BrandEnterpriseType > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.BrandEnterpriseType)}={query.BrandEnterpriseType}");
                }
                if (query.ProjectType.HasValue && query.ProjectType > 0)
                {
                    builder.Append($" AND A.{nameof(RB_BrandEnterpriseApplyFor_Extend.ProjectType)}={(int)query.ProjectType}");
                }
                if (!string.IsNullOrEmpty(query.BrandName))
                {
                    builder.AppendFormat(" AND a.{0} LIKE @Name ", nameof(RB_BrandEnterpriseApplyFor_Extend.BrandName));
                    parameters.Add("Name", "%" + query.BrandName.Trim() + "%");
                }
                if (query.BrandClassId > 0)
                {
                    builder.AppendFormat(" AND a.{0}={1} ", nameof(RB_BrandEnterpriseApplyFor_Extend.BrandClassId), query.BrandClassId);
                }
            }
            return Get<RB_BrandEnterpriseApplyFor_Extend>(builder.ToString(), parameters).ToList();
        }
    }
}
