125 lines
3.2 KiB
PHP
125 lines
3.2 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/5/17
|
|
* Time: 17:24
|
|
*/
|
|
|
|
namespace common\models;
|
|
|
|
|
|
use common\utils\RedisKeyUtil;
|
|
use Yii;
|
|
use yii\behaviors\TimestampBehavior;
|
|
use yii\db\ActiveRecord;
|
|
|
|
/**
|
|
* 农场
|
|
* @property integer $id
|
|
* @property integer $user_id 所属用户ID
|
|
* @property integer $level 等级
|
|
* @property integer $bg_image_id 背景物品ID
|
|
* @property integer $created_at
|
|
* @property integer $updated_at
|
|
*/
|
|
class Farm extends ActiveRecord
|
|
{
|
|
public function behaviors()
|
|
{
|
|
return [
|
|
TimestampBehavior::className(),
|
|
];
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
['id', 'default', 'value' => 0],
|
|
|
|
['user_id', 'required'],
|
|
['user_id', 'exist', 'targetClass' => '\common\models\User', 'targetAttribute' => 'id'],
|
|
['user_id', 'number', 'integerOnly' => true],
|
|
|
|
['level', 'default', 'value' => 1],
|
|
['level', 'number', 'integerOnly' => true, 'min' => 1, 'max' => 12],
|
|
|
|
['bg_image_id', 'default', 'value' => 0],
|
|
['bg_image_id', 'number', 'integerOnly' => true],
|
|
|
|
['created_at', 'default', 'value' => 0],
|
|
['updated_at', 'default', 'value' => 0],
|
|
];
|
|
}
|
|
|
|
public static function findByUserID($userID)
|
|
{
|
|
return self::findOne(['user_id' => $userID]);
|
|
}
|
|
|
|
public function attributeLabels()
|
|
{
|
|
$data = parent::attributeLabels();
|
|
$data['level'] = '等级';
|
|
$data['bg_image_id'] = '背景图片ID';
|
|
return $data;
|
|
}
|
|
|
|
|
|
/**
|
|
* 生成要发送给客户的数据
|
|
* @return array
|
|
*/
|
|
public function genClientData()
|
|
{
|
|
$clientData = [
|
|
'farm_level' => $this->level,
|
|
'bg_image_id' => $this->bg_image_id,
|
|
'put_bug_num' => 0,//今日放虫次数
|
|
'put_grass_num' => 0,//今日放草次数
|
|
'watering_num' => 0,//今日浇水次数
|
|
'seed_num' => 0,//今日播种次数
|
|
];
|
|
|
|
//所有田地
|
|
$landListClientData = [];
|
|
$landList = Land::findAll(['farm_id' => $this->id]);
|
|
foreach ($landList as $land)
|
|
array_push($landListClientData, $land->genClientData());
|
|
|
|
$clientData['landList'] = $landListClientData;
|
|
|
|
return $clientData;
|
|
}
|
|
|
|
public function afterFind()
|
|
{
|
|
parent::afterFind();
|
|
$this->saveRedis();
|
|
}
|
|
|
|
public function afterSave($insert, $changedAttributes)
|
|
{
|
|
parent::afterSave($insert, $changedAttributes);
|
|
$this->saveRedis();
|
|
}
|
|
|
|
public function saveRedis()
|
|
{
|
|
$key = RedisKeyUtil::farmUserID($this->user_id);
|
|
Yii::$app->redis->setex($key, Yii::$app->params['redis_cache_time'], json_encode($this->toArray()));
|
|
Yii::$app->redis->addKey($key);
|
|
}
|
|
|
|
public static function findRedis($userID)
|
|
{
|
|
$jsonStrData = Yii::$app->redis->get(RedisKeyUtil::farmUserID($userID));
|
|
if ($jsonStrData == null || empty($jsonStrData)) return self::findOne(['user_id' => $userID]);
|
|
|
|
$data = json_decode($jsonStrData, true);
|
|
$obj = new Farm();
|
|
$obj->setAttributes($data, false);
|
|
$obj->setIsNewRecord(false);
|
|
return $obj;
|
|
}
|
|
} |