<?php

namespace tw\async\activities;

use think\facade\Config;

class LuckyCalc extends ActivityCalc
{

    protected static $NAME = 'lucky';
    // protected static $Flag = Config::get('activity.lucky_spec_items', [20, 22]);

    public function __construct()
    {
    }

    protected function getId()
    {
        return Config::get('activity.lucky_cate_id');
    }

    protected function getName()
    {
        return self::$NAME;
    }

    protected function getNameCN()
    {
        return get_luck_name();
    }

    protected function repRate()
    {
        return Config::get('activity.luck_rate', 0.3);
    }

    protected function getMinimalProductNum()
    {
        return 1;
    }

    protected function getResult($leftwin = true)
    {
        $f2021 = Config::get('activity.lucky_spec_items', [20, 22]);
        return $leftwin ? $f2021[0] : $f2021[1];
    }

    /**
     * 20 分类到左边
     * 这里严格要求商品必须设置 20 22 属性
     */
    protected function leaningJudge($index, $product, $attr)
    {
        $suk = isset($attr['suk']) ? $attr['suk'] : 'any';
        $flag = $this->searchActiveInSuk($suk);

        $f2021 = Config::get('activity.lucky_spec_items', [20, 22]);
        return $flag == $f2021[0];
    }

    /**
     * 从商品的 sku 信息中找到用户选择的 20/22
     * @param $suk
     * @return bool|string
     */
    private function searchActiveInSuk($suk)
    {
        $parts = explode(",", $suk);
        if (count($parts) <= 0) {
            return false;
        }

        $f2021 = Config::get('activity.lucky_spec_items', [20, 22]);
        foreach ($parts as $part) {
            $t = trim($part);
            if ($t == $f2021[0] || $t == $f2021[1]) {
                return $t;
            }
        }

        return false;
    }
}
