using System;
using System.Collections.Generic;
using System.Text;
using Mall.Model.Entity.Product;
using Mall.Model.Extend.Product;
using System.Linq;
using Mall.Model.Entity.User;
using Mall.Common;

namespace Mall.Repository.Product
{
    /// <summary>
    /// 商品订单明细仓储层
    /// </summary>
    public class RB_Goods_OrderDetailRepository : BaseRepository<RB_Goods_OrderDetail>
    {

        /// <summary>
        /// 获取订单明细列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetOrderDetailList(RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = $" 1=1 ";
            if (dmodel.TenantId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderDetailIds))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.Id)} in({dmodel.OrderDetailIds})";
            }
            if (dmodel.OrderId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderId)}={dmodel.OrderId}";
            }
            if (dmodel.OrderType > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderType)}={(int)dmodel.OrderType}";
            }
            if (dmodel.IsComment > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.IsComment)}={(int)dmodel.IsComment}";
            }
            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }
            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderIds))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderId)} in({dmodel.OrderIds})";
            }

            string sql = $@"SELECT a.*,b.`Name` as SupplierName FROM rb_goods_orderdetail as a LEFT JOIN rb_supplier as b on a.SupplierId=b.ID where {where} order by a.Id asc";
            return Get<RB_Goods_OrderDetail_Extend>(sql).ToList();
        }


        /// <summary>
        /// 获取订单商品分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetSupplierOrderPageList(int pageIndex, int pageSize, out long count, RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and o.OrderStatus  in(2,3,4,5,6) ";
            if (dmodel.TenantId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.MallBaseId)}={dmodel.MallBaseId}";
            }

            if (dmodel.OrderType > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.OrderType)}={dmodel.OrderType}";
            }
            if (dmodel.SupplierId > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.SupplierId)}={dmodel.SupplierId}";
            }
            if (dmodel.OrderClassify > -1)
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderClassify)}={dmodel.OrderClassify}";
            }
            if (dmodel.IsFinance > 0)
            {
                if (dmodel.IsSelectAll == 1)
                {
                    if (dmodel.IsFinance == 1)
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0";
                    }
                    else
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}<=0";
                    }
                }
                else if (dmodel.IsSelectAll == 2)
                {

                    if (dmodel.IsFinance == 1)
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}>0";
                    }
                    else
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}<=0";
                    }

                }
                else if (dmodel.IsSelectAll == 3)
                {

                    if (dmodel.IsFinance == 1)
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.InsuranceFinanceId)}>0";
                    }
                    else
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.InsuranceFinanceId)}<=0";
                    }

                }
                else
                {
                    if (dmodel.OrderClassify == 0)
                    {
                        if (dmodel.IsFinance == 1)
                        {
                            where += $@" and (od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}>0 or od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0)";
                        }
                        else
                        {
                            where += $@" and (od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}<=0 or od.{nameof(RB_Goods_OrderDetail.CostFinanceId)} <= 0)";
                        }
                    }
                    else {
                        if (dmodel.IsFinance == 1)
                        {
                            where += $@" and (od.{nameof(RB_Goods_OrderDetail.InsuranceFinanceId)}>0 or od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0)";
                        }
                        else
                        {
                            where += $@" and (od.{nameof(RB_Goods_OrderDetail.InsuranceFinanceId)}<=0 or od.{nameof(RB_Goods_OrderDetail.CostFinanceId)} <= 0)";
                        }
                    }
                  
                }
            }
            if (!string.IsNullOrEmpty(dmodel.StartTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} >='{dmodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(dmodel.EndTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} <='{dmodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderNo))
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderNo)} like '%{dmodel.OrderNo}%'";
            }


            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }

            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }

            if (dmodel.IsLiveOrder == 1)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveCommission)} >0";
            }
            if (dmodel.FinanceStatus == 1)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveFinanceIds)} <> ''";
            }
            else if (dmodel.FinanceStatus == 2)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveFinanceIds)} = ''";
            }

            string sql = $@"SELECT od.*,o.OrderNo FROM rb_goods_orderdetail od
