ProductOrder.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use Exception;
  5. class ProductOrder extends Model
  6. {
  7. // 表名
  8. protected $table = 'product_order';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = 'int';
  11. // 定义时间戳字段名
  12. protected $createTime = 'create_time';
  13. protected $updateTime = 'update_time';
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'create_time_text',
  18. 'update_time_text'
  19. ];
  20. const Ordered = 0;
  21. const Paid = 1;
  22. const Transferred = 2;
  23. const Shipped = 3;
  24. const Cancelled = 4;
  25. const Closure = 5;
  26. const Popular = 0; //热销
  27. const Transfer = 1; //转让
  28. const Giveaway = 2; //赠送
  29. const Newbie = 3; //新人茶权空投
  30. const Super = 4; //新人福利空投
  31. const Airdrop = 5; //空投
  32. const Synthesi = 6; //合成
  33. /*
  34. * 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
  35. */
  36. public $order_status = [
  37. '-1' => '全部',
  38. self::Ordered => '已下单',
  39. self::Paid => '已付款',
  40. self::Transferred => '已转让',
  41. self::Shipped => '提货',
  42. self::Cancelled => '已取消',
  43. self::Closure => '关闭',
  44. ];
  45. //抢购下单:未选择地区购买
  46. public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId):bool
  47. {
  48. for ($i = 0; $i < $num; $i++) {
  49. $order_arr['price'] = $price;
  50. $order_arr['product_id']= $productId;
  51. $order_arr['area_id'] = 0;
  52. self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price);
  53. }
  54. return true;
  55. }
  56. //抢购下单:选择地区购买
  57. public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId):bool
  58. {
  59. $result = true;
  60. foreach ($areaArr as $item) {
  61. // 生成订单
  62. $order_arr['price'] = $price;
  63. $order_arr['product_id']= $productId;
  64. $order_arr['area_id'] = $item;
  65. self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price);
  66. }
  67. //修改区域状态
  68. $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::Stop);
  69. if($count != count($areaArr)) $result = false;
  70. return $result;
  71. }
  72. //合成订单
  73. public static function setSynthesisOrder($synthesis, $productId, $num, $uid):bool
  74. {
  75. $result = true;
  76. foreach ($productId as $key => $item) {
  77. //获取需求数量
  78. if(!isset($synthesis[$key.'_num'])) throw new Exception(__("参数有误,无可用产品"));;
  79. //判断持有数量足够
  80. $total_num = $synthesis[$key.'_num'] * $num;
  81. if($total_num > self::getUserOrderNum($uid, $item)) throw new Exception(__("库存不足"));;
  82. //关闭持有订单
  83. $result = self::where('user_id', $uid)->where('product_id', $item)->where('type_id', self::Popular)->where('status', self::Paid)->limit($total_num)->setField('status', self::Closure);
  84. }
  85. return $result;
  86. }
  87. /**
  88. * @param int $orderId 订单id
  89. * @param array $orderInfo 订单详情
  90. * @param int $typeId 订单类型
  91. * @param int $userId 用户id
  92. * @param int $fromUser 来源id
  93. * @param float $fees 手续费
  94. * @param float $fees 抢购价
  95. */
  96. public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
  97. {
  98. return self::create([
  99. 'order_id' => $orderId,
  100. 'product_id' => $orderInfo['product_id'],
  101. 'type_id' => $typeId,
  102. 'status' => self::Paid,
  103. 'area_id' => $orderInfo['area_id'],
  104. 'order_no' => $orderNo,
  105. 'user_id' => $userId,
  106. 'from_user' => $fromUser,
  107. 'price' => $orderInfo['price'],
  108. 'popular_price'=> $price,
  109. 'fees' => $fees,
  110. 'num' => 1
  111. ]);
  112. }
  113. //获取持有产品数量
  114. public static function getByOrderProductNum($productId, $num, $uid){
  115. if(empty($productId)) return 0;
  116. foreach ($productId as &$item) {
  117. $item['num'] = $num;
  118. $count = self::getUserOrderNum($uid, $item['id']);
  119. if(empty($count)) $count = 0;
  120. $item['hold_num'] = $count;
  121. }
  122. return $productId;
  123. }
  124. //获取持有产品数量
  125. private static function getUserOrderNum(int $uid, int $product_id): int
  126. {
  127. return self::where('user_id', $uid)->where('product_id', $product_id)->where('type_id', self::Popular)->where('status', self::Paid)->sum('num');
  128. }
  129. // 获取订单状态
  130. public static function getProductOrder($orderId, $status, string $field){
  131. return self::alias('a')->where('a.id', $orderId)
  132. ->join("product_list b", "a.product_id = b.id", "left")
  133. ->field('a.*,'.$field)
  134. ->where('a.status', $status)
  135. ->find();
  136. }
  137. // 获取新人福利领取订单记录
  138. public static function getUserWelfare($uid, $typeId){
  139. return self::where('user_id', $uid)
  140. ->where('type_id', $typeId)
  141. ->order('create_time desc')
  142. ->find();
  143. }
  144. public static function getStatusList()
  145. {
  146. return [self::Ordered => __('已下单'), self::Paid => __('已付款'), self::Transferred => __('已转让'),
  147. self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭')];
  148. }
  149. //全部类型:
  150. public static function getStatusAll()
  151. {
  152. return [
  153. ['type_id'=>self::Popular, 'status'=> self::Paid, 'text' => __('已购买')],
  154. ['type_id'=>self::Transfer, 'status'=> self::Transferred,'text' => __('转让中')],
  155. ['type_id'=>self::Giveaway, 'status'=> self::Closure, 'text' => __('已赠送')],
  156. ['type_id'=>self::Transfer, 'status'=> self::Closure, 'text' => __('已转让')],
  157. ['type_id'=>self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
  158. ['type_id'=>self::Transfer, 'status'=> self::Cancelled,'text' => __('已取消')]
  159. ];
  160. }
  161. //产品
  162. public function products()
  163. {
  164. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  165. }
  166. //用户 user_id
  167. public function users()
  168. {
  169. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  170. }
  171. //区域
  172. public function areas()
  173. {
  174. return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
  175. }
  176. //提货地址
  177. public function address()
  178. {
  179. return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
  180. }
  181. public function getCreateTimeTextAttr($value, $data)
  182. {
  183. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  184. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  185. }
  186. public function getUpdateTimeTextAttr($value, $data)
  187. {
  188. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  189. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  190. }
  191. protected function setCreateTimeAttr($value)
  192. {
  193. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  194. }
  195. protected function setUpdateTimeAttr($value)
  196. {
  197. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  198. }
  199. }