mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
63 lines
2.3 KiB
PHP
63 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace app\data\service;
|
|
|
|
use think\admin\Service;
|
|
|
|
/**
|
|
* 用户余额数据服务
|
|
* Class UserBalanceService
|
|
* @package app\data\service
|
|
*/
|
|
class UserBalanceService extends Service
|
|
{
|
|
|
|
/**
|
|
* 同步刷新用户余额
|
|
* @param int $uuid 用户UID
|
|
* @param array $nots 排除的订单
|
|
* @return array [total, count]
|
|
* @throws \think\db\exception\DbException
|
|
*/
|
|
public function amount(int $uuid, array $nots = []): array
|
|
{
|
|
if ($uuid > 0) {
|
|
$total = abs($this->app->db->name('DataUserBalance')->whereRaw("uid='{$uuid}' and amount>0 and deleted=0")->sum('amount'));
|
|
$count = abs($this->app->db->name('DataUserBalance')->whereRaw("uid='{$uuid}' and amount<0 and deleted=0")->sum('amount'));
|
|
if (empty($nots)) {
|
|
$this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]);
|
|
} else {
|
|
$count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount');
|
|
}
|
|
} else {
|
|
$total = abs($this->app->db->name('DataUserBalance')->whereRaw("amount>0 and deleted=0")->sum('amount'));
|
|
$count = abs($this->app->db->name('DataUserBalance')->whereRaw("amount<0 and deleted=0")->sum('amount'));
|
|
}
|
|
return [$total, $count];
|
|
}
|
|
|
|
/**
|
|
* 验证订单发放余额
|
|
* @param string $orderNo
|
|
* @return array [total, count]
|
|
* @throws \think\admin\Exception
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function confirm(string $orderNo): array
|
|
{
|
|
$map = [['order_no', '=', $orderNo], ['status', '>=', 4]];
|
|
$order = $this->app->db->name('ShopOrder')->where($map)->find();
|
|
if (empty($order)) throw new \think\admin\Exception('需处理的订单状态异常');
|
|
data_save('DataUserBalance', [
|
|
'uid' => $order['uid'],
|
|
'code' => $order['order_no'],
|
|
'name' => "订单余额充值",
|
|
'remark' => "来自订单{$order['order_no']}的余额充值",
|
|
'amount' => $order['reward_balance'],
|
|
], 'code');
|
|
return $this->amount($order['uid']);
|
|
}
|
|
|
|
} |