INNER JOIN rb_goods_order o on o.OrderId=od.OrderId
where {where}  order by o.CreateDate desc";
            return GetPage<RB_Goods_OrderDetail_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }


        /// <summary>
        /// 获取订单商品列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetSupplierOrderList(RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and o.OrderStatus  in(2,3,4,5,6) ";
            if (dmodel.TenantId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (dmodel.OrderClassify > -1)
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderClassify)}={dmodel.OrderClassify}";
            }

            if (dmodel.OrderType > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.OrderType)}={dmodel.OrderType}";
            }
            if (dmodel.SupplierId > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.SupplierId)}={dmodel.SupplierId}";
            }
            if (dmodel.IsFinance > 0)
            {
                if (dmodel.IsSelectAll == 1)
                {
                    if (dmodel.IsFinance == 1)
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0";
                    }
                    else
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}<=0";
                    }
                }
                else if (dmodel.IsSelectAll == 2)
                {
                    if (dmodel.IsFinance == 1)
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}>0";
                    }
                    else
                    {
                        where += $@" and od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}<=0";
                    }
                }
                else
                {
                    if (dmodel.IsFinance == 1)
                    {
                        where += $@" and (od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}>0 or od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0)";
                    }
                    else
                    {
                        where += $@" and (od.{nameof(RB_Goods_OrderDetail.FreightFinanceId)}<=0 or od.{nameof(RB_Goods_OrderDetail.CostFinanceId)} <= 0)";
                    }
                }
            }
            ////if (dmodel.IsFinance > 0)
            ////{
            ////    if (dmodel.IsFinance == 1)
            ////    {
            ////        where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0";
            ////    }
            ////    else
            ////    {
            ////        where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}<=0";
            ////    }
            ////}
            if (dmodel.IdList != null && dmodel.IdList.Any())
            {
                string ids = string.Join(",", dmodel.IdList.Select(x => x));
                where += $@" and od.{nameof(RB_Goods_OrderDetail.Id)} in({ids}) ";
            }
            if (!string.IsNullOrEmpty(dmodel.StartTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} >='{dmodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(dmodel.EndTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} <='{dmodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderNo))
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderNo)} like '%{dmodel.OrderNo}%'";
            }


            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }

            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }

            if (dmodel.IsLiveOrder == 1)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveCommission)} >0";
            }
            if (dmodel.FinanceStatus == 1)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveFinanceIds)} <> ''";
            }
            else if (dmodel.FinanceStatus == 2)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveFinanceIds)} = ''";
            }

            string sql = $@"SELECT od.*,o.OrderNo FROM rb_goods_orderdetail od
INNER JOIN rb_goods_order o on o.OrderId=od.OrderId
where {where}  order by o.CreateDate desc";
            return Get<RB_Goods_OrderDetail_Extend>(sql).ToList();
        }


        /// <summary>
        /// 获取直播订单分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetLiveGoodsOrderPageList(int pageIndex, int pageSize, out long count, RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and and o.OrderStatus =5 and o.Recycled=2 and oa.ReOrderId is null ";
            if (dmodel.TenantId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (!string.IsNullOrEmpty(dmodel.StartTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} >='{dmodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(dmodel.EndTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} <='{dmodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderNo))
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderNo)} like '%{dmodel.OrderNo}%'";
            }


            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }

            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }

            if (dmodel.IsLiveOrder == 1)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveCommission)} >0";
            }
            if (dmodel.FinanceStatus == 1)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveFinanceIds)} <> ''";
            }
            else if (dmodel.FinanceStatus == 2)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.LiveFinanceIds)} = ''";
            }

            string sql = $@"SELECT od.*,o.OrderNo FROM rb_goods_orderdetail od
