123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace Modules\Order\Listeners;
- use Modules\Order\Events\ChangeBigGiftBagEvent;
- use App\Models\BigGiftApply;
- use App\Models\GiftUserImg;
- use App\Models\ActivityLimit;
- use App\Models\Goodtest;
- use App\Models\OrderDetail;
- use Illuminate\Queue\Listener;
- use Illuminate\Support\Facades\Log;
- class ChangeBigGiftBagListener {
- /**
- * Handle the event.
- *
- * @param WechatMessageEvent $event
- * @return void
- */
- public function handle(ChangeBigGiftBagEvent $event)
- {
- $data=$event->data;
- $order_id=$data['order_id'];
- Log::info($data);
- if($data['type']== 'edit'){ //修改变更
- $apply=BigGiftApply::where('order_id',$order_id)->first();//当前申请的大礼包信息
- if($apply){
- //活动期间 销售经理 的所有大礼包信息
- $limit=ActivityLimit::where('activity_id',$apply->activity_id)->where('type',1)->where(function($query){
- $query->where('title','like','%A%')->orwhere('title','like','%B%');
- })->orderByDesc('account')->get();
- $account=$this->getNewOrderPrice($order_id);//获取订单变更后的金额
- Log::info('订单变更后金额'.$account);
- $new_limit=[];
- foreach($limit as $key=>$val){
- if($val->account <= $account){
- $new_limit=$val;
- break;
- }
- }
- if(!empty($new_limit)){
- Log::info($new_limit->id .'---------'. $apply->limit_id);
- if($data['n']==1){
- if(!empty($apply->order_change)){
- $apply->order_change .= ',合并订单';
- }else{
- $apply->order_change = '合并订单';
- }
- }elseif($data['n']==2){
- if(!empty($apply->order_change)) {
- $apply->order_change .= ',修改订单';
- }else{
- $apply->order_change = '修改订单';
- }
- }
- if($new_limit->id == $apply->limit_id){
- $apply->order_account=$account;
- $apply->save();
- }else{
- if($new_limit->title == '大礼包A'){
- $type=0;
- }else{
- $type=1;
- }
- $apply->limit_name=$new_limit->title;
- $apply->limit_id=$new_limit->id;
- $apply->gift_money=$new_limit->account;
- $apply->type=$type;
- $apply->status=0;
- $apply->order_account=$account;
- if($apply->gift_user_img_id){
- GiftUserImg::where('id',$apply->gift_user_img_id)->delete();
- }
- $apply->gift_user_img_id=null;
- $apply->save();
- }
- }else{
- if($apply->gift_user_img_id){
- GiftUserImg::where('id',$apply->gift_user_img_id)->delete();
- }
- $apply->delete();
- }
- }
- }
- if($data['type']== 'cancel'){ //作废
- $apply=BigGiftApply::where('order_id',$order_id)->first();//当前申请的大礼包信息
- if($apply){
- if($apply->gift_user_img_id){
- GiftUserImg::where('id',$apply->gift_user_img_id)->delete();
- }
- $apply->delete();
- }
- }
- }
- public function getNewOrderPrice($order_id){
- $hard_num=OrderDetail::whereHas('order',function($query){
- $query->where('is_deleted',0);
- })->where('order_id',$order_id)->where('goods_name','like','%精装%')->sum('num');
- $simple_num=OrderDetail::whereHas('order',function($query){
- $query->where('is_deleted',0);
- })->where('order_id',$order_id)->where('goods_name','like','%简约%')->sum('num');
- $old_num=OrderDetail::whereHas('order',function($query){
- $query->where('is_deleted',0);
- })->where('order_id',$order_id)->where('goods_name','like','%高腰%')->sum('num');
- $new_old_num=OrderDetail::whereHas('order',function($query){
- $query->where('is_deleted',0);
- })->where('order_id',$order_id)->where('goods_name','like','%纯棉%')->sum('num');
- $youth_num=OrderDetail::whereHas('order',function($query){
- $query->where('is_deleted',0);
- })->where('order_id',$order_id)->where('goods_name','like','%青春%')->sum('num');
- $goodsTest = new Goodtest();
- $price=$goodsTest->getPrice;
- $account=$hard_num * $price['hard']['top']
- + $simple_num * $price['simple']['top']
- + $old_num * $price['old']['top']
- + $new_old_num * $price['new_old']['top']
- + $youth_num * $price['youth']['top'];
- return $account;
- }
- }
|