php开奖实现方案,请大佬提提建议
我在写一个开奖的时候,碰到一个问题,由于我要先修改用户中奖金额,然后记录日志,并修改该条记录,如果其中某条执行失败,加入队列继续执行,用批量入库显然不太合适,代码如下,麻烦各位大佬,说说优化方案,欢迎讨论:
foreach ($result as $key => $value) {
$userObj->beginTransaction();
//计算中奖金币
$lotterySum = ($value['sum'] == MIN_BET_MONEY ? 1 : $value['sum']/MIN_BET_MONEY)*$value['odds'];
//将中奖金币写入数据库,修改用户余额
$sql = 'UPDATE `ct_user` SET `gold` = gold+'.$lotterySum.' WHERE `id` = '.$value['user_id'];
$res = $userObj->updateData($sql,[]);
if (!$res) {
//如果用户金额修改失败,加入redis队列
//将用户中奖信息加入数据库
file_put_contents("tm.txt", json_encode($result).PHP_EOL, FILE_APPEND);
$userObj->rollBack();
continue;
}
//将中奖数据写入数据库日志
$data = [
':user_id' => $value['user_id'],
':classify'=> MONEY_FLOW_LOTTERY,
':number' => $lotterySum,
':before_num' => $value['gold'],
':after_num' => $value['gold']+$lotterySum,
':explain' => '恭喜您,第:'.$issue.':'.$code.',中奖'.$lotterySum.'金币',
':data' => json_encode($value),
':create_time'=> time(),
];
$sql = 'INSERT INTO `lottery`.`ct_user_funds_log` (`user_id`, `classify`, `number`, `before_num`, `after_num`, `explain`, `data`, `create_time`) VALUES (:user_id, :classify, :number, :before_num, :after_num, :explain, :data, :create_time)';
$obj->addData($sql,$data);
$sql = 'UPDATE `ct_cathectic` SET `state`=1';
$res = $object->updateData($sql,[]);
if(!$res){
//如果操作失败,事务回滚,并加入redis
//将用户中奖信息加入数据库
file_put_contents("tm.txt", json_encode($result).PHP_EOL, FILE_APPEND);
$userObj->rollBack();
continue;
}
$userObj->commit();
continue;
}```
//解决办法1
foreach ($result as $key => $value) {
$userObj->beginTransaction();
//计算中奖金币
$lotterySum = ($value['sum'] == MIN_BET_MONEY ? 1 : $value['sum']/MIN_BET_MONEY)*$value['odds'];
//将中奖金币写入数据库,修改用户余额
$sql = 'UPDATE `ct_user` SET `gold` = gold+'.$lotterySum.' WHERE `id` = '.$value['user_id'];
$res = $userObj->updateData($sql,[]);
if (!$res) {
//如果用户金额修改失败,加入redis队列
//将用户中奖信息加入数据库
file_put_contents("tm.txt", json_encode($result).PHP_EOL, FILE_APPEND);
$userObj->rollBack();
continue;
}
//将中奖数据写入redis队列
$data = [
':user_id' => $value['user_id'],
':classify'=> MONEY_FLOW_LOTTERY,
':number' => $lotterySum,
':before_num' => $value['gold'],
':after_num' => $value['gold']+$lotterySum,
':explain' => '恭喜您,第:'.$issue.':'.$code.',中奖'.$lotterySum.'金币',
':data' => json_encode($value),
':create_time'=> time(),
];
$sql = 'UPDATE `ct_cathectic` SET `state`=1';
$res = $object->updateData($sql,[]);
if(!$res){
//如果操作失败,事务回滚,并加入redis
//将用户中奖信息加入数据库
file_put_contents("tm.txt", json_encode($result).PHP_EOL, FILE_APPEND);
$userObj->rollBack();
continue;
}
$userObj->commit();
continue;
}```
//解决方法2
private function special($collection_id, $code, $issue)
{
$sql = 'SELECT cathectic.id,cathectic.sum,cathectic.user_id,user.gold,cathectic.odds FROM ct_cathectic cathectic LEFT JOIN ct_user user ON cathectic.user_id=user.id WHERE cathectic.type='.BET_TYPE_SPECIAL.' AND cathectic.collection_id='.$collection_id.' AND cathectic.content='.$code;
$object = new cathectic();
$result = $object->getData($sql, []);
if(empty($result)){
return '-1';
}
foreach ($result as $key => $value) {
//计算中奖金币
$lotterySum = ($value['sum'] == MIN_BET_MONEY ? 1 : $value['sum']/MIN_BET_MONEY)*$value['odds'];
//以下调整为写入redis
//将中奖数据写入数据库日志
$data = [
':user_id' => $value['user_id'],
':classify'=> MONEY_FLOW_LOTTERY,
':number' => $lotterySum,
':before_num' => $value['gold'],
':after_num' => $value['gold']+$lotterySum,
':explain' => '恭喜您,第:'.$issue.':'.$code.',中奖'.$lotterySum.'金币',
':data' => json_encode($value),
':create_time'=> time(),
];
//将中奖金币,中奖用户ID写入redis,并且修改本条参与记录
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论