<?php

namespace App\Services\Business\KuaiTuanTuan;

use App\Models\Groups as GroupsModel;
use App\Models\GroupGoods;
use App\Utils\ArrayUtils;
use App\Utils\DateTimeUtils;

class Groups
{
    public static function createGroup($localGroupId, $shop)
    {
        $type = 'pdd.ktt.group.create';
        $group = GroupsModel::query()->where('parent_id', $localGroupId)->where('shop_id', $shop->id)->first();
        $groupGoods = GroupGoods::query()
            ->where('group_id', $group->parent_id)
            ->with(['goodsSku:id,stock,thumb_url'])
            ->orderBy('sort')
            ->get();
        $goodsSkus = [];
        $operator = substr($shop->ratio, 0, 1);
        $ratio = (float)trim(substr($shop->ratio, 1));
        foreach ($groupGoods as $item) {
            $thumbUrls = $item['goodsSku']['thumb_url'];
            $thumbUrls = $thumbUrls ? ArrayUtils::index($thumbUrls, 'shop_id') : [];
            $priceInFen = $item['price_in_fen']; // 常规数值
            switch ($operator) {
                case '+':
                    $priceInFen += $ratio;
                    break;
                case '-':
                    $priceInFen -= $ratio;
                    break;
                case '*':
                    $priceInFen *= $ratio;
                    break;
                case '/':
                    $priceInFen /= $ratio;
                    break;
            }
            $priceInFen *= 100;
            $info = [
                'category_name' => $item['category_name'],
                'goods_desc' => $item['goods_name'],
                'goods_name' => $item['goods_name'],
                'limit_buy' => $item['limit_buy'],
                'sku_list' => [[
                    'external_sku_id' => $item['external_sku_id'],
                    'price_in_fen' => $priceInFen ?: 10000,
                    'quantity_type' => 0,
                    'spec_id_list' => [],
                    'total_quantity' => $item['goodsSku']['stock'],
                ]]
            ];
            if (isset($thumbUrls[$shop->id]) && $thumbUrls[$shop->id]['img_url']) {
                $info['pic_url_list'] = [$thumbUrls[$shop->id]['img_url']];
            }
            $goodsSkus[] = $info;
        }
        $appendParams = [
            'end_time' => $group->getOriginal('end_time'),
            'goods_list' => json_encode($goodsSkus, 256),
            'is_save_preview' => $group['is_save_preview'],
            'start_time' => $group->getOriginal('start_time'),
            'title' => $group['title'],
        ];

        return [$type, $appendParams];
    }

    public static function queryGroupStatus($localGroupId, $shopId)
    {
        $type = 'pdd.ktt.group.query.status';
        $group = GroupsModel::query()->where('parent_id', $localGroupId)->where('shop_id', $shopId)->first();
        $appendParams = [
            'activity_no' => $group->activity_no
        ];

        return [$type, $appendParams];
    }

    public static function queryGroup()
    {
        $type = 'pdd.ktt.group.query.list';
        $time = DateTimeUtils::getMicroTime();
        $appendParams = [
            'end_update_time' => $time,
            'page' => 1,
            'size' => 10,
            'start_update_time' => $time - 3600 * 24 * 6 * 1000
        ];

        return [$type, $appendParams];
    }
}

