MiningMachine.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\models\mining;
  3. use app\models\lala\LalaPink;
  4. use app\models\lala\LalaPinkJoin;
  5. use app\models\lala\LalaSpExchange;
  6. use app\models\user\User;
  7. use app\models\user\UserLevel;
  8. use app\models\user\UserMoney;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\traits\ModelTrait;
  11. use think\db\Query;
  12. use think\Exception;
  13. class MiningMachine extends BaseModel
  14. {
  15. /**
  16. * 数据表主键
  17. * @var string
  18. */
  19. protected $pk = 'id';
  20. /**
  21. * 模型名称
  22. * @var string
  23. */
  24. protected $name = 'mining_machine';
  25. use ModelTrait;
  26. /**
  27. * @return MiningMachine
  28. */
  29. public static function valid()
  30. {
  31. return self::where('is_del', 0);
  32. }
  33. /**
  34. * @param int $page
  35. * @param int $limit
  36. * @param array $where
  37. * @return array
  38. */
  39. public static function getList(int $page = 1, int $limit = 10, array $where = []): array
  40. {
  41. $money_type = init_money_type();
  42. $model = self::valid();
  43. if (isset($where['get_money_type']) && $where['get_money_type'] != '') $model = $model->where('get_money_type', $where['get_money_type']);
  44. if (isset($where['type']) && $where['type'] != '') $model = $model->where('type', $where['type']);
  45. $count = $model->count();
  46. $data = $model->order('id', 'desc')->page($page, $limit)->select()->each(function ($item) use ($money_type, $where) {
  47. $item['_day_get'] = $item['day_get'] . $money_type[$item['get_money_type']] . '/T';
  48. $item['_cost_money'] = $item['cost_money'] . $money_type[$item['cost_money_type']];
  49. $item['_stand_money'] = $item['stand_money'] . $money_type[$item['get_money_type']];
  50. $item['tags'] = explode(',', $item['tags']);
  51. $item['award_ratio'] = 0;
  52. $item['lala'] = LalaPink::get($item['lala_id']);
  53. //$item['service'] = explode(',', $item['tags']);
  54. if (isset($where['uid']) && $where['uid']) {
  55. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  56. $item['luck_point'] = LalaPinkJoin::where('paid', 1)->where('lala_id', $item['lala_id'])->where('status', 1)->where('uid', $where['uid'])->count();
  57. $item['exchange_point'] = LalaSpExchange::where('lala_id', $item['lala_id'])->where('uid', $where['uid'])->sum('cost_time') + UserMiningMachine::where('uid', $where['uid'])->where('paid', 1)->where('lala_id', $item['lala_id'])->sum('cost_times');
  58. $item['award_ratio'] = UserMiningRatio::where('uid', $where['uid'])->where('mid', $item['id'])->value('ratio') ?: 0;
  59. $item['lower_award_ratio'] = UserMiningRatio::where('mid', $item['id'])->where('uid', 'in', $uids)->max('ratio') ?: 0;
  60. $item['user_service_ratio'] = UserMiningService::where('uid', $where['uid'])->where('mid', $item['id'])->value('ratio') ?: $item['service_ratio'];
  61. $item['lower_service_ratio'] = UserMiningService::where('mid', $item['id'])->where('uid', 'in', $uids)->min('ratio') ?: $item['service_ratio'];
  62. }
  63. $item['get_money_type'] = $money_type[$item['get_money_type']];
  64. $item['cost_money_type'] = $money_type[$item['cost_money_type']];
  65. })->toArray();
  66. return compact('count', 'data');
  67. }
  68. public static function buyMachine($id, $uid, $num)
  69. {
  70. $info = self::valid()->where('id', $id)->find();
  71. // if (bcmod($num, $info['step'], 2) > 0) return self::setErrorInfo('算力值错误');
  72. if ($num < $info['step']) return self::setErrorInfo('垓矿机单次购买算力不小于' . $info['step']);
  73. if (!$info) {
  74. return self::setErrorInfo('矿机已下架或不存在');
  75. }
  76. if ($info['stock'] < $num) {
  77. return self::setErrorInfo('矿机不足');
  78. }
  79. $money_type = init_money_type();
  80. $user_money = UserMoney::initialUserMoney($uid, $info['cost_money_type']);
  81. $user_money_stand = UserMoney::initialUserMoney($uid, $info['get_money_type']);
  82. if ($info['cost_money_type'] == $info['get_money_type']) {
  83. $cost_money = bcadd($info['cost_money'], $info['stand_money'], 8);
  84. $stand_money = 0;
  85. } else {
  86. $cost_money = $info['cost_money'];
  87. $stand_money = $info['stand_money'];
  88. }
  89. $cost_money = bcmul($cost_money, $num);
  90. $stand_money = bcmul($stand_money, $num);
  91. $info['cost_money'] = bcmul($info['cost_money'], $num);
  92. $info['stand_money'] = bcmul($info['stand_money'], $num);
  93. if ($user_money['money'] < $cost_money) {
  94. return self::setErrorInfo('购买矿机所需的' . $money_type[$info['cost_money_type']] . '不足');
  95. }
  96. if ($user_money_stand['money'] < $stand_money) {
  97. return self::setErrorInfo('部署矿机需质押的' . $money_type[$info['stand_money_type']] . '(包含GAS)不足');
  98. }
  99. $user_times = LalaPinkJoin::where('uid', $uid)->where('lala_id', $info['lala_id'])->where('status', 1)->where('paid', 1)->count();
  100. $used_times = LalaSpExchange::where('uid', $uid)->where('lala_id', $info['lala_id'])->sum('cost_time');
  101. $used_times += UserMiningMachine::where('uid', $uid)->where('paid', 1)->where('lala_id', $id)->sum('cost_times');
  102. if (bcsub($user_times, $used_times) < $info['cost_times']) {
  103. return self::setErrorInfo('剩余幸运值不足,无法兑换');
  104. }
  105. BaseModel::beginTrans();
  106. try {
  107. $res1 = UserMoney::expendMoney($uid, $info['cost_money_type'], $cost_money, 'buy_machine', '购买矿机', '购买矿机' . $info['name'] . ($num) . 'T');
  108. $res2 = true;
  109. if ($stand_money > 0)
  110. $res2 = UserMoney::expendMoney($uid, $info['stand_money_type'], $stand_money, 'buy_machine_stand', '部署矿机质押', '部署矿机' . $info['name'] . ($num) . 'T,' . '质押(包含GAS费)');
  111. if ($res1 && $res2) {
  112. $res = UserMiningMachine::create([
  113. 'uid' => $uid,
  114. 'mid' => $id,
  115. 'day_get' => $info['day_get'],
  116. 'get_money_type' => $info['get_money_type'],
  117. 'cost_money' => $info['cost_money'],
  118. 'cost_money_type' => $info['cost_money_type'],
  119. 'mortgage_money' => $info['mortgage_money'],
  120. 'mortgage_money_type' => $info['mortgage_money_type'],
  121. 'redeem_money' => $info['redeem_money'],
  122. 'redeem_money_type' => $info['redeem_money_type'],
  123. 'stand_money' => $info['stand_money'],
  124. 'add_time' => time(),
  125. 'pay_time' => time(),
  126. 'paid' => 1,
  127. 'num' => $num,
  128. 'cost_times' => $info['cost_times'],
  129. 'lala_id' => $info['lala_id'],
  130. 'mining_end_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'] + $info['third_step_time'] + $info['second_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)),
  131. 'mining_start_time' => bcadd(time(), bcmul($info['stand_time'], 3600 * 24)),
  132. 'second_step_start_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)),
  133. 'third_step_start_time' => bcadd(bcadd(time(), bcmul($info['first_step_time'] + $info['second_step_time'], 3600 * 24)), bcmul($info['stand_time'], 3600 * 24)),
  134. ]);
  135. if ($res) $res = $res && self::bcDec($id, 'stock', $num);
  136. if (!$res) {
  137. return BaseModel::setErrorInfo('购买失败', true);
  138. }
  139. } else {
  140. return BaseModel::setErrorInfo('支付失败', true);
  141. }
  142. // $uper = User::getUserInfo($uid);
  143. // while ($uper) {
  144. // UserLevel::setLevelComplete($uper['uid']);
  145. // $uper = User::getUserInfo($uper['spread_uid']);
  146. // }
  147. BaseModel::commitTrans();
  148. return true;
  149. } catch (Exception $e) {
  150. return BaseModel::setErrorInfo($e->getMessage(), true);
  151. }
  152. }
  153. // public static function sendRecommendAward($order)
  154. // {
  155. // $user = User::getUserInfo($order['uid']);
  156. // $spread = User::getUserInfo($user['spread_uid']);
  157. // if (!$spread) return true;
  158. // $num = $order['cost_money'];
  159. // $money_type = $order['cost_money_type'];
  160. // $send = 0;
  161. // $res = true;
  162. // while ($spread) {
  163. // $ratio = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']), 'recommend_award_ratio');
  164. // $brokerage = bcmul($num, bcdiv($ratio, 100, 4), 8);
  165. // if ($brokerage > $send) {
  166. // $res = $res && UserMoney::incomeMoney($spread['uid'], $money_type, bcsub($brokerage, $send, 8), 'buy_machine_spread_brokerage', '推荐佣金', '下级用户' . $user['nickname'] . '(' . $user['uid'] . ')' . '购买矿机' . $order['num'] . 'T,支付' . $order['cost_money'] . $order['cost_money_type'] . '获得推荐奖');
  167. // $send = $brokerage;
  168. // }
  169. // $spread = User::getUserInfo($spread['spread_uid']);
  170. // }
  171. // return $res;
  172. // }
  173. }