<?php
QUnit_Global::includeClass('Affiliate_API_Bl_Token');
QUnit_Global::includeClass('Affiliate_Merchants_Views_TransactionManager');

class Affiliate_Merchants_Views_Autocreate extends Affiliate_Merchants_Views_TransactionManager {

public $CampaignsID;
public $AffiliatesID;
public $bl_token;
public $lock = false;

function Affiliate_Merchants_Views_Autocreate() {
    $this->bl_token = QUnit_Global::newObj('Affiliate_API_Bl_Token','d5c54d6b055133926c897c534ba1a61c');
    $this->GetAffiliatesID();
    $this->GetCampaignsID();
    //$this->initPermissions();
    parent::__construct();
}

function checkPermissions($t='') {
    return true;
}

function GetCampaignsID() {
    $query = 'select `campaignid`,`commtype` from `wd_pa_campaigns` where `deleted`=0 and `disabled`=0';
    $rs = $GLOBALS['db']->Query($query);
    if($rs) {
        foreach($rs as $row) {
            switch($row['commtype']) {
                case '1': $this->CampaignsID['click'][] = $row['campaignid'];break;
                case '2': $this->CampaignsID['reg'][] = $row['campaignid'];break;
                case '3': $this->CampaignsID['click'][] = $row['campaignid'];$this->CampaignsID['reg'][] = $row['campaignid'];break;
                case '4': $this->CampaignsID['sale'][] = $row['transid'];break;
                case '5': $this->CampaignsID['click'][] = $row['campaignid'];$this->CampaignsID['sale'][] = $row['campaignid'];break;
            }
        }
        return true;
    }
    $this->lock = true;
}

function GetAffiliatesID() {
    $query = 'select `userid` from `wd_g_users` where `rtype`=4 and `deleted`=0';
    $rs = $GLOBALS['db']->Query($query);
    if($rs) {
        foreach($rs as $row) {
            $this->AffiliatesID[] = $row['userid'];
        }
        return true;
    }
    $this->lock = true;
}

function CreateIP() {
    for($i=1;$i<5;$i++) {
        $ip[] = rand(0,255);
    }
    $ip = join(".",$ip);
    return $ip;
}

function process() {
    if($lock) return false;

    //随机数据

    //事件类型，增加点击事件(1)的权重
    $ctype = array(1,2,1,4,1,4,4,1,1,1,1,2,1,4,1,1,2,1,1,1,4,4,1,1,1,4,4,4,1,1,4,2,1,4,1,4);
    $_POST['tm_transtype'] = $ctype[rand(0,35)];

    //推广员
    $affs_num = count($this->AffiliatesID);
    $affs_key = rand(0,$affs_num-1);
    $_POST['tm_userid'] = $this->AffiliatesID[$affs_key];


    //推广项目
    $ys = array(1=>'click',2=>'reg',4=>'sale');
    $sctype = $ys[$_POST['tm_transtype']];
    $camps_num = count($this->CampaignsID[$sctype]);
    $camps_key = rand(0,$camps_num-1);
    $_POST['tm_campaignid'] = $this->CampaignsID[$sctype][$camps_key];
    if(empty($_POST['tm_campaignid']) || $_POST['tm_campaignid']=='') { return false; exit; }


    //状态
    $_POST['tm_rstatus'] = round(1,3);


    //销售总价
    $_POST['tm_commission'] = $_POST['tm_totalcost'] = rand(10,100);
    $_POST['tm_orderid'] = time();
    $_POST['tm_productid'] = '';
    $_POST['tm_createtype'] = 'auto';
    $_POST['tm_data1'] = $this->bl_token->get1();
    //$_POST['ip'] = $this->CreateIP();

    $this->processCreateTransaction();
}

}//end class...
?>