ProductOrder.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class ProductOrder extends Model
  5. {
  6. // 表名
  7. protected $table = 'product_order';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'create_time';
  12. protected $updateTime = 'update_time';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'create_time_text',
  17. 'update_time_text'
  18. ];
  19. const Ordered = 0;
  20. const Paid = 1;
  21. const Transferred = 2;
  22. const Shipped = 3;
  23. const Cancelled = 4;
  24. const Closure = 5;
  25. const Popular = 0; //热销
  26. const Transfer = 1; //转让
  27. const Giveaway = 2; //赠送
  28. /*
  29. * 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
  30. */
  31. public $order_status = [
  32. '-1' => '全部',
  33. self::Ordered => '已下单',
  34. self::Paid => '已付款',
  35. self::Transferred => '已转让',
  36. self::Shipped => '提货',
  37. self::Cancelled => '已取消',
  38. self::Closure => '关闭',
  39. ];
  40. //抢购下单:未选择地区购买
  41. public static function setPopularNoAreaOrder($num, $order_id, $price, $product_id, $uid):bool
  42. {
  43. for ($i = 0; $i < $num; $i++) {
  44. $order_arr['price'] = $price;
  45. $order_arr['product_id']= $product_id;
  46. $order_arr['area_id'] = 0;
  47. self::setCreateOrder($order_id, $order_arr, self::Popular, $uid, 0, getOrderSN('R'), 0, $price);
  48. }
  49. return true;
  50. }
  51. //抢购下单:选择地区购买
  52. public static function setPopularAreaOrder($areaArr, $order_id, $price, $product_id, $uid):bool
  53. {
  54. $result = true;
  55. foreach ($areaArr as $item) {
  56. // 生成订单
  57. $order_arr['price'] = $price;
  58. $order_arr['product_id']= $product_id;
  59. $order_arr['area_id'] = $item;
  60. self::setCreateOrder($order_id, $order_arr, self::Popular, $uid, 0, getOrderSN('R'), 0, $price);
  61. }
  62. //修改区域状态 areaArr
  63. $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::STOP);
  64. if($count != count($areaArr)) $result = false;
  65. return $result;
  66. }
  67. /**
  68. * @param int $orderId 订单id
  69. * @param array $orderInfo 订单详情
  70. * @param int $typeId 订单类型
  71. * @param int $userId 用户id
  72. * @param int $fromUser 来源id
  73. * @param float $fees 手续费
  74. * @param float $fees 抢购价
  75. */
  76. public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
  77. {
  78. return self::create([
  79. 'order_id' => $orderId,
  80. 'product_id' => $orderInfo['product_id'],
  81. 'type_id' => $typeId,
  82. 'status' => self::Paid,
  83. 'area_id' => $orderInfo['area_id'],
  84. 'order_no' => $orderNo,
  85. 'user_id' => $userId,
  86. 'from_user' => $fromUser,
  87. 'price' => $orderInfo['price'],
  88. 'popular_price'=> $price,
  89. 'fees' => $fees,
  90. 'num' => 1
  91. ]);
  92. }
  93. public static function getStatusList()
  94. {
  95. return [self::Ordered => __('已下单'), self::Paid => __('已付款'), self::Transferred => __('已转让'),
  96. self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭')];
  97. }
  98. //全部类型:
  99. public static function getStatusAll()
  100. {
  101. return [
  102. ['type_id'=>self::Popular, 'status'=> self::Paid, 'text' => __('已购买')],
  103. ['type_id'=>self::Transfer, 'status'=> self::Transferred,'text' => __('转让中')],
  104. ['type_id'=>self::Giveaway, 'status'=> self::Closure, 'text' => __('已赠送')],
  105. ['type_id'=>self::Transfer, 'status'=> self::Closure, 'text' => __('已转让')],
  106. ['type_id'=>self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
  107. ['type_id'=>self::Transfer, 'status'=> self::Cancelled,'text' => __('已取消')]
  108. ];
  109. }
  110. //产品
  111. public function products()
  112. {
  113. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  114. }
  115. //用户 user_id
  116. public function users()
  117. {
  118. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  119. }
  120. //区域
  121. public function areas()
  122. {
  123. return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
  124. }
  125. //提货地址
  126. public function address()
  127. {
  128. return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
  129. }
  130. public function getCreateTimeTextAttr($value, $data)
  131. {
  132. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  133. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  134. }
  135. public function getUpdateTimeTextAttr($value, $data)
  136. {
  137. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  138. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  139. }
  140. protected function setCreateTimeAttr($value)
  141. {
  142. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  143. }
  144. protected function setUpdateTimeAttr($value)
  145. {
  146. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  147. }
  148. }