<?php
namespace app\crm\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
class Wxmessage  extends Command{
    protected $method = '';
    protected function configure()
    {
    	ini_set('zend.enable_gc', true);
        $this->setName('wxmessage')->setDescription('Suxuan system service: weixin sync message from mc.');
    }
    
    protected function execute(Input $input, Output $output)
    {
    	$startVersion = md5_file(APP_PATH.'const.php');
    	$day = date('Ymd');
    	while (1){
    		$nowVersion = md5_file(APP_PATH.'const.php');
            if($nowVersion != $startVersion){
                //防止应用升级后，忘记重启服务的情况
                //版本发生变化，直接停止服务，让守护进程来重启
                die;
            }
            $daynow = date('Ymd');
            if($day != $daynow){
            	//重启服务
            	$key = 'sxmonitor_serializelist';
			    $service = cache($key);
			    if(is_string($service)){
			        $service = json_decode($service,true);
			    }
			    if($service['service_list']['wxmessage']){
			    	$count = $service['service_list']['wxmessage']['restart_count'];
			    	$count--;
			    	cache($key,$service);
			    }
			    die;
            }
            $dbData = db()->master()->query("select sysdate() as time");
            // echo "Start new loop run at ".date('Y-m-d H:i:s').",dbDate:".$dbData[0]['time']."\r\n";
            $maxId = db('crm_s_wx_talk')->max('mc_id');
            // echo $maxId."\r\n";
        	$res =sx_call('5f34afb5582c2',['mid'=>$maxId]);
        	// var_dump($res);
        	controller('weixin/Weixin','lib')->saveNewWxMessage($res['data']);
        }
    }

}