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

namespace Mall.Repository.BaseSetUp
{
    public class RB_Logistics_RulesRepository : RepositoryBase<RB_Logistics_Rules>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_Logistics_Rules); } }
        /// <summary>
        /// 物料规则列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Logistics_Rules_Extend> GetPageListRepository(int pageIndex, int pageSize, out long rowCount, RB_Logistics_Rules_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT * FROM {TableName} WHERE  {nameof(RB_Logistics_Rules_Extend.Status)}=0");
            if (query != null)
            {
                // where += $@" and {nameof(RB_Customer_InfoCreate.CustomerId)}={dmodel.CustomerId}";
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.TenantId)}={query.TenantId}");
                }
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (!string.IsNullOrWhiteSpace(query.RulesName))
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.RulesName)} like '%{query.RulesName}%'");
                }
                if (query.RulesType > 0)
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.RulesType)}={(int)query.RulesType}");
                }
            }
            return GetPage<RB_Logistics_Rules_Extend>(pageIndex, pageSize, out rowCount, builder.ToString()).ToList();
        }


        /// <summary>
        /// 物流规则列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_Logistics_Rules_Extend> GetLogisticsRulesList( RB_Logistics_Rules_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT * FROM {TableName} WHERE {nameof(RB_Logistics_Rules_Extend.Status)}=0 ");
            if (query != null)
            {
                if (query.ID > 0)
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.ID)}={query.ID}");
                }
                if (query.TenantId > 0)
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.TenantId)}={query.TenantId}");
                }
                if (query.MallBaseId > 0)
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (!string.IsNullOrWhiteSpace(query.RulesName))
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.RulesName)} like '%{query.RulesName}%'");
                }
                if (query.RulesType > 0)
                {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.RulesType)}={(int)query.RulesType}");
                }
                if (query.IsDefault > 0) {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.IsDefault)}={query.IsDefault}");
                }
                if (!string.IsNullOrEmpty(query.RulesIds)) {
                    builder.Append($" AND {nameof(RB_Logistics_Rules_Extend.ID)} in({query.RulesIds})");
                }
            }
            return Get<RB_Logistics_Rules_Extend>(builder.ToString()).ToList();
        }
    }
}
