<?php


namespace App\Http\Controllers\QuickApp\Activity\Transformers;


class ReBuildData
{
    /**
     * 组装活动详情页数据
     * @param $param
     * @return array
     */
    public function buildActivityDetail($param)
    {
        $activity = $param['activity'];
        $result   = [
            'uid'         => $param['uid'],
            'name'        => getProp($activity, 'name'),
            'startTime'   => getProp($activity, 'start_time'),
            'endTime'     => getProp($activity, 'end_time'),
            'from'        => $param['from'],
            'activityId'  => getProp($activity, 'id'),
            'isForever'   => getProp($activity, 'end_time') === '9999-12-30 23:59:59' ? 1 : 0,
            'payChannels' => [
                'ali'    => 1,
                'wechat' => 1,
            ],
            'products'    => $this->buildProducts($param['products'], $param['settingProducts'])
        ];

        return $result;
    }

    /**
     * @param $products
     * @param $settingProducts
     * @return array
     */
    private function buildProducts($products, $settingProducts)
    {
        if (empty($products) || empty($settingProducts)) {
            return [];
        }

        $result = [];
        foreach ($settingProducts as $settingProduct) {
            // 获取基本信息
            $productId = getProp($settingProduct, 'product_id');
            $limit     = getProp($settingProduct, 'limit');

            // 支付产品信息
            $product = collect($products)->firstWhere('id', $productId);
            $price   = (float)getProp($product, 'price');
            $total   = $price * 100 + (int)getProp($product, 'given');

            $result[] = [
                'product_id' => $productId,
                'limit'      => $limit,
                'type'       => getProp($product, 'type'),
                'price'      => $price,
                'given'      => getProp($product, 'given'),
                'total'      => $total,
            ];
        }

        return $result;
    }
}