<?php namespace Goods\Lib;
use Dever;
use Place;
class Address
{
    public function update($db, $data)
    {
        $order = Dever::db('order', 'goods')->find($data['id']);
        if ($order) {
            $auth = new \Manage\Lib\Auth();
            $uid = $auth->uid;
            $log['type'] = 2;
            $log['uid'] = $uid;
            $this->up($order['id'], $order['uid'], $order['address_id'], $data['address_name'], $data['address_phone'], $data['address_area'], $data['address_address'], $log);
        }
        return 'end';
    }

    public function up($order_id, $uid, $address_id, $name, $phone, $area, $address, $log = false)
    {
        $update['uid'] = $uid;
        $update['name'] = $name;
        $update['phone'] = $phone;
        $update['type'] = 3;
        $update['area'] = $area;
        list($update['province_id'], $update['city_id'], $update['county_id']) = explode(',', $update['area']);
        $update['address'] = $address;
        $address = Dever::db('address', 'place')->find($address_id);
        if ($address && $address['type'] == 3) {
            # 更新
            Dever::db('address', 'place')->update($address['id'], $update);
        } else {
            $id = Dever::db('address', 'place')->insert($update);
            Dever::db('order', 'goods')->update($order_id, array('address_id' => $id));
        }
        $type = 1;
        if ($log) {
            $type = $log['type'];
            $uid = $log['uid'];
        }
        $address = Dever::load('address', 'place')->handleInfo($update);
        Dever::load('log', 'goods')->up($type, $uid, $order_id, '更改收货地址：' . $address['full']);
    }
}