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

namespace Mall.Repository.GuideCar
{
    /// <summary>
    /// 司导-车辆仓储层
    /// </summary>
    public class RB_GuideCar_CarRepository : BaseRepository<RB_GuideCar_Car>
    {
        /// <summary>
        /// 表名称
        /// </summary>
        public string TableName { get { return nameof(RB_GuideCar_Car); } }

        /// <summary>
        /// 表名称
        /// </summary>
        public string CarBrandTableName { get { return nameof(RB_GuideCar_CarBrand); } }
        /// <summary>
        /// 司导-车辆列表
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="rowCount">总条数</param>
        /// <param name="query">查询条件</param>
        /// <returns></returns>
        public List<RB_GuideCar_Car_Extend> GetPageListRepository(int pageIndex, int pageSize, out long rowCount, RB_GuideCar_Car_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(@$" SELECT a.*,b.`Name` as  CarBrand,c.GoodsNum,d.OrderNum FROM {TableName} as a LEFT JOIN {CarBrandTableName} as b on a.CarBrandId=b.ID
LEFT JOIN (SELECT CarId,COUNT(*) as GoodsNum from rb_goods where `Status`=0 GROUP BY CarId) as c on a.ID=c.CarId
LEFT JOIN (SELECT CarId,COUNT(*) as OrderNum from rb_goods_orderdetail as od
 LEFT JOIN rb_goods_order as o on od.OrderId=o.OrderId where o.`Status`=0 and o.OrderStatus !=7  GROUP BY od.CarId) as d on a.ID=d.CarId
WHERE  a.{nameof(RB_GuideCar_Car_Extend.Status)}=0");
            if (query != null)
            {
                if (query.TenantId > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.TenantId)}={query.TenantId}");
                }
                if (query.TenantId > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (!string.IsNullOrWhiteSpace(query.Name))
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.Name)} like '%{query.Name}%'");
                }
                if (query.CarBrandId > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.CarBrandId)}={query.CarBrandId}");
                }
                if (query.CarClass > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.CarClass)}={(int)query.CarClass}");
                }
            }
            return GetPage<RB_GuideCar_Car_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_GuideCar_Car_Extend> GetGuideCarCarList(RB_GuideCar_Car_Extend query)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append($" SELECT a.*,b.`Name` as  CarBrand FROM {TableName} as a LEFT JOIN {CarBrandTableName} as b on a.CarBrandId=b.ID WHERE  a.{nameof(RB_GuideCar_Car_Extend.Status)}=0");
            if (query != null)
            {
                if (query.ID > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.ID)}={query.ID}");
                }
                if (!string.IsNullOrEmpty(query.GuideCarIds))
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.ID)} in({query.GuideCarIds})");
                }
                if (query.TenantId > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.TenantId)}={query.TenantId}");
                }
                if (query.TenantId > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.MallBaseId)}={query.MallBaseId}");
                }
                if (!string.IsNullOrWhiteSpace(query.Name))
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.Name)} like '%{query.Name}%'");
                }
                if (query.CarBrandId > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.CarBrandId)}={query.CarBrandId}");
                }
                if (query.CarClass > 0)
                {
                    builder.Append($" AND a.{nameof(RB_GuideCar_Car_Extend.CarClass)}={(int)query.CarClass}");
                }
            }
            return Get<RB_GuideCar_Car_Extend>(builder.ToString()).ToList();
        }
    }
}
