MiningMachine.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\models\mining;
  3. use app\models\user\UserMoney;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. use think\db\Query;
  7. use think\Exception;
  8. class MiningMachine extends BaseModel
  9. {
  10. /**
  11. * 数据表主键
  12. * @var string
  13. */
  14. protected $pk = 'id';
  15. /**
  16. * 模型名称
  17. * @var string
  18. */
  19. protected $name = 'mining_machine';
  20. use ModelTrait;
  21. /**
  22. * @return MiningMachine
  23. */
  24. public static function valid()
  25. {
  26. return self::where('is_del', 0);
  27. }
  28. /**
  29. * @param int $page
  30. * @param int $limit
  31. * @param array $where
  32. * @return array
  33. */
  34. public static function getList(int $page = 1, int $limit = 10, array $where = []): array
  35. {
  36. $model = self::valid();
  37. $count = $model->count();
  38. $list = $model->page($page, $limit)->select()->toArray();
  39. return compact('count', 'list');
  40. }
  41. public static function buyMachine($id, $uid, $num)
  42. {
  43. $info = self::valid()->where('id', $id)->find();
  44. if (!$info) {
  45. return self::setErrorInfo('矿机已下架或不存在');
  46. }
  47. if ($info['stock'] < $num) {
  48. return self::setErrorInfo('矿机已售罄');
  49. }
  50. $money_type = init_money_type();
  51. $user_money = UserMoney::initialUserMoney($uid, $info['cost_money_type']);
  52. $user_money_stand = UserMoney::initialUserMoney($uid, $info['stand_money_type']);
  53. if ($info['cost_money_type'] == $info['stand_money_type']) {
  54. $info['cost_money'] = bcadd($info['cost_money'], $info['stand_money'], 8);
  55. $info['stand_money'] = 0;
  56. }
  57. $info['cost_money'] = bcmul($info['cost_money'], $num);
  58. $info['stand_money'] = bcmul($info['stand_money'], $num);
  59. if ($user_money['money'] <= $info['cost_money']) {
  60. return self::setErrorInfo('购买矿机所需的' . $money_type[$info['cost_money_type']] . '不足');
  61. }
  62. if ($user_money_stand['money'] <= $info['stand_money']) {
  63. return self::setErrorInfo('部署矿机需质押的' . $money_type[$info['stand_money_type']] . '(包含GAS)不足');
  64. }
  65. BaseModel::beginTrans();
  66. try {
  67. $res1 = UserMoney::expendMoney($uid, $info['cost_money_type'], $info['cost_money'], 'buy_machine', '购买矿机', '购买矿机' . $info['name'] . '*' . $num);
  68. $res2 = true;
  69. if ($info['stand_money'] > 0)
  70. $res2 = UserMoney::expendMoney($uid, $info['stand_money_type'], $info['stand_money'], 'buy_machine_stand', '部署矿机质押', '部署矿机' . $info['name'] . '*' . $num . ',' . '质押(包含GAS费)');
  71. if ($res1 && $res2) {
  72. $res = UserMiningMachine::create([
  73. 'uid' => $uid,
  74. 'mid' => $id,
  75. 'day_get' => $info['day_get'],
  76. 'get_money_type' => $info['get_money_type'],
  77. 'cost_money' => $info['cost_money'],
  78. 'cost_money_type' => $info['cost_money_type'],
  79. 'stand_money' => $info['stand_money'],
  80. 'stand_money_type' => $info['stand_money_type'],
  81. 'add_time' => time(),
  82. 'pay_time' => time(),
  83. 'paid' => 1,
  84. 'num' => $num,
  85. 'mining_end_time' => bcadd(time(), bcmul($info['mining_time'], 3600 * 24)),
  86. 'mining_start_time' => bcadd(time(), bcmul($info['stand_time'], 3600 * 24)),
  87. ]) && self::bcDec($id, 'stock', $num);
  88. if (!$res) {
  89. return BaseModel::setErrorInfo('购买失败', true);
  90. }
  91. } else {
  92. return BaseModel::setErrorInfo('支付失败', true);
  93. }
  94. BaseModel::commitTrans();
  95. return true;
  96. } catch (Exception $e) {
  97. return BaseModel::setErrorInfo($e->getMessage(), true);
  98. }
  99. }
  100. }