INNER JOIN rb_goods_order o on o.OrderId=od.OrderId
left join rb_goods_orderaftersale oa on od.Id= oa.OrderDetialId and oa.Type=1 and oa.Status=0 and oa.ReOrderStatus in (1,2,3,4,5)
where {where}  order by o.CreateDate desc";
            return GetPage<RB_Goods_OrderDetail_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 获取订单商品列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetOrderGoodsList(RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = "";
            if (dmodel.IsOut == 0)
            {
                where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and o.OrderStatus =2";
            }
            else
            {
                where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and o.OrderStatus in(2,3,4,5,6)";
            }
            string childreWhere = "";
            if (dmodel.TenantId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.TenantId)}={dmodel.TenantId}";
                childreWhere += $@" and {nameof(RB_Goods_ExpressRelevance.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.MallBaseId)}={dmodel.MallBaseId}";
                childreWhere += $@" and {nameof(RB_Goods_ExpressRelevance.MallBaseId)}={dmodel.MallBaseId}";
            }

            if (dmodel.OrderType > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.OrderType)}={dmodel.OrderType}";
            }
            if (dmodel.SupplierId > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.SupplierId)}={dmodel.SupplierId}";
            }

            if (dmodel.IsFinance > 0)
            {
                if (dmodel.IsFinance == 1)
                {
                    where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0";
                }
                else
                {
                    where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}<=0";
                }
            }
            if (dmodel.IdList != null && dmodel.IdList.Any())
            {
                string ids = string.Join(",", dmodel.IdList.Select(x => x));
                where += $@" and od.{nameof(RB_Goods_OrderDetail.Id)} in({ids}) ";
            }
            if (!string.IsNullOrEmpty(dmodel.StartTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} >='{dmodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(dmodel.EndTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} <='{dmodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderNo))
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderNo)} like '%{dmodel.OrderNo}%'";
            }


            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }

            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }
            if (dmodel.IsOut == 0)
            {
                where += $"and od.Id not in (SELECT OrderDetailId from rb_goods_expressrelevance where 1=1 {childreWhere})";
            }
            string sql = $@"SELECT od.*,o.OrderNo FROM rb_goods_orderdetail od
INNER JOIN rb_goods_order o on o.OrderId=od.OrderId  
where {where}  order by o.CreateDate desc";
            return Get<RB_Goods_OrderDetail_Extend>(sql).ToList();
        }


        /// <summary>
        /// 获取未出库的订单商品列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetNoOutOrderGoodsList(int pageIndex, int pageSize, out long count, RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = "";
            if (dmodel.IsOut == 0)
            {
                where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and o.OrderStatus =2";
            }
            else
            {
                where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and o.OrderStatus in(2,3,4,5,6)";
            }

            string childreWhere = "";
            string outWhere = "";
            if (dmodel.TenantId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.TenantId)}={dmodel.TenantId}";
                childreWhere += $@" and {nameof(RB_Goods_ExpressRelevance.TenantId)}={dmodel.TenantId}";
                outWhere += $@" and  ap.TenantId={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.MallBaseId)}={dmodel.MallBaseId}";
                childreWhere += $@" and {nameof(RB_Goods_ExpressRelevance.MallBaseId)}={dmodel.MallBaseId}";
                outWhere += $@" and ap.MallBaseId={dmodel.MallBaseId}";
            }

            if (dmodel.OrderType > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.OrderType)}={dmodel.OrderType}";
            }
            if (dmodel.SupplierId > 0)
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.SupplierId)}={dmodel.SupplierId}";
            }

            if (dmodel.IsFinance > 0)
            {
                if (dmodel.IsFinance == 1)
                {
                    where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}>0";
                }
                else
                {
                    where += $@" and od.{nameof(RB_Goods_OrderDetail.CostFinanceId)}<=0";
                }
            }
            if (dmodel.IdList != null && dmodel.IdList.Any())
            {
                string ids = string.Join(",", dmodel.IdList.Select(x => x));
                where += $@" and od.{nameof(RB_Goods_OrderDetail.Id)} in({ids}) ";
            }
            if (!string.IsNullOrEmpty(dmodel.StartTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} >='{dmodel.StartTime}'";
            }
            if (!string.IsNullOrEmpty(dmodel.EndTime))
            {
                where += $@" and o.{nameof(RB_Goods_Order.CreateDate)} <='{dmodel.EndTime} 23:59:59'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderNo))
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderNo)} like '%{dmodel.OrderNo}%'";
            }


            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }

            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and od.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }

            if (dmodel.IsOut == 0)
            {
                where += $" and od.Id not in (SELECT OrderDetailId from rb_goods_expressrelevance where 1=1 {childreWhere})";
                where += @$" AND od.Id NOT IN (SELECT OrderGoodsId	FROM	{Config.PropertyDB}.rb_warehouseout_goods AS g LEFT JOIN {Config.PropertyDB}.rb_warehouseout_apply AS ap ON g.applyId = ap.id WHERE ap.ApplyStatus in (1,2) {outWhere} )";
            }
            else if (dmodel.IsOut == 1)
            {
                where += $" and( od.Id  in (SELECT OrderDetailId from rb_goods_expressrelevance where 1=1 {childreWhere})";
                where += @$" or  od.Id  IN (SELECT OrderGoodsId	FROM	{Config.PropertyDB}.rb_warehouseout_goods AS g LEFT JOIN {Config.PropertyDB}.rb_warehouseout_apply AS ap ON g.applyId = ap.id WHERE ap.ApplyStatus in (1,2) {outWhere} ))";
            }
            string sql = $@"SELECT od.*,o.OrderNo FROM rb_goods_orderdetail od
