<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/1/22
 * Time: 上午10:22
 */

require_once(BASE_ROOT_PATH . '/helper/kdn_helper.php');
require_once(BASE_ROOT_PATH . '/helper/account_helper.php');
require_once(BASE_ROOT_PATH . '/helper/pay_helper.php');

fcgi_header("Content-Type: text/plain; charset=UTF-8");
try
{
    //tid-订单号，status-1，consign_time-发货时间，logistics_no-快递流水，logistics_company-快递公司
    $order_sn = trim($_POST['tid']);
    $status = intval($_POST['status']);
    $consign_time = urldecode(trim($_POST['consign_time']));
    $ship_time = strtotime($consign_time);
    $logistics_no = trim($_POST['logistics_no']);
    $logistics_company = trim(urldecode($_POST['logistics_company']));

    Log::record("dispatch notify value: order_sn = {$order_sn},status={$status},consign_time = {$consign_time} logistics_no = {$logistics_no} logistics_company = {$logistics_company}.", Log::DEBUG);
    $express = Model('express')->field('id')->where(array('e_name' => $logistics_company))->select();
    if (empty($express) || count($express) == 0) {
        echo 'FAIL';
        return;
    }

    $express_id = $express[0]['id'];
    $remote_addr = $_SERVER['REMOTE_ADDR'];

    if (!empty($order_sn) && !empty($logistics_no))
    {
        if ($status == 1)
        {
            $helper = new pay_helper();
            $ret = $helper->OnOmsNotify($order_sn,$express_id,$logistics_no,$logistics_company);

            if ($ret) {
                QueueClient::push('subscribeKDN',['order_sn'=>$order_sn]);
                Log::record("push oms success",Log::DEBUG);
                echo 'SUCCESS';
            } else {
                Log::record("push oms error",Log::DEBUG);
                echo 'SUCCESS';
            }
        }
        else
        {
            QueueClient::push('subscribeKDN',['order_sn'=>$order_sn]);
            echo 'SUCCESS';
        }
    } else {
        echo 'FAIL';
    }
}
catch (Exception $e)
{
    Log::record($e->getMessage(), Log::ERR);
    echo 'AGAIN';
}