The realization plan of php lottery, ask the boss to make suggestions.

when I was writing a lottery, I encountered a problem. Because I have to modify the amount of the user winning the lottery first, then record the log, and modify the record, if one of them fails, join the queue and continue to execute. It is obviously not appropriate to enter the library in bulk. The code is as follows. Please tell us about the optimization plan, welcome to discuss:

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;
        }```

/ / solution 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(),
            ];
            //IDredis,
        }
    }
Mar.10,2021
Menu