<?php

class LuckyJourneyModel extends ActBaseModel implements ActInterface
{

    function formatActData(array &$actConfig)
    {

        $luckyJourneyConfig = $actConfig['luckyJourneyConfig'];
        unset($actConfig['luckyJourneyConfig']);
        if (empty($luckyJourneyConfig)) {
            $this->response(1, '保存失败，未配置等级层级');
        }

        # 处理奖励
        foreach ($luckyJourneyConfig as $lvIdx => &$lvItem) {
            if (empty($lvItem['stages'])) {
                $this->response(-1 * __LINE__, "等级层级: {$lvIdx} ,未配置体力层级");
            }

            foreach ($lvItem['stages'] as $spinsIdx => $spinsItem) {
                foreach ($spinsItem['reward'] as $rewardName => $rewardConf) {
                    if (empty($rewardConf)) {
                        $this->response(-1 * __LINE__, "等级层级: {$lvIdx} 体力层级: {$spinsIdx} ,未配置{$rewardName}层级");
                    }

                    if ($rewardName == "dice_weight") {
                        continue;
                    }

                    foreach ($rewardConf as $rewardIdx => $rewardItem) {
                        if (empty($rewardItem['prize'])) {
                            $this->response(-1 * __LINE__, "等级层级: {$lvIdx} 体力层级: {$spinsIdx} 奖励层级: {$rewardIdx} ,奖励配置为空");
                        }

                        if ($rewardName == "stage_reward"&&$rewardItem['bonus'] == 0 && count($rewardItem['prize']) != 1) {
                            $this->response(-1 * __LINE__, "等级层级: {$lvIdx} 体力层级: {$spinsIdx} 奖励层级: {$rewardIdx} ,额外奖励只能配置1个奖励内容");
                        }
                    }
                }
            }
        }

        $actConfig['reward_info'] = json_encode($luckyJourneyConfig, JSON_NUMERIC_CHECK);

        $this->saveTokenGoods($actConfig, 'extraData');
    }

    function writeActCache(array $actConfig)
    {

        $conditionKeys = okeys::activityConditionKeys($actConfig['aid']);
        $stageRewardKeys = okeys::activityRewardsStageKeys($actConfig['aid']);
        $stageExtraConfKeys = okeys::activityStageExtraConfigKeys($actConfig['aid']);
        $rewardInfo = json_decode($actConfig['reward_info'], true);
        $extraData = json_decode($actConfig['extraData'], true);
        foreach ($rewardInfo as $idx1 => $item1) {
            foreach ($item1['stages'] as $idx2 => $item2) {
                $stageKey = "{$idx1}_{$idx2}";
                $stageConditions = $this->getStageConditionLvSpins($item1, $item2);
                oo::commonOprRedis('games')->hset($conditionKeys, $stageKey, json_encode($stageConditions, JSON_NUMERIC_CHECK));
                oo::commonOprRedis('games')->hset($stageRewardKeys, $stageKey, json_encode($item2['reward'], JSON_NUMERIC_CHECK));
                oo::commonOprRedis('games')->hset($stageExtraConfKeys, $stageKey, json_encode(['tokenGoods' =>$extraData[$stageKey]], JSON_NUMERIC_CHECK));
            }
        }
        $expireTime = ActBaseModel::getActCacheExpireTime($actConfig['end_time']);#redis最少一分钟
        oo::commonOprRedis('games')->expireAt($conditionKeys, oo::redisRandomExpire($expireTime));
        oo::commonOprRedis('games')->expireAt($stageRewardKeys, oo::redisRandomExpire($expireTime));
//        oo::commonOprRedis('games')->expireAt($stageExtraConfKeys, oo::redisRandomExpire($expireTime));
    }

    function serializeShowConf(array &$actConfig)
    {
    }

    function importActData(array &$basicConfig, $sheetArray)
    {
        $sheetIdx = 0;
        $stagesMappingArray = $sheetArray[++$sheetIdx] ?? [];
        $stagesRewardsArray = $sheetArray[++$sheetIdx] ?? [];
        $diceWeightArray = $sheetArray[++$sheetIdx] ?? [];
        $tokenShopArray = $sheetArray[++$sheetIdx] ?? [];
        $noTokenConfigArray = $sheetArray[++$sheetIdx] ?? [];
        list($activityConf, $idxMapping) = $this->parseLvSpinsStagesMapping($stagesMappingArray);

        for ($k = 1; $k < count($stagesRewardsArray); $k++) {
            $item = $stagesRewardsArray[$k];
            $filterLine = array_filter($item);
            if (empty($filterLine)) continue;
            $line = $k + 1;
            if ($k == 0) continue;

            @list($idx, $seq, $isBonus, $target) = $item;
            $rewards = array_slice($item, 4);

            $isBonus = ($isBonus == '是') ? 1 : 0;

            #奖励不能为空
            if (empty($idxMapping[$idx])) {
                $this->response(-1, "第{$line}行,唯一id:{$idx} 无对应关系");
            }
            list($lvStage, $spinsStage) = $idxMapping[$idx];

            $rewardsTemp = $this->parseLineRewards($rewards, $line);

            $activityConf[$lvStage]['stages'][$spinsStage]['reward']['stage_reward'][] = [
                "bonus" => $isBonus,
                "target" => $target,
                "prize" => $rewardsTemp,
            ];
        }

        // 骰子权重
        for ($k = 1; $k < count($diceWeightArray); $k++) {
            $item = $diceWeightArray[$k];
            $filterLine = array_filter($item);
            if (empty($filterLine)) continue;
            $line = $k + 1;
            if ($k == 0) continue;

            @list($idx, $seq, $diceTypeText,$diceNum, $weight, $limit, $minCost) = $item;

            if (empty($idxMapping[$idx])) {
                $this->response(-1, "第{$line}行,唯一id:{$idx} 无对应关系");
            }

            $diceType = 1;
            switch ($diceTypeText){
                case '点数':break;
                case 'Bonus':$diceType = 2;break;
                default:
                    $this->response(-1, "第{$line}行, 骰子类型异常：".$diceTypeText);
            }
            list($lvStage, $spinsStage) = $idxMapping[$idx];

            $activityConf[$lvStage]['stages'][$spinsStage]['reward']['dice_weight'][] = [
                "dice_type" => $diceType,
                "dice_num" => $diceNum,
                "weight" => $weight,
                "limit" => $limit,
                "min_cost" => $minCost,
            ];
        }

        // 商品兑换
        for ($k = 1; $k < count($tokenShopArray); $k++) {
            $item = $tokenShopArray[$k];
            $filterLine = array_filter($item);
            if (empty($filterLine)) continue;
            $line = $k + 1;
            if ($k == 0) continue;

            @list($idx, $id, $price, $limit) = $item;
            $rewards = array_slice($item, 4);

            if (empty($idxMapping[$idx])) {
                $this->response(-1, "第{$line}行,唯一id:{$idx} 无对应关系");
            }
            list($lvStage, $spinsStage) = $idxMapping[$idx];

            $rewardsTemp = $this->parseLineRewards($rewards, $line);

            $activityConf[$lvStage]['stages'][$spinsStage]['reward']['token_shop'][] = [
                "id" => $id,
                "price" => $price,
                "limit" => $limit,
                "prize" => $rewardsTemp
            ];
        }

        $this->formatTokenGoodsSheet($basicConfig, $noTokenConfigArray, $idxMapping);
        $basicConfig['luckyJourneyConfig'] = $activityConf;
    }
}