using Mall.Model.Entity.TradePavilion;
using Mall.Model.Extend.TradePavilion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mall.Repository.TradePavilion
{
    /// <summary>
    /// 【商载通】 载体、楼宇申请仓储类
    /// </summary>
    public class RB_CarrierEnterpriseApplyForRepository : BaseRepository<RB_CarrierEnterpriseApplyFor>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_CarrierEnterpriseApplyFor); } }


        /// <summary>
        /// 载体信息列表
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_CarrierEnterpriseApplyFor_Extend> GetCarrierEnterpriseList(RB_CarrierEnterpriseApplyFor_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT * FROM {TableName} WHERE {nameof(RB_CarrierEnterpriseApplyFor_Extend.Status)}=0 ");
            if (query != null)
            {
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.TenantId)}={query.TenantId}");
                }
                if (query.MallBaseId > 0)
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (query.ID > 0)
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.ID)}={query.ID}");
                }
                if (query.CompanyId > 0)
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.CompanyId)}={query.CompanyId}");
                }
                if (query.CategoryId > 0)
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.CategoryId)}={query.CategoryId}");
                }
                if (query.ProjectType.HasValue && query.ProjectType > 0)
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.ProjectType)}={(int)query.ProjectType}");
                }
                if (!string.IsNullOrWhiteSpace(query.CarrierName))
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.CarrierName)} like '%{query.CarrierName}%'");
                }
                if (!string.IsNullOrWhiteSpace(query.Developers))
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.Developers)} like '%{query.Developers}%'");
                }

                if (!string.IsNullOrWhiteSpace(query.StartOpenTime))
                {
                    builder.Append($" and  DATE_FORMAT({nameof(RB_CarrierEnterpriseApplyFor_Extend.OpenTime)},'%Y-%m-%d' )>=DATE_FORMAT('{query.StartOpenTime}','%Y-%m-%d' ) ");
                }
                if (!string.IsNullOrWhiteSpace(query.EndOpenTime))
                {
                    builder.Append($" and  DATE_FORMAT({nameof(RB_CarrierEnterpriseApplyFor_Extend.OpenTime)},'%Y-%m-%d' )<= DATE_FORMAT('{query.EndOpenTime}','%Y-%m-%d' ) ");
                }
                if (query.MetroNum > 0 || !string.IsNullOrWhiteSpace(query.MetroName))
                {
                    builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.ID)} in (SELECT CarrierId from rb_carriermetro where `Status`=0  ");
                    if (query.MetroNum > 0)
                    {
                        builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.MetroNum)} = {query.MetroNum}");
                    }
                    if (!string.IsNullOrWhiteSpace(query.MetroName))
                    {
                        builder.Append($" AND {nameof(RB_CarrierEnterpriseApplyFor_Extend.MetroName)} like '%{query.MetroName}%'");
                    }
                    builder.Append(" )");
                }
                if (query.OrderBy == 1)
                {
                    builder.Append($" order by {nameof(RB_CarrierEnterpriseApplyFor_Extend.CreateDate)} desc");
                }
                else if (query.OrderBy == 2)
                {
                    builder.Append($" order by {nameof(RB_CarrierEnterpriseApplyFor_Extend.OpenTime)} desc");
                }
            }
            return Get<RB_CarrierEnterpriseApplyFor_Extend>(builder.ToString()).ToList();
        }
    }
}
