169 lines
5.2 KiB
PHP
169 lines
5.2 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: zn
|
|
* Date: 2017/5/17
|
|
* Time: 18:11
|
|
*/
|
|
|
|
namespace common\models;
|
|
|
|
|
|
use common\services\LandService;
|
|
use common\utils\EnumUtil;
|
|
use common\utils\RedisKeyUtil;
|
|
use Yii;
|
|
use yii\behaviors\TimestampBehavior;
|
|
use yii\db\ActiveRecord;
|
|
|
|
/**
|
|
* 农田
|
|
* @property int $id
|
|
* @property int $farm_id 所属农场ID
|
|
* @property int $level 农田等级
|
|
* @property int $position 农田在农场中的位置
|
|
* @property int $crop_id 种植的植物ID,0未种植
|
|
* @property int $crop_start_time 种下农作物时间
|
|
* @property int $crop_gather_time 农作物收获时间
|
|
* @property int $is_ripe 是否成熟,1:成熟
|
|
* @property int $crop_num 可以收获的果实数量
|
|
* @property int $crop_min_num 保底产量
|
|
* @property int $has_dry 是否干旱;1:干旱;
|
|
* @property int $has_bug 是否有虫;1:有虫;
|
|
* @property int $has_grass 是否有草;1:有草;
|
|
* @property int $has_gather 是否收获 0.未收获 1.已收获
|
|
* @property int $disease_rand_time 上次病虫害随机时间
|
|
* @property int $is_mystery_seed 是否播种的神秘种子
|
|
* @property integer $created_at
|
|
* @property integer $updated_at
|
|
* @package common\models
|
|
*/
|
|
class Land extends ActiveRecord
|
|
{
|
|
public function behaviors()
|
|
{
|
|
return [
|
|
TimestampBehavior::className(),
|
|
];
|
|
}
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
['id', 'default', 'value' => 0],
|
|
|
|
['farm_id', 'required'],
|
|
['farm_id', 'exist', 'targetClass' => '\common\models\Farm', 'targetAttribute' => 'id'],
|
|
['farm_id', 'number', 'integerOnly' => true],
|
|
|
|
['level', 'default', 'value' => 0],
|
|
['level', 'number', 'integerOnly' => true, 'min' => 0, 'max' => EnumUtil::LAND_MAX_LEVEL],
|
|
|
|
['position', 'default', 'value' => 0],
|
|
['position', 'number', 'integerOnly' => true, 'min' => 0, 'max' => EnumUtil::LAND_MAX_POSITION],
|
|
|
|
['crop_id', 'default', 'value' => 0],
|
|
['crop_id', 'number', 'integerOnly' => true],
|
|
|
|
['crop_start_time', 'default', 'value' => 0],
|
|
['crop_gather_time', 'default', 'value' => 0],
|
|
|
|
['is_ripe', 'default', 'value' => 0],
|
|
['is_ripe', 'in', 'range' => [0, 1]],
|
|
|
|
['crop_num', 'default', 'value' => 0],
|
|
['crop_num', 'number', 'integerOnly' => true],
|
|
|
|
['crop_min_num', 'default', 'value' => 0],
|
|
|
|
['has_dry', 'default', 'value' => 0],
|
|
['has_dry', 'in', 'range' => [0, 1]],
|
|
|
|
['has_bug', 'default', 'value' => 0],
|
|
['has_bug', 'in', 'range' => [0, 1]],
|
|
|
|
['has_grass', 'default', 'value' => 0],
|
|
['has_grass', 'in', 'range' => [0, 1]],
|
|
|
|
['has_gather', 'default', 'value' => 0],
|
|
['has_gather', 'in', 'range' => [0, 1]],
|
|
|
|
['disease_rand_time', 'default', 'value' => 0],
|
|
|
|
['is_mystery_seed', 'default', 'value' => 0],
|
|
['is_mystery_seed', 'in', 'range' => [0, 1]],
|
|
|
|
['created_at', 'default', 'value' => 0],
|
|
['updated_at', 'default', 'value' => 0],
|
|
];
|
|
}
|
|
|
|
public static function findByFarmIDAndPosition($farmID, $position)
|
|
{
|
|
return self::findOne(['farm_id' => $farmID, 'position' => $position]);
|
|
}
|
|
|
|
public static function findAllByFarmID($farmID)
|
|
{
|
|
return self::findAll(['farm_id' => $farmID]);
|
|
}
|
|
|
|
/**
|
|
* 生成要发送给客户的数据
|
|
* @return array
|
|
*/
|
|
public function genClientData()
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'position' => $this->position,
|
|
'level' => $this->level,
|
|
'crop_id' => $this->crop_id,
|
|
'crop_start_time' => $this->crop_start_time,
|
|
'crop_gather_time' => $this->crop_gather_time,
|
|
'is_ripe' => $this->is_ripe,
|
|
'crop_num' => $this->crop_num,
|
|
'crop_min_num' => $this->crop_min_num,
|
|
'has_dry' => $this->has_dry,
|
|
'has_bug' => $this->has_bug,
|
|
'has_grass' => $this->has_grass,
|
|
'has_gather' => $this->has_gather,
|
|
'crop_phase' => LandService::getLandCropPhase($this),//当前农作物生长阶段
|
|
'is_mystery_seed' => $this->is_mystery_seed,
|
|
];
|
|
}
|
|
|
|
public function afterFind()
|
|
{
|
|
parent::afterFind();
|
|
$this->saveRedis();
|
|
}
|
|
|
|
public function afterSave($insert, $changedAttributes)
|
|
{
|
|
parent::afterSave($insert, $changedAttributes);
|
|
$this->saveRedis();
|
|
}
|
|
|
|
public function saveRedis()
|
|
{
|
|
$key = RedisKeyUtil::land($this->farm_id, $this->position);
|
|
Yii::$app->redis->setex($key, Yii::$app->params['redis_cache_time'], json_encode($this->toArray()));
|
|
Yii::$app->redis->addKey($key);
|
|
}
|
|
|
|
public static function findRedis($farmID, $position)
|
|
{
|
|
$jsonStrData = Yii::$app->redis->get(RedisKeyUtil::land($farmID, $position));
|
|
if ($jsonStrData == null || empty($jsonStrData)) return self::findByFarmIDAndPosition($farmID, $position);
|
|
|
|
$data = json_decode($jsonStrData, true);
|
|
$obj = new Land();
|
|
$obj->setAttributes($data, false);
|
|
$obj->setIsNewRecord(false);
|
|
return $obj;
|
|
}
|
|
|
|
|
|
}
|