<?php

namespace App\Services\goods;


use Api\PhpUtils\Common\GoodsSkuId;
use App\Models\goods\mysql\GoodsSnapshot;
use App\Models\goods\mysql\PindanGoodsSnapshot;

class GoodsSnapshotsService
{
    /**
     * 拼单商品快照批量获取
     * @param $params
     * @return array
     */
    public static function pindanGoodsSnapshots($params)
    {
        $str = empty($params["snapshots_str"]) ? "" : $params["snapshots_str"];
        $pindanGoodsSnapshot = [];
        if (!empty($str)) {
            $paramsArr = json_decode($str, true);
            if (!empty($paramsArr)) {
                foreach ($paramsArr as $param) {
                    if (isset($param["goods_sku_id"]) || isset($param["goods_version"])) {
                        $key = $param["goods_sku_id"]."_".$param["goods_version"];
                        $pindanGoodsSnapshot[$key] = PindanGoodsSnapshot::select("*", [
                            "goods_sku_id" => $param["goods_sku_id"],
                            "goods_version" => $param["goods_version"]
                        ]);
                    }
                }
            }
        }
        return $pindanGoodsSnapshot;
    }

    /**
     * 批量支持商品
     * @param $params
     * @return array
     */
    public static function goodsSnapshots($params)
    {
        $str = empty($params["snapshots_str"]) ? "" : $params["snapshots_str"];
        $snapshot = [];
        if (!empty($str)) {
            $paramsArr = json_decode($str, true);
            if (!empty($paramsArr)) {
                foreach ($paramsArr as $param) {
                    if (isset($param["goods_sku_id"]) || isset($param["goods_version"])) {
                        $goodsSkuIdParams = GoodsSkuId::getGoodsSkuIdParams($param["goods_sku_id"]);
                        $key = $param["goods_sku_id"]."_".$param["goods_version"];
                        if ($goodsSkuIdParams["table_tag"] == GoodsSkuId::TABLE_TAG_PINDAN) {
                            $snapshot[$key] = PindanGoodsSnapshot::getRecord([
                                "goods_sku_id" => $param["goods_sku_id"],
                                "goods_version" => $param["goods_version"]
                            ]);
                        } else {
                            $snapshot[$key] = GoodsSnapshot::getRecord([
                                "goods_sku_id" => $param["goods_sku_id"],
                                "goods_version" => $param["goods_version"]
                            ]);
                        }
                        if (!empty($snapshot[$key]["desc_pic_url"])) {
                            $ksyun = GoodsService::getUrlList($snapshot[$key]["desc_pic_url"]);
                            $snapshot[$key]["desc_pic_url_list"] = $ksyun;
                        }
                    }
                }
            }
        }
        return $snapshot;
    }
}