<?php namespace Goods\Lib;
use Dever;
use Place;
class Express
{
    public function update($db, $data)
    {
        $order = Dever::db('order', 'goods')->find($data['order_id']);
        if ($order && $order['status'] <= 3) {
            $auth = new \Manage\Lib\Auth();
            $uid = $auth->uid;
            $log['type'] = 2;
            $log['uid'] = $uid;
            $this->up($data['id'], $order['id'], $data['express_id'], $data['number'], $log);
        }
        return 'end';
    }

    public function up($id, $order_id, $express_id, $number, $log = false)
    {
        $update['order_id'] = $order_id;
        $update['express_id'] = $express_id;
        $update['number'] = $number;
        if ($id) {
            $express = Dever::db('order_express', 'goods')->find($id);
            if ($express) {
                Dever::db('order_express', 'goods')->update($express['id'], $update);
            }
        } else {
            Dever::db('order_express', 'goods')->insert($update);
            Dever::db('order', 'goods')->update($order_id, array('status' => 3, 'ddate' => time()));
        }
        if ($log) {
            $type = $log['type'];
            $uid = $log['uid'];
            $express_info = Dever::db('express', 'sector')->find($update['express_id']);
            $msg = '已发货：' . $express_info['name'] . ',' . $update['number'];
            Dever::load('log', 'goods')->up($type, $uid, $order_id, $msg);
        }
    }

    # 获取快递物流信息
    public function getInfo($order, $address_id, $type = 1)
    {
        if ($type == 1) {
            $info = Dever::db('order_express', 'goods')->find(array('order_id' => $order['id']));
        } else {
            $info = Dever::db('order_refund_express', 'goods')->find(array('order_refund_id' => $order['id']));
        }

        if ($info) {
            $express_info = Dever::db('express', 'sector')->find($info['express_id']);
            $info['cdate_str'] = date('Y-m-d H:i:s', $info['cdate']);
            $info['name'] = $express_info['name'];
            $log = Dever::db('express_log', 'sector')->find(array('number' => $info['number']));
            if ($log) {
                if ($log['status'] == 1) {
                    $info['log'] = Dever::json_decode($log['content']);
                } else {
                    if (time() - $log['udate'] > 3600) {
                        list($info['log'], $order['status']) = $this->get($order, $info['express_id'], $info['number'], $log['mobile'], $log['id'], $type);
                    } else {
                        $info['log'] = Dever::json_decode($log['content']);
                    }
                }
            } else {
                $address = Dever::db('address', 'place')->find($address_id);
                list($info['log'], $order['status']) = $this->get($order, $info['express_id'], $info['number'], $address['phone'], false, $type);
            }
            $order['express'] = $info;
        }
        return $order;
    }

    public function get($order, $express_id, $number, $mobile, $id = false, $type = 1)
    {
        $param['number'] = $number;
        $param['mobile'] = substr($mobile, -4);
        #$data = Dever::load('account', 'api')->run('aliyun_market', 'express', $param);
        $data['data']['list'] = array();
        $data['data']['state'] = 3;
        if ($data && isset($data['data']['list'])) {
            if ($data['data']['state'] == 3) {
                $param['status'] = 1;
                if ($type == 1) {
                    if ($order['status'] <= 3) {
                        if ($data['data']['list']) {
                            $info = $data['data']['list'][0];
                            Dever::load('log', 'goods')->up(1, $order['uid'], $order['id'], $info['status']);
                        } else {
                            $info['time'] = date('Y-m-d H:i:s', time());
                        }
                        $order['status'] = 4;
                        Dever::db('order', 'goods')->update($order['id'], array('status' => $order['status'], 'qdate' => strtotime($info['time'])));
                    }
                } else {
                    if ($order['status'] <= 3) {
                        $order_info = Dever::db('order', 'goods')->find($order['order_id']);
                        Dever::load('refund', 'goods')->finish($order_info, $order);
                    }
                }
                
            } else {
                $param['status'] = 2;
            }
            $param['content'] = Dever::json_encode($data['data']['list']);
            $param['express_id'] = $express_id;
            $param['udate'] = time();
            if ($id) {
                Dever::db('express_log', 'sector')->update($id, $param);
            } else {
                Dever::db('express_log', 'sector')->insert($param);
            }
            return array($data['data']['list'], $order['status']);
        }
        return array(array(), $order['status']);
    }
}