| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace App\Module\Game\Dtos;
- use UCore\Dto\BaseDto;
- /**
- * 土地变更临时数据传输对象
- *
- * 用于存储和传输所有类型的土地变更临时数据,包括:
- * - 土地类型变更(创建、升级)
- * - 土地状态变更(种植、收获等)
- */
- class LandChangeTempDto extends BaseDto
- {
- /**
- * 土地ID
- *
- * @var int
- */
- public int $landId;
- /**
- * 土地类型
- *
- * @var int
- */
- public int $landType = 0;
- public int $position = 0;
- /**
- * 旧土地类型(仅在类型变更时有值)
- *
- * @var int|null
- */
- public ?int $oldType = null;
- /**
- * 新土地类型(仅在类型变更时有值)
- *
- * @var int|null
- */
- public ?int $newType = null;
- /**
- * 旧状态(仅在状态变更时有值)
- *
- * @var int|null
- */
- public ?int $oldStatus = null;
- /**
- * 新状态(仅在状态变更时有值)
- *
- * @var int|null
- */
- public ?int $newStatus = null;
- /**
- * 作物ID(如果有)
- *
- * @var int|null
- */
- public ?int $cropId = null;
- /**
- * 种子配置ID(如果有)
- *
- * @var int|null
- */
- public ?int $seedId = null;
- /**
- * 变更类型:type=类型变更,status=状态变更
- *
- * @var string
- */
- public string $changeType = 'type';
- /**
- * 更新时间戳
- *
- * @var int
- */
- public int $updatedAt;
- /**
- * 从缓存数据创建DTO对象
- *
- * @param mixed $cachedData 缓存数据
- * @return array 包含DTO对象的数组
- */
- public static function fromCache($cachedData): array
- {
- if (!is_array($cachedData)) {
- return [];
- }
- $result = [];
- foreach ($cachedData as $landId => $data) {
- if (is_array($data)) {
- $dto = new self();
- $dto->landId = $data['land_id'] ?? $landId;
- $dto->position = $data['position'] ?? 0;
- $dto->landType = $data['land_type'] ?? 0;
- $dto->oldType = $data['old_type'] ?? null;
- $dto->newType = $data['new_type'] ?? null;
- $dto->oldStatus = $data['old_status'] ?? null;
- $dto->newStatus = $data['new_status'] ?? null;
- $dto->cropId = $data['crop_id'] ?? null;
- $dto->seedId = $data['seed_id'] ?? null;
- $dto->changeType = $data['change_type'] ?? 'type';
- $dto->updatedAt = $data['updated_at'] ?? time();
- $result[$landId] = $dto;
- } elseif ($data instanceof self) {
- $result[$landId] = $data;
- }
- }
- return $result;
- }
- /**
- * 转换为数组
- *
- * @return array
- */
- public function toArray(): array
- {
- return [
- 'land_id' => $this->landId,
- 'position' => $this->position,
- 'land_type' => $this->landType,
- 'old_type' => $this->oldType,
- 'new_type' => $this->newType,
- 'old_status' => $this->oldStatus,
- 'new_status' => $this->newStatus,
- 'crop_id' => $this->cropId,
- 'seed_id' => $this->seedId,
- 'change_type' => $this->changeType,
- 'updated_at' => $this->updatedAt,
- ];
- }
- /**
- * 判断是否为类型变更
- *
- * @return bool
- */
- public function isTypeChange(): bool
- {
- return $this->changeType === 'type';
- }
- /**
- * 判断是否为状态变更
- *
- * @return bool
- */
- public function isStatusChange(): bool
- {
- return $this->changeType === 'status';
- }
- }
|