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; } }