ProductOrder.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 RwaExchange = 8; //Rwa茶兑换
  37. const TeacExchange = 9; //Teac兑换
  38. // 订单状态 '已下单', 1=>'已付款', 2=>'已转让', 3=>'提货', 4=>'已取消', 5=>'完成'];
  39. public $order_status = [
  40. '-1' => '全部',
  41. self::Paid => '已付款',
  42. self::Transferred => '转让中',
  43. self::Shipped => '提货',
  44. self::Closure => '关闭',
  45. self::Freeze => '质押',
  46. ];
  47. //抢购下单:未选择地区购买
  48. public static function setPopularNoAreaOrder($num, $orderId, $price, $productId, $uid, $typeId): bool
  49. {
  50. $data = [];
  51. //抢购订单
  52. $time = time();
  53. $is_popular = ($typeId == self::Popular) ? self::Paid : 0;
  54. for ($i = 0; $i < $num; $i++) {
  55. $data[] = [
  56. 'order_id' => $orderId,
  57. 'product_id' => $productId,
  58. 'type_id' => $typeId,
  59. 'status' => self::Paid,
  60. 'area_id' => 0,
  61. 'price' => $price,
  62. 'popular_price' => $price,
  63. 'fees' => 0,
  64. 'num' => 1,
  65. 'user_id' => $uid,
  66. 'order_no' => getOrderSN('R' . $i),
  67. 'is_popular' => $is_popular,
  68. 'create_time' => $time,
  69. 'update_time' => $time
  70. ];
  71. }
  72. return self::insertAll($data);
  73. }
  74. //抢购下单:选择地区购买
  75. public static function setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, $typeId): bool
  76. {
  77. $result = true;
  78. foreach ($areaArr as $item) {
  79. // 生成订单
  80. $order_arr['price'] = $price;
  81. $order_arr['product_id'] = $productId;
  82. $order_arr['area_id'] = $item;
  83. self::setCreateOrder($orderId, $order_arr, $typeId, $uid, 0, getOrderSN('R'), 0, $price);
  84. }
  85. //修改区域状态
  86. $count = ProductArea::whereIn('id', $areaArr)->setField('status', ProductLists::Stop);
  87. if ($count != count($areaArr)) $result = false;
  88. return $result;
  89. }
  90. /**
  91. * @param int $orderId 订单id
  92. * @param array $orderInfo 订单详情
  93. * @param int $typeId 订单类型
  94. * @param int $userId 用户id
  95. * @param int $fromUser 来源id
  96. * @param float $fees 手续费
  97. * @param float $price 抢购价
  98. */
  99. public static function setCreateOrder(int $orderId, $orderInfo, $typeId, $userId, $fromUser, string $orderNo, float $fees, float $price): object
  100. {
  101. $data = [
  102. 'order_id' => $orderId,
  103. 'product_id' => $orderInfo['product_id'],
  104. 'type_id' => $typeId,
  105. 'status' => self::Paid,
  106. 'area_id' => $orderInfo['area_id'],
  107. 'order_no' => $orderNo,
  108. 'user_id' => $userId,
  109. 'from_user' => $fromUser,
  110. 'price' => $orderInfo['price'],
  111. 'popular_price' => $price,
  112. 'fees' => $fees,
  113. 'num' => 1
  114. ];
  115. //抢购订单
  116. if ($typeId == self::Popular) $data['is_popular'] = self::Paid;
  117. return self::create($data);
  118. }
  119. //判断是否购买持有产品
  120. public function isBuyHoldProduct(array $productId, int $uid)
  121. {
  122. return self::where('user_id', $uid)->whereIn('product_id', $productId)->whereIn('status', [self::Paid, self::Transferred, self::Freeze])->count();
  123. }
  124. //获取持有持有产品数量
  125. public static function getHoldProductNum(string $productId)
  126. {
  127. return self::whereIn('product_id', $productId)->whereIn('status', [self::Paid, self::Transferred, self::Freeze])
  128. ->field('user_id,sum(num) as num')
  129. ->group('user_id')
  130. ->select();
  131. }
  132. //获取抢购触发产品次数
  133. public static function getTripPopularProduct(string $productId)
  134. {
  135. return self::where('product_id', $productId)->where('is_popular', self::Paid)->whereTime('create_time', 'yesterday')->sum('num');
  136. }
  137. //获取持有产品数量
  138. public static function getByOrderProductNum($productId, $num, $uid)
  139. {
  140. if (empty($productId)) return (object)[];
  141. foreach ($productId as &$item) {
  142. $item['num'] = $num;
  143. $count = self::getUserOrderNum($uid, $item['id']);
  144. //if(empty($count)) $count = 0;
  145. $item['hold_num'] = $count;
  146. }
  147. return $productId;
  148. }
  149. //空投产品向达到一定持有量的用户
  150. public static function getUserOrderByProductId(int $rwaProductId, int $rwaNum)
  151. {
  152. return ProductOrder::alias('a')
  153. ->join('product_list b', 'a.product_id = b.id', 'left')
  154. ->where('b.is_show', ProductLists::Normal)
  155. ->whereIn('a.status', [self::Paid, self::Transferred, self::Freeze])
  156. ->where('a.product_id', $rwaProductId)
  157. ->group('a.user_id')->having('total_num>=' . $rwaNum)
  158. ->field('a.id,a.user_id,count(a.num) as total_num')
  159. ->select();
  160. }
  161. //获取持有产品数量订单
  162. public static function getUserOrderByProductNum(int $uid, int $product_id, int $totalNum): array
  163. {
  164. return self::where('user_id', $uid)->where('product_id', $product_id)->where('status', self::Paid)->limit($totalNum)->column('id');
  165. }
  166. //获取持有产品数量
  167. private static function getUserOrderNum(int $uid, int $product_id): int
  168. {
  169. return self::alias('a')
  170. ->join('product_list b', 'a.product_id = b.id', 'left')
  171. ->where('b.is_show', ProductLists::Normal)
  172. ->where('a.user_id', $uid)
  173. ->where('a.product_id', $product_id)
  174. ->where('a.status', '=', self::Paid)
  175. ->sum('a.num');
  176. }
  177. // 获取订单状态
  178. public static function getProductOrder($orderId, $status, string $field)
  179. {
  180. return self::alias('a')->where('a.id', $orderId)
  181. ->join("product_list b", "a.product_id = b.id", "left")
  182. ->field('a.*,' . $field)
  183. ->where('a.status', $status)
  184. ->find();
  185. }
  186. // 获取新人福利领取订单记录
  187. public static function getUserWelfare($uid, $typeId)
  188. {
  189. return self::where('user_id', $uid)->where('type_id', $typeId)->order('create_time desc')->find();
  190. }
  191. public static function getStatusList()
  192. {
  193. return [self::Paid => __('已付款'), self::Transferred => __('已转让'), self::Shipped => __('提货'), self::Cancelled => __('已取消'), self::Closure => __('关闭'), self::Freeze => __('质押')];
  194. }
  195. //全部类型:
  196. public static function getStatusAll()
  197. {
  198. return [
  199. ['type_id' => self::Popular, 'status' => self::Paid, 'text' => __('已购买')],
  200. ['type_id' => self::Popular, 'status' => self::Closure, 'text' => __('已关闭')],
  201. ['type_id' => self::Transfer, 'status' => self::Transferred, 'text' => __('转让中')],
  202. ['type_id' => self::Giveaway, 'status' => self::Closure, 'text' => __('已赠送')],
  203. ['type_id' => self::Giveaway, 'status' => self::Paid, 'text' => __('受赠')],
  204. ['type_id' => self::Transfer, 'status' => self::Closure, 'text' => __('已转让')],
  205. ['type_id' => self::Popular, 'status' => self::Shipped, 'text' => __('已提货')],
  206. ['type_id' => self::Transfer, 'status' => self::Cancelled, 'text' => __('已取消')],
  207. ['type_id' => self::Popular, 'status' => self::Freeze, 'text' => __('质押')],
  208. ['type_id' => self::Transfer, 'status' => self::Paid, 'text' => __('购买寄售')],
  209. ['type_id' => self::Airdrop, 'status' => self::Paid, 'text' => __('空投')],
  210. ['type_id' => self::Airdrop, 'status' => self::Closure, 'text' => __('空投关闭')],
  211. ['type_id' => self::Synthesi, 'status' => self::Paid, 'text' => __('合成')],
  212. ['type_id' => self::Synthesi, 'status' => self::Closure, 'text' => __('合成关闭')],
  213. ['type_id' => self::Buying, 'status' => self::Paid, 'text' => __('求购')],
  214. ['type_id' => self::Buying, 'status' => self::Closure, 'text' => __('求购关闭')],
  215. ['type_id' => self::Newbie, 'status' => self::Paid, 'text' => __('新人茶权')],
  216. ['type_id' => self::Newbie, 'status' => self::Closure, 'text' => __('新人茶权关闭')],
  217. ['type_id' => self::Super, 'status' => self::Paid, 'text' => __('新人福利')],
  218. ['type_id' => self::Super, 'status' => self::Closure, 'text' => __('新人福利关闭')],
  219. ['type_id' => self::RwaExchange, 'status' => self::Paid, 'text' => __('Rwa兑换')],
  220. ['type_id' => self::RwaExchange, 'status' => self::Closure, 'text' => __('Rwa兑换关闭')],
  221. ['type_id' => self::TeacExchange, 'status' => self::Paid, 'text' => __('Teac兑换')],
  222. ['type_id' => self::TeacExchange, 'status' => self::Closure, 'text' => __('Teac兑换关闭')],
  223. ];
  224. }
  225. //产品
  226. public function products()
  227. {
  228. return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
  229. }
  230. //用户 user_id
  231. public function users()
  232. {
  233. return $this->hasOne('UserModel', 'id', 'user_id', [], 'LEFT')->setEagerlyType(0);
  234. }
  235. //区域
  236. public function areas()
  237. {
  238. return $this->hasOne('ProductArea', 'id', 'area_id', [], 'LEFT')->setEagerlyType(0);
  239. }
  240. //提货地址
  241. public function address()
  242. {
  243. return $this->hasOne('UserArea', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
  244. }
  245. public function getCreateTimeTextAttr($value, $data)
  246. {
  247. $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
  248. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  249. }
  250. public function getUpdateTimeTextAttr($value, $data)
  251. {
  252. $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
  253. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  254. }
  255. protected function setCreateTimeAttr($value)
  256. {
  257. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  258. }
  259. protected function setUpdateTimeAttr($value)
  260. {
  261. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  262. }
  263. //抢购下单:未选择地区购买
  264. public function setGiverwagoods($orderId, $price, $productId, $uid, $typeId)
  265. {
  266. //抢购订单
  267. $time = time();
  268. $is_popular = 0;
  269. $data = [
  270. 'order_id' => $orderId,
  271. 'product_id' => $productId,
  272. 'type_id' => $typeId,
  273. 'status' => 1,
  274. 'area_id' => 0,
  275. 'price' => $price,
  276. 'popular_price' => $price,
  277. 'fees' => 0,
  278. 'num' => 1,
  279. 'user_id' => $uid,
  280. 'order_no' => getOrderSN('R'),
  281. 'is_popular' => $is_popular,
  282. 'create_time' => $time,
  283. 'update_time' => $time
  284. ];
  285. return $data;
  286. // return self::insert($data);
  287. }
  288. }