FeedLogModel.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * 信息流滑动日志表
  4. * User: easyboom
  5. * Date: 2019/1/17
  6. * Time: 下午4:09
  7. */
  8. namespace App\Models\Log;
  9. use App\Models\Model;
  10. use App\Models\PartnerModel;
  11. use Illuminate\Database\Eloquent\ModelNotFoundException;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Schema;
  14. /**
  15. * * Class FeedLogModel
  16. * * @package App\Models\Log
  17. *
  18. * @property int $id
  19. * @property \Illuminate\Support\Carbon $created_at
  20. * @property \Illuminate\Support\Carbon $updated_at
  21. * @property int $uid 用户id
  22. * @property int $partner_id 卡片id
  23. * @property int $ismock 是否模拟
  24. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel newModelQuery()
  25. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel newQuery()
  26. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel query()
  27. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereCreatedAt($value)
  28. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereId($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereIsmock($value)
  30. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel wherePartnerId($value)
  31. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereUid($value)
  32. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereUpdatedAt($value)
  33. * @mixin \Eloquent
  34. */
  35. class FeedLogModel extends Model
  36. {
  37. protected $connection = "mysql_fenpeiduixiang";
  38. protected $table = "kdgx_fpdx_feed_log";
  39. public const TABLE = "kdgx_fpdx_feed_log";
  40. protected static $index = "kdgx_fpdx_feed_log";
  41. protected $dateFormat = 'U';
  42. protected $fillable = [
  43. 'uid',
  44. 'partner_id'
  45. ];
  46. public function uidPartner()
  47. {
  48. return $this->belongsTo(PartnerModel::class, 'uid', 'uid');
  49. }
  50. public static function boot()
  51. {
  52. parent::boot();
  53. self::creating(function ($model) {
  54. try {
  55. /** @var FeedLogModel $model */
  56. $partner = PartnerModel::findOrFail($model->partner_id);
  57. $partner->increment("feed_cnt", 1);
  58. $partner->increment("last_three_day_feed", 1);
  59. } catch (ModelNotFoundException $exception) {
  60. // 放弃本次不创建
  61. return false;
  62. }
  63. });
  64. }
  65. /**
  66. * 设置分割值
  67. * @param int $partnerId
  68. * @return FeedLogModel
  69. */
  70. public static function PID(int $partnerId): FeedLogModel
  71. {
  72. $instance = new static();
  73. $instance->setSliceKey($partnerId);
  74. return $instance;
  75. }
  76. public function getTable()
  77. {
  78. if (!Schema::hasTable($this->table)) {
  79. DB::update('create table ' . $this->table . ' like ' . self::TABLE);
  80. }
  81. return parent::getTable();
  82. }
  83. /**
  84. * Create a new instance of the given model.
  85. *
  86. * @param array $attributes
  87. * @param bool $exists
  88. * @return Model
  89. */
  90. public function newInstance($attributes = [], $exists = false): Model
  91. {
  92. $model = parent::newInstance($attributes, $exists);
  93. // if ($model->getAttribute('partner_id')) {
  94. // $this->sliceKey = $model->getAttribute('partner_id');
  95. // }
  96. $model->setSliceKey($this->sliceKey);
  97. return $model;
  98. }
  99. }