Bez popisu

MonitorSkuToRedis.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\OrderGoodsSkus;
  6. use App\GoodsSkus;
  7. use App\Order;
  8. use App\RedisModel;
  9. use YPSMS;
  10. class MonitorSkuToRedis extends Command {
  11. protected $signature = 'MonitorSkuToRedis';
  12. /**
  13. * The console command description.
  14. *
  15. * @var string
  16. */
  17. protected $description = '监控预占库存';
  18. public function handle()
  19. {
  20. $this->MonitorSkuToRedis();
  21. }
  22. public function MonitorSkuToRedis(){
  23. echo "\nStart: ".date('Y-m-d H:i:s');
  24. $order_ids = Order::where('warehouse', 3)->where('is_del', 0)->whereNull('orderCode')->lists('id');
  25. $res = OrderGoodsSkus::select(DB::raw('sum(num) as num , sku_id'))->whereIn('order_id', $order_ids)->where('mj_status', 0)->where('is_del', 0)->groupBy('sku_id')->get();
  26. $res = json_decode(json_encode($res), true);
  27. $sku_arr = array();
  28. foreach($res as $k=>$v){
  29. $sku_arr[$v['sku_id']] = $v['num'];
  30. }
  31. $order_ids = DB::table('seafood_fx.order')->where('warehouse', 3)->where('is_del', 0)->whereNull('orderCode')->lists('id');
  32. if(!empty($order_ids)){
  33. $res = DB::table('seafood_fx.order_goods_skus')->select(DB::raw('sum(num) as num , sku_id'))->whereIn('order_id', $order_ids)->where('mj_status', 0)->where('is_del', 0)->groupBy('sku_id')->get();
  34. $res = json_decode(json_encode($res), true);
  35. foreach($res as $k=>$v){
  36. if(isset($sku_arr[$v['sku_id']])){
  37. $sku_arr[$v['sku_id']] += $v['num'];
  38. }else{
  39. $sku_arr[$v['sku_id']] = $v['num'];
  40. }
  41. }
  42. }
  43. $order_ids = DB::table('seafood_fx2.order')->where('warehouse', 3)->where('is_del', 0)->whereNull('orderCode')->lists('id');
  44. if(!empty($order_ids)){
  45. $res = DB::table('seafood_fx2.order_goods_skus')->select(DB::raw('sum(num) as num , sku_id'))->whereIn('order_id', $order_ids)->where('mj_status', 0)->where('is_del', 0)->groupBy('sku_id')->get();
  46. $res = json_decode(json_encode($res), true);
  47. foreach($res as $k=>$v){
  48. if(isset($sku_arr[$v['sku_id']])){
  49. $sku_arr[$v['sku_id']] += $v['num'];
  50. }else{
  51. $sku_arr[$v['sku_id']] = $v['num'];
  52. }
  53. }
  54. }
  55. $redis_table = config('constants.SKU_QUANTITY_TABLE');
  56. foreach($sku_arr as $key=>$val){
  57. $code = GoodsSkus::where('id', $key)->pluck('code');
  58. $sku_redis_val = RedisModel::hGet($redis_table, $code);
  59. if($val != $sku_redis_val){
  60. echo "\nWARNING_Sku: ". $key.' '.$code.' true_num->redis_num:'.$val.' -> '.$sku_redis_val;
  61. $msg = '规格:'.$code. ',真实预占库存:'.$val.',Redis值:'.$sku_redis_val;
  62. YPSMS::sendSkuWnum('18501257479', $msg);
  63. }else{
  64. //echo "\nSUCCESS_Sku: ". $key.' '.$code.' true_num->redis_num:'.$val.' -> '.$sku_redis_val;
  65. }
  66. }
  67. }
  68. }