<?php

namespace app\traits\spu;

use app\model\Sku as SkuModel;
use app\model\sku\SpecParam;
use app\model\Spu;
use app\repository\SpuRepository;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

trait SkuTrait
{
    /**
     * 根据sku编码 获取商品列表[普通商品和活动商品]
     *
     * @param  array  $codings
     * @param  bool  $lock
     * @return SkuModel[]|array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function listBySkuCoding(array $codings, bool $lock = false)
    {
        $query = new SkuModel();
        if ($lock) {
            $query->lock(true);
        }

        $list = SkuModel::when($lock, function ($q) {
            $q->lock(true);
        })
            ->with([
                'spu'         => function ($query) {
                    $query->where('id', '>', 0)->withoutField('content');
                }, 'limitTime' => function ($query) {
                    $query->where('id', '>', 0);
                }
            ])
            ->where('coding', 'in', $codings)
            ->field('id,coding,spu_id,spu_activity_id,title,stock,price,original_price,score,spec_text')
            ->select();

        return $list->each(function ($item) {
            $specArr = json_decode($item->spec_text, true);
            $item->spec_text = $specArr;
            $item->spec_info = SpuRepository::getInstance()->convertSpecInfo($specArr);

            $item->is_activity    = (int) ($item['spu_activity_id'] > 0);
            $item->goods_name     = $item->is_activity > 0 ? $item->limitTime['name'] : $item->spu['name'];
            $item->goods_subtitle = $item->is_activity > 0 ? $item->limitTime['subtitle'] : $item->spu['subtitle'];
            $item->limit_time     = $item->is_activity > 0 ? $item->limitTime['limit_time'] : $item->spu['limit_time'];
            $item->limit_num      = $item->is_activity > 0 ? $item->limitTime['limit_num'] : $item->spu['limit_num'];

            $item->spu_cover = $item->is_activity > 0 ? $item->limitTime['cover'] : $item->spu['cover'];
            $item->sku_cover = $item->spu_cover;
            $item->spu_name  = $item->is_activity > 0 ? $item->limitTime['name'] : $item->spu['name'];
            $item->sku_name  = $item->title;

            $item->begin_at = $item->is_activity > 0 ? $item->limitTime['begin_at'] : '';
            $item->end_at   = $item->is_activity > 0 ? $item->limitTime['end_at'] : '';
            $item->unit  = $item->spu->unit ?? '';

            unset($item->spu);
            unset($item->activity);
        });
    }

    /**
     * 根据sku ID 获取信息
     *
     * @param  int  $id
     * @param  bool  $lock
     * @return mixed
     */
    public function infoBySkuId(int $id, bool $lock = false)
    {
        $query = new SkuModel();
        if ($lock) {
            $query->lock(true);
        }

        $fields = 'sku.`id`,sku.stock,sku.coding,sku.score as sku_score,sku.price,sku.original_price,sku.spu_id,sku.spu_activity_id,
        sku.title as sku_name,sku.main_image,spu.`name` as spu_name,spu.subtitle,spu.is_score,spu_activity.`name` as spu_activity_name,spu_activity.subtitle as spu_activity_subtitle';
        return $query->alias('sku')
            ->where('sku.id', $id)
            ->leftJoin('spu', 'spu.id = sku.spu_id')
            ->leftJoin('spu_activity', 'spu_activity.id = sku.spu_activity_id')
            ->field($fields)
            ->find();
    }

    //    public function getSpecBySku(array $ids)
    //    {
    //        $skuSpecList = SkuModel::leftJoin('spu', 'spu.id = sku.spu_id')
    //            ->whereIn('sku.id', $ids)
    //            ->field('sku.id,sku.coding,sku.indexes,spu.spec')
    //            ->select();
    //
    //        $paramIds = [];//规格ID列表
    //        $valueIds  = [];//规格值ID列表
    //
    //        foreach ($skuSpecList as $skuInfo) {
    //            $param = array_keys(json_decode($skuInfo['spec'], true));
    //            $paramIds = array_merge($paramIds, $param);
    //
    //            $value = array_keys(explode('_', $skuInfo['indexes']));
    //            $valueIds = array_merge($valueIds, $value);
    //        }
    //
    //        $paramIds = array_unique($paramIds);
    //        $valueIds = array_unique($valueIds);
    //
    //        $paramList = SpecParam::whereIn('id', $paramIds)
    //            ->field('id,title')
    //            ->order('sort', 'desc')
    //            ->order('id', 'asc')
    //            ->select();
    //
    //        $valueIds = SpecParam::whereIn('id', $valueIds)
    //            ->field('id,title,spec_id')
    //            ->order('sort', 'desc')
    //            ->order('id', 'asc')
    //            ->select();
    //    }
}