add
This commit is contained in:
293
app/admin/model/Cash.php
Normal file
293
app/admin/model/Cash.php
Normal file
@@ -0,0 +1,293 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\library\DatabaseRoute;
|
||||
use ba\Exception;
|
||||
use think\facade\Db;
|
||||
use think\Model;
|
||||
|
||||
|
||||
class Cash extends Model
|
||||
{
|
||||
public static function statisticsIncomeMoney($time, $flag, $num)
|
||||
{
|
||||
$rest = DatabaseRoute::getAllDbData('pay_details', function ($query) use ($time, $flag, $num) {
|
||||
$query->where(['state' => 1]);
|
||||
if(!empty($flag)) {
|
||||
if($flag == 1) {
|
||||
$query->whereTime('create_time', '>=', strtotime(date('Y-m-d 00:00:00', strtotime($time))));
|
||||
$query->whereTime('create_time', '<=', strtotime(date('Y-m-d 23:59:59', strtotime($time))));
|
||||
}
|
||||
if($flag == 2) {
|
||||
$query->whereTime('create_time', '>=', strtotime(date('Y-m-01 00:00:00', strtotime($time))));
|
||||
$query->whereTime('create_time', '<=', strtotime(date('Y-m-t 23:59:59', strtotime($time))));
|
||||
}
|
||||
if($flag == 3) {
|
||||
$query->whereTime('create_time', '>=', strtotime(date('Y-01-01 00:00:00', strtotime($time))));
|
||||
$query->whereTime('create_time', '<=', strtotime(date('Y-12-31 23:59:59', strtotime($time))));
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
})->sum('money');
|
||||
return $rest;
|
||||
}
|
||||
|
||||
|
||||
public static function selectPayDetails($get)
|
||||
{
|
||||
return DatabaseRoute::paginateAllDb('pay_details', function ($query)use($get){
|
||||
$query->alias('s')
|
||||
->field([
|
||||
's.id',
|
||||
's.classify',
|
||||
's.order_id as orderId',
|
||||
's.money',
|
||||
's.user_id as userId',
|
||||
's.pay_diamond as payDiamond',
|
||||
's.diamond',
|
||||
's.state',
|
||||
's.create_time as createTime',
|
||||
's.pay_time as payTime',
|
||||
'u.user_name as userName',
|
||||
'u.phone'
|
||||
])
|
||||
->leftJoin('tb_user u', 'u.user_id = s.user_id');
|
||||
// 添加动态查询条件
|
||||
if (!empty($get['startTime']) && !empty($get['endTime'])) {
|
||||
$query->whereBetween('s.create_time', [$get['startTime'], $get['endTime']]);
|
||||
}
|
||||
if (!empty($get['userName'])) {
|
||||
$query->where('u.user_name', 'like', "%{$get['userName']}%");
|
||||
}
|
||||
if (!empty($get['orderId'])) {
|
||||
$query->where('s.order_id', 'like', "%{$get['orderId']}%");
|
||||
}
|
||||
if (isset($get['userId']) && $get['userId'] !== null) {
|
||||
$query->where('u.user_id', $get['userId']);
|
||||
}
|
||||
if (isset($get['state']) && $get['state'] !== -1) {
|
||||
$query->where('s.state', $get['state']);
|
||||
} elseif (!isset($get['state']) || $get['state'] === -1) {
|
||||
$query->where('s.state', '<>', -1);
|
||||
}
|
||||
return $query;
|
||||
}, $get['page'], $get['limit'], 's.create_time');
|
||||
}
|
||||
|
||||
|
||||
public static function selectCashOutList($page, $limit, $cashOut, $isApp = false):array
|
||||
{
|
||||
$cashOutList = DatabaseRoute::paginateAllDb('cash_out', function ($query)use($page, $limit, $cashOut, $isApp){
|
||||
// 根据请求端设置不同查询条件
|
||||
if ($isApp) {
|
||||
// APP端:查询用户自身的提现记录(用户类型1)
|
||||
$query->where('user_id', $cashOut['user_id'] ?? 0)
|
||||
->where('user_type', 1);
|
||||
} else {
|
||||
// 管理后台:根据条件查询
|
||||
if (isset($cashOut['user_id'])) {
|
||||
$query->where('user_id', $cashOut['user_id']);
|
||||
} else {
|
||||
if (!isset($cashOut['sys_user_id'])) {
|
||||
return $query;
|
||||
} else {
|
||||
// 查询系统用户的提现记录(用户类型2)
|
||||
$query->where('user_id', $cashOut['sys_user_id'])
|
||||
->where('user_type', 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}, $page, $limit, 'create_at');
|
||||
|
||||
if (!$isApp) {
|
||||
// 管理后台:补充用户信息和统计数据
|
||||
$userIdList = [];
|
||||
foreach ($cashOutList['list'] as $out) {
|
||||
$userIdList[] = $out['user_id'];
|
||||
}
|
||||
|
||||
// 查询用户提现总数和总金额
|
||||
$cashoutSumMap = [];
|
||||
$cashoutVerifySumMap = [];
|
||||
$userinfoMap = [];
|
||||
|
||||
if (!empty($userIdList)) {
|
||||
// 获取已完成提现统计
|
||||
$cashoutSumList = \app\api\model\Cash::selectSumByUserIdList($userIdList, 1);
|
||||
$cashoutSumMap = array_column($cashoutSumList, null, 'user_id');
|
||||
|
||||
// 获取审核中提现统计
|
||||
$cashoutVerifyList = \app\api\model\Cash::selectSumByUserIdList($userIdList, 3);
|
||||
$cashoutVerifySumMap = array_column($cashoutVerifyList, null, 'user_id');
|
||||
|
||||
// 获取用户信息
|
||||
$userList = DatabaseRoute::getAllDbData('tb_user', function ($query) use($userIdList) {
|
||||
return $query->whereIn('user_id', $userIdList)
|
||||
->field('user_id, user_name');
|
||||
})->select();
|
||||
|
||||
|
||||
$userinfoMap = array_column($userList->toArray(), 'user_name', 'user_id');
|
||||
}
|
||||
|
||||
// 补充数据到提现记录
|
||||
foreach ($cashOutList['list'] as &$item) {
|
||||
$info = $cashoutSumMap[$item['user_id']] ?? null;
|
||||
$info2 = $cashoutVerifySumMap[$item['user_id']] ?? null;
|
||||
|
||||
$item['user_name'] = $userinfoMap[$item['user_id']] ?? '';
|
||||
$item['count'] = $info ? $info['count'] : 0;
|
||||
$item['total'] = $info ? $info['total'] : 0.00;
|
||||
$item['verify_count'] = $info2 ? $info2['count'] : 0;
|
||||
$item['verify_total'] = $info2 ? $info2['total'] : 0.00;
|
||||
}
|
||||
}
|
||||
|
||||
if ($isApp) {
|
||||
// APP端:对敏感信息进行脱敏处理
|
||||
foreach ($cashOutList['list'] as &$item) {
|
||||
if (!empty($item['bank_name'])) {
|
||||
// 银行卡号脱敏
|
||||
$item['zhifubao'] = bankCard($item['zhifubao']);
|
||||
} elseif (filter_var($item['zhifubao'], FILTER_VALIDATE_EMAIL)) {
|
||||
// 邮箱脱敏
|
||||
$item['zhifubao'] = email($item['zhifubao']);
|
||||
} elseif (preg_match('/^1[3-9]\d{9}$/', $item['zhifubao'])) {
|
||||
// 手机号脱敏
|
||||
$item['zhifubao'] = maskPhoneNumber($item['zhifubao']);
|
||||
}
|
||||
}
|
||||
}
|
||||
return $cashOutList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 退回提现金额
|
||||
* @param array $entity 提现实体
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function backCashAmount($entity, $db)
|
||||
{
|
||||
// 开启事务确保数据一致性
|
||||
$db->startTrans();
|
||||
try {
|
||||
if ($entity['user_type'] == 2) {
|
||||
// 代理用户退款逻辑
|
||||
$detailsData = [
|
||||
'user_id' => $entity['user_id'],
|
||||
'operate_id' => $entity['user_id'],
|
||||
'title' => "提现失败存入余额",
|
||||
'type' => 4,
|
||||
'money_type' => 1,
|
||||
'money' => $entity['money'],
|
||||
'content' => "提现失败存入余额{$entity['money']}元",
|
||||
'status' => 1,
|
||||
'create_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
// 记录资金流水
|
||||
$db->name('sys_user_money_details')->insert($detailsData);
|
||||
|
||||
// 更新代理账户余额(增加余额)
|
||||
self::updateSysMoney(1, $entity['user_id'], $entity['money']);
|
||||
} else {
|
||||
// 普通用户退款逻辑
|
||||
self::updateByUserId($entity, $db);
|
||||
|
||||
$detailsData = [
|
||||
'user_id' => $entity['user_id'],
|
||||
'sys_user_id' => $entity['sys_user_id'] ?? null,
|
||||
'title' => "[提现退款]",
|
||||
'type' => 4,
|
||||
'money_type' => 1,
|
||||
'money' => $entity['money'],
|
||||
'content' => "提现失败,自动退款{$entity['money']}元",
|
||||
'status' => 1,
|
||||
'cash_out_id' => $entity['id'],
|
||||
'create_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
// 记录资金流水
|
||||
$db->name('user_money_details')->insert($detailsData);
|
||||
|
||||
// 归还用户余额
|
||||
self::updateAmount(1, $entity['user_id'], $entity['money'], $db);
|
||||
}
|
||||
|
||||
// 提交事务
|
||||
$db->commit();
|
||||
} catch (Exception $e) {
|
||||
// 回滚事务
|
||||
$db->rollback();
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
public static function updateSysMoney($type, $userId, $money)
|
||||
{
|
||||
$query = Db::name('sys_user_money')
|
||||
->where('user_id', $userId);
|
||||
|
||||
// 根据类型决定是增加还是减少余额
|
||||
if ($type == 1) {
|
||||
// 增加余额
|
||||
return $query->inc('money', $money)->update();
|
||||
} elseif ($type == 2) {
|
||||
// 减少余额
|
||||
return $query->dec('money', $money)->update();
|
||||
}
|
||||
return 0; // 无效类型返回 0
|
||||
}
|
||||
|
||||
|
||||
public static function updateAmount($type, $userId, $amount, $db)
|
||||
{
|
||||
User::selectUserMoneyByUserId($userId);
|
||||
// 构建基础查询
|
||||
$query = $db->name('user_money')
|
||||
->where('user_id', $userId);
|
||||
|
||||
// 根据类型执行增减操作
|
||||
if ($type == 1) {
|
||||
// 增加金额:amount = amount + #{amount}
|
||||
return $query->inc('amount', $amount)->update();
|
||||
} elseif ($type == 2) {
|
||||
// 减少金额:amount = amount - #{amount}
|
||||
// 可选:添加余额充足校验
|
||||
return $query->where('amount', '>=', $amount) // 确保余额不小于要减少的金额
|
||||
->dec('amount', $amount)
|
||||
->update();
|
||||
}
|
||||
|
||||
return 0; // 无效类型返回0
|
||||
}
|
||||
|
||||
|
||||
public static function updateByUserId($entity, $db)
|
||||
{
|
||||
// 验证userId是否存在
|
||||
if (empty($entity['user_id'])) {
|
||||
throw new Exception("cashOut修改失败: userId必须传递");
|
||||
}
|
||||
|
||||
// 构建更新条件
|
||||
$conditions = [
|
||||
'user_id' => $entity['user_id'],
|
||||
'id' => $entity['id']
|
||||
];
|
||||
|
||||
// 过滤掉主键和条件字段,避免更新这些字段
|
||||
$updateData = $entity;
|
||||
unset($updateData['user_id'], $updateData['id']);
|
||||
|
||||
// 执行更新操作
|
||||
$db->name('cash_out')
|
||||
->where($conditions)
|
||||
->update($updateData);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user