抖音小程序

AdPlacement.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Models;
  3. use App\Log;
  4. use Illuminate\Database\Eloquent\Model;
  5. class AdPlacement extends Model
  6. {
  7. protected $table = 'ad_placement';
  8. public $timestamps = false;
  9. protected static $unguarded = true;
  10. CONST SYNC_AD_PLACEMENT_LIST = 'DouApp::syncAdPlacementList';
  11. CONST AD_PLACEMENT_TYPE = [
  12. 1 => 'Banner广告',
  13. 2 => '视频激励广告',
  14. 3 => '信息流广告',
  15. 4 => '插屏广告',
  16. 5 => '视频前贴片广告',
  17. 6 => '视频后贴片广告'
  18. ];
  19. public static function saveData($appId, $adPlacementList) {
  20. \DB::begintransaction();
  21. foreach($adPlacementList as $adPlacementInfo) {
  22. $con = [
  23. 'app_id' => $appId,
  24. 'ad_placement_id' => $adPlacementInfo['ad_placement_id'] ?? '',
  25. ];
  26. $attr = [
  27. 'ad_placement_name' => $adPlacementInfo['ad_placement_name'] ?? '',
  28. 'ad_placement_type' => $adPlacementInfo['ad_placement_type'] ?? '',
  29. 'status' => $adPlacementInfo['status'],
  30. ];
  31. $res = self::query()->updateOrCreate($con, $attr);
  32. if(!$res) {
  33. Log::logError('保存广告位列表异常', ['app_id' => $appId, 'ad_placement_list' => $adPlacementList], 'adPlacement');
  34. \DB::rollback();
  35. return false;
  36. }
  37. }
  38. \DB::commit();
  39. return true;
  40. }
  41. public static function searchData($search, $page, $pageSize) {
  42. $model = self::query()->where('enable', 1);
  43. if(isset($search['app_id']) && !empty($search['app_id'])) {
  44. $model->where('app_id', $search['app_id']);
  45. }
  46. if(isset($search['status']) && is_numeric($search['status'])) {
  47. $model = $model->where('status', $search['status']);
  48. }
  49. if(isset($search['type']) && !empty($search['type'])) {
  50. $model = $model->where('ad_placement_type', $search['type']);
  51. }
  52. if(isset($search['keyword']) && !empty($search['keyword'])) {
  53. $model = $model->where('ad_placement_name', 'like', '%'.$search['keyword'].'%');
  54. }
  55. $count = $model->count();
  56. $data = $model->selectRaw('id, ad_placement_id, ad_placement_type as type, ad_placement_name, status,'
  57. .' app_id')->orderBy('sort', 'desc')->orderBy('id', 'desc')
  58. ->offset(($page - 1) * $pageSize)->limit($pageSize)->get();
  59. return [$data, $count];
  60. }
  61. public static function updateData($appId, $adPlacementId, $status) {
  62. return self::query()->where('app_id', $appId)->where('ad_placement_id', $adPlacementId)
  63. ->where('enable', 1)->update(['status' => $status]);
  64. }
  65. }