RestoreOutboundListener.php 3.2 KB

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