['success' => 0, 'fail' => 0, 'total' => count($supplementOrderList)], 'data' => [] ]; # 对分组后的订单分别处理 foreach ($request as $advertiserId => $supplementOrderIdList) { // 查询选择订单中当状态为推广中的订单 $activeSupplementOrderIdList = self::getActiveSupplementOrderIdList($advertiserId, $supplementOrderIdList); # 避免一次请求订单数太多导致失败,将订单分批次处理 $supplementOrderIdArr = array_chunk($activeSupplementOrderIdList, 100); $successSupplementOrderIdList = []; foreach ($supplementOrderIdArr as $newSupplementOrderIdList) { $data = self::supplementOrderStopRequest($advertiserId, $supplementOrderType, $newSupplementOrderIdList); Log::info('助推订单批量结束请求统计结果', ['supplement_order' => $newSupplementOrderIdList, 'res' => $data], 'supplementOrder'); $result['stat']['fail'] += $data['fail']['num']; $result['stat']['success'] = $result['stat']['total'] - $result['stat']['fail']; $successSupplementOrderIdList = array_merge($successSupplementOrderIdList, $data['success']['list']); if ($data['fail']['num'] + $data['success']['num'] != count($newSupplementOrderIdList)) { EmailQueue::rPush('助推订单停投处理结果异常 - 预处理订单数与返回订单总数不符', json_encode(['supplement_order' => $newSupplementOrderIdList, 'res' => $data, 256]) , ['song.shen@kuxuan-inc.com'], '聚星系统'); } } if ($result['stat']['success'] > 0) { // 请求成功的直接修改订单状态为推广完成 JxStarVideoFlowOrderList::updateData($advertiserId, $successSupplementOrderIdList, ['status' => 2]); // 将广告主账户ID放入更新队列中 self::syncFlowOrder($advertiserId); } } Log::info('助推订单结束响应结果', ['supplementOrderList' => $supplementOrderList, 'supplementOrderType' => $supplementOrderType, 'result' => $result], 'supplementOrder'); return [$result, 0]; } catch (\Exception $exception) { EmailQueue::rPush('批量结束助推订单程序异常', $exception->getFile() . '(' . $exception->getLine() . ') : ' . $exception->getMessage(), ['song.shen@kuxuan-inc.com'], '聚星系统'); Log::error('批量结束助推订单程序异常', ['supplement_order_list' => $supplementOrderList, 'file' => $exception->getFile(), 'line' => $exception->getLine() , 'message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString()], 'supplementOrderError'); } } /** * 批量修改助推订单出价 * */ public static function supplementOrderUpdateUnitPriceBatch($supplementOrderList, $unitPrice) { try { $supplementOrderList = json_decode($supplementOrderList, true); # 定义返回数据结构 $result = [ 'stat' => ['success' => 0, 'fail' => 0, 'total' => count($supplementOrderList)], 'data' => [] ]; $updateTotal = 0; foreach ($supplementOrderList as $supplementOrder) { $updateTotal++; $advertiserId = $supplementOrder['advertiser_id'] ?? ''; $supplementOrderId = $supplementOrder['supplement_order_id'] ?? ''; if (empty($advertiserId) || empty($supplementOrderId)) { Log::error('请求数据格式不合法', [$supplementOrderList], 'SupplementOrderUpdateUnitPriceBatch'); return [[], 1102]; } // 修改出价 $response = KsOpenApiService::supplementOrderUpdatePrice($advertiserId, intval($supplementOrderId), $unitPrice); if($response===false || $response['code']) { // 修改失败 $result['stat']['fail']++; array_push($result['data'], $supplementOrderId); } else { // 修改成功 self::syncFlowOrder($advertiserId); $result['stat']['success']++; } } }catch (\Exception $e) { EmailQueue::rPush('批量结束助推订单流程出现异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '聚星系统'); Log::error('批量结束助推订单流程出现异常', [ 'supplement_order_list' => $supplementOrderList, 'line' => $e->getLine(), 'msg' => $e->getMessage() ], 'SupplementOrderUpdateUnitPriceBatch'); return [[], 1090]; } return [$result, 0]; } /** * 批量修改助推订单预算 * */ public static function supplementOrderUpgradeAmountBatch($supplementOrderList, $amount, $payMethod) { try { $supplementOrderList = json_decode($supplementOrderList, true); # 定义返回数据结构 $result = [ 'stat' => ['success' => 0, 'fail' => 0, 'total' => count($supplementOrderList)], 'data' => [] ]; $updateTotal = 0; foreach ($supplementOrderList as $supplementOrder) { $updateTotal++; $advertiserId = $supplementOrder['advertiser_id'] ?? ''; $supplementOrderId = $supplementOrder['supplement_order_id'] ?? ''; if (empty($advertiserId) || empty($supplementOrderId)) { Log::error('请求数据格式不合法', [$supplementOrderList], 'SupplementOrderUpgradeAmountBatch'); return [[], 1102]; } // 升单 $response = KsOpenApiService::supplementOrderAmountUpgrade($advertiserId, intval($supplementOrderId), $amount, $payMethod); if($response===false || $response['code']) { // 修改失败 $result['stat']['fail']++; array_push($result['data'], $supplementOrderId); } else { // 修改成功 $result['stat']['success']++; self::syncFlowOrder($advertiserId); } } }catch (\Exception $e) { EmailQueue::rPush('批量升单流程出现异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '聚星系统'); Log::error('批量升单流程出现异常', [ 'supplement_order_list' => $supplementOrderList, 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'amount' => $amount, 'pay_method' => $payMethod ], 'SupplementOrderUpgradeAmountBatch'); return [[], 1091]; } return [$result, 0]; } /** * 批量修改助推订单推广时间 * */ public static function supplementOrderUpdatePromotionEndTimeBatch($supplementOrderList, $promotionEndTime) { try { $supplementOrderList = json_decode($supplementOrderList, true); # 定义返回数据结构 $result = [ 'stat' => ['success' => 0, 'fail' => 0, 'total' => count($supplementOrderList)], 'data' => [] ]; $updateTotal = 0; foreach ($supplementOrderList as $supplementOrder) { $updateTotal++; $advertiserId = $supplementOrder['advertiser_id'] ?? ''; $supplementOrderId = $supplementOrder['supplement_order_id'] ?? ''; if (empty($advertiserId) || empty($supplementOrderId)) { Log::error('请求数据格式不合法', [$supplementOrderList], 'SupplementOrderUpgradeAmountBatch'); return [[], 1102]; } // 修改出价 $response = KsOpenApiService::supplementOrderUpdatePromotionEndTime($advertiserId, intval($supplementOrderId), $promotionEndTime); if($response===false || $response['code']) { // 修改失败 $result['stat']['fail']++; array_push($result['data'], $supplementOrderId); } else { // 修改成功 self::syncFlowOrder($advertiserId); $result['stat']['success']++; } } }catch (\Exception $e) { EmailQueue::rPush('批量修改推广时间流程出现异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '聚星系统'); Log::error('批量修改推广时间流程出现异常', [ 'supplement_order_list' => $supplementOrderList, 'line' => $e->getLine(), 'msg' => $e->getMessage(), 'end_time' => $promotionEndTime ], 'SupplementOrderUpdatePromotionEndTimeBatch'); return [[], 1092]; } return [$result, 0]; } public static function syncFlowOrder($advertiserId) { $stTime = date('Y-m-d', strtotime('-90 days')); $enTime = date('Y-m-d'); # 获取时间段数组 $timeRangeArr = getTimeRangeArr($stTime, $enTime); foreach ($timeRangeArr as $timeRange) { # 5分钟内不重复更新 $redisKey = 'juxingFlowStarVideoSyncAdvidListLck-'.$advertiserId; $redisValue = RedisModel::get($redisKey); if(!empty($redisValue)) { continue; } # 插入队列 $advertiserItem = [ 'advertiserId' => $advertiserId, 'timeRange' => $timeRange ]; RedisModel::lPush(JuxingAdAccount::JUXING_FLOW_STAR_VIDEO_SYNC_ADVID_LIST, json_encode($advertiserItem)); # 插入队列 RedisModel::lPush(JuxingAdAccount::JUXING_FLOW_QTASK_VIDEO_SYNC_ADVID_LIST, json_encode($advertiserItem)); RedisModel::set($redisKey, 1); RedisModel::expire($redisValue, 300); } } public static function supplementOrderStopRequest($advertiserId, $supplementOrderType, $supplementOrderIdList, $retry=0) { $accessToken = JuxingAdAccount::getAccessToken($advertiserId); $juxing = new JuXing($accessToken); $param = [ 'advertiser_id' => intval($advertiserId), 'supplement_order_type' => intval($supplementOrderType), 'supplement_order_ids' => $supplementOrderIdList ]; $rst = $juxing->supplementOrderV2()->stop($param); if($rst === false && $retry < 3) { $retry++; return self::supplementOrderStopRequest($advertiserId, $supplementOrderType, $supplementOrderIdList, $retry); } Log::info('助推订单停投', ['param' => $param, 'ret' => $rst, 'operator_id' => Auth::id()], 'supplementOrder'); $data = [ 'success' => ['list' => [], 'num' => 0], 'fail' => ['list' => [], 'num' => 0], 'total' => count($supplementOrderIdList) ]; if(isset($rst['code']) && $rst['code'] == 0) {# 请求成功 $supplementOrder = $rst['data']['stop_supplement_orders'] ?? []; foreach($supplementOrder as $order) { if(isset($order['result']) && $order['result']) { $data['success']['list'][] = $order['supplement_order_id']; $data['success']['num']++; } else { $data['fail']['list'][] = $order['supplement_order_id']; $data['fail']['num']++; } } } else { if($retry < 3) { $retry++; return self::supplementOrderStopRequest($advertiserId, $supplementOrderType, $supplementOrderIdList, $retry); } $data['fail']['list'] = $supplementOrderIdList; $data['fail']['num'] = count($supplementOrderIdList); EmailQueue::rPush('助推订单停投返回异常', json_encode([ 'advertiser_id' => $advertiserId, 'supplement_order_id_list' => $supplementOrderIdList, 'supplement_order_type' => $supplementOrderType, 'response' => $rst ], 256), ['song.shen@kuxuan-inc.com'], '聚星系统'); } return $data; } public static function supplementOrderUpdatePromotionEndTime($advertiserId, $supplementOrderId, $promotionEndTime, $retry = 0) { $accessToken = JuxingAdAccount::getAccessToken($advertiserId); $juxing = new JuXing($accessToken); $realPromotionEndTime = date("Y-m-d H:i:s", strtotime($promotionEndTime)).'.'.rand(100, 999); $param = [ 'advertiser_id' => intval($advertiserId), 'supplement_order_id' => intval($supplementOrderId), 'promotion_end_time' => $realPromotionEndTime ]; $rst = $juxing->supplementOrderV2()->promotionEndtimeUpdate($param); if($rst === false && $retry < 3) { $retry++; return self::supplementOrderUpdatePromotionEndTime($advertiserId, $supplementOrderId, $promotionEndTime, $retry); } Log::info('助推订单修改推广结束时间', ['param' => $param, 'ret' => $rst, 'operator_id' => Auth::id()], 'supplementOrder'); if(isset($rst['code']) && $rst['code'] == 0) {# 请求成功 // 将广告主账户ID放入更新队列中或者直接更新系统数据 self::syncFlowOrder($advertiserId); return ['', 0, null]; } else { if($retry < 3) { $retry++; return self::supplementOrderUpdatePromotionEndTime($advertiserId, $supplementOrderId, $promotionEndTime, $retry); } EmailQueue::rPush('助推订单修改推广结束时间返回异常', json_encode([ 'advertiser_id' => $advertiserId, 'supplement_order_id' => $supplementOrderId, 'promotion_end_time' => $realPromotionEndTime, 'response' => $rst ], 256), ['song.shen@kuxuan-inc.com'], '聚星系统'); return ['请求失败', 501, $rst['message']]; } } public static function supplementOrderUpgradeAmount($advertiserId, $supplementOrderId, $payMethod, $amount, $retry = 0) { $accessToken = JuxingAdAccount::getAccessToken($advertiserId); $juxing = new JuXing($accessToken); $param = [ 'advertiser_id' => intval($advertiserId), 'supplement_order_id' => intval($supplementOrderId), 'pay_method' => $payMethod, 'increased_amount' => intval($amount * 1000), // 单位厘 ]; $rst = $juxing->supplementOrderV2()->amountUpgrade($param); if($rst === false && $retry < 3) { $retry++; return self::supplementOrderUpgradeAmount($advertiserId, $supplementOrderId, $payMethod, $amount, $retry); } Log::info('助推订单升单', ['param' => $param, 'ret' => $rst, 'operator_id' => Auth::id()], 'supplementOrder'); if(isset($rst['code']) && $rst['code'] == 0) {# 请求成功 // 将广告主账户ID放入更新队列中 self::syncFlowOrder($advertiserId); return ['', 0, null]; } else { if($retry < 3) { $retry++; return self::supplementOrderUpgradeAmount($advertiserId, $supplementOrderId, $payMethod, $amount, $retry); } EmailQueue::rPush('助推订单升单请求失败', json_encode([ 'advertiser_id' => $advertiserId, 'supplement_order_id' => $supplementOrderId, 'pay_method' => $payMethod, 'amount' => $amount, 'response' => $rst ], 256), ['song.shen@kuxuan-inc.com'], '聚星系统'); return ['请求失败', 501, $rst['message']]; } } public static function supplementOrderUpdateUnitPrice($advertiserId, $supplementOrderId, $price, $retry = 0) { $accessToken = JuxingAdAccount::getAccessToken($advertiserId); $juxing = new JuXing($accessToken); $param = [ 'advertiser_id' => intval($advertiserId), 'supplement_order_id' => intval($supplementOrderId), 'unit_price' => intval($price * 1000)// 单位厘 ]; $rst = $juxing->supplementOrderV2()->unitPriceUpdate($param); if($rst === false && $retry < 3) { $retry++; return self::supplementOrderUpdateUnitPrice($advertiserId, $supplementOrderId, $price, $retry); } Log::info('助推订单修改出价', ['param' => $param, 'ret' => $rst, 'operator_id' => Auth::id()], 'supplementOrder'); if(isset($rst['code']) && $rst['code'] == 0) {# 请求成功 // 将广告主账户ID放入更新队列中或者直接更新系统数据 self::syncFlowOrder($advertiserId); return ['', 0, null]; } else { if($retry < 3) { $retry++; return self::supplementOrderUpdateUnitPrice($advertiserId, $supplementOrderId, $price, $retry); } EmailQueue::rPush('助推订单修改出价请求失败', json_encode([ 'advertiser_id' => $advertiserId, 'supplement_order_id' => $supplementOrderId, 'price' => $price, 'response' => $rst ], 256), ['song.shen@kuxuan-inc.com'], '聚星系统'); return ['请求失败', 501, $rst['message']]; } } # 获取所选则订单中状态为推广中的订单列表 public static function getActiveSupplementOrderIdList($advertiserId, $supplementOrderIdList) { Log::info('停投订单ID记录 - 全部', ['order_list' => $supplementOrderIdList], 'supplementOrder'); $adverIds = [$advertiserId]; $keyword = $type = $taskIds = $stDate = $enDate = null; $status = 1; $supplementOrderModel = JxStarVideoFlowOrderList::getOrderListQuery($adverIds, $keyword, $type, $status, $taskIds, $stDate, $enDate, $supplementOrderIdList); $supplementOrderList = $supplementOrderModel->selectRaw('supplement_order_id')->get(); $activeSupplementOrderIdList = []; if($supplementOrderList->isNotEmpty()) { $activeSupplementOrderIdList = array_column($supplementOrderList->toArray(), 'supplement_order_id'); } Log::info('停投订单记录 - 推广中', ['order_list' => $activeSupplementOrderIdList], 'supplementOrder'); if(count($activeSupplementOrderIdList) > count($supplementOrderIdList)) { EmailQueue::rPush('订单停投处理结果异常 - 查询推广中订单总数超过选择订单总数', json_encode(['select_supplement_order_list' => $supplementOrderIdList , 'active_supplement_order_list' => $activeSupplementOrderIdList], 256), ['song.shen@kuxuan-inc.com'], '聚星系统'); } # 推广完成状态订单ID $finishSupplementOrderModel = JxStarVideoFlowOrderList::getOrderListQuery($adverIds, $keyword, $type, 2, $taskIds, $stDate, $enDate, $supplementOrderIdList); $finishSupplementOrderList = $finishSupplementOrderModel->selectRaw('supplement_order_id')->get(); Log::info('停投订单记录 - 推广完成', ['order_list' => $finishSupplementOrderList->toArray()], 'supplementOrder'); if($finishSupplementOrderList->count() + count($activeSupplementOrderIdList) != count($supplementOrderIdList)) { EmailQueue::rPush('订单停投处理结果异常 - 推广中订单及推广完成订单总数不等于选择订单总数', json_encode(['select_supplement_order_count' => count($supplementOrderIdList) , 'active_supplement_order_count' => count($activeSupplementOrderIdList), 'finish_supplement_order_count' => $finishSupplementOrderList->count()], 256) , ['song.shen@kuxuan-inc.com'], '聚星系统'); } return $activeSupplementOrderIdList; } }