123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace app\models\mining;
- use app\models\user\UserMoney;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\db\Query;
- use think\Exception;
- class MiningMachine extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'mining_machine';
- use ModelTrait;
- /**
- * @return MiningMachine
- */
- public static function valid()
- {
- return self::where('is_del', 0);
- }
- /**
- * @param int $page
- * @param int $limit
- * @param array $where
- * @return array
- */
- public static function getList(int $page = 1, int $limit = 10, array $where = []): array
- {
- $model = self::valid();
- $count = $model->count();
- $list = $model->page($page, $limit)->select()->toArray();
- return compact('count', 'list');
- }
- public static function buyMachine($id, $uid, $num)
- {
- $info = self::valid()->where('id', $id)->find();
- if (!$info) {
- return self::setErrorInfo('矿机已下架或不存在');
- }
- if ($info['stock'] < $num) {
- return self::setErrorInfo('矿机已售罄');
- }
- $money_type = init_money_type();
- $user_money = UserMoney::initialUserMoney($uid, $info['cost_money_type']);
- $user_money_stand = UserMoney::initialUserMoney($uid, $info['stand_money_type']);
- if ($info['cost_money_type'] == $info['stand_money_type']) {
- $info['cost_money'] = bcadd($info['cost_money'], $info['stand_money'], 8);
- $info['stand_money'] = 0;
- }
- $info['cost_money'] = bcmul($info['cost_money'], $num);
- $info['stand_money'] = bcmul($info['stand_money'], $num);
- if ($user_money['money'] <= $info['cost_money']) {
- return self::setErrorInfo('购买矿机所需的' . $money_type[$info['cost_money_type']] . '不足');
- }
- if ($user_money_stand['money'] <= $info['stand_money']) {
- return self::setErrorInfo('部署矿机需质押的' . $money_type[$info['stand_money_type']] . '(包含GAS)不足');
- }
- BaseModel::beginTrans();
- try {
- $res1 = UserMoney::expendMoney($uid, $info['cost_money_type'], $info['cost_money'], 'buy_machine', '购买矿机', '购买矿机' . $info['name'] . '*' . $num);
- $res2 = true;
- if ($info['stand_money'] > 0)
- $res2 = UserMoney::expendMoney($uid, $info['stand_money_type'], $info['stand_money'], 'buy_machine_stand', '部署矿机质押', '部署矿机' . $info['name'] . '*' . $num . ',' . '质押(包含GAS费)');
- if ($res1 && $res2) {
- $res = UserMiningMachine::create([
- 'uid' => $uid,
- 'mid' => $id,
- 'day_get' => $info['day_get'],
- 'get_money_type' => $info['get_money_type'],
- 'cost_money' => $info['cost_money'],
- 'cost_money_type' => $info['cost_money_type'],
- 'stand_money' => $info['stand_money'],
- 'stand_money_type' => $info['stand_money_type'],
- 'add_time' => time(),
- 'pay_time' => time(),
- 'paid' => 1,
- 'num' => $num,
- 'mining_end_time' => bcadd(time(), bcmul($info['mining_time'], 3600 * 24)),
- 'mining_start_time' => bcadd(time(), bcmul($info['stand_time'], 3600 * 24)),
- ]) && self::bcDec($id, 'stock', $num);
- if (!$res) {
- return BaseModel::setErrorInfo('购买失败', true);
- }
- } else {
- return BaseModel::setErrorInfo('支付失败', true);
- }
- BaseModel::commitTrans();
- return true;
- } catch (Exception $e) {
- return BaseModel::setErrorInfo($e->getMessage(), true);
- }
- }
- }
|