diff --git a/app/data/controller/api/auth/Balance.php b/app/data/controller/api/auth/Balance.php index 504f1d1c0..06b31eb8b 100644 --- a/app/data/controller/api/auth/Balance.php +++ b/app/data/controller/api/auth/Balance.php @@ -3,9 +3,6 @@ namespace app\data\controller\api\auth; use app\data\controller\api\Auth; -use app\data\service\UserUpgradeService; -use app\data\service\UserService; -use think\admin\extend\CodeExtend; /** * 用户余额转账 @@ -18,10 +15,10 @@ class Balance extends Auth * 绑定数据表 * @var string */ - private $table = 'DataUserBalanceTransfer'; + private $table = 'DataUserBalance'; /** - * 获取用户转账记录 + * 获取用户余额记录 * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException @@ -29,47 +26,8 @@ class Balance extends Auth public function get() { $query = $this->_query($this->table); - $query->where(['uid|from' => $this->uuid, 'deleted' => 0]); - $result = $query->order('id desc')->page(true, false, false, 15); - if (count($result['list']) > 0) { - UserService::instance()->buildByUid($result['list'], 'uid', 'selfer'); - UserService::instance()->buildByUid($result['list'], 'pid1', 'fromer'); - } + $query->where(['uid' => $this->uuid, 'deleted' => 0]); + $result = $query->like('create_at#date')->order('id desc')->page(true, false, false, 15); $this->success('获取数据成功', $result); } - - /** - * 创建余额转账申请 - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - public function add() - { - $data = $this->_vali([ - 'from.value' => $this->uuid, - 'code.value' => CodeExtend::uniqidDate(18, 'T'), - 'uid.require' => '用户不能为空!', - 'name.default' => '用户余额转账', - 'amount.require' => '金额不能为空!', - ]); - if ($data['uid'] == $this->uuid) { - $this->error('不能给自己转账!'); - } - // 检测目标用户状态 - $map = ['id' => $data['uid'], 'deleted' => 0]; - $user = $this->app->db->name('DataUser')->where($map)->find(); - if (empty($user)) $this->error('目标用户不存在!'); - // 检测余额否有足够 - [$total, $count] = UserUpgradeService::instance()->balance($this->uuid); - if ($data['amount'] > $total - $count) $this->error('可转账余额不足!'); - // 写入余额转账记录 - if ($this->app->db->name($this->table)->insert($data) !== false) { - UserUpgradeService::instance()->balance($data['uid']); - UserUpgradeService::instance()->balance($data['from']); - $this->success('余额转账成功!'); - } else { - $this->error('余额转账失败!'); - } - } } \ No newline at end of file diff --git a/app/data/controller/api/auth/Rebate.php b/app/data/controller/api/auth/Rebate.php new file mode 100644 index 000000000..ccd6120d4 --- /dev/null +++ b/app/data/controller/api/auth/Rebate.php @@ -0,0 +1,32 @@ + $this->uuid]; + $query = $this->_query($this->table)->where($map); + $query->like('create_at#date')->order('id desc')->page(true, false, false, 15); + } +} \ No newline at end of file diff --git a/app/data/service/UserUpgradeService.php b/app/data/service/UserUpgradeService.php index a4c568c5a..3939e03f8 100644 --- a/app/data/service/UserUpgradeService.php +++ b/app/data/service/UserUpgradeService.php @@ -21,16 +21,17 @@ class UserUpgradeService extends Service */ public function balance(int $uuid, array $nots = []): array { - $total = $this->app->db->name('DataUserBalance')->where(['uid' => $uuid, 'deleted' => 0])->sum('amount'); - $total += $this->app->db->name('DataUserBalanceTransfer')->where(['uid' => $uuid, 'deleted' => 0])->sum('amount'); - $count = $this->app->db->name('DataUserBalanceTransfer')->where(['from' => $uuid, 'deleted' => 0])->sum('amount'); + $total = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount>0 and deleted=0")->sum('amount')); + $count = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount<0 and deleted=0")->sum('amount')); + $locks = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=0 and amount>0 and deleted=0")->sum('amount')); if (empty($nots)) { - $count += $this->app->db->name('ShopOrder')->whereRaw("uid={$uuid} and status>1")->sum('payment_balance'); - $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]); + $this->app->db->name('DataUser')->where(['id' => $uuid])->update([ + 'balance_total' => $total, 'balance_used' => $count, 'balance_lock' => $locks, + ]); } else { - $count += $this->app->db->name('ShopOrder')->whereRaw("uid={$uuid} and status>1")->whereNotIn('order_no', $nots)->sum('payment_balance'); + $count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount'); } - return [$total, $count]; + return [$total, $count, $locks]; } /** diff --git a/app/data/service/payment/BalancePyamentService.php b/app/data/service/payment/BalancePyamentService.php index afef4a1ad..c02b40f7e 100644 --- a/app/data/service/payment/BalancePyamentService.php +++ b/app/data/service/payment/BalancePyamentService.php @@ -56,9 +56,17 @@ class BalancePyamentService extends PaymentService // 创建支付行为 $this->createPaymentAction($orderNo, $paymentTitle, $paymentAmount); // 扣减用户余额 - [$total, $used] = UserUpgradeService::instance()->balance($order['uid'], [$orderNo]); - if ($paymentAmount > $total - $used) throw new Exception("可抵扣余额不足"); + [$total, $count] = UserUpgradeService::instance()->balance($order['uid'], [$orderNo]); + if ($paymentAmount > $total - $count) throw new Exception("可抵扣余额不足"); $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['payment_balance' => $paymentAmount]); + // 扣除余额金额 + data_save('DataUserBalance', [ + 'uid' => $order['uid'], + 'code' => $order['order_no'], + 'name' => "订单余额支付", + 'remark' => "使用余额支付订单{$order['order_no']}金额{$paymentAmount}元", + 'amount' => -$paymentAmount, + ], 'code'); // 更新支付行为 $this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付'); // 刷新用户余额