UserMiningMachine.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. namespace app\models\mining;
  3. use app\models\user\User;
  4. use app\models\user\UserBill;
  5. use app\models\user\UserLevel;
  6. use app\models\user\UserMoney;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\traits\ModelTrait;
  9. use think\Exception;
  10. class UserMiningMachine extends BaseModel
  11. {
  12. /**
  13. * 数据表主键
  14. * @var string
  15. */
  16. protected $pk = 'id';
  17. /**
  18. * 模型名称
  19. * @var string
  20. */
  21. protected $name = 'user_mining_machine';
  22. use ModelTrait;
  23. /**
  24. * @param int $status
  25. * @return UserMiningMachine
  26. */
  27. public static function valid($status = 1)
  28. {
  29. return self::where('status', $status)->where('mining_start_time', '<=', time())
  30. ->where('mining_end_time', '>', time())->where('paid', 1);
  31. }
  32. /**
  33. * @return UserMiningMachine
  34. */
  35. public static function dayMiningStatusStart()
  36. {
  37. return self::valid(0)->update(['status' => 1]);
  38. }
  39. /**
  40. * @return bool
  41. */
  42. public static function dayMiningStatusEnd()
  43. {
  44. self::where('status', 'in', [0, 1])->where('mining_end_time', '<=', time())
  45. ->where('paid', 1)->update(['status' => 2]);
  46. self::where('status', 3)->where('mortgage_max_time', '<=', time())
  47. ->update(['status' => 4]);
  48. return true;
  49. }
  50. public static function dayMining()
  51. {
  52. //今日已发放矿机
  53. BaseModel::beginTrans();
  54. self::dayMiningStatusEnd();
  55. self::dayMiningStatusStart();
  56. try {
  57. $res = true;
  58. $send_ids = UserMining::where('add_date', date('Y-m-d'))->column('umid');
  59. // var_dump(UserMining::getLastSql());
  60. // var_dump($send_ids);
  61. //今日需发放且未发放的矿机
  62. $list = self::where('status', 'in', [1, 3])->where('mining_start_time', '<=', time() - (3600 * 24))
  63. ->where('mining_end_time', '>', time())->where('paid', 1)->whereNotIn('id', $send_ids)->select();
  64. // var_dump($list);
  65. // var_dump($list);
  66. // var_dump($list);
  67. if (count($list)) {
  68. // var_dump($list->toArray());
  69. $money_types = MiningMachine::group('get_money_type')->field('COUNT(id),get_money_type')->select();
  70. $day_gets = [];
  71. foreach ($money_types as $v) {
  72. if ($v['get_money_type'] == 'XCH') {
  73. $day_gets[$v['get_money_type']] = get_hpool_price($v['get_money_type']);
  74. } else if ($v['get_money_type'] == 'FIL') {
  75. $day_gets[$v['get_money_type']] = get_fil_price() ?: 0;
  76. } else {
  77. $day_gets[$v['get_money_type']] = get_hpool_price($v['get_money_type']);
  78. }
  79. }
  80. foreach ($list as $v) {
  81. $machine = MiningMachine::get($v['mid']);
  82. //var_dump(date('Y-m-d H:i:s',$v['third_step_start_time']));
  83. //var_dump($day_gets);
  84. //exit();
  85. if (time() > $v['third_step_start_time']) {
  86. //第三阶段 释放质押和未发放完的部分
  87. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  88. if ($v['get_money_type'] == 'XCH')
  89. $day_get = bcmul($day_get, 0.84, 8);
  90. $send_stand = bcdiv($v['stand_money'], $machine['third_step_time'], 8);
  91. $day_unlock = 0;
  92. $locks = UserMining::where('umid', $v['id'])->where('add_date', '<>', strtotime('Y-m-d'))->where('lock_money', '>', 0)->select();
  93. foreach ($locks as $vv) {
  94. if ($vv['lock_money'] >= $vv['day_unlock_money']) {
  95. $day_unlock = bcadd($day_unlock, $vv['day_unlock_money'], 8);
  96. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['day_unlock_money'])->update();
  97. } else {
  98. $day_unlock = bcadd($day_unlock, $vv['lock_money'], 8);
  99. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['lock_money'])->update();
  100. }
  101. }
  102. if (bcadd($send_stand, $day_unlock, 8)) {
  103. $res = $res && UserMining::create([
  104. 'umid' => $v['id'],
  105. 'get_money' => bcadd($send_stand, $day_unlock, 8),
  106. 'get_money_type' => $v['get_money_type'],
  107. 'add_time' => time(),
  108. 'unstand' => $send_stand,
  109. 'unlock' => $day_unlock,
  110. 'step' => 3,
  111. 'add_date' => date('Y-m-d'),
  112. ]);
  113. }
  114. if ($day_unlock > 0) {
  115. $res = $res && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], $day_unlock, 'mining', '服务器产出', '第二阶段锁定金释放' . $day_unlock . init_money_type()[$v['get_money_type']], $v['id'], $v['status'] == 1 ? 1 : 0)
  116. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], $day_unlock, $v['status'] == 1 ? 1 : 0, $v['id']);
  117. // && self::sendSystemAward($v['uid'], $v['get_money_type'], $day_unlock);
  118. }
  119. if ($send_stand > 0) {
  120. $res = $res && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], $send_stand, 'mining', '服务器产出', '第一阶段质押金释放' . $send_stand . init_money_type()[$v['get_money_type']], $v['id'], $v['status'] == 1 ? 1 : 0);
  121. }
  122. } elseif (time() > $v['second_step_start_time']) {
  123. //var_dump($day_gets);
  124. $day_unlock = 0;
  125. $locks = UserMining::where('umid', $v['id'])->where('add_date', '<>', strtotime('Y-m-d'))->where('lock_money', '>', 0)->select();
  126. //var_dump($locks->toArray());
  127. foreach ($locks as $vv) {
  128. if ($vv['lock_money'] >= $vv['day_unlock_money']) {
  129. $day_unlock = bcadd($day_unlock, $vv['day_unlock_money'], 8);
  130. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['day_unlock_money'])->update();
  131. } else {
  132. $day_unlock = bcadd($day_unlock, $vv['lock_money'], 8);
  133. UserMining::where('id', $vv['id'])->dec('lock_money', $vv['lock_money'])->update();
  134. }
  135. }
  136. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  137. //var_dump($day_get);
  138. //exit();
  139. if ($v['get_money_type'] == 'XCH')
  140. $day_get = bcmul($day_get, 0.84, 8);
  141. $service_ratio = $machine['service_ratio'];
  142. $service_ratio = UserMiningService::where('uid', $v['uid'])->where('mid', $machine['id'])->value('ratio') ?: $service_ratio;
  143. $service_ratio = bcsub(1, bcdiv($service_ratio, 100, 4), 4);
  144. $day_service_get = bcmul($service_ratio, $day_get, 8);
  145. $second_step_get_ratio = bcdiv($machine['second_step_get_ratio'], 100, 4);
  146. $day_real_get = bcmul($second_step_get_ratio, $day_service_get, 8);
  147. $day_lock = bcsub($day_service_get, $day_real_get, 8);
  148. if ($day_real_get > 0) {
  149. $res = $res && UserMining::create([
  150. 'umid' => $v['id'],
  151. 'get_money' => bcadd($day_real_get, $day_unlock, 8),
  152. 'get_money_type' => $v['get_money_type'],
  153. 'add_time' => time(),
  154. 'add_date' => date('Y-m-d'),
  155. 'lock_money' => $day_lock,
  156. 'all_lock' => $day_lock,
  157. 'unlock' => $day_unlock,
  158. 'step' => 2,
  159. 'day_unlock_money' => bcdiv($day_lock, $machine['third_step_time'], 8),
  160. ]) && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8), 'mining', '服务器产出', '第二阶段每日释放' . $day_get . init_money_type()[$v['get_money_type']] . ',扣除技术服务费后实际到账' . $day_real_get . init_money_type()[$v['get_money_type']] . ($day_lock > 0 ? ',锁定' . $day_lock . init_money_type()[$v['get_money_type']] : '') . ($day_unlock > 0 ? ',解锁第二阶段锁定金' . $day_unlock . init_money_type()[$v['get_money_type']] : ''), $v['id'], $v['status'] == 1 ? 1 : 0)
  161. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8), $v['status'] == 1 ? 1 : 0, $v['id']);
  162. // && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], $day_get)
  163. // && self::sendSystemAward($v['uid'], $v['get_money_type'], bcadd($day_real_get, $day_unlock, 8));
  164. }
  165. } else {
  166. // var_dump($day_gets);
  167. $day_get = bcmul($machine['day_get'] > 0 ? $machine['day_get'] : $day_gets[$machine['get_money_type']], $v['num'], 8);
  168. // var_dump($day_get);
  169. if ($v['get_money_type'] == 'XCH')
  170. $day_get = bcmul($day_get, 0.84, 8);
  171. $service_ratio = $machine['service_ratio'];
  172. $service_ratio = UserMiningService::where('uid', $v['uid'])->where('mid', $machine['id'])->value('ratio') ?: $service_ratio;
  173. $service_ratio = bcsub(1, bcdiv($service_ratio, 100, 4), 4);
  174. $day_service_get = bcmul($service_ratio, $day_get, 8);
  175. $first_step_get_ratio = bcdiv($machine['first_step_get_ratio'], 100, 4);
  176. $day_real_get = bcmul($first_step_get_ratio, $day_service_get, 8);
  177. $day_stand = bcsub($day_service_get, $day_real_get, 8);
  178. // var_dump($day_real_get);
  179. // var_dump(compact('day_get', 'service_ratio', 'day_service_get', 'first_step_get_ratio', 'day_real_get', 'day_stand'));
  180. if ($day_real_get > 0) {
  181. $res = $res && UserMining::create([
  182. 'umid' => $v['id'],
  183. 'get_money' => $day_real_get,
  184. 'get_money_type' => $v['get_money_type'],
  185. 'add_time' => time(),
  186. 'step' => 1,
  187. 'add_date' => date('Y-m-d'),
  188. ]) && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], bcadd($day_real_get, 0, 8), 'mining', '服务器产出', '第一阶段每日释放' . $day_get . init_money_type()[$v['get_money_type']] . ',扣除技术服务费后实际到账' . $day_real_get . init_money_type()[$v['get_money_type']] . ($day_stand > 0 ? ',质押' . $day_stand . init_money_type()[$v['get_money_type']] : ''), $v['id'], $v['status'] == 1 ? 1 : 0)
  189. && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], bcadd($day_real_get, 0, 8), $v['status'] == 1 ? 1 : 0, $v['id']);
  190. // && self::sendGroupAward($v['uid'], $v['mid'], $v['get_money_type'], $day_get)
  191. // && self::sendSystemAward($v['uid'], $v['get_money_type'], bcadd($day_real_get, 0, 8));
  192. }
  193. if ($day_stand > 0) {
  194. $res = $res && self::where('id', $v['id'])->inc('stand_money', $day_stand)->update();
  195. }
  196. }
  197. MiningLog::create(['day' => date('Y-m-d'), 'mid' => $v['id'], 'mining' => $day_get]);
  198. }
  199. }
  200. // var_dump($res);
  201. if ($res) {
  202. BaseModel::commitTrans();
  203. return true;
  204. } else
  205. return BaseModel::setErrorInfo(self::getErrorInfo(), true);
  206. } catch (Exception $e) {
  207. //var_dump($e->getMessage());
  208. return BaseModel::setErrorInfo($e->getMessage(), true);
  209. }
  210. }
  211. public static function sendGroupAward($uid, $mid, $money_type, $num, $status, $umid)
  212. {
  213. $user = User::getUserInfo($uid);
  214. $spread = User::getUserInfo($user['spread_uid']);
  215. $ratio = UserMiningService::where('uid', $uid)->where('mid', $mid)->value('ratio');
  216. if (!$ratio) $ratio = MiningMachine::where('id', $mid)->value('service_ratio');
  217. $num = bcdiv($num, bcsub(1, bcdiv($ratio, 100, 4), 4), 8);
  218. $res = true;
  219. //var_dump($ratio);
  220. //var_dump($num);
  221. //var_dump($user);
  222. //var_dump($spread);
  223. // $send = 0;
  224. $v = 0;
  225. while ($spread) {
  226. $ratio2 = UserMiningService::where('uid', $spread['uid'])->where('mid', $mid)->value('ratio');
  227. if (!$ratio2) $ratio2 = MiningMachine::where('id', $mid)->value('service_ratio');
  228. //var_dump($ratio2);
  229. if ($ratio2 >= $ratio) {
  230. $spread = User::getUserInfo($spread['spread_uid']);
  231. } else {
  232. $brokerage = bcmul($num, bcdiv(bcsub($ratio, $ratio2), 100, 4), 8);
  233. if ($brokerage > 0) {
  234. //if (bcsub($brokerage, $send, 8) > 0) {
  235. $v++;
  236. //$v = UserLevel::getUserLevelInfo(UserLevel::getUserLevel($spread['uid']), 'level_id');
  237. $res = $res && UserMoney::incomeMoney($spread['uid'], $money_type, $brokerage, 'group_create_brokerage', '分享算力', '获得分享算力V' . $v . ':' . $user['nickname'] . '(' . $user['uid'] . ')' . '今日矿机收益分红(' . bcsub($ratio, $ratio2) . '%×' . $num . ')' . ',合计' . $brokerage . init_money_type()[$money_type], $umid, $status);
  238. // }
  239. // $send = $brokerage;
  240. $ratio = $ratio2;
  241. }
  242. $spread = User::getUserInfo($spread['spread_uid']);
  243. }
  244. }
  245. return $res;
  246. }
  247. // public static function sendSystemAward($uid, $money_type, $num)
  248. // {
  249. // $user = User::getUserInfo($uid);
  250. // $ratio = sys_config('system_create_award_ratio', 0);
  251. // if (!$ratio) return true;
  252. // $brokerage = bcmul($num, bcdiv($ratio, 100, 4), 8);
  253. // if ($brokerage <= 0) return true;
  254. // $users = UserLevel::valiWhere()->where(function ($query) {
  255. // $query->where('is_forever', 1)->whereOr('valid_time', '>', time());
  256. // })->where('system_create_award_ratio', 1)->column('uid');
  257. // foreach ($users as &$v) {
  258. // if (!UserLevel::getUserLevelInfo(UserLevel::getUserLevel($v), 'system_create_award_ratio')) {
  259. // unset($v);
  260. // }
  261. // }
  262. // $res = true;
  263. // if (count($users) > 0)
  264. // $brokerage = bcdiv($brokerage, count($users), 8);
  265. // else
  266. // $brokerage = 0;
  267. // if ($brokerage > 0) {
  268. // foreach ($users as $v) {
  269. // $res = $res && UserMoney::incomeMoney($v, $money_type, $brokerage, 'system_create_brokerage', '系统产币分红', '获得系统用户' . $user['nickname'] . '(' . $user['uid'] . ')' . '今日矿机收益分红');
  270. // }
  271. // }
  272. // return $res;
  273. // }
  274. public static function getList($where)
  275. {
  276. $model = self::getOrderWhere($where, self::alias('a')
  277. ->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')
  278. ->field('a.*,r.nickname,r.phone,r.spread_uid');
  279. $model = $model->order('a.id desc');
  280. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()->each(function ($item) use ($where) {
  281. $item['machine'] = MiningMachine::get($item['mid']);
  282. $item['user'] = User::getUserInfo($item['uid'])['nickname'] . '/' . $item['uid'];
  283. $item['_user'] = User::getUserInfo($item['uid']);
  284. $item['_status'] = $item['status'] == 1 ? "挖矿中" : ($item['status'] == 2 ? "已结束" : ($item['status'] == 3 ? "已抵押" : "等待中"));
  285. $item['sum_get'] = UserMining::where('umid', $item['id'])->sum('get_money');
  286. $item['sum_lock'] = UserMining::where('umid', $item['id'])->sum('lock_money');
  287. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  288. $item['_stop_time'] = date('Y-m-d H:i:s', $item['mining_end_time']);
  289. $item['mining_end_time'] = $item['_stop_time'];
  290. $item['_cost_money'] = $item['cost_money'] * 1 . init_money_type()[$item['cost_money_type']];
  291. $item['_stand_money'] = $item['stand_money'] * 1 . init_money_type()[$item['get_money_type']];
  292. $item['mining_end_time'] = $item['_stop_time'];
  293. $item['_start_time'] = date('Y-m-d H:i:s', $item['mining_start_time']);
  294. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  295. if (isset($where['uid']) && $where['uid']) {
  296. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  297. $item['award_ratio'] = UserMiningRatio::where('uid', $where['uid'])->where('mid', $item['mid'])->value('ratio') ?: 0;
  298. $item['lower_award_ratio'] = UserMiningRatio::where('mid', $item['id'])->where('uid', 'in', $uids)->max('ratio') ?: 0;
  299. $item['user_service_ratio'] = UserMiningService::where('uid', $where['uid'])->where('mid', $item['mid'])->value('ratio') ?: $item['machine']['service_ratio'];
  300. $item['lower_service_ratio'] = UserMiningService::where('mid', $item['mid'])->where('uid', 'in', $uids)->min('ratio') ?: $item['machine']['service_ratio'];
  301. }
  302. $item['machine']['get_money_type'] = get_money_name($item['machine']['get_money_type']);
  303. $item['machine']['cost_money_type'] = get_money_name($item['machine']['cost_money_type']);
  304. $item['mortgage_money_type'] = get_money_name($item['mortgage_money_type']);
  305. $item['redeem_money_type'] = get_money_name($item['redeem_money_type']);
  306. })) && count($data) ? $data->toArray() : [];
  307. $count = self::getOrderWhere($where, self::alias('a')->join('user r', 'r.uid=a.uid', 'LEFT'), 'a.', 'r')->count();
  308. return compact('count', 'data');
  309. }
  310. public static function orderCount()
  311. {
  312. $data['wf'] = self::statusByWhere(-1, new self())->count();
  313. $data['dd'] = self::statusByWhere(0, new self())->count();
  314. $data['wk'] = self::statusByWhere(1, new self())->count();
  315. $data['js'] = self::statusByWhere(2, new self())->count();
  316. return $data;
  317. }
  318. public static function statusByWhere($status, $model = null, $alert = '')
  319. {
  320. if ($model == null) $model = new self;
  321. if ('' === $status)
  322. return $model;
  323. else if ($status == 0)//等待种
  324. return $model->where($alert . 'paid', 1)->where($alert . 'status', 0);
  325. else if ($status == 1)//已支付 未发货
  326. return $model->where($alert . 'paid', 1)->where($alert . 'status', 1);
  327. else if ($status == 2)//已支付 待收货
  328. return $model->where($alert . 'paid', 1)->where($alert . 'status', 2);
  329. else if ($status == -1)//未支付
  330. return $model->where($alert . 'paid', 0);
  331. else
  332. return $model;
  333. }
  334. public static function getBadge($where)
  335. {
  336. $price = self::getOrderPrice($where);
  337. $data = [
  338. [
  339. 'name' => '订单数量',
  340. 'field' => '件',
  341. 'count' => $price['count_sum'],
  342. 'background_color' => 'layui-bg-blue',
  343. 'col' => 2
  344. ],
  345. [
  346. 'name' => '算力总量',
  347. 'field' => 'T',
  348. 'count' => $price['total_num'],
  349. 'background_color' => 'layui-bg-blue',
  350. 'col' => 2
  351. ],
  352. ];
  353. $money_type = init_money_type();
  354. foreach ($money_type as $k => $v) {
  355. $data = array_merge(
  356. $data,
  357. [[
  358. 'name' => $v . '购买支付',
  359. 'field' => $k,
  360. 'count' => $price[$k . '_pay'] * 1,
  361. 'background_color' => 'layui-bg-blue',
  362. 'col' => 2
  363. ], [
  364. 'name' => $v . '质押',
  365. 'field' => $k,
  366. 'count' => $price[$k . '_stand'] * 1,
  367. 'background_color' => 'layui-bg-blue',
  368. 'col' => 2
  369. ]]
  370. );
  371. }
  372. return $data;
  373. }
  374. public static function getOrderWhere($where, $model, $aler = '', $join = '')
  375. {
  376. $model = self::statusByWhere($where['status'] ?? '', $model, $aler);
  377. if (isset($where['uid']) && $where['uid'] != '' && $where['uid'] != []) {
  378. if (is_array($where['uid']))
  379. $model = $model->where($aler . 'uid', 'in', $where['uid']);
  380. else
  381. $model = $model->where($aler . 'uid', $where['uid']);
  382. }
  383. if (isset($where['real_name']) && $where['real_name'] != '') {
  384. $model = $model->where($aler . 'id' . ($join ? '|' . $join . '.nickname|' . $join . '.uid|' . $join . '.phone' : ''), 'LIKE', "%$where[real_name]%");
  385. }
  386. if (isset($where['data']) && $where['data'] !== '') {
  387. $model = self::getModelTime($where, $model, $aler . 'add_time');
  388. }
  389. if (isset($where['cost_money_type']) && $where['cost_money_type'] !== '') {
  390. $model = $model->where($aler . 'cost_money_type', $where['cost_money_type']);
  391. }
  392. // var_dump($model);
  393. return $model;
  394. }
  395. /**
  396. * 处理订单金额
  397. * @param $where
  398. * @return array
  399. */
  400. public static function getOrderPrice($where)
  401. {
  402. $model = new self;
  403. $price = [];
  404. $price['count_sum'] = 0;//支付金额
  405. $price['total_num'] = 0;
  406. $money_type = init_money_type();
  407. foreach ($money_type as $k => $v) {
  408. $whereData['cost_money_type'] = $k;
  409. $sumNumber = self::getOrderWhere(array_merge($where, $whereData), $model)->field([
  410. 'sum(cost_money) as sum',
  411. 'sum(stand_money) as stand_sum',
  412. ])->find();
  413. // var_dump($sumNumber);
  414. // var_dump(self::getLastSql());
  415. $price[$k . '_pay'] = $sumNumber['sum'];
  416. $price[$k . '_stand'] = $sumNumber['stand_sum'];
  417. }
  418. // var_dump($where);
  419. $sumNumber = self::getOrderWhere($where, $model)->field([
  420. 'sum(num) as sum_total_num',
  421. 'count(id) as count_sum',
  422. ])->find();
  423. if ($sumNumber) {
  424. $price['count_sum'] = $sumNumber['count_sum'];
  425. $price['total_num'] = $sumNumber['sum_total_num'];
  426. }
  427. return $price;
  428. }
  429. public static function mortgageMachine($id, $uid)
  430. {
  431. $info = self::where('status', 'in', [0, 1])->where('paid', 1)->where('id', $id)->find();
  432. if (!$info) return self::setErrorInfo('可抵押矿机不存在');
  433. $info = $info->toArray();
  434. if ($uid != $info['uid']) return self::setErrorInfo('矿机错误');
  435. if ($info['mortgage_money'] == 0) return self::setErrorInfo('矿机不可抵押');
  436. if ($info['mortgage_time'] > 0) return self::setErrorInfo('矿机已被抵押过,不可再次抵押');
  437. $info['name'] = MiningMachine::where('id', $info['mid'])->value('name');
  438. $redeem_day = MiningMachine::where('id', $info['mid'])->value('redeem_day');
  439. $mortgage_money = bcmul($info['mortgage_money'], $info['num']);
  440. BaseModel::beginTrans();
  441. try {
  442. UserMoney::incomeMoney($uid, $info['mortgage_money_type'], $mortgage_money, 'mortgage_machine', '矿机抵押', '抵押' . $info['name'] . '获得' . $mortgage_money . $info['mortgage_money_type']);
  443. self::where('id', $id)->update(['status' => 3, 'mortgage_time' => time(), 'mortgage_max_time' => ($redeem_day > 0 ? (time() + $redeem_day * 24 * 3600) : 0)]);
  444. BaseModel::commitTrans();
  445. return true;
  446. } catch (Exception $e) {
  447. return BaseModel::setErrorInfo($e->getMessage(), true);
  448. }
  449. }
  450. public static function redeemMachine($id, $uid)
  451. {
  452. $info = self::where('status', 3)->where('paid', 1)->where('id', $id)->find();
  453. if (!$info) return self::setErrorInfo('可赎回矿机不存在');
  454. $info = $info->toArray();
  455. if ($uid != $info['uid']) return self::setErrorInfo('矿机错误');
  456. if ($info['redeem_money'] == 0) return self::setErrorInfo('矿机不可赎回');
  457. $redeem_money = bcmul($info['redeem_money'], $info['num']);
  458. $money_type = init_money_type();
  459. $user_money = UserMoney::initialUserMoney($uid, $info['redeem_money_type']);
  460. if ($user_money['money'] < $redeem_money) {
  461. return self::setErrorInfo('赎回矿机所需的' . $money_type[$info['redeem_money_type']] . '不足');
  462. }
  463. if ($info['mortgage_max_time'] > 0 && $info['mortgage_max_time'] < time()) {
  464. return self::setErrorInfo('赎回期已过,矿机已回收');
  465. }
  466. $info['name'] = MiningMachine::where('id', $info['mid'])->value('name');
  467. BaseModel::beginTrans();
  468. try {
  469. UserMoney::expendMoney($uid, $info['redeem_money_type'], $redeem_money, 'redeem_machine', '赎回矿机', '赎回矿机消耗' . $info['name'] . $redeem_money . $info['redeem_money_type']);
  470. if ($info['mining_start_time'] <= time() && $info['mining_end_time'] > time())
  471. self::where('id', $id)->update(['status' => 1]);
  472. else if ($info['mining_start_time'] > time()) {
  473. self::where('id', $id)->update(['status' => 0]);
  474. } else {
  475. self::where('id', $id)->update(['status' => 2]);
  476. }
  477. self::sendRedeemMining($info['id']);
  478. BaseModel::commitTrans();
  479. return true;
  480. } catch (Exception $e) {
  481. return BaseModel::setErrorInfo($e->getMessage(), true);
  482. }
  483. }
  484. public static function sendRedeemMining($link_id)
  485. {
  486. $list = UserBill::where(['pm' => 1, 'status' => 0])
  487. ->where('link_id', $link_id)
  488. ->where('type', 'in', ['mining', 'group_create_brokerage'])
  489. ->select();
  490. if ($list) {
  491. self::beginTrans();
  492. try {
  493. foreach ($list as $v) {
  494. UserMoney::sendMoney($v['id']);
  495. }
  496. self::commitTrans();
  497. return true;
  498. } catch (Exception $e) {
  499. self::rollbackTrans();
  500. return false;
  501. }
  502. }
  503. return true;
  504. }
  505. }