UserBorrowMoney.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\models\manage;
  3. use app\models\user\UserMoney;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. use think\Exception;
  7. class UserBorrowMoney extends BaseModel
  8. {
  9. /**
  10. * 数据表主键
  11. * @var string
  12. */
  13. protected $pk = 'id';
  14. /**
  15. * 模型名称
  16. * @var string
  17. */
  18. protected $name = 'user_borrow_money';
  19. use ModelTrait;
  20. /**
  21. * @param int $status
  22. * @return UserBorrowMoney
  23. */
  24. public static function valid($status = 1)
  25. {
  26. return self::where('status', $status)
  27. ->where('send_start_time', '<', time())
  28. ->where('finish_time', null);
  29. }
  30. /**
  31. * @param int $page
  32. * @param int $limit
  33. * @param array $where
  34. * @return array
  35. */
  36. public static function getList(int $page = 1, int $limit = 10, array $where = []): array
  37. {
  38. $model = new self();
  39. if (isset($where['status']) && $where['status'] != '') $model = $model->where('status', $where['status']);
  40. $count = $model->count();
  41. $list = $model->page($page, $limit)->select()->each(function ($item) {
  42. $item['body'] = BorrowMoneyProduct::get($item['mid']);
  43. })->toArray();
  44. return compact('count', 'list');
  45. }
  46. /**
  47. * @return UserBorrowMoney
  48. */
  49. public static function dayMiningStatusStart()
  50. {
  51. return self::valid(0)->update(['status' => 1]);
  52. }
  53. public static function daySend()
  54. {
  55. //今日已发放矿机
  56. BaseModel::beginTrans();
  57. self::dayMiningStatusStart();
  58. try {
  59. $res = true;
  60. $list = self::valid()->where(function ($query) {
  61. $query->where('next_send_time', date('Y-m-d'))
  62. ->whereOr('next_send_time', null);
  63. })->select();
  64. if (count($list)) {
  65. foreach ($list as $v) {
  66. $ratio = $v['ratio'];
  67. $year = bcmul(bcdiv($ratio, 100, 4), $v['money'], 8);
  68. $year = bcdiv($year, 365, 8);
  69. $res = $res && self::bcInc($v['id'], 'all_send', $year, 'id') && (self::where('id', $v['id'])->update(['next_send_time' => date('Y-m-d', strtotime('+1 day'))]));
  70. }
  71. }
  72. if ($res) {
  73. BaseModel::commitTrans();
  74. return true;
  75. } else
  76. return self::setErrorInfo(self::getErrorInfo(), false);
  77. } catch (Exception $e) {
  78. return self::setErrorInfo($e->getMessage(), true);
  79. }
  80. }
  81. public static function endManege($id, $num)
  82. {
  83. $info = self::get($id);
  84. if (!$info || !$info['status'] == 2) return self::setErrorInfo('理财记录异常');
  85. $minfo = BorrowMoneyProduct::get($info['mid']);
  86. $money_type = init_money_type();
  87. $user_money = UserMoney::initialUserMoney($info['uid'], $info['money_type']);
  88. $all_money = bcadd($info['money'], $info['all_send'], 8);
  89. if ($num >= $all_money) {
  90. if ($user_money['money'] < $all_money) {
  91. return self::setErrorInfo('还款所需的' . $money_type[$info['money_type']] . '不足');
  92. }
  93. return self::where('id', $id)->update(['status' => 2, 'real_finish_time' => time()]) && UserMoney::expendMoney($info['uid'], $info['money_type'], $all_money, 'return_borrow_money', '全额还款' . $minfo['name']);
  94. } else {
  95. if ($user_money['money'] < $num) {
  96. return self::setErrorInfo('还款所需的' . $money_type[$info['money_type']] . '不足');
  97. }
  98. if ($num > $info['all_send']) {
  99. return self::where('id', $id)->update(['all_send' => bcsub($info['all_send'], $info['all_send'], 8)]) && UserMoney::expendMoney($info['uid'], $info['money_type'], $info['all_send'], 'return_borrow_money_sub', '还款' . $minfo['name'] . '利息' . $num)
  100. && self::where('id', $id)->update(['money' => bcsub($info['money'], bcsub($num, $info['all_send'], 8), 8)]) && UserMoney::expendMoney($info['uid'], $info['money_type'], bcsub($num, $info['all_send'], 8), 'return_borrow_money_sub', '还款' . $minfo['name'] . '本金' . $num);
  101. } else {
  102. return self::where('id', $id)->update(['all_send' => bcsub($info['all_send'], $num)]) && UserMoney::expendMoney($info['uid'], $info['money_type'], $num, 'return_borrow_money_sub', '还款' . $minfo['name'] . '利息' . $num);
  103. }
  104. }
  105. }
  106. }