ChangeBigGiftBagListener.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Modules\Order\Listeners;
  3. use Modules\Order\Events\ChangeBigGiftBagEvent;
  4. use App\Models\BigGiftApply;
  5. use App\Models\GiftUserImg;
  6. use App\Models\ActivityLimit;
  7. use App\Models\Goodtest;
  8. use App\Models\OrderDetail;
  9. use Illuminate\Queue\Listener;
  10. use Illuminate\Support\Facades\Log;
  11. class ChangeBigGiftBagListener {
  12. /**
  13. * Handle the event.
  14. *
  15. * @param WechatMessageEvent $event
  16. * @return void
  17. */
  18. public function handle(ChangeBigGiftBagEvent $event)
  19. {
  20. $data=$event->data;
  21. $order_id=$data['order_id'];
  22. Log::info($data);
  23. if($data['type']== 'edit'){ //修改变更
  24. $apply=BigGiftApply::where('order_id',$order_id)->first();//当前申请的大礼包信息
  25. if($apply){
  26. //活动期间 销售经理 的所有大礼包信息
  27. $limit=ActivityLimit::where('activity_id',$apply->activity_id)->where('type',1)->where(function($query){
  28. $query->where('title','like','%A%')->orwhere('title','like','%B%');
  29. })->orderByDesc('account')->get();
  30. $account=$this->getNewOrderPrice($order_id);//获取订单变更后的金额
  31. Log::info('订单变更后金额'.$account);
  32. $new_limit=[];
  33. foreach($limit as $key=>$val){
  34. if($val->account <= $account){
  35. $new_limit=$val;
  36. break;
  37. }
  38. }
  39. if(!empty($new_limit)){
  40. Log::info($new_limit->id .'---------'. $apply->limit_id);
  41. if($data['n']==1){
  42. if(!empty($apply->order_change)){
  43. $apply->order_change .= ',合并订单';
  44. }else{
  45. $apply->order_change = '合并订单';
  46. }
  47. }elseif($data['n']==2){
  48. if(!empty($apply->order_change)) {
  49. $apply->order_change .= ',修改订单';
  50. }else{
  51. $apply->order_change = '修改订单';
  52. }
  53. }
  54. if($new_limit->id == $apply->limit_id){
  55. $apply->order_account=$account;
  56. $apply->save();
  57. }else{
  58. if($new_limit->title == '大礼包A'){
  59. $type=0;
  60. }else{
  61. $type=1;
  62. }
  63. $apply->limit_name=$new_limit->title;
  64. $apply->limit_id=$new_limit->id;
  65. $apply->gift_money=$new_limit->account;
  66. $apply->type=$type;
  67. $apply->status=0;
  68. $apply->order_account=$account;
  69. if($apply->gift_user_img_id){
  70. GiftUserImg::where('id',$apply->gift_user_img_id)->delete();
  71. }
  72. $apply->gift_user_img_id=null;
  73. $apply->save();
  74. }
  75. }else{
  76. if($apply->gift_user_img_id){
  77. GiftUserImg::where('id',$apply->gift_user_img_id)->delete();
  78. }
  79. $apply->delete();
  80. }
  81. }
  82. }
  83. if($data['type']== 'cancel'){ //作废
  84. $apply=BigGiftApply::where('order_id',$order_id)->first();//当前申请的大礼包信息
  85. if($apply){
  86. if($apply->gift_user_img_id){
  87. GiftUserImg::where('id',$apply->gift_user_img_id)->delete();
  88. }
  89. $apply->delete();
  90. }
  91. }
  92. }
  93. public function getNewOrderPrice($order_id){
  94. $hard_num=OrderDetail::whereHas('order',function($query){
  95. $query->where('is_deleted',0);
  96. })->where('order_id',$order_id)->where('goods_name','like','%精装%')->sum('num');
  97. $simple_num=OrderDetail::whereHas('order',function($query){
  98. $query->where('is_deleted',0);
  99. })->where('order_id',$order_id)->where('goods_name','like','%简约%')->sum('num');
  100. $old_num=OrderDetail::whereHas('order',function($query){
  101. $query->where('is_deleted',0);
  102. })->where('order_id',$order_id)->where('goods_name','like','%高腰%')->sum('num');
  103. $new_old_num=OrderDetail::whereHas('order',function($query){
  104. $query->where('is_deleted',0);
  105. })->where('order_id',$order_id)->where('goods_name','like','%纯棉%')->sum('num');
  106. $youth_num=OrderDetail::whereHas('order',function($query){
  107. $query->where('is_deleted',0);
  108. })->where('order_id',$order_id)->where('goods_name','like','%青春%')->sum('num');
  109. $goodsTest = new Goodtest();
  110. $price=$goodsTest->getPrice;
  111. $account=$hard_num * $price['hard']['top']
  112. + $simple_num * $price['simple']['top']
  113. + $old_num * $price['old']['top']
  114. + $new_old_num * $price['new_old']['top']
  115. + $youth_num * $price['youth']['top'];
  116. return $account;
  117. }
  118. }