PointsTransformer.php 2.0 KB

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