Files
zzp-server/app/adminapi/logic/finance/RechargeRecordLogic.php
2026-01-19 14:19:22 +08:00

279 lines
8.3 KiB
PHP

<?php
namespace app\adminapi\logic\finance;
use app\common\service\{UtilsService};
use app\common\model\finance\{RechargeRecord,WithdrawRecord};
use app\common\model\goods\{GoodsRecord};
use app\common\model\user\{User,UserRelationAgent};
use app\common\model\setting\RechargeMethod;
use app\common\logic\BaseLogic;
use think\facade\Db;
/**
* 充值记录逻辑
* Class RechargeRecordLogic
* @package app\adminapi\logic\finance
*/
class RechargeRecordLogic extends BaseLogic
{
/**
* @notes 删除充值记录
* @param array $params
* @return bool
* @author bd
* @date 2024/01/31 14:07
*/
public static function delete(array $params): bool
{
return RechargeRecord::destroy($params['id']);
}
/**
* @notes 同意充值
* @param array $params
* @return bool
* @author bd
* @date 2024/01/31 14:07
*/
public static function agree(array $params): bool
{
Db::startTrans();
try {
$record = RechargeRecord::find($params['id']);
if ($record->isEmpty()) {
throw new \Exception('记录不存在');
}
if ($record['status']!=0) {
throw new \Exception('状态异常');
}
$user = User::where(['id' => $record['user_id']])->findOrEmpty();
if ($record->isEmpty()) {
throw new \Exception('用户不存在');
}
RechargeRecord::update([
'id' => $params['id'],
'status' => 1
]);
//记录日志
UtilsService::user_finance_add(
$record['user_id'],
1,
1,
$record['amount'],
$record['sn'],
'',
1//冻结
);
//用户资金修改
UtilsService::user_money_change($record['user_id'], 1, $record['amount'],'user_money');
//充值金额增加
UtilsService::user_money_change($record['user_id'], 1, $record['amount'],'total_recharge');
//团队充值奖励
// UtilsService::team_reward_add($record['user_id'],$record['amount'],1);
//充值活动奖励
UtilsService::activity_reward_add($record['user_id'],$record['amount']);
//更新充值记录用户余额
$user = User::where(['id' => $record['user_id']])->findOrEmpty();
if ($user->isEmpty()) {
throw new \Exception('用户不存在');
}
//充值次数+1
User::update([
'id' => $user['id'],
'recharge_num' => $user['recharge_num'] + 1
]);
RechargeRecord::update([
'id' => $params['id'],
'user_money' => $user['user_money']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 拒绝充值
* @param array $params
* @return bool
* @author bd
* @date 2024/01/31 14:07
*/
public static function refuse(array $params): bool
{
Db::startTrans();
try {
$record = RechargeRecord::find($params['id']);
if ($record->isEmpty()) {
throw new \Exception('记录不存在');
}
if ($record['status']!=0) {
throw new \Exception('状态异常');
}
RechargeRecord::update([
'id' => $params['id'],
'status' => 2
]);
//更新充值记录用户余额
$user = User::where(['id' => $record['user_id']])->findOrEmpty();
if ($user->isEmpty()) {
throw new \Exception('用户不存在');
}
RechargeRecord::update([
'id' => $params['id'],
'user_money' => $user['user_money']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 充值记录备注
* @param array $params
* @return bool
* @author bd
* @date 2024/01/31 14:07
*/
public static function remark(array $params): bool
{
Db::startTrans();
try {
$record = RechargeRecord::find($params['id']);
if ($record->isEmpty()) {
throw new \Exception('记录不存在');
}
//代理操作需查询是否有权限
$user = User::where(['agent_id' => $params['admin_id']])->findOrEmpty();
if (!$user->isEmpty()) {
$userRelation = UserRelationAgent::where(['user_id' => $record['user_id'],'parent_id' => $user['id']])->findOrEmpty();
if ($userRelation->isEmpty()) {
throw new \Exception('参数异常');
}
}
RechargeRecord::update([
'id' => $params['id'],
'remark2' => $params['content']
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 修改充值金额
* @param array $params
* @return bool
* @author bd
* @date 2024/01/31 14:07
*/
public static function changeAmount(array $params): bool
{
Db::startTrans();
try {
$record = RechargeRecord::where(['status' => 0])->find($params['id']);
if ($record->isEmpty()) {
throw new \Exception('记录不存在');
}
if($params['amount'] <= 0){
throw new \Exception('请输入正确的金额');
}
$precision = 2;
$method = RechargeMethod::find($record['method_id']);
if (!$method->isEmpty()) {
$precision = $method['precision'];
}
$amount_act = round($params['amount'] * $method['rate'] , $precision);
RechargeRecord::update([
'id' => $params['id'],
'amount' => $params['amount'],
'amount_act' => $amount_act,
]);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
/**
* @notes 新充值提现条数
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author bd
* @date 2024/01/31 14:07
*/
public static function warmCount()
{
$rechargeCount = RechargeRecord::where(['status' => 0])->count();
$withdrawCount = WithdrawRecord::where(['status' => 0])->count();
// $orderCount = GoodsRecord::where(['status' => 5])->count();
return [
'recharge' => $rechargeCount,
'withdraw' => $withdrawCount,
// 'order' => $orderCount
];
}
/**
* @notes 充值统计
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author bd
* @date 2024/01/31 14:07
*/
public static function stat()
{
$total = RechargeRecord::sum('amount');
$ing = RechargeRecord::where(['status' => 0])->sum('amount');
$success = RechargeRecord::where(['status' => 1])->sum('amount');
$error = RechargeRecord::where(['status' => 2])->sum('amount');
return [
'total' => round($total, 2),
'ing' => round($ing, 2),
'success' => round($success, 2),
'error' => round($error, 2),
];
}
}