OrderModel.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\rob\model;
  12. use think\Model;
  13. use app\user\model\UserModel;
  14. class OrderModel extends Model
  15. {
  16. // 开启自动写入时间戳字段
  17. protected $autoWriteTimestamp = true;
  18. public function orderProduct()
  19. {
  20. return $this->hasMany(OrderProductModel::class, 'order_id', 'id');
  21. }
  22. public function user()
  23. {
  24. return $this->hasOne(UserModel::class, 'id', 'member_id');
  25. }
  26. /**
  27. * 状态名
  28. */
  29. public function getStatusTextAttr($value, $data)
  30. {
  31. $status = [1 => '待领取', 2 => '已完成'];
  32. return $status[$data['status']];
  33. }
  34. /**
  35. * 添加订单
  36. */
  37. public static function addOrder($user, $product, $quantity)
  38. {
  39. //添加订单
  40. $order_add = [
  41. 'order_no' => get_trade_no(),
  42. 'member_id' => $user['id'],
  43. ];
  44. $order_add['status'] = $order_add['type'] = $product['type'];
  45. if ($product['type'] == 2) {
  46. //虚拟券
  47. $order_add['receive_time'] = time();
  48. } else {
  49. //实物
  50. $order_add['product_address'] = $product['address'];
  51. $order_add['product_mobile'] = $product['mobile'];
  52. }
  53. $order = self::create($order_add);
  54. //添加商品
  55. $product_add = [
  56. 'order_id' => $order['id'],
  57. 'product_id' => $product['id'],
  58. 'product_name' => $product['product_name'],
  59. 'product_image' => $product['image_main'],
  60. 'quantity' => 1,
  61. 'score' => $product['point'],
  62. ];
  63. if ($product['type'] == 2) {
  64. $code = ProductCodeModel::where('product_id', $product['id'])->where('status', 2)->find();
  65. $code->status = 1;
  66. $code->use_time = time();
  67. $product_add['code'] = $code['code'];
  68. }
  69. OrderProductModel::create($product_add);
  70. }
  71. }