作者 赵彬吉

fb ad cost

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Inquiry;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Models\Inquiry\ReInquiryCost;
  7 +use App\Utils\HttpUtils;
  8 +use GuzzleHttp\Exception\GuzzleException;
  9 +use Illuminate\Console\Command;
  10 +
  11 +/**
  12 + * Class SyncAdCost
  13 + * @package App\Console\Commands\Inquiry
  14 + */
  15 +class SyncAdCost extends Command
  16 +{
  17 + /**
  18 + * The name and signature of the console command.
  19 + *
  20 + * @var string
  21 + */
  22 + protected $signature = 'sync_ad_cost';
  23 +
  24 + /**
  25 + * The console command description.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $description = '同步广告费用';
  30 +
  31 + /**
  32 + * Create a new command instance.
  33 + *
  34 + * @return void
  35 + */
  36 + public function __construct()
  37 + {
  38 + parent::__construct();
  39 + }
  40 +
  41 + /**
  42 + * @author zbj
  43 + * @date 2024/12/4
  44 + */
  45 + public function handle()
  46 + {
  47 + $list = $this->getAdCost();
  48 + foreach ($list as $item) {
  49 + ReInquiryCost::saveData($item['ad_id'], $item['spend'], $item['lead'], $item['single_cost'], $item['start_date'], $item['end_date']);
  50 + }
  51 + }
  52 +
  53 + /**
  54 + * 获取远程询盘信息
  55 + * @return array
  56 + * @throws \GuzzleHttp\Exception\GuzzleException
  57 + */
  58 + public function getAdCost()
  59 + {
  60 + try {
  61 + $res = HttpUtils::get('https://fob.ai.cc/api/fb_ad_cost', []);
  62 + $res = Arr::s2a($res);
  63 + } catch (\Exception | GuzzleException $e) {
  64 + $this->output($e->getMessage());
  65 + return [];
  66 + }
  67 + return $res['data']?? [];
  68 + }
  69 +
  70 +
  71 + public function output($message)
  72 + {
  73 + echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
  74 + }
  75 +}
@@ -42,6 +42,8 @@ class Kernel extends ConsoleKernel @@ -42,6 +42,8 @@ class Kernel extends ConsoleKernel
42 $schedule->command('updateAiProjects')->everyFourHours()->withoutOverlapping(1); 42 $schedule->command('updateAiProjects')->everyFourHours()->withoutOverlapping(1);
43 //每日同步询盘文案 43 //每日同步询盘文案
44 $schedule->command('sync_inquiry_text')->dailyAt('09:00')->withoutOverlapping(1); 44 $schedule->command('sync_inquiry_text')->dailyAt('09:00')->withoutOverlapping(1);
  45 + //FB询盘费用同步
  46 + $schedule->command('facebook_ad_cost')->everyThirtyMinutes()->withoutOverlapping(1);
