作者 赵彬吉

fb ad cost

<?php
namespace App\Console\Commands\Inquiry;
use App\Helper\Arr;
use App\Models\Inquiry\ReInquiryCost;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Console\Command;
/**
* Class SyncAdCost
* @package App\Console\Commands\Inquiry
*/
class SyncAdCost extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync_ad_cost';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步广告费用';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* @author zbj
* @date 2024/12/4
*/
public function handle()
{
$list = $this->getAdCost();
foreach ($list as $item) {
ReInquiryCost::saveData($item['ad_id'], $item['spend'], $item['lead'], $item['single_cost'], $item['start_date'], $item['end_date']);
}
}
/**
* 获取远程询盘信息
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getAdCost()
{
try {
$res = HttpUtils::get('https://fob.ai.cc/api/fb_ad_cost', []);
$res = Arr::s2a($res);
} catch (\Exception | GuzzleException $e) {
$this->output($e->getMessage());
return [];
}
return $res['data']?? [];
}
public function output($message)
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
}
}
... ...
... ... @@ -42,6 +42,8 @@ class Kernel extends ConsoleKernel
$schedule->command('updateAiProjects')->everyFourHours()->withoutOverlapping(1);
//每日同步询盘文案
$schedule->command('sync_inquiry_text')->dailyAt('09:00')->withoutOverlapping(1);
//FB询盘费用同步
$schedule->command('facebook_ad_cost')->everyThirtyMinutes()->withoutOverlapping(1);
}
/**
... ...
... ... @@ -13,6 +13,7 @@ use App\Helper\QuanqiusouApi;
use App\Http\Controllers\Aside\BaseController;
use App\Models\Channel\Channel;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\ReInquiryCost;
use App\Models\Inquiry\ReInquiryCount;
use App\Models\Inquiry\ReInquiryDetail;
use App\Models\Inquiry\ReInquiryForm;
... ... @@ -57,15 +58,21 @@ class AdsController extends BaseController
}
})
->orderBy('id', 'desc')
->paginate($page_size);
->paginate($page_size)
->toArray();
$relay_site_total = 0;
foreach ($result as &$item){
$relay_site_total += count($item->target);
$item->requiry_num = ReInquiryDetail::where('task_id', $item->id)->where('status', ReInquiryDetail::STATUS_SUCCESS)->count();
$item->form_num = ReInquiryForm::whereIn('ad_id', explode(',', $item->ad_id))->count();
foreach ($result['list'] as &$item){
$relay_site_total += count($item['target']);
$item['requiry_num'] = ReInquiryDetail::where('task_id', $item['id'])->where('status', ReInquiryDetail::STATUS_SUCCESS)->count();
$item['form_num'] = ReInquiryForm::whereIn('ad_id', explode(',', $item['ad_id']))->count();
//关联网站是否有重复的
foreach ($item['target'] as $k=>$target){
$repeat = ReInquiryTask::where('target', 'like', '%'.$target['url'].'%')->where('id', '<>', $item['id'])->first();
$item['target'][$k]['is_repeat'] = $repeat ? 1 : 0;
}
$item['cost'] = ReInquiryCost::getCostByAdIds($item['ad_id']);
}
$result = $result->toArray();
$result['relay_site_total'] = $relay_site_total;
$result['default_ai_param'] = ReInquiryTask::DEFAULT_AI_PARAM;
... ...
<?php
namespace App\Models\Inquiry;
use App\Models\Base;
use Illuminate\Support\Facades\Cache;
/**
* Class ReInquiryCost
* @package App\Models\Inquiry
* @author zbj
* @date 2024/12/4
*/
class ReInquiryCost extends Base
{
//设置关联表名
/**
* @var mixed
*/
protected $table = 'gl_re_inquiry_cost';
public static function saveData($ad_id, $spend, $lead, $single_cost, $start_date, $end_date){
$cost = self::where('ad_id', $ad_id)->first();
if(!$cost){
$cost = new self();
}
$cost->ad_id = $ad_id;
$cost->spend = $spend;
$cost->lead = $lead;
$cost->single_cost = $single_cost;
$cost->start_date = $start_date;
$cost->end_date = $end_date;
$cost->save();
}
public static function getCostByAdIds($ad_ids){
if(is_string($ad_ids)){
$ad_ids = explode(',',$ad_ids);
}
$cache_key = 'GET_COST_BY_AD_IDS_' . implode(',', $ad_ids);
$data = Cache::get($cache_key);
if(!$data){
$data = [];
$list = self::whereIn('ad_id', $ad_ids)->get();
foreach ($list as $item){
$data['cost'][] = '$' . $item['spend'];
$data['single_cost'][] = '$' . $item['single_cost'];
}
Cache::set($cache_key, $data);
}
return $data;
}
}
... ...