ProductOrder.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. //状态
  21. const Paid = 1;
  22. const Transferred = 2;
  23. const Shipped = 3;
  24. const Cancelled = 4; //取消
  25. const Closure = 5; //关闭
  26. const Freeze = 6; //存储
  27. //类型
  28. const Popular = 0; //热销
  29. const Transfer = 1; //转让
  30. const Giveaway = 2; //赠送
  31. const Newbie = 3; //新人茶权空投
  32. const Super = 4; //新人福利空投
  33. const Airdrop = 5; //空投
  34. const Synthesi = 6; //合成
  35. const Buying = 7; //求购
  36. const Exchange = 8; //兑换
  37. // 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
  38. public $order_status = [
  39. '-1' => '全部',
  40. self::Paid => '已付款',
  41. self::Transferred => '转让中',
  42. self::Shipped => '提货',
  43. self::Closure => '关闭',
  44. self::Freeze => '质押',
  45. ];
  46. //抢购下单:未选择地区购买
  47. public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId):bool
  48. {
  49. for ($i = 0; $i < $num; $i++) {
  50. $order_arr['price'] = $price;
  51. $order_arr['product_id']= $productId;
  52. $order_arr['area_id'] = 0;
  53. self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'.$i), 0, $price);
  54. }
  55. return true;
  56. }
  57. //抢购下单:选择地区购买
  58. public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId):bool
  59. {
  60. $result = true;
  61. foreach ($areaArr as $item) {
  62. // 生成订单
  63. $order_arr['price'] = $price;
  64. $order_arr['product_id']= $productId;
  65. $order_arr['area_id'] = $item;
  66. self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price);
  67. }
  68. //修改区域状态
  69. $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::Stop);
  70. if($count != count($areaArr)) $result = false;
  71. return $result;
  72. }
  73. /**
  74. * @param int $orderId 订单id
  75. * @param array $orderInfo 订单详情
  76. * @param int $typeId 订单类型
  77. * @param int $userId 用户id
  78. * @param int $fromUser 来源id
  79. * @param float $fees 手续费
  80. * @param float $fees 抢购价
  81. */
  82. public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
  83. {
  84. return self::create([
  85. 'order_id' => $orderId,
  86. 'product_id' => $orderInfo['product_id'],
  87. 'type_id' => $typeId,
  88. 'status' => self::Paid,
  89. 'area_id' => $orderInfo['area_id'],
  90. 'order_no' => $orderNo,
  91. 'user_id' => $userId,
  92. 'from_user' => $fromUser,
  93. 'price' => $orderInfo['price'],
  94. 'popular_price'=> $price,
  95. 'fees' => $fees,
  96. 'num' => 1
  97. ]);
  98. }
  99. //获取持有产品数量
  100. public static function getByOrderProductNum($productId, $num, $uid)
  101. {
  102. if(empty($productId)) return (object)[];
  103. foreach ($productId as &$item) {
  104. $item['num'] = $num;
  105. $count = self::getUserOrderNum($uid, $item['id']);
  106. if(empty($count)) $count = 0;
  107. $item['hold_num'] = $count;
  108. }
  109. return $productId;
  110. }
  111. //空投产品向达到一定持有量的用户
  112. public static function getUserOrderByProductId(int $rwaProductId, int $rwaNum){
  113. return ProductOrder::whereIn('status', [self::Paid, self::Transferred, self::Freeze])
  114. ->where('product_id', $rwaProductId)
  115. ->group('user_id')->having('total_num>='.$rwaNum)
  116. ->field('id,user_id,count(num) as total_num')
  117. ->select();
  118. }
  119. //获取持有产品数量订单
  120. public static function getUserOrderByProductNum(int $uid, int $product_id, int $totalNum): array
  121. {
  122. return self::where('user_id', $uid)->where('product_id', $product_id)->where('status', '=', self::Paid)->limit($totalNum)->column('id');
  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('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::Paid => __('已付款'), self::Transferred => __('已转让'),
  147. self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭'), self::Freeze => __('质押')];
  148. }
  149. //全部类型:
  150. public static function getStatusAll()
  151. {
  152. return [
  153. ['type_id'=>self::Popular, 'status'=> self::Paid, 'text' => __('已购买')],
  154. ['type_id'=>self::Popular, 'status'=> self::Closure, 'text' => __('已关闭')],
  155. ['type_id'=>self::Transfer, 'status'=> self::Transferred,'text' => __('转让中')],
  156. ['type_id'=>self::Giveaway, 'status'=> self::Closure, 'text' => __('已赠送')],
  157. ['type_id'=>self::Transfer, 'status'=> self::Closure, 'text' => __('已转让')],
  158. ['type_id'=>self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
  159. ['type_id'=>self::Transfer, 'status'=> self::Cancelled,'text' => __('已取消')],
  160. ['type_id'=>self::Popular, 'status' => self::Freeze, 'text' => __('质押')],
  161. ['type_id'=>self::Transfer, 'status' => self::Paid, 'text' => __('购买寄售')],
  162. ['type_id'=>self::Airdrop, 'status' => self::Paid, 'text' => __('空投')],
  163. ['type_id'=>self::Airdrop, 'status' => self::Closure, 'text' => __('空投关闭')],
  164. ['type_id'=>self::Synthesi, 'status' => self::Paid, 'text' => __('合成')],
  165. ['type_id'=>self::Synthesi, 'status' => self::Closure, 'text' => __('合成关闭')],
  166. ['type_id'=>self::Buying, 'status' => self::Paid, 'text' => __('求购')],
  167. ['type_id'=>self::Buying, 'status' => self::Closure, 'text' => __('求购关闭')],
  168. ['type_id'=>self::Newbie, 'status' => self::Paid, 'text' => __('新人茶权')],
  169. ['type_id'=>self::Newbie, 'status' => self::Closure, 'text' => __('新人茶权关闭')],
  170. ['type_id'=>self::Super, 'status' => self::Paid, 'text' => __('新人福利')],
  171. ['type_id'=>self::Super, 'status' => self::Closure, 'text' => __('新人福利关闭')],
  172. ];
  173. }
  174. //产品
  175. public function products()
  176. {
  177. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  178. }
  179. //用户 user_id
  180. public function users()
  181. {
  182. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  183. }
  184. //区域
  185. public function areas()
  186. {
  187. return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
  188. }
  189. //提货地址
  190. public function address()
  191. {
  192. return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
  193. }
  194. public function getCreateTimeTextAttr($value, $data)
  195. {
  196. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  197. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  198. }
  199. public function getUpdateTimeTextAttr($value, $data)
  200. {
  201. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  202. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  203. }
  204. protected function setCreateTimeAttr($value)
  205. {
  206. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  207. }
  208. protected function setUpdateTimeAttr($value)
  209. {
  210. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  211. }
  212. }