webman_duanju/app/admin/model/DiscSpinning.php

151 lines
5.0 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\admin\model;
use app\common\library\DatabaseRoute;
use ba\Exception;
use think\facade\Db;
use support\Log;
use think\Model;
/**
* UserGroup 模型
*/
class DiscSpinning extends Model
{
public static function updateBatchById(array $entityList, int $batchSize = 50)
{
// 验证实体列表非空
if (empty($entityList)) {
throw new Exception("error: entityList must not be empty");
}
// 获取主键字段名(默认假设为 'id',可根据实际情况调整)
$primaryKey = 'id';
$modelName = 'disc_spinning';
// 开启事务确保数据一致性
Db::startTrans();
try {
$count = 0;
foreach ($entityList as $entity) {
// 验证实体包含主键
if (!isset($entity[$primaryKey])) {
throw new Exception("Entity must contain primary key '{$primaryKey}'");
}
// 构建更新数据(排除主键字段)
$updateData = array_diff_key($entity, [$primaryKey => null]);
// 执行更新使用模型或Db类
Db::name($modelName)
->where($primaryKey, $entity[$primaryKey])
->update($updateData);
// 每batchSize条记录刷新一次提交部分事务
$count++;
if ($count % $batchSize === 0) {
Db::commit();
Db::startTrans(); // 重新开启事务
}
}
// 提交剩余事务
Db::commit();
return true;
} catch (Exception $e) {
// 回滚事务
Db::rollback();
throw $e;
}
}
public static function receive1($receive)
{
$userId = $receive['user_id'] ?? 0;
$drawCount = self::countDraw($userId);
$maxDraws = Db::name('common_info')
->where('type', 901)
->value('value');
// 校验是否超过限制
if ($drawCount > $maxDraws) {
Log::info('超过限制' . $receive['id'] . '/' . $drawCount);
return false; // 超过次数限制,终止处理
}
// 查询抽奖记录
$recordId = $receive['id'] ?? 0;
$db = Db::connect(DatabaseRoute::getConnection('disc_spinning_record', ['user_id' => $userId]));
$record = $db->name('disc_spinning_record')->find($recordId);
// 校验记录是否已处理
if (!empty($record['target_id'])) {
Log::info('记录已处理无需继续处理' . $record['id'] . '/' . $record['target_id']);
return false; // 已处理,终止处理
}
self::receiveAsync($record);
}
public static function countDraw($userId)
{
return DatabaseRoute::getDb('disc_spinning_record', $userId)->where('source', 'order')->where('draw_day', date('Y-m-d'))
->count();
}
public static function receiveAsync($receive)
{
Log::info('正式补偿' . $receive['id']);
// 校验奖励类型必须为2
if (($receive['type'] ?? 0) != 2) {
Log::info("非现金转盘奖励type={$receive['type']}");
return false;
}
$db_name = DatabaseRoute::getConnection('tb_user', ['user_id' => $receive['user_id']], true);
$db = Db::connect($db_name);
// 获取用户信息
$userInfo = $db->name('tb_user')->where('user_id', $receive['user_id'])->find();
if (!$userInfo || $userInfo['status'] == 0) {
Log::info("用户状态无效user_id={$receive['user_id']}");
return false;
}
// 开启事务确保数据一致性
$db->startTrans();
try {
// 创建资金流水记录
$moneyDetails = [
'user_id' => $receive['user_id'],
'title' => "[现金大转盘]",
'type' => 1,
'money_type' => 1,
'money' => $receive['number'],
'content' => "现金红包奖励{$receive['number']}",
'source_id' => $receive['id'],
'create_time' => date('Y-m-d H:i:s', time() - 1) // 上一秒时间
];
$detailId = $db->name('user_money_details')->insertGetId($moneyDetails);
// 更新奖励记录
$a = $db->name('disc_spinning_record')
->where('id', $receive['id'])
->update([
'target' => "2",
'target_id' => $detailId
]);
Log::info('更新奖励' . $a);
Cash::updateAmount(1, $receive['user_id'], $receive['number'], $db);
// 提交事务
$db->commit();
return true;
} catch (\Exception $e) {
// 回滚事务
$db->rollback();
Log::error("现金转盘奖励处理失败:{$e->getMessage()}");
}
}
}