123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- namespace app\models\manage;
- use app\models\user\User;
- use app\models\user\UserMoney;
- use crmeb\basic\BaseModel;
- use crmeb\traits\ModelTrait;
- use think\db\Query;
- use think\Exception;
- class UserManageMoney extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'user_manage_money';
- protected static $unit = [
- 1 => ['chs' => '天', 'time' => 'day'],
- 2 => ['chs' => '月', 'time' => 'month'],
- 3 => ['chs' => '年', 'time' => 'year'],
- ];
- use ModelTrait;
- /**
- * @param int $status
- * @return UserManageMoney
- */
- public static function valid($status = 1)
- {
- return self::where('status', $status)
- ->where('send_start_time', '<', time())
- ->where(function ($query) {
- $query->where('end_time', '>', time())
- ->whereOr('end_time', null)
- ->whereOr('end_time', 0);
- });
- }
- /**
- * @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();
- $manage_money_from = sys_config('manage_money_from', 1);
- if (isset($where['status']) && $where['status'] != '') $model = $model->where('status', $where['status']);
- if (isset($where['mid']) && $where['mid'] != '') $model = $model->where('mid', $where['mid']);
- $count = $model->count();
- $data = $model->page($page, $limit)->select()->each(function ($item) {
- $item['_add_time'] = date('Y-m-d', $item['add_time']);
- $item['_end_time'] = $item['end_time'] ? date('Y-m-d', $item['end_time']) : '--';
- $item['_real_finish_time'] = $item['real_finish_time'] ? date('Y-m-d', $item['real_finish_time']) : '--';
- $item['_status'] = $item['status'] == 1 ? '理财中' : ($item['status'] == 2 ? '理财结束' : '年化准备中');
- $item['user'] = User::get($item['uid'])['nickname'] . '/' . $item['uid'];
- $item['body'] = ManageMoneyProduct::get($item['mid']);
- })->toArray();
- return compact('count', 'data', 'manage_money_from');
- }
- /**
- * @return UserManageMoney
- */
- public static function dayMiningStatusStart()
- {
- // var_dump(111);
- return self::valid(0)->update(['status' => 1]);
- }
- /**
- * @return UserManageMoney
- */
- public static function dayMiningStatusEnd()
- {
- // var_dump(111);
- return self::where('status', 'in', [0, 1])->where('end_time', '<', time())
- ->update(['status' => 2, 'real_finish_time' => time()]);
- }
- public static function daySend()
- {
- //今日已发放矿机
- BaseModel::beginTrans();
- try {
- self::dayMiningStatusEnd();
- self::dayMiningStatusStart();
- $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'];
- if (sys_config('manage_money_from', 1) == 2) {
- $product = ManageMoneyProduct::get($v['mid']);//
- $ratio = $product['ratio'];
- }
- $year = bcmul(bcdiv($ratio, 100, 4), $v['money'], 8);
- switch ($v['unit']) {
- case 2:
- $year = bcdiv($year, 12, 8);
- break;
- case 3:
- $year = bcdiv($year, 1, 8);
- break;
- default:
- $year = bcdiv($year, 365, 8);
- break;
- }
- $get_money = $v['time'] > 0 ? bcmul($v['time'], $year, 8) : $year;
- $res1 = self::bcInc($v['id'], 'all_send', $get_money, 'id');
- $res2 = UserMoney::incomeMoney($v['uid'], $v['money_type'], $get_money, 'manage_money', '理财收益', '理财收益发放');
- $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'])));
- $res = $res && $res1 && $res2 && $res3;
- }
- }
- 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)
- {
- // var_dump($id);
- $info = self::get($id);
- if (!$info || !$info['status'] == 2) return self::setErrorInfo('理财记录异常');
- // $res = self::where('id', $id)->update(['status' => 2, 'real_finish_time' => time()]);
- // var_dump($res);
- $info['status'] = 2;
- $info['real_finish_time'] = time();
- $res = $info->save();
- // var_dump($res);
- $res = $res && UserMoney::incomeMoney($info['uid'], $info['money_type'], $info['money'], 'manage_money_back', '理财结束', '理财结束资金释放');
- // var_dump(self::getLastSql());
- return $res;
- }
- }
|