LandChangeTempDto.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App\Module\Game\Dtos;
  3. use UCore\Dto\BaseDto;
  4. /**
  5. * 土地变更临时数据传输对象
  6. *
  7. * 用于存储和传输所有类型的土地变更临时数据,包括:
  8. * - 土地类型变更(创建、升级)
  9. * - 土地状态变更(种植、收获等)
  10. */
  11. class LandChangeTempDto extends BaseDto
  12. {
  13. /**
  14. * 土地ID
  15. *
  16. * @var int
  17. */
  18. public int $landId;
  19. /**
  20. * 土地类型
  21. *
  22. * @var int
  23. */
  24. public int $landType = 0;
  25. public int $position = 0;
  26. /**
  27. * 旧土地类型(仅在类型变更时有值)
  28. *
  29. * @var int|null
  30. */
  31. public ?int $oldType = null;
  32. /**
  33. * 新土地类型(仅在类型变更时有值)
  34. *
  35. * @var int|null
  36. */
  37. public ?int $newType = null;
  38. /**
  39. * 旧状态(仅在状态变更时有值)
  40. *
  41. * @var int|null
  42. */
  43. public ?int $oldStatus = null;
  44. /**
  45. * 新状态(仅在状态变更时有值)
  46. *
  47. * @var int|null
  48. */
  49. public ?int $newStatus = null;
  50. /**
  51. * 作物ID(如果有)
  52. *
  53. * @var int|null
  54. */
  55. public ?int $cropId = null;
  56. /**
  57. * 种子配置ID(如果有)
  58. *
  59. * @var int|null
  60. */
  61. public ?int $seedId = null;
  62. /**
  63. * 变更类型:type=类型变更,status=状态变更
  64. *
  65. * @var string
  66. */
  67. public string $changeType = 'type';
  68. /**
  69. * 更新时间戳
  70. *
  71. * @var int
  72. */
  73. public int $updatedAt;
  74. /**
  75. * 从缓存数据创建DTO对象
  76. *
  77. * @param mixed $cachedData 缓存数据
  78. * @return array 包含DTO对象的数组
  79. */
  80. public static function fromCache($cachedData): array
  81. {
  82. if (!is_array($cachedData)) {
  83. return [];
  84. }
  85. $result = [];
  86. foreach ($cachedData as $landId => $data) {
  87. if (is_array($data)) {
  88. $dto = new self();
  89. $dto->landId = $data['land_id'] ?? $landId;
  90. $dto->position = $data['position'] ?? 0;
  91. $dto->landType = $data['land_type'] ?? 0;
  92. $dto->oldType = $data['old_type'] ?? null;
  93. $dto->newType = $data['new_type'] ?? null;
  94. $dto->oldStatus = $data['old_status'] ?? null;
  95. $dto->newStatus = $data['new_status'] ?? null;
  96. $dto->cropId = $data['crop_id'] ?? null;
  97. $dto->seedId = $data['seed_id'] ?? null;
  98. $dto->changeType = $data['change_type'] ?? 'type';
  99. $dto->updatedAt = $data['updated_at'] ?? time();
  100. $result[$landId] = $dto;
  101. } elseif ($data instanceof self) {
  102. $result[$landId] = $data;
  103. }
  104. }
  105. return $result;
  106. }
  107. /**
  108. * 转换为数组
  109. *
  110. * @return array
  111. */
  112. public function toArray(): array
  113. {
  114. return [
  115. 'land_id' => $this->landId,
  116. 'position' => $this->position,
  117. 'land_type' => $this->landType,
  118. 'old_type' => $this->oldType,
  119. 'new_type' => $this->newType,
  120. 'old_status' => $this->oldStatus,
  121. 'new_status' => $this->newStatus,
  122. 'crop_id' => $this->cropId,
  123. 'seed_id' => $this->seedId,
  124. 'change_type' => $this->changeType,
  125. 'updated_at' => $this->updatedAt,
  126. ];
  127. }
  128. /**
  129. * 判断是否为类型变更
  130. *
  131. * @return bool
  132. */
  133. public function isTypeChange(): bool
  134. {
  135. return $this->changeType === 'type';
  136. }
  137. /**
  138. * 判断是否为状态变更
  139. *
  140. * @return bool
  141. */
  142. public function isStatusChange(): bool
  143. {
  144. return $this->changeType === 'status';
  145. }
  146. }