OrderLogic.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\api\logic;
  3. use app\api\controller\Product;
  4. use Exception;
  5. use think\Env;
  6. use think\Cache;
  7. use think\Loader ;
  8. use fast\Asset;
  9. use app\common\model\ProductOrder;
  10. use app\common\model\ProductLists;
  11. use app\common\model\ProductPopular;
  12. use app\common\model\ProductTransfer;
  13. //订单
  14. class OrderLogic
  15. {
  16. //创建订单 is_popular
  17. public static function createOrderByType(int $type, array $areaArr, int $orderId, int $price, int $productId, int $uid, int $areaNum): bool
  18. {
  19. if($type == 1){
  20. $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, ProductOrder::Popular);
  21. }else{
  22. $result = ProductOrder::setPopularNoAreaOrder($areaNum, $orderId, $price, $productId, $uid,ProductOrder::Popular);
  23. }
  24. return $result;
  25. }
  26. //获取产品发行量
  27. public static function getProductIssue(int $productId): int
  28. {
  29. $popularNum = ProductPopular::where('product_id', $productId)->sum('stock-num');
  30. $holdNum = ProductOrder::where('product_id', $productId)->where('status', ProductOrder::Paid)->sum('num');
  31. return bcadd($popularNum, $holdNum);
  32. }
  33. //获取产品流通量
  34. public static function getProductCirculation(int $productId): int
  35. {
  36. return Loader::model('ProductOrder')::where('product_id', $productId)->whereIn('status', [ProductOrder::Paid, ProductOrder::Transferred, ProductOrder::Freeze])->count();
  37. }
  38. //获取产品有效的订单
  39. public static function getUserProductOrder(int $uid, int $productId,string $lan): array
  40. {
  41. return Loader::model('ProductOrder')->alias('a')
  42. ->join("product_list b", "a.product_id = b.id", "left")
  43. ->field('a.id,'.'b.'.$lan.'_name as name,b.thum as img_url,a.order_no')
  44. ->where('b.is_show', ProductLists::Normal)
  45. ->where('a.product_id', $productId)
  46. ->where('a.user_id', $uid)
  47. ->where('a.status', ProductOrder::Paid)
  48. ->order('id desc')
  49. ->select();
  50. }
  51. //获取转让产品订单列表
  52. public static function getProductTransferOrder(string $orderId, int $uid, int $puid)//: array
  53. {
  54. $productOrder = Loader::model('ProductOrder');
  55. $productLists = Loader::model('ProductLists');
  56. $productTransfer = Loader::model('ProductTransfer');
  57. $userModel = Loader::model('UserModel');
  58. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  59. $ledgerTokenChangeModel = Loader::model('LedgerTokenChangeModel');
  60. $result = $productTransfer::whereIn('id', $orderId)->where('status', ProductTransfer::Normal)->select();
  61. if (count($result) != count(explode(",", $orderId))) throw new Exception(__("订单不存在"));
  62. $time = time();
  63. $endTime = config('market_transfer.lock_time');
  64. foreach ($result as $item) {
  65. if($item->user_id == $uid) throw new Exception(__("不能购买自己寄售的商品"));
  66. if($item->lock_uid != $uid && $item->lock_time + $endTime > $time) throw new Exception(__("茶权已被他人锁定,无法操作"));
  67. //抢购订单
  68. $popular_order = $productOrder->where('id', $item->order_id)->find();
  69. // 生成订单
  70. $popularPrice = empty($popular_order->popular_price)? $item->price: $popular_order->popular_price;
  71. $productOrder::setCreateOrder($item->order_id, $item, $productOrder::Transfer, $uid, $item->user_id, $popular_order->order_no, $item->fees, $popularPrice);
  72. //扣除余额记录
  73. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, -$item->price, $ledgerTokenChangeModel::Payment, $item->user_id);
  74. //增加转让人余额
  75. $amount = bcsub($item->price, $item->fees, 2);
  76. $newAmount = $ledgerWalletModel->changeWalletAccount($item->user_id, Asset::TOKEN, $amount, $ledgerTokenChangeModel::Receive, $uid);
  77. $show = $productLists::where('id', $item->product_id)->value('is_show');
  78. if(!empty($show) && $popular_order->popular_price > config('min_rwa_price')) {
  79. //扣除转让人Rwa有效
  80. $userModel::updateForRwaNum($item->user_id, $userModel::getByParentId($item->user_id), 1, '-');
  81. //增加购买Rwa有效
  82. $userModel::updateForRwaNum($uid, $puid, 1, '+');
  83. }
  84. //等级分佣
  85. CommonLogic::setTeamLevelIncome($item->user_id, $item->fees, Asset::TOKEN, $ledgerTokenChangeModel::TeamLevel);
  86. //修改原订单状态
  87. $popular_order->status=$productOrder::Closure;
  88. $popular_order->save();
  89. //记录转让手续费
  90. $ledgerTokenChangeModel::createChangeFees($item->user_id, $uid, -$item->fees, $newAmount);
  91. //修改状态
  92. $item->status = $productTransfer::Stop;
  93. $item->save();
  94. }
  95. return true;
  96. }
  97. //获取赠送产品订单数量
  98. public static function getWaitPayOrderCount(int $uid, int $endTime): int
  99. {
  100. return Loader::model('ProductTransfer')::where('lock_uid', $uid)->where('lock_time', '>', time()-$endTime)->where('status', ProductTransfer::Normal)->count();
  101. }
  102. //获取赠送产品订单列表
  103. public static function getWaitPayOrderList(int $uid, int $endTime, int $page, string $lan): object
  104. {
  105. $result =Loader::model('ProductTransfer')::alias('a')
  106. ->join("product_list b", "a.product_id = b.id", "left")
  107. ->where('a.lock_uid', $uid)
  108. ->where('a.lock_time', '>', time()-$endTime)
  109. ->where('a.status', ProductTransfer::Normal)
  110. ->field('a.id,a.order_id,a.price,a.fees,a.lock_time,b.thum,b.' . $lan . '_name as name')
  111. ->order('a.id desc')
  112. ->paginate($page);
  113. return $result;
  114. }
  115. //取消赠送产品订单
  116. public static function setCancelWaitPayOrder(int $uid, int $transfer_id): bool
  117. {
  118. return Loader::model('ProductTransfer')::where('id', $transfer_id)->where('lock_uid', $uid)->update(['lock_uid' => 0, 'lock_time' => 0]);
  119. }
  120. }