123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- namespace app\models\manage;
- use app\models\user\UserMoney;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\Exception;
- class UserBorrowMoney extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'user_borrow_money';
- use ModelTrait;
- /**
- * @param int $status
- * @return UserBorrowMoney
- */
- public static function valid($status = 1)
- {
- return self::where('status', $status)
- ->where('send_start_time', '<', time())
- ->where('finish_time', null);
- }
- /**
- * @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 = new self();
- if (isset($where['status']) && $where['status'] != '') $model = $model->where('status', $where['status']);
- $count = $model->count();
- $list = $model->page($page, $limit)->select()->each(function ($item) {
- $item['body'] = BorrowMoneyProduct::get($item['mid']);
- })->toArray();
- return compact('count', 'list');
- }
- /**
- * @return UserBorrowMoney
- */
- public static function dayMiningStatusStart()
- {
- return self::valid(0)->update(['status' => 1]);
- }
- public static function daySend()
- {
- //今日已发放矿机
- BaseModel::beginTrans();
- self::dayMiningStatusStart();
- try {
- $res = true;
- $list = self::valid()->where(function ($query) {
- $query->where('next_send_time', date('Y-m-d'))
- ->whereOr('next_send_time', null);
- })->select();
- if (count($list)) {
- foreach ($list as $v) {
- $ratio = $v['ratio'];
- $year = bcmul(bcdiv($ratio, 100, 4), $v['money'], 8);
- $year = bcdiv($year, 365, 8);
- $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'))]));
- }
- }
- if ($res) {
- BaseModel::commitTrans();
- return true;
- } else
- return self::setErrorInfo(self::getErrorInfo(), false);
- } catch (Exception $e) {
- return self::setErrorInfo($e->getMessage(), true);
- }
- }
- public static function endManege($id, $num)
- {
- $info = self::get($id);
- if (!$info || !$info['status'] == 2) return self::setErrorInfo('理财记录异常');
- $minfo = BorrowMoneyProduct::get($info['mid']);
- $money_type = init_money_type();
- $user_money = UserMoney::initialUserMoney($info['uid'], $info['money_type']);
- $all_money = bcadd($info['money'], $info['all_send'], 8);
- if ($num >= $all_money) {
- if ($user_money['money'] < $all_money) {
- return self::setErrorInfo('还款所需的' . $money_type[$info['money_type']] . '不足');
- }
- 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']);
- } else {
- if ($user_money['money'] < $num) {
- return self::setErrorInfo('还款所需的' . $money_type[$info['money_type']] . '不足');
- }
- if ($num > $info['all_send']) {
- 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)
- && 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);
- } else {
- 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);
- }
- }
- }
- }
|