<?php

namespace app\common\model\unishop;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class OrderExtend extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'unishop_order_extend';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];

    /**
     * 订单信息扩展添加
     * @param $user_id  用户ID
     * @param $order_id  订单ID
     * @param $coupon_id 优惠券ID
     * @param $delivery_id 运费模版ID
     * @param $address_id  地址ID
     * @return void
     */
    public function add($user_id,$order_id,$coupon_id,$delivery_id,$address_id){

        $data['user_id'] = $user_id;
        $data['order_id'] = $order_id;
        $data['coupon_id'] = $coupon_id;
        $data['delivery_id'] = $delivery_id;
        $data['address_id'] = $address_id;
        $data['createtime'] = time();
        $data['updatetime'] = time();

        if($coupon_id != 0){
            $coupon_json = Db::name("unishop_coupon")->where('switch',1)->find($coupon_id);
            if(!empty($coupon_json)){
                $data['coupon_json'] = json_encode($coupon_json,JSON_UNESCAPED_UNICODE);
            }

        }
        if($delivery_id != 0){
            $delivery_json = Db::name("unishop_delivery")->where('switch',1)->find($delivery_id);
            if(!empty($delivery_json)){
                $data['delivery_json'] = json_encode($delivery_json,JSON_UNESCAPED_UNICODE);
            }

        }
        if($address_id != 0){
            $address_json = Db::name("unishop_address")->find($address_id);
            if(!empty($address_json)){
                $data['address_json'] = json_encode($address_json,JSON_UNESCAPED_UNICODE);
            }

        }

        $add = self::insert($data);

        if($add){
            return true;
        }else{
            return false;
        }

    }


}
