123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 老猫 <thinkcmf@126.com>
- // +----------------------------------------------------------------------
- namespace app\rob\model;
- use think\Model;
- use app\user\model\UserModel;
- class OrderModel extends Model
- {
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = true;
- public function orderProduct()
- {
- return $this->hasMany(OrderProductModel::class, 'order_id', 'id');
- }
- public function user()
- {
- return $this->hasOne(UserModel::class, 'id', 'member_id');
- }
- /**
- * 状态名
- */
- public function getStatusTextAttr($value, $data)
- {
- $status = [1 => '待领取', 2 => '已完成'];
- return $status[$data['status']];
- }
- /**
- * 添加订单
- */
- public static function addOrder($user, $product, $quantity)
- {
- //添加订单
- $order_add = [
- 'order_no' => get_trade_no(),
- 'member_id' => $user['id'],
- ];
- $order_add['status'] = $order_add['type'] = $product['type'];
- if ($product['type'] == 2) {
- //虚拟券
- $order_add['receive_time'] = time();
- } else {
- //实物
- $order_add['product_address'] = $product['address'];
- $order_add['product_mobile'] = $product['mobile'];
- }
- $order = self::create($order_add);
- //添加商品
- $product_add = [
- 'order_id' => $order['id'],
- 'product_id' => $product['id'],
- 'product_name' => $product['product_name'],
- 'product_image' => $product['image_main'],
- 'quantity' => 1,
- 'score' => $product['point'],
- ];
- if ($product['type'] == 2) {
- $code = ProductCodeModel::where('product_id', $product['id'])->where('status', 2)->find();
- $code->status = 1;
- $code->use_time = time();
- $product_add['code'] = $code['code'];
- }
- OrderProductModel::create($product_add);
- }
- }
|