UserMiningMachine.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Exception;
  7. class UserMiningMachine extends BaseModel
  8. {
  9. /**
  10. * 数据表主键
  11. * @var string
  12. */
  13. protected $pk = 'id';
  14. /**
  15. * 模型名称
  16. * @var string
  17. */
  18. protected $name = 'user_mining_machine';
  19. use ModelTrait;
  20. /**
  21. * @param int $status
  22. * @return UserMiningMachine
  23. */
  24. public static function valid($status = 1)
  25. {
  26. return self::where('status', $status)->where('mining_start_time', '>', time())
  27. ->where('mining_end_time', '<', time())->where('paid', 1);
  28. }
  29. /**
  30. * @return UserMiningMachine
  31. */
  32. public static function dayMiningStatusStart()
  33. {
  34. return self::valid(0)->update(['status' => 1]);
  35. }
  36. /**
  37. * @return UserMiningMachine
  38. */
  39. public static function dayMiningStatusEnd()
  40. {
  41. return self::where('status', [0, 1])->where('mining_end_time', '>', time())
  42. ->where('paid', 1)->update(['status' => 2]);
  43. }
  44. public static function dayMining()
  45. {
  46. //今日已发放矿机
  47. BaseModel::beginTrans();
  48. self::dayMiningStatusEnd();
  49. self::dayMiningStatusStart();
  50. try {
  51. $res = true;
  52. $send_ids = UserMining::where('add_date', strtotime('Y-m-d'))->column('umid');
  53. //今日需发放且未发放的矿机
  54. $list = self::valid()->whereNotIn('id', $send_ids)->select();
  55. if (count($list)) {
  56. foreach ($list as $v) {
  57. $res = $res && UserMining::create([
  58. 'umid' => $v['id'],
  59. 'get_money' => bcmul($v['day_get'], $v['num'], 8),
  60. 'get_money_type' => $v['get_money_type'],
  61. 'add_time' => time(),
  62. 'add_date' => date('Y-m-d'),
  63. ]) && UserMoney::incomeMoney($v['uid'], $v['get_money_type'], bcmul($v['day_get'], $v['num'], 8), 'mining', '挖矿', '本日挖矿释放');
  64. }
  65. }
  66. if ($res) {
  67. BaseModel::commitTrans();
  68. return true;
  69. } else
  70. return self::setErrorInfo(self::getErrorInfo(), false);
  71. } catch (Exception $e) {
  72. return self::setErrorInfo($e->getMessage(), true);
  73. }
  74. }
  75. }