<?php
namespace crmeb\services\async;

use crmeb\services\async\ActivityCalc;
use app\models\system\SystemAwardHistory;
use think\facade\Config;

class LuckyCalc extends ActivityCalc{

    protected static $NAME = 'lucky';

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

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

    protected function getNameCN() {
        return '幸运2021';
    }

    protected function repRate() {
        return 0.2;
    }

    protected function getResult($leftwin=true) {
        return $leftwin ? 20 : 21;
    }

    protected function leaningJudge($index, $product, $attr) {
        $suk = isset($attr['suk']) ? $attr['suk'] : 'any';
        $flag = $this->searchActiveInSuk($suk);

        return $flag == 20;
    }

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

        return false;
    }
}