/// <summary>
        /// 获取卖家当前时间可用的限购信息
        /// </summary>
        /// <param name="sellerId">卖家Id</param>
        /// <returns></returns>
        public IList <Promotion.LimitBuy> PromotionLimitBuyList(int sellerId)
        {
            const string sql = @"select * from LimitBuy where Status=1 and StartedOn<getdate() and StoppedOn>getdate() and SellerId=@sellerId;";
            var          cmd = SqlBuilder.Raw(sql, new { sellerId }).ToCommand();

            return(PromotionConn.Query <Promotion.LimitBuy>(cmd).ToList());
        }
        /// <summary>
        /// 获取卖家当前时间可用的店铺优惠活动
        /// </summary>
        /// <param name="sellerId">卖家Id</param>
        /// <returns></returns>
        public IList <Promotion.Activity> PromotionActivityList(int sellerId)
        {
            const string sql = @"select * from Activity where Status=1 and StartedOn<now() and StoppedOn>now() and SellerId=@sellerId;";
            var          cmd = SqlBuilder.Raw(sql, new { sellerId }).ToCommand();

            return(PromotionConn.Query <Promotion.Activity>(cmd).ToList());
        }
        /// <summary>
        /// 获取卖家当前时间可用的购物车促销
        /// </summary>
        /// <param name="sellerId">卖家Id</param>
        /// <returns></returns>
        public IList <Promotion.CartActivity> PromotionCartActivityList(int sellerId)
        {
            const string sql = @"select * from CartActivity where Status=1 and StartedOn<now() and StoppedOn>now() and SellerId=@sellerId;
            select a.* from CartActivityRule a inner join CartActivity b on a.ParentId=b.Id where a.Status=1 and b.Status=1 and b.StartedOn<now() and b.StoppedOn>now() and a.SellerId=@sellerId";
            var          cmd = SqlBuilder.Raw(sql, new { sellerId }).ToCommand();

            return(PromotionConn.Query <Promotion.CartActivity, Promotion.CartActivity.Rule, int>(cmd, o => o.Id, o => o.ParentId, (a, b) => { a.Rules = b.Where(x => x.ParentId == a.Id).ToList(); }).ToList());
        }
        /// <summary>
        /// 获取卖家当前时间可用的搭配组合套餐
        /// </summary>
        /// <param name="sellerId">卖家Id</param>
        /// <returns></returns>
        public IList <Promotion.Package> PromotionPackageList(int sellerId)
        {
            const string sql = @"select * from Package where Status=1 and StartedOn<now() and StoppedOn>now() and SellerId=@sellerId;
            select a.* from PackageItem a inner join Package b on a.ParentId=b.Id where a.Status=1 and b.Status=1 and b.StartedOn<now() and b.StoppedOn>now() and a.SellerId=@sellerId";
            var          cmd = SqlBuilder.Raw(sql, new { sellerId }).ToCommand();

            return(PromotionConn.Query <Promotion.Package, Promotion.Package.Item, int>(cmd, o => o.Id, o => o.ParentId, (a, b) => { a.Items = b.Where(x => x.ParentId == a.Id).ToList(); }).ToList());
        }
        /// <summary>
        /// 获取卖家当前时间可用的购物车促销
        /// </summary>
        /// <param name="sellerId">卖家Id</param>
        /// <returns></returns>
        public IList <Promotion.Cart> PromotionCartList(int sellerId)
        {
            const string sql = @"select * from Cart where Status=1 and StartedOn<getdate() and StoppedOn>getdate() and SellerId=@sellerId;
            select a.* from CartRule a inner join Cart b on a.CartId=b.Id where a.Status=1 and b.Status=1 and b.StartedOn<getdate() and b.StoppedOn>getdate() and a.SellerId=@sellerId";
            var          cmd = SqlBuilder.Raw(sql, new { sellerId }).ToCommand();

            return(PromotionConn.Query <Promotion.Cart, Promotion.Cart.Rule, int>(cmd, o => o.Id, o => o.CartId, (a, b) => { a.Rules = b.Where(x => x.CartId == a.Id).ToList(); }).ToList());
        }
        /// <summary>
        /// 获取卖家当前时间可用的店铺优惠活动
        /// </summary>
        /// <param name="sellerId">卖家Id</param>
        /// <returns></returns>
        public IList <Promotion.Normal> PromotionNormalList(int sellerId)
        {
            const string sql = @"select * from Normal where Status=1 and StartedOn<getdate() and StoppedOn>getdate() and SellerId=@sellerId;
            select a.* from NormalRule a inner join Normal b on a.NormalId=b.Id where a.Status=1 and b.Status=1 and b.StartedOn<getdate() and b.StoppedOn>getdate() and a.SellerId=@sellerId";
            var          cmd = SqlBuilder.Raw(sql, new { sellerId }).ToCommand();

            return(PromotionConn.Query <Promotion.Normal, Promotion.Normal.Rule, int>(cmd, o => o.Id, o => o.NormalId, (a, b) => { a.Rules = b.Where(x => x.NormalId == a.Id).OrderBy(x => x.Upon).ToList(); }).ToList());
        }
        /// <summary>
        /// 获取卖家当前时间可用的特价
        /// </summary>
        /// <param name="sellerId">卖家Id</param>
        /// <returns></returns>
        public IList <Promotion.SpecialPrice> PromotionSpecialPriceList(int sellerId)
        {
            const string sql = @"select * from SpecialPrice where Status=1 and StartedOn<getdate() and StoppedOn>getdate() and SellerId=@sellerId;
            select a.* from SpecialPriceItem a inner join SpecialPrice b on a.SpecialPriceId=b.Id where a.Status=1 and b.Status=1 and b.StartedOn<getdate() and b.StoppedOn>getdate() and a.SellerId=@sellerId";
            var          cmd = SqlBuilder.Raw(sql, new { sellerId }).ToCommand();

            return(PromotionConn.Query <Promotion.SpecialPrice, Promotion.SpecialPrice.Item, int>(cmd, o => o.Id, o => o.SpecialPriceId, (a, b) => { a.Items = b.Where(x => x.SpecialPriceId == a.Id).ToList(); }).ToList());
        }
        /*
         * /// <summary>
         * /// 创建店铺优惠活动规则
         * /// </summary>
         * /// <param name="o">店铺优惠活动规则</param>
         * /// <returns></returns>
         * public bool PromotionDefaultRuleCreate(Promotion.Default.Rule o)
         * {
         *  try
         *  {
         *      var cmd = SqlBuilder
         *          .Insert("[Default.Rule]")
         *          .Column("ParentId", o.ParentId)
         *          .Column("Upon", o.Upon)
         *          .Column("Value", o.Value)
         *          .Column("SendGift", o.SendGift)
         *          .Column("GiftJson", o.GiftData)
         *          .Column("SendCoupon", o.SendCoupon)
         *          .Column("CouponJson", o.CouponData)
         *          .Column("SellerId", o.SellerId)
         *          .Column("Status", o.Status)
         *          .Column("CreatedBy", o.CreatedBy)
         *          .Column("CreatedOn", o.CreatedOn)
         *          .ToCommand();
         *      return PromotionConn.Execute(cmd) > 0;
         *  }
         *  catch (Exception e)
         *  {
         *      OnException(e);
         *  }
         *  return false;
         * }
         *
         * /// <summary>
         * /// 更新店铺优惠活动规则
         * /// </summary>
         * /// <param name="o">店铺优惠活动规则</param>
         * /// <returns></returns>
         * public bool PromotionDefaultRuleUpdate(Promotion.Default.Rule o)
         * {
         *  try
         *  {
         *      var cmd = SqlBuilder
         *          .Update("[Default.Rule]")
         *          .Column("Upon", o.Upon)
         *          .Column("Value", o.Value)
         *          .Column("SendGift", o.SendGift)
         *          .Column("GiftJson", o.GiftData)
         *          .Column("SendCoupon", o.SendCoupon)
         *          .Column("CouponJson", o.CouponData)
         *          .Column("Status", o.Status)
         *          .Where("Id=@Id", new { o.Id })
         *          .ToCommand();
         *      return PromotionConn.Execute(cmd) > 0;
         *  }
         *  catch (Exception e)
         *  {
         *      OnException(e);
         *  }
         *  return false;
         * }
         *
         * /// <summary>
         * /// 获取店铺优惠活动规则
         * /// </summary>
         * /// <param name="id">店铺优惠活动规则Id</param>
         * /// <param name="sellerId">卖家Id</param>
         * /// <returns></returns>
         * public Promotion.Default.Rule PromotionDefaultRuleGet(int id, int? sellerId = null)
         * {
         *  try
         *  {
         *      var cmd = SqlBuilder
         *          .Select("*")
         *          .From("[Default.Rule]")
         *          .Where("Id=@Id", new { id })
         *          .Where(sellerId.HasValue, "SellerId=@sellerId", new { sellerId })
         *          .ToCommand();
         *      return PromotionConn.Query<Promotion.Default.Rule>(cmd).FirstOrDefault();
         *  }
         *  catch (Exception e)
         *  {
         *      OnException(e);
         *  }
         *  return null;
         * }
         *
         * /// <summary>
         * /// 设置店铺优惠活动规则为不可用
         * /// </summary>
         * /// <param name="id">店铺优惠活动规则Id</param>
         * /// <param name="sellerId">卖家Id</param>
         * /// <returns></returns>
         * public bool PromotionDefaultRuleDisable(int id, int? sellerId = null)
         * {
         *  var sql = "update [Default.Rule] set [Status]=0 where Id=@Id";
         *  if (sellerId.HasValue)
         *  {
         *      sql = "update [Default.Rule] set [Status]=0 where Id=@Id and Seller=@SellerId";
         *  }
         *  var cmd = SqlBuilder.Raw(sql).Append(sellerId.HasValue, "", new { sellerId }).ToCommand();
         *  try
         *  {
         *      return PromotionConn.Execute(cmd) > 0;
         *  }
         *  catch (Exception e)
         *  {
         *      OnException(e);
         *  }
         *  return false;
         * }
         */

        #endregion

        /// <summary>
        /// 获取搭配组合套餐
        /// </summary>
        /// <param name="id">搭配组合套餐Id</param>
        /// <param name="includeItems">是否包含套餐附属商品信息</param>
        /// <returns></returns>
        public Promotion.Package PromotionPackageGet(int id, bool includeItems = false)
        {
            if (includeItems)
            {
                const string sql = @"select * from Package where Id=@id;select * from PackageItem where ParentId=@id";
                var          cmd = SqlBuilder.Raw(sql, new { id }).ToCommand();
                return
                    (StoreConn.Query <Promotion.Package, Promotion.Package.Item, int>(cmd, o => o.Id,
                                                                                      o => o.ParentId, (a, b) =>
                {
                    a.Items = b.ToList();
                }).FirstOrDefault());
            }
            else
            {
                var cmd = SqlBuilder.Select("*")
                          .From("Package")
                          .Where("Id=@id", new { id })
                          .ToCommand();

                return(PromotionConn.Query <Promotion.Package>(cmd).FirstOrDefault());
            }
        }