BaseAdvertLogic.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\v1\logic\tool\advertCost;
  3. use app\v1\logic\advert\AgentSiteLogic;
  4. use GuzzleHttp\Client;
  5. use support\think\Db;
  6. abstract class BaseAdvertLogic
  7. {
  8. protected string $dateTable = "media_cost";
  9. protected string $hourTable = "media_cost_hour";
  10. protected string $date;
  11. protected array $advertiserIds=[];
  12. protected array $siteMap=[];
  13. protected int $apiId=0;
  14. public function run($params=[])
  15. {
  16. // 重跑用,传数组
  17. if(!empty($params['date'])){
  18. $sDate = is_array($params['date']) ? $params['date'][0] : $params['date'];
  19. $eDate = !empty($params['date'][1]) ? $params['date'][1] : $params['date'][0];
  20. // 检查日期格式是否正确
  21. if(!isValidDate($sDate) || !isValidDate($eDate)){
  22. return json_encode(["status"=>"error", "msg"=>"日期格式不正确"], 256);
  23. }
  24. }else{
  25. $sDate = date('Y-m-d');
  26. $eDate = date('Y-m-d');
  27. }
  28. // 重跑用,传数组
  29. if(!empty($params['advertiser_ids'])){
  30. $this->advertiserIds = $params['advertiser_ids'];
  31. }
  32. // 重跑用,传int
  33. if(!empty($params['api_id'])){
  34. $this->apiId = $params['api_id'];
  35. }
  36. $this->siteMap = (new AgentSiteLogic())->getSiteAuth();
  37. for ($date = $sDate; $date <= $eDate; $date = date('Y-m-d', strtotime($date . '+1 day'))){
  38. $this->date = $date;
  39. try {
  40. $this->initStart();
  41. }catch (\Exception $e){
  42. return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
  43. }
  44. }
  45. $this->reRun();
  46. return json_encode(["status"=>"success", "msg"=>""], 256);
  47. }
  48. // 重跑
  49. protected function reRun(): void
  50. {
  51. if (date('H') == 8 && date('i') < 20) {
  52. $this->date = date('Y-m-d', strtotime("-1 days"));
  53. $this->initStart();
  54. }
  55. }
  56. abstract protected function initStart();
  57. }