暫無描述

GoodsSkus.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/9/29
  6. * Time: 15:07
  7. */
  8. namespace App;
  9. use Illuminate\Database\Eloquent\Model;
  10. class GoodsSkus extends Model
  11. {
  12. public $timestamps = false;
  13. protected $table = "goods_skus";
  14. public static function actualQuantity($sku_id){
  15. //计算实际库存 -还未到卖家云的量
  16. $sku_quantity = GoodsSkus::where('id', $sku_id)->pluck('quantity');
  17. $w_num = OrderGoodsSkus::where('sku_id', $sku_id)->where('is_del',0)->where('mj_status',0)->sum('num');
  18. $fx_num = FxOrderGoodsSkus::where('sku_id', $sku_id)->where('is_del',0)->where('mj_status',0)->sum('num');
  19. $quantity = $sku_quantity - $w_num - $fx_num;
  20. return $quantity;
  21. }
  22. public static function actualQuantityByRedis($sku_id){
  23. //计算实际库存 -还未到卖家云的量
  24. $sku_quantity = GoodsSkus::where('id', $sku_id)->pluck('quantity');
  25. $redisKey = config('constants.SKU_QUANTITY_TABLE');
  26. //货号为column
  27. $code = GoodsSkus::where('id', $sku_id)->pluck('code');
  28. $w_num = RedisModel::hGet($redisKey, $code);
  29. if(!$w_num) $w_num = 0;
  30. $quantity = $sku_quantity - $w_num;
  31. return ['quantity' => $quantity, 'w_num' => $w_num];
  32. }
  33. public static function actualQuantityNotNull(){
  34. //取出所有商品规格信息
  35. $skuList = GoodsSkus::all();
  36. $actualSkuList = array();
  37. //遍历商品规格信息,分别计算库用库存并判断
  38. foreach($skuList as $sku){
  39. $quantityArr = self::actualQuantityByRedis($sku['id']);
  40. if($quantityArr['quantity'] > 0){
  41. //将可用库存大于0的赋值给新数组
  42. $actualSkuList[] = $sku['id'];
  43. }
  44. }
  45. return $actualSkuList;
  46. }
  47. }