INNER JOIN rb_goods_order o on o.OrderId=od.OrderId  
where {where}  order by o.CreateDate desc";
            return GetPage<RB_Goods_OrderDetail_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }

        /// <summary>
        /// 获取待评论商品列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetAppletGoodsWaitCommentPageList(int pageIndex, int pageSize, out long count, RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = $" 1=1 and o.{nameof(RB_Goods_Order.Status)}=0 and o.{nameof(RB_Goods_Order.OrderStatus)} in(4,5)";
            if (dmodel.TenantId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderDetailIds))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.Id)} in({dmodel.OrderDetailIds})";
            }
            if (dmodel.OrderId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderId)}={dmodel.OrderId}";
            }
            if (dmodel.OrderClassify > -1)
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderClassify)}={dmodel.OrderClassify}";
            }
            if (dmodel.OrderType > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderType)}={(int)dmodel.OrderType}";
            }
            if (dmodel.IsComment > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.IsComment)}={(int)dmodel.IsComment}";
            }
            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }
            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderIds))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderId)} in({dmodel.OrderIds})";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderNo))
            {
                where += $@" and o.{nameof(RB_Goods_Order.OrderNo)} ='{dmodel.OrderNo}'";
            }
            if (dmodel.UserId > 0)
            {
                where += $@" and o.{nameof(RB_Goods_Order.UserId)} ={dmodel.UserId}";
            }

            string sql = $@"SELECT a.* FROM rb_goods_orderdetail as a 
left join rb_goods_order o on a.OrderId = o.OrderId
where {where} order by a.Id desc";
            return GetPage<RB_Goods_OrderDetail_Extend>(pageIndex, pageSize, out count, sql).ToList();
        }





        /// <summary>
        /// 获取司导订单明细列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <param name="dmodel"></param>
        /// <returns></returns>
        public List<RB_Goods_OrderDetail_Extend> GetGuideCarOrderDetailList(RB_Goods_OrderDetail_Extend dmodel)
        {
            string where = $" 1=1 ";
            if (dmodel.TenantId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.TenantId)}={dmodel.TenantId}";
            }
            if (dmodel.MallBaseId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.MallBaseId)}={dmodel.MallBaseId}";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderDetailIds))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.Id)} in({dmodel.OrderDetailIds})";
            }
            if (dmodel.OrderId > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderId)}={dmodel.OrderId}";
            }
            if (dmodel.OrderType > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderType)}={(int)dmodel.OrderType}";
            }
            if (dmodel.IsComment > 0)
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.IsComment)}={(int)dmodel.IsComment}";
            }
            if (!string.IsNullOrEmpty(dmodel.GoodsName))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.GoodsName)} like '%{dmodel.GoodsName}%'";
            }
            if (!string.IsNullOrEmpty(dmodel.ProductCode))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.ProductCode)} like '%{dmodel.ProductCode}%'";
            }
            if (!string.IsNullOrEmpty(dmodel.OrderIds))
            {
                where += $@" and a.{nameof(RB_Goods_OrderDetail.OrderId)} in({dmodel.OrderIds})";
            }

            string sql = $@"SELECT a.*,b.`Name` as SupplierName,e.`Name` as CarName,e.CarType as GuideCarType,e.CarClass,f.ColorName as CarColorName,cb.`Name` as CarBrandName,g.RideNum,gcg.Name as GuideName,gcg.GuidePhoto,gcg.WorkYears as GuideWorkYears,gcg.Score as GuideScore,gcg.Telephone as GuideTelephone FROM rb_goods_orderdetail as a 
LEFT JOIN rb_supplier as b on a.SupplierId=b.ID
LEFT JOIN rb_guidecar_car as e on e.ID=a.CarId
LEFT JOIN rb_guidecar_carbrand as cb on e.CarBrandId=cb.ID
LEFT JOIN rb_guidecar_carcolor  as f on f.ID=a.CarColorId
LEFT JOIN rb_guidecar_guide as gcg on gcg.ID=a.GuideId
LEFT JOIN rb_goods as g on g.ID=a.GoodsId
where {where} order by a.Id asc";
            return Get<RB_Goods_OrderDetail_Extend>(sql).ToList();
        }
    }
}
