102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: zn
|
||
* Date: 2017/5/31
|
||
* Time: 15:37
|
||
*/
|
||
|
||
namespace common\models;
|
||
|
||
use common\utils\EnumUtil;
|
||
use common\utils\RedisKeyUtil;
|
||
use Yii;
|
||
use yii\behaviors\TimestampBehavior;
|
||
use yii\db\ActiveRecord;
|
||
|
||
/**
|
||
* 农场玩家之间的互动,比如偷菜、放虫、除虫等
|
||
* Class Interaction
|
||
* @property int $id
|
||
* @property int $from_user_id 发起操作的用ID
|
||
* @property int $be_user_id 受害者的ID,被偷菜等
|
||
* @property int $land_create_time 被操作者农田创建时间,同一块农田同一颗植物只能偷一次,和农田位置一起标记这颗植物的唯一性
|
||
* @property int $position 农田位置
|
||
* @property int $type 操作类型;0:偷菜;1:放虫;2:放草;3:浇水;4:除虫;5:除草;
|
||
* @property int $num 偷取数量
|
||
* @property int $created_at
|
||
* @property int $updated_at
|
||
*/
|
||
class Interaction extends ActiveRecord
|
||
{
|
||
public function behaviors()
|
||
{
|
||
return [
|
||
TimestampBehavior::className(),
|
||
];
|
||
}
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
['from_user_id', 'exist', 'targetClass' => '\common\models\User', 'targetAttribute' => 'id'],
|
||
['be_user_id', 'exist', 'targetClass' => '\common\models\User', 'targetAttribute' => 'id'],
|
||
|
||
['land_create_time', 'exist', 'targetClass' => '\common\models\Land', 'targetAttribute' => 'crop_start_time'],
|
||
|
||
['position', 'number', 'integerOnly' => true, 'min' => 0, 'max' => EnumUtil::LAND_MAX_POSITION],
|
||
|
||
['type', 'in', 'range' => EnumUtil::INTERACTION_TYPE_LIST],
|
||
|
||
['num', 'default', 'value' => 0],
|
||
['num', 'number', 'integerOnly' => true, 'min' => 0],
|
||
|
||
['created_at', 'default', 'value' => 0],
|
||
['updated_at', 'default', 'value' => 0],
|
||
];
|
||
}
|
||
|
||
public static function findBy($from_user_id, $be_user_id, $land_create_time, $position, $type)
|
||
{
|
||
return self::findOne([
|
||
'from_user_id' => $from_user_id,
|
||
'be_user_id' => $be_user_id,
|
||
'land_create_time' => $land_create_time,
|
||
'position' => $position,
|
||
'type' => $type,
|
||
]);
|
||
}
|
||
|
||
public function afterFind()
|
||
{
|
||
parent::afterFind();
|
||
$this->saveRedis();
|
||
}
|
||
|
||
public function afterSave($insert, $changedAttributes)
|
||
{
|
||
parent::afterSave($insert, $changedAttributes);
|
||
$this->saveRedis();
|
||
}
|
||
|
||
public function saveRedis()
|
||
{
|
||
$key = RedisKeyUtil::interaction($this->from_user_id, $this->be_user_id, $this->land_create_time, $this->position, $this->type);
|
||
Yii::$app->redis->setex($key, Yii::$app->params['redis_cache_time'], json_encode($this->toArray()));
|
||
Yii::$app->redis->addKey($key);
|
||
}
|
||
|
||
public static function findRedis($from_user_id, $be_user_id, $land_create_time, $position, $type)
|
||
{
|
||
|
||
$jsonStrData = Yii::$app->redis->get(RedisKeyUtil::interaction($from_user_id, $be_user_id, $land_create_time, $position, $type));
|
||
if ($jsonStrData == null || empty($jsonStrData)) return self::findBy($from_user_id, $be_user_id, $land_create_time, $position, $type);
|
||
|
||
$data = json_decode($jsonStrData, true);
|
||
$obj = new Interaction();
|
||
$obj->setAttributes($data, false);
|
||
$obj->setIsNewRecord(false);
|
||
return $obj;
|
||
}
|
||
|
||
} |