webman_duanju/app/admin/model/UserIntegral.php

57 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\model;
use think\facade\Db;
use think\Model;
/**
* UserGroup 模型
*/
class UserIntegral extends Model
{
public static function selectUserIntegralDetailsByUserId($page, $limit, $userId)
{
$query = Db::connect(get_slave_connect_name())->name('user_integral_details');
// 添加用户ID条件如果不为空
if ($userId !== null) {
$query = $query->where('user_id', $userId);
}
$count = $query->count();
$query = $query->order('create_time', 'desc')->limit(page($page, $limit), $limit)->select()->toArray();
return [
'currPage' => $page,
'pageSize' => $limit,
'list' => convertToCamelCase($query),
'totalCount' => $count,
'totalPage' => ceil($count / $limit),
];
}
public static function updateIntegral($type, $userId, $num, $db)
{
$db = $db->name('user_integral');
$query = $db->where('user_id', $userId);
// 根据类型决定是增加还是减少积分
if ($type == 1) {
// 增加积分
$query->inc('integral_num', $num);
} elseif ($type == 2) {
// 减少积分
$query->where('integral_num', '>=', $num);
$query->dec('integral_num', $num);
}
// 执行更新
return $query->update();
}
}