<?php

/**
 * 信息流滑动日志表
 * User: easyboom
 * Date: 2019/1/17
 * Time: 下午4:09
 */

namespace App\Models\Log;

use App\Models\Model;
use App\Models\PartnerModel;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
 * * Class FeedLogModel
 *  * @package App\Models\Log
 *
 * @property int $id
 * @property \Illuminate\Support\Carbon $created_at
 * @property \Illuminate\Support\Carbon $updated_at
 * @property int $uid 用户id
 * @property int $partner_id 卡片id
 * @property int $ismock 是否模拟
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereIsmock($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel wherePartnerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Log\FeedLogModel whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class FeedLogModel extends Model
{
    protected $connection = "mysql_fenpeiduixiang";

    protected $table = "kdgx_fpdx_feed_log";

    public const TABLE = "kdgx_fpdx_feed_log";

    protected static $index = "kdgx_fpdx_feed_log";

    protected $dateFormat = 'U';

    protected $fillable = [
        'uid',
        'partner_id'
    ];

    public function uidPartner()
    {
        return $this->belongsTo(PartnerModel::class, 'uid', 'uid');
    }

    public static function boot()
    {
        parent::boot();
        self::creating(function ($model) {
            try {
                /** @var FeedLogModel $model */
                $partner = PartnerModel::findOrFail($model->partner_id);
                $partner->increment("feed_cnt", 1);
                $partner->increment("last_three_day_feed", 1);
            } catch (ModelNotFoundException $exception) {
                // 放弃本次不创建
                return false;
            }
        });
    }

    /**
     * 设置分割值
     * @param  int  $partnerId
     * @return FeedLogModel
     */
    public static function PID(int $partnerId): FeedLogModel
    {
        $instance = new static();
        $instance->setSliceKey($partnerId);
        return $instance;
    }

    public function getTable()
    {
        if (!Schema::hasTable($this->table)) {
            DB::update('create table ' . $this->table . ' like ' . self::TABLE);
        }
        return parent::getTable();
    }


    /**
     * Create a new instance of the given model.
     *
     * @param  array  $attributes
     * @param  bool  $exists
     * @return Model
     */
    public function newInstance($attributes = [], $exists = false): Model
    {
        $model = parent::newInstance($attributes, $exists);
//        if ($model->getAttribute('partner_id')) {
//            $this->sliceKey = $model->getAttribute('partner_id');
//        }
        $model->setSliceKey($this->sliceKey);
        return $model;
    }
}
