ProductOrder.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. const Newbie = 3; //新人茶权空投
  29. const Super = 4; //新人福利空投
  30. /*
  31. * 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
  32. */
  33. public $order_status = [
  34. '-1' => '全部',
  35. self::Ordered => '已下单',
  36. self::Paid => '已付款',
  37. self::Transferred => '已转让',
  38. self::Shipped => '提货',
  39. self::Cancelled => '已取消',
  40. self::Closure => '关闭',
  41. ];
  42. //抢购下单:未选择地区购买
  43. public static function setPopularNoAreaOrder($num, $order_id, $price, $product_id, $uid):bool
  44. {
  45. for ($i = 0; $i < $num; $i++) {
  46. $order_arr['price'] = $price;
  47. $order_arr['product_id']= $product_id;
  48. $order_arr['area_id'] = 0;
  49. self::setCreateOrder($order_id, $order_arr, self::Popular, $uid, 0, getOrderSN('R'), 0, $price);
  50. }
  51. return true;
  52. }
  53. //抢购下单:选择地区购买
  54. public static function setPopularAreaOrder($areaArr, $order_id, $price, $product_id, $uid):bool
  55. {
  56. $result = true;
  57. foreach ($areaArr as $item) {
  58. // 生成订单
  59. $order_arr['price'] = $price;
  60. $order_arr['product_id']= $product_id;
  61. $order_arr['area_id'] = $item;
  62. self::setCreateOrder($order_id, $order_arr, self::Popular, $uid, 0, getOrderSN('R'), 0, $price);
  63. }
  64. //修改区域状态
  65. $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::STOP);
  66. if($count != count($areaArr)) $result = false;
  67. return $result;
  68. }
  69. /**
  70. * @param int $orderId 订单id
  71. * @param array $orderInfo 订单详情
  72. * @param int $typeId 订单类型
  73. * @param int $userId 用户id
  74. * @param int $fromUser 来源id
  75. * @param float $fees 手续费
  76. * @param float $fees 抢购价
  77. */
  78. public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
  79. {
  80. return self::create([
  81. 'order_id' => $orderId,
  82. 'product_id' => $orderInfo['product_id'],
  83. 'type_id' => $typeId,
  84. 'status' => self::Paid,
  85. 'area_id' => $orderInfo['area_id'],
  86. 'order_no' => $orderNo,
  87. 'user_id' => $userId,
  88. 'from_user' => $fromUser,
  89. 'price' => $orderInfo['price'],
  90. 'popular_price'=> $price,
  91. 'fees' => $fees,
  92. 'num' => 1
  93. ]);
  94. }
  95. // 获取订单状态
  96. public static function getProductOrder($orderId, $status, string $field){
  97. return self::alias('a')->where('a.id', $orderId)
  98. ->join("product_list b", "a.product_id = b.id", "left")
  99. ->field('a.*,'.$field)
  100. ->where('a.status', $status)
  101. ->find();
  102. }
  103. // 获取新人福利领取订单记录
  104. public static function getUserWelfare($uid, $typeId){
  105. return self::where('user_id', $uid)
  106. ->where('type_id', $typeId)
  107. ->select();
  108. }
  109. public static function getStatusList()
  110. {
  111. return [self::Ordered => __('已下单'), self::Paid => __('已付款'), self::Transferred => __('已转让'),
  112. self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭')];
  113. }
  114. //全部类型:
  115. public static function getStatusAll()
  116. {
  117. return [
  118. ['type_id'=>self::Popular, 'status'=> self::Paid, 'text' => __('已购买')],
  119. ['type_id'=>self::Transfer, 'status'=> self::Transferred,'text' => __('转让中')],
  120. ['type_id'=>self::Giveaway, 'status'=> self::Closure, 'text' => __('已赠送')],
  121. ['type_id'=>self::Transfer, 'status'=> self::Closure, 'text' => __('已转让')],
  122. ['type_id'=>self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
  123. ['type_id'=>self::Transfer, 'status'=> self::Cancelled,'text' => __('已取消')]
  124. ];
  125. }
  126. //产品
  127. public function products()
  128. {
  129. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  130. }
  131. //用户 user_id
  132. public function users()
  133. {
  134. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  135. }
  136. //区域
  137. public function areas()
  138. {
  139. return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
  140. }
  141. //提货地址
  142. public function address()
  143. {
  144. return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
  145. }
  146. public function getCreateTimeTextAttr($value, $data)
  147. {
  148. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  149. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  150. }
  151. public function getUpdateTimeTextAttr($value, $data)
  152. {
  153. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  154. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  155. }
  156. protected function setCreateTimeAttr($value)
  157. {
  158. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  159. }
  160. protected function setUpdateTimeAttr($value)
  161. {
  162. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  163. }
  164. }