PointsTransformer.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Transformer\Points;
  3. class PointsTransformer
  4. {
  5. const TYPE_LABELS = [
  6. 'video' => '视频生成',
  7. 'video_refund' => '视频生成退款',
  8. ];
  9. /**
  10. * 积分流水列表
  11. *
  12. * @param $data
  13. * @return array
  14. */
  15. public function newBuildPointsRecords($data): array
  16. {
  17. return [
  18. 'meta' => getMeta($data),
  19. 'list' => $this->newEachPointsRecord($data),
  20. ];
  21. }
  22. /**
  23. * 单条积分流水
  24. *
  25. * @param $list
  26. * @return array
  27. */
  28. public function newEachPointsRecord($list): array
  29. {
  30. $result = [];
  31. if (empty($list)) return $result;
  32. foreach ($list as $item) {
  33. $chargeInfo = getProp($item, 'charge_info');
  34. if (is_string($chargeInfo)) {
  35. $chargeInfo = json_decode($chargeInfo, true);
  36. }
  37. $chargeInfo = is_array($chargeInfo) ? $chargeInfo : [];
  38. $type = getProp($item, 'type', '');
  39. $result[] = [
  40. 'id' => (int)getProp($item, 'id'),
  41. 'type' => $type,
  42. 'type_label' => self::TYPE_LABELS[$type] ?? $type,
  43. 'api_type' => getProp($item, 'api_type'),
  44. 'task_id' => (int)getProp($item, 'task_id'),
  45. 'charge_info' => [
  46. 'model' => getProp($chargeInfo, 'model'),
  47. 'video_resolution' => getProp($chargeInfo, 'video_resolution'),
  48. 'video_duration' => getProp($chargeInfo, 'video_duration'),
  49. 'ratio' => getProp($chargeInfo, 'ratio'),
  50. ],
  51. 'points_consumed' => (float)getProp($item, 'points_consumed', 0),
  52. 'points_before' => (float)getProp($item, 'points_before', 0),
  53. 'points_after' => (float)getProp($item, 'points_after', 0),
  54. 'tokens_consumed' => (int)getProp($item, 'tokens_consumed', 0),
  55. 'remark' => getProp($item, 'remark'),
  56. 'created_at' => getProp($item, 'created_at'),
  57. ];
  58. }
  59. return $result;
  60. }
  61. }