45 } 47 }
46 48
47 /** 49 /**
@@ -13,6 +13,7 @@ use App\Helper\QuanqiusouApi; @@ -13,6 +13,7 @@ use App\Helper\QuanqiusouApi;
13 use App\Http\Controllers\Aside\BaseController; 13 use App\Http\Controllers\Aside\BaseController;
14 use App\Models\Channel\Channel; 14 use App\Models\Channel\Channel;
15 use App\Models\Domain\DomainInfo; 15 use App\Models\Domain\DomainInfo;
  16 +use App\Models\Inquiry\ReInquiryCost;
16 use App\Models\Inquiry\ReInquiryCount; 17 use App\Models\Inquiry\ReInquiryCount;
17 use App\Models\Inquiry\ReInquiryDetail; 18 use App\Models\Inquiry\ReInquiryDetail;
18 use App\Models\Inquiry\ReInquiryForm; 19 use App\Models\Inquiry\ReInquiryForm;
@@ -57,15 +58,21 @@ class AdsController extends BaseController @@ -57,15 +58,21 @@ class AdsController extends BaseController
57 } 58 }
58 }) 59 })
59 ->orderBy('id', 'desc') 60 ->orderBy('id', 'desc')
60 - ->paginate($page_size); 61 + ->paginate($page_size)
  62 + ->toArray();
61 63
62 $relay_site_total = 0; 64 $relay_site_total = 0;
63 - foreach ($result as &$item){  
64 - $relay_site_total += count($item->target);  
65 - $item->requiry_num = ReInquiryDetail::where('task_id', $item->id)->where('status', ReInquiryDetail::STATUS_SUCCESS)->count();  
66 - $item->form_num = ReInquiryForm::whereIn('ad_id', explode(',', $item->ad_id))->count(); 65 + foreach ($result['list'] as &$item){
  66 + $relay_site_total += count($item['target']);
  67 + $item['requiry_num'] = ReInquiryDetail::where('task_id', $item['id'])->where('status', ReInquiryDetail::STATUS_SUCCESS)->count();
  68 + $item['form_num'] = ReInquiryForm::whereIn('ad_id', explode(',', $item['ad_id']))->count();
  69 + //关联网站是否有重复的
  70 + foreach ($item['target'] as $k=>$target){
  71 + $repeat = ReInquiryTask::where('target', 'like', '%'.$target['url'].'%')->where('id', '<>', $item['id'])->first();
  72 + $item['target'][$k]['is_repeat'] = $repeat ? 1 : 0;
  73 + }
  74 + $item['cost'] = ReInquiryCost::getCostByAdIds($item['ad_id']);
67 } 75 }
68 - $result = $result->toArray();  
69 $result['relay_site_total'] = $relay_site_total; 76 $result['relay_site_total'] = $relay_site_total;
70 $result['default_ai_param'] = ReInquiryTask::DEFAULT_AI_PARAM; 77 $result['default_ai_param'] = ReInquiryTask::DEFAULT_AI_PARAM;
71 78
  1 +<?php
  2 +
  3 +namespace App\Models\Inquiry;
  4 +
  5 +use App\Models\Base;
  6 +use Illuminate\Support\Facades\Cache;
  7 +
  8 +/**
  9 + * Class ReInquiryCost
  10 + * @package App\Models\Inquiry
  11 + * @author zbj
  12 + * @date 2024/12/4
  13 + */
  14 +class ReInquiryCost extends Base
  15 +{
  16 +
  17 + //设置关联表名
  18 + /**
  19 + * @var mixed
  20 + */
  21 + protected $table = 'gl_re_inquiry_cost';
  22 +
  23 +
  24 + public static function saveData($ad_id, $spend, $lead, $single_cost, $start_date, $end_date){
  25 + $cost = self::where('ad_id', $ad_id)->first();
  26 + if(!$cost){
  27 + $cost = new self();
  28 + }
  29 +
  30 + $cost->ad_id = $ad_id;
  31 + $cost->spend = $spend;
  32 + $cost->lead = $lead;
  33 + $cost->single_cost = $single_cost;
  34 + $cost->start_date = $start_date;
  35 + $cost->end_date = $end_date;
  36 + $cost->save();
  37 + }
  38 +
  39 + public static function getCostByAdIds($ad_ids){
  40 + if(is_string($ad_ids)){
  41 + $ad_ids = explode(',',$ad_ids);
  42 + }
  43 + $cache_key = 'GET_COST_BY_AD_IDS_' . implode(',', $ad_ids);
  44 + $data = Cache::get($cache_key);
  45 + if(!$data){
  46 + $data = [];
  47 + $list = self::whereIn('ad_id', $ad_ids)->get();
  48 + foreach ($list as $item){
  49 + $data['cost'][] = '$' . $item['spend'];
  50 + $data['single_cost'][] = '$' . $item['single_cost'];
  51 + }
  52 + Cache::set($cache_key, $data);
  53 + }
  54 + return $data;
  55 + }
  56 +
  57 +}