<?php
declare (strict_types=1);

namespace app\command;

use app\admin\model\Test1;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;
use think\facade\Db;

//获取实时快递信息
class getLogisticsInformationByKuaidi extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('getLogisticsInformationByKuaidi')
            ->setDescription('获取实时快递信息');
    }

    //获取实时快递信息
    protected function execute(Input $input, Output $output)
    {
        $order_out_list = Db::name('order_out')
            ->alias('oo')
            ->where('oo.status', 2)//status==2 已发货待收货
            ->leftJoin('order_addr oa', 'oa.id=oo.addrid AND oa.is_del=0')
            ->lock(true)
            ->field('oo.id,oo.post_code,oo.updatetime,oa.mobile,oo.post_name')
            ->cursor();

        if (empty($order_out_list)) $output->writeln('没有符合条件的记录');
        else {
            foreach ($order_out_list as $order_out) {
                $this->_handlePostData($order_out['id'], $order_out['post_code'], $order_out['mobile'], $order_out['post_name']);
            }
            $output->writeln(date('Y-m-d H:i:s') . '|处理成功');
        }
    }


    //处理物流数据
    //@param string order_out_id 发货单ID
    //@param string post_data 快递单号
    //@param string mobile 收货人手机号,目前只有顺丰需要
    //@param string post_name 物流公司
    private function _handlePostData(int $order_out_id = 0, string $post_code = '', string $mobile = '', string $post_name = '')
    {

        $remark = [0 => '物流单号暂无结果', 3 => '在途', 4 => '揽件', 5 => '疑难', 6 => '签收', 7 => '退签', 8 => '派件', 9 => '退回'];

        Db::startTrans();
        try {

            $data = get_logistics_information($post_code, substr($mobile, -4));

            if (isset($data['errMsg']) || (isset($data['success']) && $data['success'] != true)) throw new Exception($data['reason']);

            $rs = Db::name('express_data')
                ->field('id')
                ->where(['post_code' => $post_code, 'order_out_id' => $order_out_id])
                ->find();

            if (empty($rs)) {
                Db::name('express_data')
                    ->insert([
                        'order_out_id' => $order_out_id,
                        'post_code' => $post_code,
                        'post_name' => $data['company'],
                        'post_express' => $data['exname'],
                        'post_logo' => $data['ico'],
                        'post_data' => json_encode($data['data'], JSON_UNESCAPED_UNICODE),
                        'status' => $data['status'],
                        'remark' => $remark[$data['status']],
                        'addtime' => date('Y-m-d H:i:s'),
                        'updatetime' => date('Y-m-d H:i:s'),
                    ]);
            } else {
                Db::name('express_data')
                    ->where(['id' => $rs['id'], 'order_out_id' => $order_out_id])
                    ->update([
                        'post_data' => json_encode($data['data'], JSON_UNESCAPED_UNICODE),
                        'status' => $data['status'],
                        'remark' => $remark[$data['status']],
                        'updatetime' => date('Y-m-d H:i:s'),
                    ]);
            }

            //确认收货
            if ($data['status'] == 6) {
                Db::name('order_out')
                    ->where(['post_code' => $post_code, 'id' => $order_out_id, 'status' => 2])
                    ->update([
                        'status' => 3,//status==3 已收货
                        'updatetime' => date('Y-m-d H:i:s'),
                    ]);
            }

            //更新物流公司
            if ($post_name == '') {
                Db::name('order_out')
                    ->where(['post_code' => $post_code, 'id' => $order_out_id])
                    ->update([
                        'post_name' => $post_name,
                        'updatetime' => date('Y-m-d H:i:s'),
                    ]);
            }

            Db::commit();
            return true;
        } catch (Exception $exception) {
            Db::rollback();
            return false;
        }
    }


}