RetailOutboundListener.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\RetailOutboundEvent;
  4. use App\Models\ExpressStorage;
  5. use App\Models\OrderDetailW;
  6. use App\Models\UserStorage;
  7. use App\Models\User;
  8. use App\Models\Store;
  9. use App\Models\OrderW;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Queue\InteractsWithQueue;
  15. class RetailOutboundListener
  16. {
  17. /**
  18. * @param ExampleEvent $event
  19. * @return void
  20. */
  21. public function handle(RetailOutboundEvent $event)
  22. {
  23. Log::info('111111111123');
  24. Log::error('//'.json_encode($event));
  25. $order_no=$event->data;
  26. $order=OrderW::where('order_no',$order_no)->first();
  27. $user_id=Store::where('id',$order->store_id)->value('user_id');
  28. $user=User::find($user_id);
  29. Log::error('//'.json_encode($order_no));
  30. if($user->level==3){
  31. $order_goods=OrderDetailW::with('goods')->where('order_no',$order_no)->get();
  32. foreach($order_goods as $key=>$val){
  33. $goods=$val->goods;
  34. Log::info('!!!@@@'.$goods->main_attr);
  35. $type=$this->type($goods->main_attr);
  36. $sex=$this->sex($val->type);
  37. $size=$this->size($val->size);
  38. $num=(int)$val->num;
  39. $data=UserStorage::where('user_id',$user_id)->where('type',$type)
  40. ->where('sex',$sex)->where('size',$size)->lockForUpdate()->first();
  41. if($data){
  42. $data->num -= $num;
  43. $data->save();
  44. }else{
  45. UserStorage::create([
  46. 'user_id'=> $user_id,
  47. 'type' => $type,
  48. 'sex' => $sex,
  49. 'size' => $size,
  50. 'num' => -$num,
  51. ]);
  52. }
  53. ExpressStorage::create([
  54. 'user_id'=>$user_id,
  55. 'size'=>$size,
  56. 'sex'=>$sex,
  57. 'type'=>$type,
  58. 'num'=>$num,
  59. 'storage_type'=>1,
  60. 'out_time'=>date("Y-m-d H:i:s"),
  61. 'out_type'=>0,
  62. 'order_no'=>$order_no,
  63. 'remark'=>'零售减库存'
  64. ]);
  65. }
  66. }
  67. }
  68. function type($attr_main){
  69. switch($attr_main){
  70. case '精装版':
  71. return 0;
  72. break;
  73. case '简约版':
  74. return 1;
  75. break;
  76. case '老人版':
  77. case '高腰版':
  78. return 2;
  79. break;
  80. case '纯棉老人版':
  81. return 3;
  82. break;
  83. case '青春版':
  84. return 4;
  85. break;
  86. default:
  87. return 9;
  88. }
  89. }
  90. function sex($sex){
  91. if($sex=='女款'){
  92. return 0;
  93. }
  94. return 1;
  95. }
  96. function size($size){
  97. $size=strtoupper($size);
  98. if($size=='XXL'){
  99. return '2XL';
  100. }
  101. if($size=='XXXL'){
  102. return '3XL';
  103. }
  104. if($size=='XXXXL'){
  105. return '4XL';
  106. }
  107. if($size=='XXXXXL'){
  108. return '5XL';
  109. }
  110. return $size;
  111. }
  112. }