<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 18-2-28
 * Time: 下午3:18
 * To change this template use File | Settings | File Templates.
 */
require_once ('config.php');
$flagFile = APPLICATION_PATH . '/../data/log/ALBPGTrackingNumberWarn.lock';
if (@file_exists($flagFile)) {
    echo 'Anather process is running.'."\r\n";
    echo $flagFile."\r\n";
    exit();
}
//防止重复调用生成文件
$link = fopen($flagFile, 'wb');
fclose($link);

define('RUNTIME', '[' . date('Y-m-d H:i:s') . '] ');
echo RUNTIME . "Starting.\r\n\r\n";

$warnNum = 50;
try {
    $condition = array();
    $condition['sm_code'] = 'ALBP-G';
    $condition['stn_status'] = 0;
    $trackingNumCount = Service_ServiceTrackingNumber::getByCondition($condition, 'count(*)');
    echo '[' . date('Y-m-d H:i:s') . ']'."剩余未用服务商单号" . $trackingNumCount ."个\r\n";

    if ($trackingNumCount < $warnNum) {
        $params = array(
            'email' => array('service@globex.cn'),
            'subject' => '[出口WMS]渠道ALBP-G预存单号不足50个',
            'body' => "出口WMS 渠道ALBP-G 预存单号剩余" . $trackingNumCount . "个，不足" . $warnNum . "个",
        );
        if(!Common_Email::sendMail($params)){
            throw new Exception("发送邮件失败");
        }
        echo '[' . date('Y-m-d H:i:s') . ']'."邮件发送成功, 请登录查收" . $params['email'][0] ."\r\n";
    }
} catch (Exception $e) {
    @unlink($flagFile);
    echo '[' . date('Y-m-d H:i:s') . '] Fail Exception:' . $e->getMessage() ."\r\n";
}

echo "\r\n[" . date('Y-m-d H:i:s') . "] End run\r\n";
@unlink($flagFile);