LandChangeTempDto.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 $position;
  25. /**
  26. * 土地类型
  27. *
  28. * @var int
  29. */
  30. public int $landType;
  31. /**
  32. * 旧土地类型(仅在类型变更时有值)
  33. *
  34. * @var int|null
  35. */
  36. public ?int $oldType = null;
  37. /**
  38. * 新土地类型(仅在类型变更时有值)
  39. *
  40. * @var int|null
  41. */
  42. public ?int $newType = null;
  43. /**
  44. * 旧状态(仅在状态变更时有值)
  45. *
  46. * @var int|null
  47. */
  48. public ?int $oldStatus = null;
  49. /**
  50. * 新状态(仅在状态变更时有值)
  51. *
  52. * @var int|null
  53. */
  54. public ?int $newStatus = null;
  55. /**
  56. * 作物ID(如果有)
  57. *
  58. * @var int|null
  59. */
  60. public ?int $cropId = null;
  61. /**
  62. * 种子配置ID(如果有)
  63. *
  64. * @var int|null
  65. */
  66. public ?int $seedId = null;
  67. /**
  68. * 变更类型:type=类型变更,status=状态变更
  69. *
  70. * @var string
  71. */
  72. public string $changeType;
  73. /**
  74. * 更新时间戳
  75. *
  76. * @var int
  77. */
  78. public int $updatedAt;
  79. /**
  80. * 从缓存数据创建DTO对象
  81. *
  82. * @param mixed $cachedData 缓存数据
  83. * @return array 包含DTO对象的数组
  84. */
  85. public static function fromCache($cachedData): array
  86. {
  87. if (!is_array($cachedData)) {
  88. return [];
  89. }
  90. $result = [];
  91. foreach ($cachedData as $landId => $data) {
  92. if (is_array($data)) {
  93. $dto = new self();
  94. $dto->landId = $data['land_id'] ?? $landId;
  95. $dto->position = $data['position'] ?? 0;
  96. $dto->landType = $data['land_type'] ?? 0;
  97. $dto->oldType = $data['old_type'] ?? null;
  98. $dto->newType = $data['new_type'] ?? null;
  99. $dto->oldStatus = $data['old_status'] ?? null;
  100. $dto->newStatus = $data['new_status'] ?? null;
  101. $dto->cropId = $data['crop_id'] ?? null;
  102. $dto->seedId = $data['seed_id'] ?? null;
  103. $dto->changeType = $data['change_type'] ?? 'type';
  104. $dto->updatedAt = $data['updated_at'] ?? time();
  105. $result[$landId] = $dto;
  106. } elseif ($data instanceof self) {
  107. $result[$landId] = $data;
  108. }
  109. }
  110. return $result;
  111. }
  112. /**
  113. * 转换为数组
  114. *
  115. * @return array
  116. */
  117. public function toArray(): array
  118. {
  119. return [
  120. 'land_id' => $this->landId,
  121. 'position' => $this->position,
  122. 'land_type' => $this->landType,
  123. 'old_type' => $this->oldType,
  124. 'new_type' => $this->newType,
  125. 'old_status' => $this->oldStatus,
  126. 'new_status' => $this->newStatus,
  127. 'crop_id' => $this->cropId,
  128. 'seed_id' => $this->seedId,
  129. 'change_type' => $this->changeType,
  130. 'updated_at' => $this->updatedAt,
  131. ];
  132. }
  133. /**
  134. * 判断是否为类型变更
  135. *
  136. * @return bool
  137. */
  138. public function isTypeChange(): bool
  139. {
  140. return $this->changeType === 'type';
  141. }
  142. /**
  143. * 判断是否为状态变更
  144. *
  145. * @return bool
  146. */
  147. public function isStatusChange(): bool
  148. {
  149. return $this->changeType === 'status';
  150. }
  151. }