leftJoin('group_activity_category b','a.group_category_id=b.id') ->leftJoin('group_activity c','a.group_activity_id=c.id') ->where('a.id', $group_order_id) ->field('b.service_time,b.master_worker_id,c.price,c.work_amount,c.settlement_amount') ->findOrEmpty() ->toArray(); try { //查询拼团用户订单 $userOrder = GroupUserOrder::alias('a') ->leftJoin('user b','a.user_id=b.id') ->where('group_order_id', $group_order_id) ->where('pay_status', 1) ->where('refund_status', 0) ->field('a.*,b.mobile') ->select() ->toArray(); if (empty($userOrder)) { return true; } Db::startTrans(); foreach ($userOrder as $k => $item) { if (empty($goods)) { $goods = UserEquity::alias('a')->leftJoin('goods b','a.goods_id=b.id') ->where('a.id', $item['user_equity_id']) ->field('a.number,a.goods_id,b.*') ->findOrEmpty() ->toArray(); } //生成拼团服务工单 $work_data = [ 'work_sn' => generate_sn(GroupServiceWork::class, 'work_sn'), 'real_name' => '', 'mobile' => $item['mobile'], 'address' => $item['area'].$item['address'], 'title' => $goods['goods_name'], 'category_type' => $goods['category_type'], 'goods_category_ids' => $goods['goods_category_ids'], 'goods_category_id' => $goods['goods_category_id'], 'work_pay_status'=>WorkEnum::IS_PAY_STATUS, 'master_worker_id' => $category['master_worker_id'], 'appointment_time' => $category['service_time'], 'dispatch_time' => time(), 'receive_time' => time(), 'user_id'=>$item['user_id'], 'user_equity_id'=>0, 'third_type'=>1, 'work_total'=>$item['paid_amount'], 'work_status' => 5, 'user_confirm_status' => 3, 'service_status' => 1, 'remark' => '拼团用户订单:'.$item['id'], 'user_equity_id' => $item['user_equity_id'], 'group_order_id' => $group_order_id, 'group_user_order_id' => $item['id'], 'work_total' => $category['price'], 'work_amount' => $category['work_amount'], 'settlement_amount' => $category['settlement_amount'], ]; $service_work = GroupServiceWork::create($work_data); //生成订单服务详情 OrderGoods::create([ 'sn' => $service_work['sn'], 'goods_id' => $goods['goods_id'], 'category_type' => $goods['category_type'], 'goods_category_ids' => $goods['goods_category_ids'], 'goods_category_id' => $goods['goods_category_id'], 'goods_name' => $goods['goods_name'], 'goods_image' => $goods['goods_image'], 'goods_video' => $goods['goods_video'], 'goods_number' => $goods['goods_number'], 'good_unit' => $goods['good_unit'], 'goods_size' => $goods['goods_size'], 'goods_type' => $goods['goods_type'], 'goods_brand' => $goods['goods_brand'], 'install_guide' => $goods['install_guide'], 'goods_payment_type'=>$goods['goods_payment_type'], 'base_service_fee' => $goods['base_service_fee'], 'service_total' => $goods['service_total'], 'service_fee' => $goods['service_fee'], 'service_image' => $goods['service_image'], 'warranty_period'=>$goods['warranty_period'], 'fee_schedule' => $goods['fee_schedule'], 'goods_status' => $goods['goods_status'], ]); //更新用户的权益卡剩余次数 UserEquity::where('id', $item['user_equity_id'])->update(['number' => 0, 'update_time' => time()]); //记录用户的权益卡使用记录 UserEquityLog::create([ 'user_id' => $item['user_id'], 'number' => $goods['number'], 'user_equity_id' => $item['user_equity_id'], 'create_time' => time(), ]); } GroupOrder::where('id', $group_order_id)->update(['work_status' => 2]); Db::commit(); // 任务执行完毕后删除任务 $job->delete(); return true; } catch (\Exception $e) { Db::rollback(); Log::write("拼团订单批量生成服务工单异常:".$e->getMessage()); // 任务执行失败,重试任务 if ($job->attempts() > 3) { // 超过最大重试次数,删除任务 $job->delete(); } else { $job->release(10); // 10 秒后重试 } return false; } } }