<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/12/2
 * Time: 下午7:53
 */

namespace async;

use bonus\parameters;
use bonus_helper;

abstract class operator
{
    public function __construct()
    {
    }

    abstract public function operate(array $minfos,$params);
    public static function create($params)
    {
        $type = $params['type'];
        if($type == 'bonus') {
            return new bonus_operator($params);
        }
        else {
            return null;
        }
    }
}

class bonus_operator extends operator
{
    private $mRate;
    private $mAmount;

    public function __construct($params) {
        $this->mRate = intval($params['rate']);
        $this->mAmount = floatval($params['amount']);
    }

    public function operate(array $minfos,$params)
    {
        $uids = [];
        foreach ($minfos as $minfo)
        {
            $uid = $minfo->member_id();
            if($uid > 0) {
                $uids[] = $uid;
            }
        }
        if(!empty($uids)) {
            return $this->make_bonus($uids,$params);
        } else {
            return false;
        }
    }

    private function make_bonus($uids,$params)
    {
        $bless = $params['type_bless'];
        $name = $params['type_name'];

        $params = parameters::awken_user($this->mRate,$this->mAmount,count($uids),$name,$bless);
        foreach ($params as $param)
        {
            $ret = bonus_helper::make_bonus($param,$param['rate_money']);

            if($ret != false)
            {
                $type_sn = $ret['type_sn'];
                bonus_helper::send($type_sn,$uids);
            }
            else {
                return false;
            }
        }

        return ['type_sn' => $type_sn];
    }
}


