<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/11/15
 * Time: 下午4:20
 */

namespace async;

use push_helper;
use order_helper;
use member_info;
use Exception;
use bonus;
use bonus_helper;
use Log;
use util;

class broadcast_order extends IAsync
{
    private $simple_order;
    private $user_info;
    private $paysn;

    public function __construct($paysn)
    {
        $this->paysn = $paysn;
        $order = $this->order(['pay_sn' => $paysn]);
        if(empty($order)) throw new Exception("错误的订单");

        $this->simple_order = new simple_order($order);
        $user = $this->simple_order->user();
        $this->user_info = new member_info($user);
    }
    public function run()
    {
    }

    public function order_bonus($amount,$days = 30)
    {
        $mod_member = Model('member');
        $days = intval($days);
        $cond['member_time'] = ['gt',util::first_day_secs(time() - $days * 86400)];
        $i = 0;
        while (true)
        {
            $start = $i * 1000;
            $items = $mod_member->field('member_id')->where($cond)->order('member_id asc')->limit("{$start},1000")->select();
            if(empty($items)) {
                return;
            }
            $i++;

            $uids = [];
            foreach ($items as $item)
            {
                $user = intval($item['member_id']);
                if($user <= 0) continue;
                $uids[] = $user;
            }

            $num = count($uids);

            $params = bonus\parameters::order_complete_all($this->user_info->nickname(),$num,$amount);
            foreach ($params as $param)
            {
                $type = bonus_helper::create_type_input($param);
                $rate  = $type->bonus_rate();
                $ret = bonus_helper::make_bonus($param,$param['rate_money']);

                if($ret != false)
                {
                    $type_sn = $ret['type_sn'];
                    bonus_helper::send($type_sn,$uids);
                    foreach ($uids as $user)
                    {
                        try
                        {
                            push_helper::all_order_complete_bonus($user,$this->user_info,$amount,$rate,$type_sn);
                            $this->add_reward($user,$amount);
                            usleep(100000);
                        }
                        catch (Exception $ex) {
                            Log::record(__METHOD__ . " error " . $ex->getTraceAsString(),Log::ERR);
                        }
                    }
                }
                else {
                    return false;
                }
            }
        }

        return true;
    }

    private function order($condition)
    {
        $model_order = Model('order');
        $order_list = $model_order->getNormalOrderList($condition,0, '*', 'order_id desc', '', ['order_common','order_address', 'order_goods']);
        $order_helper = new order_helper($order_list);
        $orders = $order_helper->format();
        if(!empty($orders)) {
            return $orders[0];
        } else {
            return false;
        }
    }
}