<?php
namespace process;

use FilesystemIterator;
use Workerman\Timer;
use support\Db;
use support\Log;
use support\ApiTask;
use support\LibDb;

class TaskSaleCross
{
    private $task;
    public function onWorkerStart()
    {
        set_time_limit(3600 * 100);
        ini_set('memory_limit', '20480M');
        
        // 每隔60秒检查一次
        // 执行一次大概15秒
        Timer::add(15, function(){
            $this->task = new ApiTask('TaskSaleCross');
    		$data = $this->task->get();
    		if($data->enable 
    		   && time()>strtotime($data->begin_start_time) //开始时间
    		   && (empty($data->access_time) || time()-strtotime($data->access_time) > $data->interval)
    		){
                $nowTime = getNowTime();
                if($data->interval >= 86400){ //达到或超过1天时
                    $t = explode(' ',$data->begin_start_time); 
                    $jgTime = $t[1];
                    $t = explode(' ',getNowTime()); 
                    $date = $t[0];
                    $nowTime  = $date .' '. $jgTime;
                }
    			$this->task->begin($nowTime);
                $this->createSaleIdxByCountry();
                $this->task->complete(getNowTime());
    		}
        });
    }


    //同市场交叉销售
	public function createSaleIdxByCountry()
    {
        set_time_limit(3600 * 100);
        ini_set('memory_limit', '2048M');
		$yearRegion =  date("Y-m-d", strtotime("-3 year"));
        $countryRows = Db::table('smt_sale_base')
        ->select('ssb_country')
        ->groupBy('ssb_country')
        ->orderBy('ssb_country')
        ->get();
        Db::table('smt_sale_cross')->truncate();
        foreach($countryRows as $k=>$countryRow){
           $country = $countryRow->ssb_country;
           $rows = Db::table('smt_sale_base')
            ->select('ssb_cid','ssb_country','ssb_p_id')
            ->where('ssb_country','=',$country)
            ->where('ssb_deliverydate','>=',$yearRegion)
            ->distinct()
			->orderBy('ssb_country','asc')
			->orderBy('ssb_p_id','asc')
            ->get();
           $dataset = [];
           $total = Db::table('smt_sale_base')
             ->selectRaw('COUNT(DISTINCT ssb_cid) as total')
             ->where('ssb_country','=',$country)
             ->first();
           $total = $total->total;
           $isInsert = false;
           $oldKey = '';
           echo "country: ".$country."\r\n";
           foreach($rows as $row){
                $key = $country.','.$row->ssb_p_id;
                if($oldKey!='' && count($dataset)>=1000 && $oldKey != $key){
                    Db::table('smt_sale_cross')->insert($dataset);
                    $dataset = [];
                }
                if($oldKey=='' || $oldKey!=$key) $oldKey = $key;

                if(!isset($dataset[$key])){
                    $dataset[$key]['ssc_find'] = 0;
                    $dataset[$key]['ssc_country'] = $country;
                    $dataset[$key]['ssc_pid'] = $row->ssb_p_id;
                    $dataset[$key]['ssc_total'] = $total;
                }
                $dataset[$key]['ssc_find']++;
                $dataset[$key]['ssc_bl'] = round($dataset[$key]['ssc_find'] / $total,4);
           }
           if(!empty($dataset)) Db::table('smt_sale_cross')->insert($dataset);
        }
	}       
}