<?php

/**
 * 同布赌注表
 * User: ben
 */
class Sync_Offer extends Sync_Base {
    protected $dbKey = 'dw_baccarat';
    protected $frequency = 1;
    protected $index = 4;

    protected $dataField = ['player','win', 'win_int', 'state', 'update_time_int', 'transfer_trx_id'];

    private $objRewardRelation;
    private $objAccount;

    /**
     * 清除数据的时间间隔
     * @var int
     */
    protected $clearTtl = 600;
    protected $clearAction = 'clearoffer';

    public function __construct() {
        $this->objRewardRelation = new TableHelper('referee_relation', 'dw_eos');
        $this->objAccount = new Account();

        parent::__construct('offer', 'id', 'update_time_int');
    }

    protected function addNewRows($newRows) {
        $ids = [];
        $players = [];
        foreach ($newRows as $k => $row) {
            $players[] = $row['player'];

            $row['offerall_int'] = Eos::toNumber($row['offerall']);
            $row['offertype'] = implode('|', $row['offertype']);
            $row['offeramount_int'] = implode('|', array_map('Eos::toNumber', $row['offeramount']));
            $row['offeramount'] = implode('|', $row['offeramount']);
            $row['create_time'] = $this->formatDateTime($row['create_time']);
            $row['win_int'] = Eos::toNumber($row['win']);

            $ids[] = $row['id'];

            $newRows[$k] = $row;

        }

        parent::addNewRows($newRows);

        $vips = $this->objAccount->getVipLevels($players);

        $offerAmount = [];
        $offerIds = [];
        foreach ($newRows as $row) {
            $player = $row['player'];

            $eventData = [
                'id' => $row['id'],
                'game_id' => $row['game_id'],
                'player' => $row['player'],
                'offerall' => $row['offerall_int'],
                'offertype' => $row['offertype'],
                'offeramount' => $row['offeramount_int'],
                'vip_level' => $vips[$player] ?: 0,
            ];
            Eos::pubEvent('baccarat:new_offer', $eventData);

            $offerAmount[$player] = $row['offerall_int'];
            $offerIds[$player] = $row['id'];

            // 绑定信息
            if ($row['ref']) { // 有邀请人
                list($rt, $msg) = Account::bindReferee($player, $row['ref'], $this->objRewardRelation, $this->objAccount);
                if (!$rt) {
                    Eos::log("Referee: offerId:{$row['id']}, player:{$player} 受邀请失败,msg:{$msg}");
                }
            }
        }

        //检查推广奖励
        $relations = $this->objRewardRelation->getAll(['player' => $players]);
        $refereePlayers = arrayFormatKey($relations, 'player', 'referee_player');

        foreach ($refereePlayers as $player => $refereePlayer) {
            $refereeData = [
                'gameid' => $offerIds[$player],
                'bet_amount' => $offerAmount[$player],
            ];
            list($flag, $msg) = Account::addRefereeReward($player, $refereePlayer, $refereeData);
            if (!$flag) {
                Eos::log("offerId:{$offerIds[$player]}, player:{$player}, refereePlayer:{$refereePlayer}, referee reward error:{$msg}");
            }
        }
    }

//    protected function updateDb($rows) {
//
//        parent::updateDb($rows);
//    }

    protected function updateRow($row, $priValue, $dbRow) {

        $row['offerall_int'] = Eos::toNumber($row['offerall']);
        $row['offertype'] = implode('|', $row['offertype']);
        $row['offeramount'] = implode('|', $row['offeramount']);
        $row['create_time'] = $this->formatDateTime($row['create_time']);
        $row['win_int'] = Eos::toNumber($row['win']);

        parent::updateRow($row, $priValue, $dbRow);

    }


}
