<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/11/2
 * Time: 上午9:44
 */

//todo 送首单礼品，活动推荐，红包，F码过期提醒，邀请，购物奖励

namespace async;

use member_info;
use fcode;
use push_helper;
use bonus;
use bonus_helper;

class status extends IAsync
{
    private $member_id;
    private $mod_member;
    private $mem_info;

    public function __construct($member_id)
    {
        $this->member_id = $member_id;
        $this->mod_member = Model('member');
        $mem_info = $this->mod_member->getMemberInfoByID($member_id);
        $this->mem_info = new member_info($mem_info);
    }

    public function run()
    {
        if($this->check_present()) return;
        if($this->check_fcode())   return;
    }

    public function topup()
    {
        $mod_bonus = Model('user_bonus');
        $total = $mod_bonus->getUsableSum($this->member_id) + $mod_bonus->getBindSum($this->member_id);
        if($total > 50) return;

        $params = bonus\parameters::poor_user();
        foreach ($params as $param)
        {
            $type = bonus_helper::create_type_input($param);
            $money = $type->getTotal_amount();
            $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,[$this->member_id]);
                push_helper::poor_user_bonus($this->member_id,$money,$rate,$type_sn);
            } else {
                return false;
            }
        }
    }

    private function check_present()
    {
        if($this->mem_info->order_num() > 0) {
            return false;
        }
        if($this->mem_info->first_present() > 0) {
            return false;
        }

        $result = fcode\present_manager::instance()->fetch($this->mem_info->mobile(),null);
        if($result == false) return false;
        $fcode = $result['fcode'];
        $this->mod_member->editMember(['member_id' => $this->member_id],['first_present' => intval($fcode['fc_id'])]);
        push_helper::first_present($this->member_id,$fcode);

        return true;
    }

    private function check_fcode()
    {
        return false;
    }
}