Php sms信用扣减并发问题

Php sms信用扣减并发问题,php,mysql,pear,Php,Mysql,Pear,我目前正在为最终用户编写一个sms系统。然而,当我对函数进行测试时,我遇到了sms信用扣减错误 此功能中有三个表,即公司、用户和交易 我对这个函数做了很多测试。只是不能正确获得信用扣减 例如,每个表中有1000个学分 初始值 company.sms_bal=1000 user.user\u credit\u bal=1000 transaction.transaction\u余额=1000 测试#1 每张表扣1分。它能正确地推断 company.sms_bal=999 user.user\u c

我目前正在为最终用户编写一个sms系统。然而,当我对函数进行测试时,我遇到了sms信用扣减错误

此功能中有三个表,即公司、用户和交易

我对这个函数做了很多测试。只是不能正确获得信用扣减

例如,每个表中有1000个学分

初始值

company.sms_bal=1000

user.user\u credit\u bal=1000

transaction.transaction\u余额=1000

测试#1 每张表扣1分。它能正确地推断

company.sms_bal=999

user.user\u credit\u bal=999

transaction.transaction\u余额=999

测试#2 每张表扣2分。它推断错误

company.sms_bal=997

user.user\u credit\u 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;
功能处理发送短信($data){
全局$CONF、$Q、$OUT、$DB、$DBSH、$CODE、$LANG;
//
//检查信用证
//
$check\u credit=check\u credit($data);
如果($check_credit['status']=='100'){
//
//检查信用并更新信用和更新跟踪
//
//获得$company\u credit\u bal
$DBSH->u_选择('company',$company_数据,数组(
'company\u id'=>$company\u id,
));
$company_credit_bal=$company_data['sms_bal'];
//获取$user\u credit\u bal
$DBSH->u_选择('user',$user_数据,数组(
'user\u id'=>$user\u id,
));
$user\u credit\u bal=trim($user\u data['user\u credit\u bal');
$number\u bulk\u mt=trim($total\u credit);
//
//更新公司
//
$rows=$DBSH->更新(数组)(
'表'=>'公司',
“set'=>数组(数组('sms\u bal=?',$company\u credit\u bal-$number\u bulk\u mt),),
'其中'=>数组(数组('company_id=?',$company_id),),);
$rows=$DBSH->更新(数组)(
'表'=>'用户',
“set'=>数组(数组('user\u credit\u bal=?',$user\u credit\u bal-$number\u bulk\u mt),),
'其中'=>数组(数组('user\u id=?',$user\u id),),);
}否则{
$return_data['error']=$check_credit['error'];
}
如果($check_credit['status']=='100'){
//
//插入到曲目中
//
$arr=debug_backtrace();
$function_name=$arr[0][“function”];
$data=数组(
“无电话”=>总积分$,
'company\u id'=>$company\u id,
'user\u id'=>$user\u id,
'msg_type'=>$function_name,
“track\u mt\u create\u time”=>$timestr,
“跟踪更新日期”=>$timestr,
);
$DBSH->u_insert('bulk_mt_log_track',$data);
$ref_id=$DBSH->u_lastid('bulk_mtu log_track');
如果($sendsmscsv=='Y'){
foreach($k=>v){
$phone_no=$v['phone'];
$message=$v['message'];
$coding=1;
如果($v['lang']='2'){
$coding=3;
}
$check\u credit=$credit\u class->check\u per\u credit($phone\u no,$is\u intern);
$send_c_code=$check_credit['c_code'];
$per_credit=$check_credit['per_credit'];
如果($电话号码){
做{
$msgid=session\u get().“\u”。$user\u id;
$check_valid===false;
$found=$DBSH->u\u计数('bulk\u mt\u log\u data',数组(
'msgid'=>$msgid,
));
如果(!$found){
$check\u valid===true;
}
$found=$DBSH->u\u count('bulk\u mt\u log',数组(
'msgid'=>$msgid,);
如果(!$found){
$check\u valid===true;
}
}while($check_valid===false);
//
//执行合并短信
//
$message=process\u message\u merge($phone\u no、$user\u id、$message、$company\u id);
如果($coding=='3'){
$len=mbstrlen($message'UTF-8');
$arr=array();
对于($i=0;$i$substr){
$message2.=uniord($substr);
}
$message=$message2;
}
否则{
$coding=0;
}   
$data=数组(
“大容量日志id”=>null,
'user\u id'=>$user\u id,
'company\u id'=>$company\u id,
'mt_to'=>$phone_no,
“活动”=>$campaign\u名称,
“mt_状态”=>“P”,
'mt_text'=>$message,
“coding”=>$coding,
'msgid'=>$msgid,
'is_intern'=>$is_intern?$is_intern:'NOT',
“每笔信用”=>每笔信用?$每笔信用:'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\u id'=>$gateway\u id,
);
$DBSH->u_insert('bulk_mt_log',$data);
$rows=$DBSH->选择(数组)(
'字段'=>数组('*'),
'表'=>'事务',
'其中'=>数组(数组('company_id=?')$