<?php
namespace App\Transformer\Channel;


class ChannelTransformer
{
    // 站点统计日数据
    public function newBuildStatisticsByDay($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->newEachStatisticsByDay($data->items())
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'date'                      => getProp($item, 'day'),
                'total_order_num'           => (int)getProp($item, 'total_order_num'),
                'paid_order_num'            => (int)getProp($item, 'paid_order_num'),
                'register_num'              => (int)getProp($item, 'register_num'),
                'register_pay_num'          => (int)getProp($item, 'register_pay_num'),
                'register_pay_amount'       => (float)getProp($item, 'register_pay_amount'),
                'register_pay_percent'      => getProp($item, 'register_num') ? (float)sprintf('%.2f', getProp($item, 'register_pay_num') / getProp($item, 'register_num') * 100) : 0,
                'total_pay_num'             => (int)getProp($item, 'total_pay_num'),
                'total_pay_amount'          => (float)getProp($item, 'total_pay_amount'),
            ];
        }

        return $result;
    }

    // 站点统计推广员日数据
    public function newBuildStatisticsByDayForMaster($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->newEachStatisticsByDayForMaster($data->items())
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'date'                      => getProp($item, 'day'),
                'distribution_channel_id'   => getProp($item, 'distribution_channel_id'),
                'promote_user'              => getProp($item, 'nickname'),
                'total_order_num'           => (int)getProp($item, 'total_order_num'),
                'paid_order_num'            => (int)getProp($item, 'paid_order_num'),
                'register_num'              => (int)getProp($item, 'register_num'),
                'register_pay_num'          => (int)getProp($item, 'register_pay_num'),
                'register_pay_amount'       => (float)getProp($item, 'register_pay_amount'),
                'register_pay_percent'      => getProp($item, 'register_num') ? (float)sprintf('%.2f', getProp($item, 'register_pay_num') / getProp($item, 'register_num') * 100) : 0,
                'total_pay_num'             => (int)getProp($item, 'total_pay_num'),
                'total_pay_amount'          => (float)getProp($item, 'total_pay_amount'),
            ];
        }

        return $result;
    }

    // 站点统计月数据
    public function newBuildStatisticsByMonth($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->newEachStatisticsByMonth($data->items())
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'date'                      => getProp($item, 'month'),
                'total_order_num'           => (int)getProp($item, 'total_order_num'),
                'paid_order_num'            => (int)getProp($item, 'paid_order_num'),
                'register_num'              => (int)getProp($item, 'register_num'),
                'register_pay_num'          => (int)getProp($item, 'register_pay_num'),
                'register_pay_amount'       => (float)getProp($item, 'register_pay_amount'),
                'register_pay_percent'      => getProp($item, 'register_num') ? (float)sprintf('%.2f', getProp($item, 'register_pay_num') / getProp($item, 'register_num') * 100) : 0,
                'total_pay_num'             => (int)getProp($item, 'total_pay_num'),
                'total_pay_amount'          => (float)getProp($item, 'total_pay_amount'),
            ];
        }

        return $result;
    }

    // 站点统计推广员月数据
    public function newBuildStatisticsByMonthForMaster($data): array
    {
        return [
            'meta' => getMeta($data),
            'list' => $this->newEachStatisticsByMonthForMaster($data->items())
        ];
    }

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

        foreach ($list as $item) {
            $result[] = [
                'date'                      => getProp($item, 'month'),
                'promote_user'              => getProp($item, 'nickname'),
                'distribution_channel_id'   => getProp($item, 'distribution_channel_id'),
                'total_order_num'           => (int)getProp($item, 'total_order_num'),
                'paid_order_num'            => (int)getProp($item, 'paid_order_num'),
                'register_num'              => (int)getProp($item, 'register_num'),
                'register_pay_num'          => (int)getProp($item, 'register_pay_num'),
                'register_pay_amount'       => (float)getProp($item, 'register_pay_amount'),
                'register_pay_percent'      => getProp($item, 'register_num') ? (float)sprintf('%.2f', getProp($item, 'register_pay_num') / getProp($item, 'register_num') * 100) : 0,
                'total_pay_num'             => (int)getProp($item, 'total_pay_num'),
                'total_pay_amount'          => (float)getProp($item, 'total_pay_amount'),
            ];
        }

        return $result;
    }
}
