<?php

namespace App\Exports;

use App\Models\DailyStockRecord;
use App\Models\Log;
use App\Utils\ArrayUtils;
use App\Utils\DateTimeUtils;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use App\Models\GoodsSku;
use Illuminate\Support\Collection;

class DiffTodayPriceGoodsExport implements FromCollection, ShouldAutoSize
{
    private $data;

    public function __construct($data)
    {
        $this->data = $this->createData($data);
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public function collection()
    {
        return new Collection($this->data);
    }

    private function createData($data)
    {
        $headTitle = [
            '店铺',
            '团购标题',
            '商品名称',
            '快团团价格',
            '今日价格',
            '编码',
        ];
        $bodyData = [];
        foreach ($data as $shopName => $activity) {
            foreach ($activity as $activityNo => $item) {
                foreach ($item as $k => $v) {
                    foreach ($v as $i) {
                        if ('diff_price' === $k) {
                            $bodyData[] = [$shopName, $i['title'], $i['goods_name'], $i['price_in_fen'], $i['today_price'], $i['external_sku_id']];
                        }
                        if ('not_in_group' === $k) {
                            $bodyData[] = [$shopName, $i['title'], $i['goods_name'], '团购无此商品', $i['shop_price'][$shopName], $i['external_sku_id']];
                        }
                    }
                }
            }
        }

        return [$headTitle, $bodyData];
    }
}
