Order.php 3.4 KB
<?php

namespace app\common\model;

use think\Model;
use think\Db;

class Order extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    /**
     * 订单商品列表
     */
    public function goods()
    {
        return $this->hasMany('OrderGoods');
    }

    /**
     * 关联订单收货地址表
     */
    public function address()
    {
        return $this->hasOne('OrderAddress');
    }

    /**
     * 新增订单
     */
    public function add($user_id, $order)
    {
        if (empty($order['address'])) {
            $this->error = '请先选择收货地址';
            return false;
        }
        Db::startTrans();
        // 记录订单信息
        $this->save([
            'user_id' => $user_id,
            'order_no' => date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8),
            'total_price' => $order['total_price'],
            'order_price' => $order['order_price'],
            'express_price' => $order['express_price'],
            'reduce_price' => $order['reduce_price'],
            'user_coupon_id' => $order['coupon']['user_coupon_id'],
            'score' => $order['score']['score_price'] > 0 ? $order['score']['score'] : 0,
        ]);
        // 订单商品列表
        $goodsList = [];
        foreach ($order['goods_list'] as $goods) {
            $goodsList[] = [
                'goods_id' => $goods['goods_id'],
                'goods_name' => $goods['goods']['goods_name'],
                'goods_image' => $goods['goods']['goods_image'],
                'goods_price' => $goods['goods_price'],
                'goods_num' => $goods['goods_num'],
                'user_size_id' => !empty($goods['user_size']) ? $goods['user_size']['id'] : 0,
                'goods_style' => $goods['goods_style']
            ];
        }
        // 保存订单商品信息
        $this->goods()->saveAll($goodsList);
        // 记录收货地址
        $this->address()->save([
            'name' => $order['address']['name'],
            'phone' => $order['address']['phone'],
            'province_id' => $order['address']['province_id'],
            'city_id' => $order['address']['city_id'],
            'district_id' => $order['address']['district_id'],
            'detail' => $order['address']['detail'],
        ]);
        Db::commit();
        return true;
    }

    /**
     * 用户中心订单列表
     */
    public function getList($user_id, $type = 'all')
    {
        // 筛选条件
        $filter = [];
        // 订单数据类型
        switch ($type) {
            case 'all':
                break;
            case 'payment';
                $filter['pay_status'] = 10;
                break;
            case 'delivery';
                $filter['pay_status'] = 20;
                $filter['delivery_status'] = 10;
                break;
            case 'received';
                $filter['pay_status'] = 20;
                $filter['delivery_status'] = 20;
                $filter['receipt_status'] = 10;
                break;
        }
        return $this->with(['goods.image'])
            ->where('user_id', '=', $user_id)
            ->where('order_status', '<>', 20)
            ->where($filter)
            ->order(['create_time' => 'desc'])
            ->select();
    }
}