2023-09-18 17:04:42 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\api\controller\user;
|
|
|
|
|
|
|
|
use app\api\controller\BaseApiController;
|
|
|
|
use app\common\model\user\User;
|
|
|
|
use app\common\model\user\UserBalance;
|
|
|
|
use app\common\validate\user\UserBalanceValidate;
|
2023-09-19 14:07:28 +08:00
|
|
|
use think\facade\Db;
|
2023-09-18 17:04:42 +08:00
|
|
|
use think\facade\Log;
|
|
|
|
use think\response\Json;
|
|
|
|
|
|
|
|
class UserBalanceController extends BaseApiController
|
|
|
|
{
|
2023-09-19 14:07:28 +08:00
|
|
|
//用户余额变更记录
|
2023-09-18 17:04:42 +08:00
|
|
|
public function addBalanceRecord(): Json
|
|
|
|
{
|
|
|
|
// 获取参数
|
|
|
|
$params = (new UserBalanceValidate())->post()->goCheck('add');
|
2023-09-19 14:07:28 +08:00
|
|
|
//获取用户信息
|
|
|
|
$user = User::field('id,total_balance')->where('id',$this->userId)->findOrEmpty();
|
|
|
|
if($user->isEmpty()){
|
|
|
|
return $this->fail('未找到用户信息');
|
|
|
|
}
|
|
|
|
if($params['type'] == 0) {
|
|
|
|
$total_balance = $user['total_balance'] - $params['amount'];
|
|
|
|
}else{
|
|
|
|
$total_balance = $user['total_balance'] + $params['amount'];
|
|
|
|
}
|
2023-09-18 17:04:42 +08:00
|
|
|
// 添加数据
|
2023-09-19 14:07:28 +08:00
|
|
|
Db::startTrans();
|
2023-09-18 17:04:42 +08:00
|
|
|
try {
|
2023-09-19 14:07:28 +08:00
|
|
|
UserBalance::create([
|
|
|
|
'user_id' => $this->userId,
|
2023-09-18 17:04:42 +08:00
|
|
|
'record_id' => $params['record_id'],
|
|
|
|
'record_table' => $params['record_table'],
|
|
|
|
'amount' => $params['amount'],
|
2023-09-19 14:07:28 +08:00
|
|
|
'total_amount' => $total_balance,
|
2023-09-18 17:04:42 +08:00
|
|
|
'type' => $params['type'],
|
|
|
|
'pay_type' => $params['pay_type'],
|
|
|
|
'mark' => $params['mark'],
|
|
|
|
'appid' => $this->request->header('appid'),
|
|
|
|
'create_time' => time(),
|
|
|
|
]);
|
2023-09-19 14:07:28 +08:00
|
|
|
User::update([
|
|
|
|
'id' => $this->userId,
|
|
|
|
'total_balance' => $total_balance,
|
|
|
|
]);
|
|
|
|
Db::commit();
|
|
|
|
return $this->success('添加成功');
|
2023-09-18 17:04:42 +08:00
|
|
|
}catch (\Exception $e) {
|
2023-09-19 14:07:28 +08:00
|
|
|
Db::rollback();
|
2023-09-18 17:04:42 +08:00
|
|
|
Log::error($e->getMessage());
|
2023-09-19 14:07:28 +08:00
|
|
|
return $this->fail($e->getMessage());
|
2023-09-18 17:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|