123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- <?php
- namespace App\Services\Data;
- use App\Models\JuxingAdAccount;
- use App\Models\JxStarVideoFlowOrderList;
- use App\Services\KsOpenApiService;
- use App\Support\EmailQueue;
- use App\Support\Log;
- use App\Support\RedisModel;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Redis;
- use kwaiSDK\JuXing;
- class SupplementOrderService
- {
- public static function supplementOrderStop($supplementOrderList, $supplementOrderType)
- {
- try {
- # 处理订单列表信息(按广告主账户分组)
- $request = [];
- $supplementOrderList = json_decode($supplementOrderList, 1);
- foreach ($supplementOrderList as $supplementOrder) {
- $advertiserId = $supplementOrder['advertiser_id'] ?? '';
- $supplementOrderId = $supplementOrder['supplement_order_id'] ?? '';
- $supplementOrderId = intval($supplementOrderId);
- if (empty($advertiserId) || empty($supplementOrderId)) {
- Log::error('请求数据格式不合法', [$supplementOrderList], 'supplementOrder');
- return ['请求失败', 1102];
- }
- $request[$advertiserId][] = $supplementOrderId;
- }
- # 返回数组定义
- $result = [
- 'stat' => ['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;
- }
- }
|