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

169 lines
6.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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) {
// 获取用户信息
$quotaType = $params['quota_type'] ?? 1;
$currentUser = $this->searchModel(['uid'=>$uid,'quota_type'=>$quotaType])->findOrEmpty();
$currentUserAvailable = (float)sprintf("%.2f",$currentUser->surplus_quota - $currentUser->freeze_quota);
$transferUser = $this->searchModel(['uid'=>$params['transfer_uid'],'quota_type'=>$quotaType])->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 = $transferUser->surplus_quota;
$transferUser->quota_type = $quotaType;
$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,
'quota_type' => $quotaType
],
// 接收用户变更记录
[
'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,
'quota_type' => $quotaType
]
];
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'],
'quota_type' => $quotaType
]);
});
}
/**
* Common: 获取信息
* Author: wu-hui
* Time: 2024/06/04 10:42
* @param int $uid
* @param int $quotaType 额度类型1=酒卡额度(瓶装酒)2=菜卡额度3=封坛酒额度4=加油卡额度
* @return mixed
*/
public function getInfo(int $uid,int $quotaType = 1){
$info = $this->searchModel([
'uid' => $uid,
'quota_type' => $quotaType,
])
->field('id,uid,total_quota,use_quota,surplus_quota,freeze_quota,quota_type')
->findOrEmpty()
->toArray();
if($info){
// 计算可用额度
$info['available_quota'] = (float)sprintf("%.2f", $info['surplus_quota'] - $info['freeze_quota']);
}
return $info;
}
}