OrderLogic.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\api\logic;
  3. use Exception;
  4. use think\Env;
  5. use think\Cache;
  6. use think\Loader ;
  7. use fast\Asset;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\LedgerTeacChangeModel;
  10. use app\common\model\ProductPopular;
  11. use app\common\model\ProductTransfer;
  12. //订单
  13. class OrderLogic
  14. {
  15. //创建订单
  16. public static function createOrderByType(int $type, array $areaArr, int $orderId, int $price, int $productId, int $uid, int $areaNum): bool
  17. {
  18. if($type == 1){
  19. $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, ProductOrder::Popular);
  20. }else{
  21. $result = ProductOrder::setPopularNoAreaOrder($areaNum, $orderId, $price, $productId, $uid,ProductOrder::Popular);
  22. }
  23. return $result;
  24. }
  25. //获取产品发行量
  26. public static function getProductIssue(int $productId): int
  27. {
  28. $popularNum = ProductPopular::where('product_id', $productId)->sum('stock-num');
  29. $holdNum = ProductOrder::where('product_id', $productId)->where('status', ProductOrder::Paid)->sum('num');
  30. return bcadd($popularNum, $holdNum);
  31. }
  32. //获取产品流通量
  33. public static function getProductCirculation(int $productId): int
  34. {
  35. return Loader::model('ProductOrder')::where('product_id', $productId)->where('status', ProductOrder::Paid)->count();
  36. }
  37. //获取产品有效的订单
  38. public static function getUserProductOrder(int $uid, int $productId,string $lan): array
  39. {
  40. return Loader::model('ProductOrder')->alias('a')
  41. ->join("product_list b", "a.product_id = b.id", "left")
  42. ->field('a.id,'.'b.'.$lan.'_name as name,b.thum as img_url,a.order_no')
  43. ->where('a.product_id', $productId)
  44. ->where('a.user_id', $uid)
  45. ->where('a.status', ProductOrder::Paid)
  46. ->order('id desc')
  47. ->select();
  48. }
  49. //获取转让产品订单列表
  50. public static function getProductTransferOrder(string $orderId, int $uid, int $puid)//: array
  51. {
  52. $productOrder = Loader::model('ProductOrder');
  53. $productTransfer = Loader::model('ProductTransfer');
  54. $userModel = Loader::model('UserModel');
  55. $ledgerWalletModel = Loader::model('LedgerWalletModel');
  56. $ledgerTokenChangeModel = Loader::model('LedgerTokenChangeModel');
  57. $result = $productTransfer::whereIn('id', $orderId)->where('status', ProductTransfer::Normal)->select();
  58. if (count($result) != count(explode(",", $orderId))) {
  59. if(empty($order_info)) throw new Exception(__("订单不存在"));
  60. }
  61. $totalAmount = 0; //总金额
  62. foreach ($result as $item) {
  63. if($item->user_id == $uid) throw new Exception(__("不能购买自己寄售的商品"));
  64. $totalAmount = bcadd($totalAmount, $item->price, 2);
  65. //抢购订单
  66. $popular_order = $productOrder->where('id', $item->order_id)->find();
  67. // 生成订单
  68. $popularPrice = empty($popular_order->popular_price)? $item->price: $popular_order->popular_price;
  69. $productOrder::setCreateOrder($item->order_id, $item, $productOrder::Transfer, $uid, $item->user_id, $popular_order->order_no, $item->fees, $popularPrice);
  70. //扣除余额记录
  71. $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, -$item->price, $ledgerTokenChangeModel::Payment, $item->user_id);
  72. //增加转让人余额
  73. $amount = bcsub($item->price, $item->fees, 2);
  74. $ledgerWalletModel->changeWalletAccount($item->user_id, Asset::TOKEN, $amount, $ledgerTokenChangeModel::Receive, $uid);
  75. if($popular_order->popular_price > config('min_rwa_price')) {
  76. //扣除转让人Rwa有效
  77. $userModel::updateForRwaNum($item->user_id, $userModel::getByParentId($item->user_id), 1, '-');
  78. //增加购买Rwa有效
  79. $userModel::updateForRwaNum($uid, $puid, 1, '+');
  80. }
  81. //修改原订单状态
  82. $popular_order->status=$productOrder::Closure;
  83. $popular_order->save();
  84. //修改状态
  85. $item->status = $productTransfer::Stop;
  86. $item->save();
  87. }
  88. $chabao = $ledgerWalletModel::getWalletChaBao($uid);
  89. if(bccomp($totalAmount, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
  90. return true;
  91. }
  92. }