LandStatusTempDto.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. use UCore\Dto\BaseDto;
  4. /**
  5. * 土地状态临时数据传输对象
  6. *
  7. * 用于存储和传输土地状态变更的临时数据
  8. */
  9. class LandStatusTempDto extends BaseDto
  10. {
  11. /**
  12. * 土地ID
  13. *
  14. * @var int
  15. */
  16. public int $landId;
  17. /**
  18. * 土地位置
  19. *
  20. * @var int
  21. */
  22. public int $position;
  23. /**
  24. * 土地类型
  25. *
  26. * @var int
  27. */
  28. public int $landType;
  29. /**
  30. * 旧状态
  31. *
  32. * @var int
  33. */
  34. public int $oldStatus;
  35. /**
  36. * 新状态
  37. *
  38. * @var int
  39. */
  40. public int $newStatus;
  41. /**
  42. * 作物ID(如果有)
  43. *
  44. * @var int|null
  45. */
  46. public ?int $cropId = null;
  47. /**
  48. * 种子配置ID(如果有)
  49. *
  50. * @var int|null
  51. */
  52. public ?int $seedId = null;
  53. /**
  54. * 更新时间戳
  55. *
  56. * @var int
  57. */
  58. public int $updatedAt;
  59. /**
  60. * 从缓存数据创建DTO对象
  61. *
  62. * @param mixed $cachedData 缓存数据
  63. * @return array 包含DTO对象的数组
  64. */
  65. public static function fromCache($cachedData): array
  66. {
  67. if (!is_array($cachedData)) {
  68. return [];
  69. }
  70. $result = [];
  71. foreach ($cachedData as $landId => $data) {
  72. if (is_array($data)) {
  73. $dto = new self();
  74. $dto->landId = $data['land_id'] ?? $landId;
  75. $dto->position = $data['position'] ?? 0;
  76. $dto->landType = $data['land_type'] ?? 0;
  77. $dto->oldStatus = $data['old_status'] ?? 0;
  78. $dto->newStatus = $data['new_status'] ?? 0;
  79. $dto->cropId = $data['crop_id'] ?? null;
  80. $dto->seedId = $data['seed_id'] ?? null;
  81. $dto->updatedAt = $data['updated_at'] ?? time();
  82. $result[$landId] = $dto;
  83. } elseif ($data instanceof self) {
  84. $result[$landId] = $data;
  85. }
  86. }
  87. return $result;
  88. }
  89. /**
  90. * 转换为数组
  91. *
  92. * @return array
  93. */
  94. public function toArray(): array
  95. {
  96. return [
  97. 'land_id' => $this->landId,
  98. 'position' => $this->position,
  99. 'land_type' => $this->landType,
  100. 'old_status' => $this->oldStatus,
  101. 'new_status' => $this->newStatus,
  102. 'crop_id' => $this->cropId,
  103. 'seed_id' => $this->seedId,
  104. 'updated_at' => $this->updatedAt,
  105. ];
  106. }
  107. }