<?php

namespace app\command;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-12-15 16:39
// +----------------------------------------------------------------------

use app\model\api\OrderInfo;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class ExpInquiry extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('ExpInquiry')
            ->setDescription('快递追踪系统');
    }

    protected function execute(Input $input, Output $output)
    {
        //3天内
        $time = strtotime('-3 day');
        //今天不查询
        $timeLast = strtotime(date('Y-m-d'));
        //8小时内查过不查
        $time2 = strtotime('-8 hour');
        $true = true;
        $orderInfo = new OrderInfo;
        $p = 1;
        while ($true) {
            $data = $orderInfo
                ->where('status', 2)
                ->where('exp_status', 0)
                ->where('send_time', '<', $timeLast)
                ->where('send_time', '>', $time)
                ->where('exp_g_time', '<=', $time2)
                ->where('warehouse_id','>',16)
                ->page($p, 100)
                ->select()
                ->toArray();
            foreach ($data as $v) {
                echo $v['exp_number'] . ' ' . $v['name'] . ' ' . $v['address'] . ' 发货时间:' . date('Y-m-d H:i:s', $v['time']) . PHP_EOL;
                $this->setActionData($v);
            }
            echo "-----------------------------------------页码[" . $p . "]-------------------------------------";
            if (empty($data)) {
                $true = false;
            }
            $p++;
        }

        echo "处理结束" . PHP_EOL;

    }

    /**
     * @param $data
     */
    private function setActionData($oData)
    {
        $exp = new \library\lib\expInfo;
        $ocode = Db::name("express")->where('id', $oData['exp_id'])->value('ocode');
        $data = $exp->showapiExpInfo(trim($oData['exp_number']), empty($ocode) ? 'auto' : $ocode);
        if (empty($data['showapi_res_body'])) {
            OrderInfo::where('id', $oData['id'])->save(['exp_g_time' => time()]);
            return;
        }
        $expData = empty($data['showapi_res_body']['data']) ? [] : $data['showapi_res_body']['data'];
        $signFor = 0;
        $signTime = 0;
        $inAr = ['揽收', '收件'];
        foreach ($expData as $v) {
            $bAr = array_filter($inAr, function ($item) use ($v) {
                if (strpos($v['context'], $item) !== false) {
                    return true;
                }
                return false;
            });
            if (!empty($bAr)) {
                $signFor = 1;
                $signTime = strtotime($v['time']);
                break;
            }
        }
        //超过2次信息算【揽收】
        if (count($expData) > 2) {
            $signFor = 1;
            $signTime = time();
        }
        if ($signFor) {
            //存储数据
            OrderInfo::where('id', $oData['id'])
                ->save([
                    'exp_status' => $signFor,
                    'exp_time' => $signTime,
                    'exp_msg' => json_encode($expData),
                    'exp_g_time' => time()
                ]);
        } else {
            OrderInfo::where('id', $oData['id'])->save(['exp_g_time' => time()]);
        }
        echo '[' . $oData['id'] . ']已查询订单，' . ($signFor ? '已揽件' : '未揽件') . PHP_EOL;
    }
}
