| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- namespace App\Transformer\Points;
- use App\Libs\PointsDisplay;
- class PointsTransformer
- {
- const TYPE_LABELS = [
- 'video' => '视频生成',
- 'video_refund' => '视频生成退款',
- 'image' => '图片生成',
- 'image_refund' => '图片生成退款',
- 'chat' => 'AI对话',
- 'chat_refund' => 'AI对话退款',
- 'system' => '平台发放',
- 'company' => '组织发放',
- ];
- /**
- * 积分流水列表
- *
- * @param $data
- * @return array
- */
- public function newBuildPointsRecords($data): array
- {
- return [
- 'meta' => getMeta($data),
- 'list' => $this->newEachPointsRecord($data),
- ];
- }
- /**
- * 单条积分流水
- *
- * 模型名统一做展示映射(隐藏百度/快快等供应商字样);
- * 分辨率按类型展示:视频 480p/720p/1080p/4k 与 超分720p/超分1080p,图片 实际宽*高,其余为空。
- * 名称字段只返回一个 anime_name:动漫名优先,其次剧本名(与动漫/剧本无关时为空)。
- * 模型名保持原键名 charge_info.model 不变,值改为展示名(隐藏供应商字样),前端无需调整取数位置。
- *
- * @param $list
- * @return array
- */
- public function newEachPointsRecord($list): array
- {
- $result = [];
- if (empty($list)) return $result;
- foreach ($list as $item) {
- $chargeInfo = getProp($item, 'charge_info');
- if (is_string($chargeInfo)) {
- $chargeInfo = json_decode($chargeInfo, true);
- }
- $chargeInfo = is_array($chargeInfo) ? $chargeInfo : [];
- $type = getProp($item, 'type', '');
- $apiType = (string)getProp($item, 'api_type', '');
- $rawModel = (string)getProp($chargeInfo, 'model', '');
- $modelName = PointsDisplay::modelName($rawModel);
- $resolution = PointsDisplay::resolution($type, $chargeInfo);
- // 名称只保留一个:动漫名优先,其次剧本名
- $animeName = trim((string)getProp($item, 'anime_name', ''));
- $scriptName = trim((string)getProp($item, 'script_name', ''));
- $displayName = $animeName !== '' ? $animeName : $scriptName;
- // 展示层隐藏供应商字样(库中原始 charge_info 不变)
- if ($rawModel !== '' && $modelName !== '') {
- $chargeInfo['model'] = $modelName;
- }
- $pointsConsumed = (float)getProp($item, 'points_consumed', 0);
- if ($pointsConsumed > 0) {
- // 消耗:带负号
- $pointsChange = '-' . $pointsConsumed;
- } elseif ($pointsConsumed < 0) {
- // 赠送/入账:带正号
- $pointsChange = '+' . abs($pointsConsumed);
- } else {
- $pointsChange = '0';
- }
- $result[] = [
- 'id' => (int)getProp($item, 'id'),
- 'type' => $type,
- 'type_label' => self::TYPE_LABELS[$type] ?? $type,
- 'account' => (string)getProp($item, 'account', ''),
- 'api_type' => $apiType,
- 'task_id' => (int)getProp($item, 'task_id'),
- 'resolution' => $resolution,
- 'anime_id' => (int)getProp($item, 'anime_id', 0),
- 'script_id' => (int)getProp($item, 'script_id', 0),
- 'anime_name' => $displayName,
- 'charge_info' => $chargeInfo,
- 'points_consumed' => $pointsConsumed,
- 'points_change' => $pointsChange,
- 'points_before' => (float)getProp($item, 'points_before', 0),
- 'points_after' => (float)getProp($item, 'points_after', 0),
- 'tokens_consumed' => (int)getProp($item, 'tokens_consumed', 0),
- 'remark' => getProp($item, 'remark'),
- 'created_at' => transDate(getProp($item, 'created_at')),
- ];
- }
- return $result;
- }
- }
|