You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
2.7 KiB
97 lines
2.7 KiB
<?php
|
|
|
|
namespace app\store\model;
|
|
|
|
use app\common\model\User as UserModel;
|
|
use app\store\model\user\BalanceLog as BalanceLogModel;
|
|
use app\common\enum\user\balanceLog\Scene as SceneEnum;
|
|
|
|
/**
|
|
* 用户模型
|
|
* Class User
|
|
* @package app\store\model
|
|
*/
|
|
class User extends UserModel
|
|
{
|
|
/**
|
|
* 获取当前用户总数
|
|
* @param null $day
|
|
* @return int|string
|
|
* @throws \think\Exception
|
|
*/
|
|
public function getUserTotal($day = null)
|
|
{
|
|
if (!is_null($day)) {
|
|
$startTime = strtotime($day);
|
|
$this->where('create_time', '>=', $startTime)
|
|
->where('create_time', '<', $startTime + 86400);
|
|
}
|
|
return $this->where('is_delete', '=', '0')->count();
|
|
}
|
|
|
|
/**
|
|
* 获取用户列表
|
|
* @param string $nickName
|
|
* @param int $gender
|
|
* @return \think\Paginator
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function getList($nickName = '', $gender = -1)
|
|
{
|
|
// 检索条件:微信昵称
|
|
!empty($nickName) && $this->where('nickName', 'like', "%$nickName%");
|
|
// 检索条件:性别
|
|
if ($gender !== '' && $gender > -1) {
|
|
$this->where('gender', '=', (int)$gender);
|
|
}
|
|
return $this->where('is_delete', '=', '0')
|
|
->order(['create_time' => 'desc'])
|
|
->paginate(15, false, [
|
|
'query' => \request()->request()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 软删除
|
|
* @return false|int
|
|
*/
|
|
public function setDelete()
|
|
{
|
|
return $this->save(['is_delete' => 1]);
|
|
}
|
|
|
|
/**
|
|
* 用户充值
|
|
* @param string $storeUserName 当前操作人用户名
|
|
* @param $data
|
|
* @return bool
|
|
*/
|
|
public function recharge($storeUserName, $data)
|
|
{
|
|
if (!isset($data['money']) || $data['money'] === '' || $data['money'] < 0) {
|
|
$this->error = '请输入正确的金额';
|
|
return false;
|
|
}
|
|
// 判断充值方式,计算最终金额
|
|
if ($data['mode'] === 'inc') {
|
|
$diffMoney = $data['money'];
|
|
} elseif ($data['mode'] === 'dec') {
|
|
$diffMoney = -$data['money'];
|
|
} else {
|
|
$diffMoney = $data['money'] - $this['balance'];
|
|
}
|
|
// 更新记录
|
|
$this->transaction(function () use ($storeUserName, $data, $diffMoney) {
|
|
// 更新账户余额
|
|
$this->setInc('balance', $diffMoney);
|
|
// 新增余额变动记录
|
|
BalanceLogModel::add(SceneEnum::ADMIN, [
|
|
'user_id' => $this['user_id'],
|
|
'money' => $diffMoney,
|
|
'remark' => $data['remark'],
|
|
], [$storeUserName]);
|
|
});
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|