<?php

namespace App\Transformer\Order;

use App\Cache\StatisticCache;
use App\Consts\BaseConst;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Vinkla\Hashids\Facades\Hashids;

class OrderTransformer
{
    // 用户列表
    public function newBuildUserList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachUserList($data),
        ];
    }

    private function newEachUserList($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            $result[] = [
                'uid'                       => getProp($item, 'id'),
                'nickname'                  => getProp($item, 'nickname'),
                'channel_id'                => getProp($item, 'distribution_channel_id'),
                'register_ip'               => getProp($item, 'register_ip'),
                'send_order_id'             => getProp($item, 'send_order_id'),
                'vip_limit_date'            => transDate(getProp($item, 'vip_limit_date')),
                'balance'                   => getProp($item, 'balance'),
                'charge_balance'            => getProp($item, 'charge_balance'),
                'reward_balance'            => getProp($item, 'reward_balance'),
                'invite_code'               => getProp($item, 'invite_code'),
                'created_at'                => transDate(getProp($item, 'created_at')),
                'bind_send_order_time'      => transDate(getProp($item, 'bind_send_order_time')),
//                'trade_no'                  => getProp($item, 'trade_no')
            ];
        }

        return $result;
    }

    // 派单列表
    public function newBuildSendOrderList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newSendOrderList($data),
        ];
    }

    private function newSendOrderList($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        foreach ($list as $item) {
            $result[] = [
                'send_order_id'             => getProp($item, 'id'),
                'send_order_name'           => getProp($item, 'name'),
                'channel_id'                => getProp($item, 'distribution_channel_id'),
                'book_name'                 => getProp($item, 'book_name'),
                'chapter_name'              => getProp($item, 'chapter_name'),
                'report_percent'            => getProp($item, 'report_percent'),
                'report_receive_num'        => getProp($item, 'report_receive_num'),
                'report_post_num'           => getProp($item, 'report_post_num'),
                'created_at'                => transDate(getProp($item, 'created_at')),
            ];
        }

        return $result;
    }


    // 订单列表
    public function newBuildOrderList($data): array
    {
        return [
            'meta' => getMeta($data),
            'list'  => $this->newEachOrderList($data),
        ];
    }

    private function newEachOrderList($list): array
    {
        $result = [];
        if (empty($list)) return $result;

        $order_status_arr = [
            'UNPAID'    => '未支付',
            'PAID'      => '支付成功',
            'FAIL'      => '支付失败',
            'REFUND'    => '已退款'
        ];

        $oder_type_arr = [
            'RECHARGE'  => '书币充值',
            'WEEK'      => '周卡',
            'MONTH'     => '月卡',
            'QUARTER'   => '季卡',
            'HALF_YEAR' => '半年卡',
            'YEAR'      => '年卡'
        ];

        foreach ($list as $item) {
            $remark           = getProp($item, 'remark');
            $remark           = getProp($item, 'id') && $remark == '' ? '回传成功' : $remark;
            if (!getProp($item, 'report_id')) $remark = '无匹配广告计划id';
            $order_status     = isset($order_status_arr[getProp($item, 'status')]) ? $order_status_arr[getProp($item, 'status')] : '未知状态';
            $order_type     = isset($oder_type_arr[getProp($item, 'order_type')]) ? $oder_type_arr[getProp($item, 'order_type')] : '未知订单类型';

            switch (mb_substr(getProp($item, 'transaction_id'), 0, 2)) {
                case '42':
                    $pay_type = '微信';
                    break;
                case '20':
                    $pay_type = '支付宝';
                    break;
                case 'TP':
                    $pay_type = '抖音';
                    break;
                default:
                    $pay_type = '未知支付方式';
                    break;
            }

            $result[] = [
                'order_id'                  => getProp($item, 'id'),
                'trade_no'                  => getProp($item, 'trade_no'),
                'uid'                       => getProp($item, 'uid'),
                'register_time'             => transDate(getProp($item, 'register_time')),
                'bind_send_order_time'      => transDate(getProp($item, 'bind_send_order_time')),
                'price'                     => getProp($item, 'price'),
                'order_status'              => $order_status,
                'order_type'                => $order_type,
                'created_at'                => transDate(getProp($item, 'created_at')),
                'pay_time'                  => transDate(getProp($item, 'pay_time')),
                'channel_id'                => getProp($item, 'distribution_channel_id'),
                'send_order_id'             => getProp($item, 'send_order_id'),
                'advertiser_id'             => getProp($item, 'advertiserid'),
                'promotion_id'              => getProp($item, 'promotionid'),
                'report_result'             => $remark,
                'report_time'               => transDate(getProp($item, 'report_time')),
                'pay_num'                   => getProp($item, 'pay_num'),
                'pay_type'                  => $pay_type,
                'book_name'                 => getProp($item, 'book_name'),
            ];
        }

        return $result;
    }
}
