dongpai1942 2018-05-05 09:53
浏览 54

处理大型请求时PHP失败

I am building an online shop using PHP that handles more that 200,000 requests at once but I get duplicated values in my database and negetive values too.

enter image description here

The screenshot above is the only condition that checks if the user has enough money in his account. But I get the following results below

Simulation result from database

I have attached the code here

<?php

require 'db.php';
use Illuminate\Database\Capsule\Manager as DB;

index();

function index()
{
    ini_set('max_execution_time', 3000);
    $members = DB::table("tempcurrentamount")->get();

    $products = DB::table('product')->get();

    $tmp_items = [];

    foreach ($products as $product) {
        array_push($tmp_items, $product->id);
    }

    foreach ($members as $member) {
        // GENERATE RANDOM ITEMS AND QUANTITY
        $items = [];
        $qty = [];

        for ($i=0; $i < 5; $i++) {
            array_push($qty, rand(1,20));
        }

        for ($i=0; $i < 5; $i++) {
            array_push($items, array_rand(array_flip($tmp_items), 1));
        }

        submitRequest($member->id, $qty, $items);
    }
}

function submitRequest($id, $qty, $items)
{
    $user_id = $id;

    $account = DB::table('tempcurrentamount')->where('id', $user_id)->first();

    $date_created= date('Y-m-d');
    $product_id = $items;
    $quantity = $qty;
    $total_amount = 0.0;
    $amount = [];

    // if($user_stage == 0){
    //     return json_encode("SORRY, You can not order for any product in Stage 0.");
    // }

    // Get Amount of each products
    for ($i=0; $i < count($product_id); $i++) { 

            $product_price = DB::table('product')->where('id', $product_id[$i])->first()->price;
            $total_amount += $product_price * $quantity[$i];

        //Store product prices in an array
        array_push($amount, ($product_price / 200));
    }

    // BEGIN TRANSACTION
    echo $id . "Total Amount: " . $total_amount ." Account Balance: ". (($account->foodcash + $account->payoutcash) * 200) . "<br>";

    if ($total_amount < (($account->foodcash + $account->payoutcash) * 200)) {

        processPayment($total_amount, $account, $user_id, $product_id, $date_created, $quantity, $amount);

        echo $id . " Successful<br><br>";     
        return;
    }else{
        echo $id . " Failed Insufficient Balance<br><br>";     
        return;
    }
}

function processPayment($total_amount, $account, $user_id, $product_id, $date_created, $quantity, $amount)
{

    DB::beginTransaction();

    try {

        //Convert Total Amount To The MLM System rate
        $order_amount_in_system_rate = $total_amount / 200;

        //Deduct Amount From table tempcurrentamount
        #===================================================================================
      //Check If Amount is greater than foodcash
        $deductFromPayCash = 0;

        if($order_amount_in_system_rate > $account->foodcash){
            $deductFromPayCash = $order_amount_in_system_rate - $account->foodcash;
        }

      //Perform Deduction
        if($deductFromPayCash > 0 && $account->payoutcash > 0){

            DB::table('tempcurrentamount')
            ->where('id', '=', $user_id)
            ->decrement('foodcash', $account->foodcash);

            DB::table('tempcurrentamount')
            ->where('id', '=', $user_id)
            ->decrement('payoutcash', $deductFromPayCash);
        }else{
            DB::table('tempcurrentamount')
            ->where('id', '=', $user_id)
            ->decrement('foodcash', $order_amount_in_system_rate);
        }
        #===================================================================================

        //If Decrement Fails, ROLLBACK TRANSACTION

        //Add Items to table mlm_foodcollection
        for ($i=0; $i < count($product_id); $i++) {

            //Check if Product is empty  
            if($product_id[$i] == 0){
                continue;
            }

            //Insert Info into mlm_foodcollection table
            DB::table('itemsordered')->insert([
                'user_id' => $user_id,
                'group_leader_id' => rand(1,10),
                'product_id' => $product_id[$i],
                'quantity' => $quantity[$i],
                'amount' => $amount[$i],
                'date_created' => $date_created,
                ]);
        }

        //Log collected item To table mlm_goodscollectionlog
        DB::table('log')->insert([
            'user_id' => $user_id,
            'prev_amount' => $account->foodcash + $account->payoutcash,
            'amount_deducted' => $order_amount_in_system_rate,
            'trans_date' => date('Y-m-d h:i:s'),
            ]);

        // IF NOT QUERY FAILS, COMMIT TRANSACTION
        DB::commit();

        echo $user_id . " Successful<br>";     
        return;

        // all good
    } catch (\Exception $e) {
        DB::rollback();
        echo $user_id . "Something went wrong<br>";     
        return;
    }
}

Thanks guys

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)