UserManageMoney.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace app\models\manage;
  3. use app\models\user\User;
  4. use app\models\user\UserMoney;
  5. use crmeb\basic\BaseModel;
  6. use crmeb\traits\ModelTrait;
  7. use think\db\Query;
  8. use think\Exception;
  9. class UserManageMoney extends BaseModel
  10. {
  11. /**
  12. * 数据表主键
  13. * @var string
  14. */
  15. protected $pk = 'id';
  16. /**
  17. * 模型名称
  18. * @var string
  19. */
  20. protected $name = 'user_manage_money';
  21. protected static $unit = [
  22. 1 => ['chs' => '天', 'time' => 'day'],
  23. 2 => ['chs' => '月', 'time' => 'month'],
  24. 3 => ['chs' => '年', 'time' => 'year'],
  25. ];
  26. use ModelTrait;
  27. /**
  28. * @param int $status
  29. * @return UserManageMoney
  30. */
  31. public static function valid($status = 1)
  32. {
  33. return self::where('status', $status)
  34. ->where('send_start_time', '<', time())
  35. ->where(function ($query) {
  36. $query->where('end_time', '>', time())
  37. ->whereOr('end_time', null)
  38. ->whereOr('end_time', 0);
  39. });
  40. }
  41. /**
  42. * @param int $page
  43. * @param int $limit
  44. * @param array $where
  45. * @return array
  46. */
  47. public static function getList(int $page = 1, int $limit = 10, array $where = []): array
  48. {
  49. $model = new self();
  50. $manage_money_from = sys_config('manage_money_from', 1);
  51. if (isset($where['status']) && $where['status'] != '') $model = $model->where('status', $where['status']);
  52. if (isset($where['mid']) && $where['mid'] != '') $model = $model->where('mid', $where['mid']);
  53. $count = $model->count();
  54. $data = $model->page($page, $limit)->select()->each(function ($item) {
  55. $item['_add_time'] = date('Y-m-d', $item['add_time']);
  56. $item['_end_time'] = $item['end_time'] ? date('Y-m-d', $item['end_time']) : '--';
  57. $item['_real_finish_time'] = $item['real_finish_time'] ? date('Y-m-d', $item['real_finish_time']) : '--';
  58. $item['_status'] = $item['status'] == 1 ? '理财中' : ($item['status'] == 2 ? '理财结束' : '年化准备中');
  59. $item['user'] = User::get($item['uid'])['nickname'] . '/' . $item['uid'];
  60. $item['body'] = ManageMoneyProduct::get($item['mid']);
  61. })->toArray();
  62. return compact('count', 'data', 'manage_money_from');
  63. }
  64. /**
  65. * @return UserManageMoney
  66. */
  67. public static function dayMiningStatusStart()
  68. {
  69. // var_dump(111);
  70. return self::valid(0)->update(['status' => 1]);
  71. }
  72. /**
  73. * @return UserManageMoney
  74. */
  75. public static function dayMiningStatusEnd()
  76. {
  77. // var_dump(111);
  78. return self::where('status', 'in', [0, 1])->where('end_time', '<', time())
  79. ->update(['status' => 2, 'real_finish_time' => time()]);
  80. }
  81. public static function daySend()
  82. {
  83. //今日已发放矿机
  84. BaseModel::beginTrans();
  85. try {
  86. self::dayMiningStatusEnd();
  87. self::dayMiningStatusStart();
  88. $res = true;
  89. $list = self::valid()->where(function ($query) {
  90. $query->where('next_send_time', date('Y-m-d'))
  91. ->whereOr('next_send_time', null);
  92. })->select();
  93. if (count($list)) {
  94. foreach ($list as $v) {
  95. $ratio = $v['ratio'];
  96. if (sys_config('manage_money_from', 1) == 2) {
  97. $product = ManageMoneyProduct::get($v['mid']);//
  98. $ratio = $product['ratio'];
  99. }
  100. $year = bcmul(bcdiv($ratio, 100, 4), $v['money'], 8);
  101. switch ($v['unit']) {
  102. case 2:
  103. $year = bcdiv($year, 12, 8);
  104. break;
  105. case 3:
  106. $year = bcdiv($year, 1, 8);
  107. break;
  108. default:
  109. $year = bcdiv($year, 365, 8);
  110. break;
  111. }
  112. $get_money = $v['time'] > 0 ? bcmul($v['time'], $year, 8) : $year;
  113. $res1 = self::bcInc($v['id'], 'all_send', $get_money, 'id');
  114. $res2 = UserMoney::incomeMoney($v['uid'], $v['money_type'], $get_money, 'manage_money', '理财收益', '理财收益发放');
  115. $res3 = ($v['time'] > 0 ? self::endManege($v['id']) : (strtotime(date('Y-m-d', $v['end_time']) . '00:00:00') > strtotime(date('Y-m-d', strtotime('+1 ' . self::$unit[$v['unit']]['time'])) . '00:00:00') ? self::where('id', $v['id'])->update(['next_send_time' => date('Y-m-d', strtotime('+1 ' . self::$unit[$v['unit']]['time'])), 'last_send_time' => date('Y-m-d')]) : self::endManege($v['id'])));
  116. $res = $res && $res1 && $res2 && $res3;
  117. }
  118. }
  119. if ($res) {
  120. BaseModel::commitTrans();
  121. return true;
  122. } else
  123. return self::setErrorInfo(self::getErrorInfo(), false);
  124. } catch (Exception $e) {
  125. return self::setErrorInfo($e->getMessage(), true);
  126. }
  127. }
  128. public static function endManege($id)
  129. {
  130. // var_dump($id);
  131. $info = self::get($id);
  132. if (!$info || !$info['status'] == 2) return self::setErrorInfo('理财记录异常');
  133. // $res = self::where('id', $id)->update(['status' => 2, 'real_finish_time' => time()]);
  134. // var_dump($res);
  135. $info['status'] = 2;
  136. $info['real_finish_time'] = time();
  137. $res = $info->save();
  138. // var_dump($res);
  139. $res = $res && UserMoney::incomeMoney($info['uid'], $info['money_type'], $info['money'], 'manage_money_back', '理财结束', '理财结束资金释放');
  140. // var_dump(self::getLastSql());
  141. return $res;
  142. }
  143. }