短信信用扣除并发问题

时间:2014-03-31 02:31:20

标签: php mysql pear

我目前正在为最终用户编写短信系统。但是,当我对功能进行测试时,我会遇到sms信用扣除错误。

此功能有三个表,分别是公司,用户和交易。

我对这个功能进行了很多测试。只是无法正确获得信用扣除。

例如,每张表中有1000个信用卡。

初始值

company.sms_bal = 1000

user.user_credit_bal = 1000

transaction.transaction_balance = 1000

测试#1 每张桌子上扣除1个积分。它正确扣除。

company.sms_bal = 999

user.user_credit_bal = 999

transaction.transaction_balance = 999

测试#2 每张桌子上扣除2个积分。它推得不正确,

company.sms_bal = 997

user.user_credit_bal = 997

transaction.transaction_balance = 998假设是997。

P / S并不总是错误地扣除,有时候。请建议我。但是,如果您不确定我的问题,请告诉我,我愿意帮助您。

以下是供您参考的功能

function process_send_sms($data){
global $CONF, $Q, $OUT, $DB, $DBSH, $CODE, $LANG;


//
//  check credit
//
$check_credit = check_credit($data);

if($check_credit['status'] == '100'){

    //
    // check credit and update credit and update track
    //
    // get $company_credit_bal
    $DBSH->u_select('company', $company_data, array(
        'company_id' => $company_id,
    ));
    $company_credit_bal = $company_data['sms_bal'];
    // get $user_credit_bal
    $DBSH->u_select('user', $user_data, array(
        'user_id' => $user_id,
    ));
    $user_credit_bal = trim($user_data['user_credit_bal']);     
    $number_bulk_mt  = trim($total_credit);

    //
    // update company
    //
    $rows = $DBSH->update(array(
        'table'  => 'company',
        'set'    => array(array('sms_bal = ?', $company_credit_bal - $number_bulk_mt),),
        'where'  => array(array('company_id = ?', $company_id),),));            

    $rows = $DBSH->update(array(
        'table'  => 'user',
        'set'    => array(array('user_credit_bal = ?', $user_credit_bal - $number_bulk_mt),),
        'where'  => array(array('user_id = ?', $user_id),),));  


}else{
    $return_data['error'] = $check_credit['error'];
}

if($check_credit['status'] == '100'){
    //
    // insert to the track
    //
    $arr = debug_backtrace();
    $function_name =  $arr[0]["function"];  
    $data = array(
        'no_phones'             => $total_credit,
        'company_id'            => $company_id,
        'user_id'               => $user_id,
        'msg_type'              => $function_name,
        'track_mt_create_time'  => $timestr,
        'track_mt_update_date'  => $timestr,
    );

    $DBSH->u_insert('bulk_mt_log_track', $data);
    $ref_id = $DBSH->u_lastid('bulk_mt_log_track');

    if($sendsmscsv == 'Y'){         
        foreach($phone_tem as $k => $v){
            $phone_no = $v['phone'];
            $message  = $v['message'];
            $coding   = 1;
            if($v['lang'] == '2'){
                $coding = 3;
            }
            $check_credit = $credit_class->check_per_credit($phone_no,$is_intern);
            $send_c_code  = $check_credit['c_code'    ];
            $per_credit   = $check_credit['per_credit'];
            if($phone_no){
                do{
                    $msgid = session_get()."_".$user_id;
                    $check_valid === false;
                    $found = $DBSH->u_count('bulk_mt_log_data', array(
                        'msgid' => $msgid,
                    ));
                    if(!$found){
                        $check_valid === true;
                    }
                    $found = $DBSH->u_count('bulk_mt_log', array(
                        'msgid' => $msgid,));
                    if(!$found){
                        $check_valid === true;
                    }
                }while($check_valid === false);
                //
                //  Perform merge sms
                //
                $message = process_message_merge($phone_no,$user_id,$message,$company_id);

                if($coding == '3'){

                    $len = mb_strlen($message, 'UTF-8');

                    $arr = array();
                    for( $i=0; $i<$len; $i++){
                       $arr[] = mb_substr($message,$i,1,'UTF-8');;
                    } 

                    $message2="";
                    foreach($arr as $k => $substr){
                        $message2 .= uniord($substr);
                    }
                    $message = $message2;
                }
                else{
                    $coding = 0;
                }   
                $data = array(
                        'bulk_mt_log_id'   => null,
                        'user_id'          => $user_id,
                        'company_id'       => $company_id,
                        'mt_to'            => $phone_no,
                        'campaign'         => $campaign_name,
                        'mt_status'        => 'P',
                        'mt_text'          => $message,
                        'coding'           => $coding,                          
                        'msgid'            => $msgid,    
                        'is_intern'        => $is_intern?$is_intern:'NOT',
                        'per_credit'       => $per_credit ? $per_credit:'0',
                        'mt_from'          => $mt_from?$mt_from:'',
                        'send_c_code'      => $send_c_code,
                        'mt_create_date'   => $timestr,
                        'mt_update_date'   => $timestr,
                        'ref_id'           => $ref_id,
                        'gateway_id'       => $gateway_id,
                        );
                $DBSH->u_insert('bulk_mt_log', $data);

                $rows = $DBSH->select(array(
                    'field' => array('*'),
                    'table'  => 'transaction',
                    'where'  => array(array('company_id = ?', $company_id),
                                     array('transaction_balance > ?', 0),
                                     array('transaction_sms_expire_date > ?', $timestr),),                       
                    'order' =>  'transaction_sms_expire_date ASC',  
                ));
                $data_transaction_id = array();
                while($row = $DBSH->fetchRow(DB_FETCHMODE_ASSOC)){
                    $data_transaction_id[] = $row[transaction_id];
                } 
                $transaction_id = $data_transaction_id[0];
                if($transaction_id){
                    $rows = $DBSH->update(array(
                    'table'  => 'transaction',
                    'set'    => array(array('transaction_balance = transaction_balance - '.$per_credit),),
                    'where'  => array(array('company_id = ?',   $company_id),
                                     array('transaction_id = ?',    $transaction_id),),
                    ));
                }
            }
        }
    }else{
        if ($phone){
            foreach($phone as $k => $phone_no){
                foreach($sms_message as $km => $message){

                    $check_credit = $credit_class->check_per_credit($phone_no,$is_intern);
                    $send_c_code  = $check_credit['c_code'    ];
                    $per_credit   = $check_credit['per_credit'];
                    if($phone_no){
                        do{
                            $msgid = session_get()."_".$user_id;
                            $check_valid === false;
                            $found = $DBSH->u_count('bulk_mt_log_data', array(
                                'msgid' => $msgid,
                            ));
                            if(!$found){
                                $check_valid === true;
                            }
                            $found = $DBSH->u_count('bulk_mt_log', array(
                                'msgid' => $msgid,));
                            if(!$found){
                                $check_valid === true;
                            }
                        }while($check_valid === false);
                        //
                        //  Perform merge sms
                        //
                        $message = process_message_merge($phone_no,$user_id,$message,$company_id);

                        if($message_type == 'unicode'){
                            $coding = 3;
                            $len = mb_strlen($message, 'UTF-8');

                            $arr = array();
                            for( $i=0; $i<$len; $i++){
                               $arr[] = mb_substr($message,$i,1,'UTF-8');;
                            } 

                            $message2="";
                            foreach($arr as $k => $substr){
                                $message2 .= uniord($substr);
                            }
                            $message = $message2;
                        }
                        else{
                            $coding = 0;
                        }       
                        $data = array(
                            'bulk_mt_log_id'   => null,
                            'user_id'          => $user_id,
                            'company_id'       => $company_id,
                            'mt_to'            => $phone_no,
                            'campaign'         => $campaign_name,
                            'mt_status'        => 'P',
                            'mt_text'          => $message,
                            'coding'           => $coding,                          
                            'msgid'            => $msgid,    
                            'is_intern'        => $is_intern?$is_intern:'NOT',
                            'per_credit'       => $per_credit ? $per_credit:'0',
                            'send_c_code'      => $send_c_code,
                            'mt_create_date'   => $timestr,
                            'mt_update_date'   => $timestr,
                            'ref_id'           => $ref_id,
                            'mt_from'          => $mt_from?$mt_from:'',
                            'gateway_id'       => $gateway_id,
                        );
                        $DBSH->u_insert('bulk_mt_log', $data);

                        $rows = $DBSH->select(array(
                            'field' => array('*'),
                            'table' => 'transaction',
                            'where' => array(array('company_id = ?', $company_id),
                                            array('transaction_balance > ?', 0),
                                            array('transaction_sms_expire_date > ?', $timestr),
                                        ),                       
                            'order' => 'transaction_sms_expire_date ASC',  
                        ));

                        $data_transaction_id = array();
                        while($row = $DBSH->fetchRow(DB_FETCHMODE_ASSOC)){
                            $data_transaction_id[] = $row[transaction_id];
                        } 
                        $transaction_id = $data_transaction_id[0];
                        if($transaction_id){
                            $rows = $DBSH->update(array(
                                'table'  => 'transaction',
                                'set'    => array(array('transaction_balance = transaction_balance - '.$per_credit),),
                                'where'  => array(array('company_id = ?',   $company_id),
                                                array('transaction_id = ?', $transaction_id),),
                            ));
                        }
                    }
                }
            }
        }
    }   


}else{
    $return_data['error'] = $check_credit['error'];
}

if($return_data){
    $return_data['status'] ='102';
}else{
    $return_data['status'] ='100';
}
return $return_data;

}

0 个答案:

没有答案