Orders.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. *
  4. * User: anyluck
  5. * Date: 2020/6/3
  6. * Time: 10:55
  7. */
  8. namespace app\web\model;
  9. use app\common\model\Mining;
  10. use app\common\model\Order;
  11. use app\common\model\OrderFrequency;
  12. use app\common\model\UserMessage;
  13. use Ramsey\Uuid\Uuid;
  14. class Orders
  15. {
  16. // todo 订单
  17. /** 发布订单列表
  18. * @param $type 0买入数据 1卖出数据 2我的购买数据 3我的卖出数据
  19. * @param int $user_id 用户id
  20. * @param int $status 0 未匹配 1 已匹配或待支付 2已上传数据 3 已完成 4 已撤销 5 交易失败
  21. * @param int $page
  22. * @param int $limit
  23. * @throws \think\db\exception\DbException
  24. */
  25. public static function order_list($type, $user_id = 0, $status = 0, $page = 1, $limit = 10)
  26. {
  27. $where = [];
  28. switch ($type) {
  29. case 0:// 买入数据
  30. $where[] = ["type", "=", 0];
  31. break;
  32. case 1: // 卖出数据
  33. $where[] = ["type", "=", 1];
  34. break;
  35. case 2:// 我的购买数据
  36. $where[] = ["buy_user", "=", $user_id];
  37. break;
  38. case 3:// 我的卖出数据
  39. $where[] = ["push", "=", $user_id];
  40. break;
  41. }
  42. if ($status != "") {
  43. $where[] = ["status", "=", $status];
  44. }
  45. $order = Order::where($where)->order("status asc,id asc")->paginate(['list_rows' => $limit, 'page' => $page])->each(function ($item) {
  46. if ($item["status"] == 0) {// 未支付结算时间计算
  47. if (time() > $item["add_time"]) {
  48. $item["stat_time"] = time() - $item["add_time"];
  49. } else {
  50. $item["stat_time"] = 0;
  51. }
  52. } else {
  53. $item["stat_time"] = 0;
  54. }
  55. // 添加时间
  56. $item["add_times"] = date("Y-m-d H:i", $item["add_time"]);
  57. if ($item["end_time"]) {
  58. $item["end_time"] = date("Y-m-d H:i", $item["end_time"]);
  59. }
  60. return $item;
  61. })->toArray();
  62. return $order;
  63. }
  64. /** 买入和卖出
  65. * @param $user 用户数据
  66. * @param $user_id 用户id
  67. * @param int $type 0 卖出 1 买入
  68. * @param int $qit_id // 卖出传买入的id
  69. * @param int $ming_id // 买入传 区块id
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public static function push_order($user,$user_id,$type=0,$qit_id=0,$ming_id=0)
  75. {
  76. // todo 未完成卖出订单时间控制,
  77. // 查看用户是否添加收账信息
  78. $userMeesage=UserMessage::where(["user_id"=>$user_id])->find();
  79. if (!$userMeesage){
  80. json_result(400,"请完善收账信息");
  81. }
  82. if ($type==0){// 直接卖出 更新原有数据
  83. $where["id"]=$qit_id;
  84. $order=Order::where($where)->find();
  85. if (!$order){
  86. json_result(400,"买入订单id错误");
  87. }
  88. // 查看矿区
  89. $mining=Mining::where(["id"=>$order->mining_id])->find();
  90. if ($user->ensure_money<$mining->ensure_money){
  91. json_result(400,"保证金不足");
  92. }
  93. if ($user->push_money<$order->number){
  94. json_result(400,"可售额度不足");
  95. }
  96. // 查看未完成订单
  97. $where_status[]=["status","in",[0,1,2]];
  98. $where_status[]=["push_user","=",$user_id];
  99. $count_order=Order::where($where_status)->count();
  100. // 查看该矿区交易次数
  101. $order_frequency=OrderFrequency::where(["user_id"=>$user_id,"mining_id"=>$order->mining_id])->find();
  102. if ($order_frequency){// 因为后台交易不加次数
  103. $count_order=$order_frequency->push+$count_order;
  104. if ($count_order==5){
  105. json_result(400,"每个区块只能卖出5次");
  106. }
  107. }
  108. $update["push_user"]=$user_id;
  109. $update["status"]=1;
  110. $update["stat_time"]=time()+4*3600;
  111. Order::where($where)->update($update);
  112. }else{// 下单买入
  113. $where_ming["id"]=$ming_id;
  114. $mining=Mining::where($where_ming)->find();
  115. if (!$mining){
  116. json_result(400,"交易区块id错误");
  117. }
  118. // 查看未完成订单
  119. $where_status[]=["status","in",[0,1,2]];
  120. $where_status[]=["buy_user","=",$user_id];
  121. $count_order=Order::where($where_status)->count();
  122. // 查看该矿区交易次数
  123. $order_frequency=OrderFrequency::where(["user_id"=>$user_id,"mining_id"=>$ming_id])->find();
  124. if ($order_frequency){// 因为后台交易不加次数
  125. $count_order=$order_frequency->buy+$count_order;
  126. if ($count_order==4){
  127. json_result(400,"每个区块只能买入4次");
  128. }
  129. }
  130. $uuid = Uuid::uuid4(time());
  131. $order = $uuid->getHex();
  132. $add["type"]=0;
  133. $add["buy_user"]='';
  134. $add["push_user"]=$user_id;
  135. $add["number"]=$mining["num"];
  136. $add["enlarge_quota"]=$mining["enlarge_quota"];
  137. $add["order"]=$order;
  138. $add["mining_id"]=$ming_id;
  139. $add["status"]=0;
  140. $add["add_time"]=time();
  141. $add["breach_money"]=$mining["breach_money"];
  142. $add["out_game"]=$mining["out_game"];
  143. Order::create($add);
  144. }
  145. json_result(200,"操作成功");
  146. }
  147. }