new-admin-api/app/common/repositories/user/ExchangeQuotaRepository.php

140 lines
5.7 KiB
PHP

<?php
namespace app\common\repositories\user;
use app\common\dao\user\ExchangeQuotaDao;
use app\common\model\user\ExchangeQuota;
use app\common\model\user\ExchangeQuotaRecord;
use app\common\model\user\ExchangeQuotaTransfer;
use app\common\model\user\User;
use app\common\repositories\BaseRepository;
use think\facade\Db;
class ExchangeQuotaRepository extends BaseRepository{
protected $dao;
public function __construct(ExchangeQuotaDao $dao){
$this->dao = $dao;
}
/**
* Common: 公共查询模型
* Author: wu-hui
* Time: 2024/02/03 9:53
* @param $search
* @return mixed
*/
public function searchModel($search){
return $this->dao->searchModel($search);
}
/**
* Common: 获取统计信息
* Author: wu-hui
* Time: 2024/01/11 9:51
* @param $params
* @return array
*/
public function getStat($params):array{
$model = $this->dao->searchModel($params);
return [
'sum_total_quota' => $model->sum('total_quota'),// 平台总额度
'sum_use_quota' => $model->sum('use_quota'),// 平台已使用额度
'sum_surplus_quota' => $model->sum('surplus_quota'),// 平台剩余额度
'sum_freeze_quota' => $model->sum('freeze_quota'),// 平台冻结额度
'sum_people_num' => $model->count(),// 参与人数
];
}
/**
* Common: 获取信息列表
* Author: wu-hui
* Time: 2024/01/11 9:59
* @param array $params
* @param int $page
* @param int $limit
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getList(array $params,int $page,int $limit):array{
$query = $this->dao->searchModel($params);
$count = $query->count();
$list = $query->page($page,$limit)->select();
return compact('count','list');
}
/**
* Common: 转赠
* Author: wu-hui
* Time: 2024/01/18 16:04
* @param $uid
* @param $params
* @return mixed
*/
public function transfer($uid,$params){
return Db::transaction(function () use ($uid,$params) {
// 获取用户信息
$currentUser = ExchangeQuota::where('uid',$uid)->findOrEmpty();
$currentUserAvailable = (float)sprintf("%.2f",$currentUser->surplus_quota - $currentUser->freeze_quota);
$transferUser = ExchangeQuota::where('uid',$params['transfer_uid'])->findOrEmpty();
if((int)$transferUser->uid <= 0) $transferUser->uid = $params['transfer_uid'];
// 判断:当前用户持有数量是否充足
if($currentUserAvailable < $params['transfer_num']) throw new \Exception('转赠额度不能超过剩余可用额度!');
// 转账操作 - 减少当前用户额度;增加接收用户额度
$currentUserChangeFront = $currentUser->surplus_quota;
$currentUser->total_quota = (float)sprintf("%.2f",(float)$currentUser->total_quota - (float)$params['transfer_num']);
$currentUser->surplus_quota = (float)sprintf("%.2f",(float)$currentUser->surplus_quota - (float)$params['transfer_num']);
$currentUser->save();
$transferUserChangeFront = $currentUser->surplus_quota;
$transferUser->total_quota = (float)sprintf("%.2f",(float)$transferUser->total_quota + (float)$params['receipt_num']);
$transferUser->surplus_quota = (float)sprintf("%.2f",(float)$transferUser->surplus_quota + (float)$params['receipt_num']);
$transferUser->save();
// 获取用户信息
$currentMemberName = User::where('uid',$uid)->value('nickname');
$transferMemberName = User::where('uid',$params['transfer_uid'])->value('nickname');
// 变更记录
$insertData = [
// 当前用户变更记录
[
'uid' => $uid,
'product_id' => 0,
'order_id' => 0,
'order_product_id' => 0,
'change_type' => 0,
'change_quantity' => (float)$params['transfer_num'],
'change_front' => $currentUserChangeFront,
'change_after' => (float)$currentUser->surplus_quota,
'remark' => "转赠给【{$transferMemberName}",
'source' => 4,
],
// 接收用户变更记录
[
'uid' => $params['transfer_uid'],
'product_id' => 0,
'order_id' => 0,
'order_product_id' => 0,
'change_type' => 1,
'change_quantity' => (float)$params['transfer_num'],
'change_front' => $transferUserChangeFront,
'change_after' => (float)$transferUser->surplus_quota,
'remark' => "来自【{$currentMemberName}】的转赠",
'source' => 4,
]
];
ExchangeQuotaRecord::insertAll($insertData);
// 转赠记录
ExchangeQuotaTransfer::insert([
'uid' => $uid,
'transfer_uid' => $params['transfer_uid'],
'transfer_num' => (float)$params['transfer_num'],
'receipt_num' => (float)$params['receipt_num'],
'service_charge' => (float)$params['service_charge'],
]);
});
}
}