2024-04-01 15:54:27 +08:00
< ?
if ( is_file ( " /home/www/ftdcdb.inc.php " )) require ( " /home/www/ftdcdb.inc.php " );
elseif ( is_file ( " ./ftdcdb.inc.php " )) require ( " ./ftdcdb.inc.php " );
elseif ( is_file ( " /Users/liuwei/Code/WEB/ftdcdb.inc.php " )) require ( " /Users/liuwei/Code/WEB/ftdcdb.inc.php " );
else require ( " d:/web/ftdcdb.inc.php " );
error_reporting ( E_ALL & ~ E_NOTICE );
if ( @ $GLOBALS [ 'CI' ] && @ $GLOBALS [ 'CI' ] -> rest && @ $GLOBALS [ 'CI' ] -> rest -> db ) {
$GLOBALS [ 'gpay_dbtype' ] = 'mysqli' ;
$GLOBALS [ 'gpay_db' ] = $GLOBALS [ 'CI' ] -> rest -> db ;
$GLOBALS [ 'gpay_db' ] -> db_debug = false ;
} else if ( @ $this && @ $this -> db ) {
$GLOBALS [ 'gpay_dbtype' ] = 'mysqli' ;
$GLOBALS [ 'gpay_db' ] = $this -> db ;
$GLOBALS [ 'gpay_db' ] -> db_debug = false ;
} else {
$GLOBALS [ 'gpay_dbtype' ] = 'mysql' ;
$hinfo = @ mysql_get_host_info ();
if ( $hinfo && stristr ( $hinfo , $ftdcdb_host )) {
$GLOBALS [ 'gpay_db' ] = '' ;
} else {
$GLOBALS [ 'gpay_db' ] = mysql_connect ( $ftdcdb_host , $ftdcdb_username , $ftdcdb_password );
mysql_select_db ( $ftdcdb_database );
mysql_query ( " set names utf8 " );
}
}
if ( ! function_exists ( " gpay_query " )) {
function gpay_query ( $sql )
{
global $gpay_dbtype , $gpay_db ;
if ( $gpay_dbtype == 'mysqli' ) {
return $gpay_db -> query ( $sql );
} else {
if ( $gpay_db ) return mysql_query ( $sql , $gpay_db );
else return mysql_query ( $sql );
}
}
}
if ( ! function_exists ( " gpay_fetchOne " )) {
function gpay_fetchOne ( $sql , $result_type = MYSQL_ASSOC )
{
global $gpay_dbtype , $gpay_db ;
if ( $gpay_dbtype == 'mysqli' ) {
return $gpay_db -> query ( $sql ) -> row_array ();
} else {
if ( $gpay_db ) $result = mysql_query ( $sql , $gpay_db );
else $result = mysql_query ( $sql );
if ( $result ) {
$row = mysql_fetch_array ( $result , $result_type );
return $row ;
} else {
return array ();
}
}
}
}
if ( ! function_exists ( " gpay_fetchAll " )) {
function gpay_fetchAll ( $sql , $result_type = MYSQL_ASSOC )
{
global $gpay_dbtype , $gpay_db ;
if ( $gpay_dbtype == 'mysqli' ) {
return $gpay_db -> query ( $sql ) -> result_array ();
} else {
if ( $gpay_db ) $result = mysql_query ( $sql , $gpay_db );
else $result = mysql_query ( $sql );
$rows = array ();
while ( @ $row = mysql_fetch_array ( $result , $result_type )) {
$rows [] = $row ;
}
return $rows ;
}
}
}
if ( ! function_exists ( " gpay_affected_rows " )) {
function gpay_affected_rows ()
{
global $gpay_dbtype , $gpay_db ;
if ( $gpay_dbtype == 'mysqli' ) {
return $gpay_db -> affected_rows ();
} else {
if ( $gpay_db ) return mysql_affected_rows ( $gpay_db );
else return mysql_affected_rows ();
}
}
}
if ( ! function_exists ( " gpay_insert_id " )) {
function gpay_insert_id ()
{
global $gpay_dbtype , $gpay_db ;
if ( $gpay_dbtype == 'mysqli' ) {
return $gpay_db -> insert_id ();
} else {
if ( $gpay_db ) return mysql_insert_id ( $gpay_db );
else return mysql_insert_id ();
}
}
}
function gpay_lock ( $type , $key )
{
$suc = false ;
$N = 0 ;
while ( true ) {
$t2 = time () - 20 ;
gpay_query ( " delete from tb_lock where type=' { $type } ' and `key`=' { $key } ' and t< " . $t2 );
gpay_query ( " insert into tb_lock set type=' { $type } ', `key`=' { $key } ', t= " . time ());
if ( gpay_affected_rows () == 1 ) {
$suc = true ;
break ;
}
$N ++ ;
if ( $N >= 100 ) {
break ;
}
usleep ( 100000 );
}
return $suc ;
}
function gpay_unlock ( $type , $key )
{
gpay_query ( " delete from tb_lock where type=' { $type } ' and `key`=' { $key } ' " );
}
function gpay_get_user_by_id ( $uid )
{
$row = gpay_fetchOne ( " select a.*,b.name type_name from tb_user a, tb_user_type b where a.type_id = b.id and a.id = { $uid } " ); // and a.enabled not in (0, -99)
return $row ;
}
function gpay_get_user_account ( $uid , $type_id )
{
$row = gpay_fetchOne ( " select sum(account) as ac from tb_account where user_id= { $uid } and type_id= { $type_id } " );
if ( $row ) return $row [ 'ac' ];
return 0 ;
}
function gpay_get_user_account_list ( $uid , $type_id = - 1 , $supplier_id = - 1 , $recharge_fund_type = 0 , $dy = true )
{
$sqlext = '' ;
if ( $type_id != - 1 ) {
$sqlext .= " and type_id= { $type_id } " ;
}
if ( $supplier_id != - 1 ) {
$sqlext .= " and supplier_id= { $supplier_id } " ;
}
if ( $recharge_fund_type != 0 ) {
$sqlext .= " and recharge_fund_type= { $recharge_fund_type } " ;
}
if ( $dy ) { //<2F> <> <EFBFBD> <EFBFBD> 0
$sqlext .= " and account>0 " ;
}
return gpay_fetchAll ( " select a.*,b.supplier_name from tb_account a left join tb_supplier b on a.supplier_id=b.id where a.user_id= { $uid } { $sqlext } order by a.id " );
}
/**
* 减钱
* @ param $uid 用户ID
* @ param $ac 金额(正数)
* @ param $ftid 饭堂ID( 可为0)
* @ param $supplier_id 供应商ID( 可为0)
* @ param $rtype tb_recharge_type 操作类型描述
* @ param $oid 订单ID
* @ param $channelid POS机ID
* @ param string $kkfrom xj或fl或all
* @ param int $admin_id 管理员ID
* @ param int $recharge_fund_type 忽略
* @ param string $updateInfo 忽略
* @ return bool | string suc : 成功 yebz : 余额不足
*/
//function gpay_pay_user_account_one($uid, $ac, $ftid, $supplier_id, $rtype, $oid, $channelid, $kkfrom = 'all', $admin_id = 0, $recharge_fund_type = 0, &$updateInfo = '')
function gpay_pay_user_account_one ( $uid , $ac , $info , & $updateInfo = '' )
{
if ( $ac == 0 && ! isset ( $info [ 'extra_fee' ])) return true ;
if ( $ac == 0 ){
if ( ! gpay_lock ( " account " , $uid )) return " lockfail " ;
// 获取当前用户的所有账户列表
$accountList = gpay_get_user_account_list ( $uid , - 1 , - 1 , 0 , false );
if ( ! empty ( $accountList )){
// 获取所有账户的金额
$account = 0 ;
foreach ( $accountList as $k => $v ){
$account += $v [ 'account' ];
}
if ( $account == 0 && $info [ 'extra_fee' ] == 0 ){
// 录入tb_recharge 金额0的记录
$oid = intval ( $info [ 'oid' ]);
$rtype = intval ( $info [ 'rtype' ]);
$ftid = intval ( $info [ 'ftid' ]);
$channelid = intval ( $info [ 'channelid' ]);
$admin_id = intval ( $info [ 'admin_id' ]);
$recharge_fund_type = intval ( $info [ 'recharge_fund_type' ]);
$extra_fee = floatval ( $info [ 'extra_fee' ]);
if ( isset ( $info [ 'result' ])) $result_v = intval ( $info [ 'result' ]);
else $result_v = 1 ;
$to_account_id = intval ( $info [ 'to_account_id' ]);
$from_account_id = intval ( $info [ 'from_account_id' ]);
$recInfo = array (
'account_id' => $accountList [ 0 ][ 'id' ],
'recharge_amount' => $account ,
'new_amount' => $accountList [ 0 ][ 'account' ],
'old_amount' => $accountList [ 0 ][ 'account' ],
'order_id' => $oid ,
'type' => $rtype ,
'ftid' => $ftid ,
'channel_id' => $channelid ,
'admin_id' => $admin_id ,
'recharge_fund_type' => $recharge_fund_type ,
'extra_fee' => $extra_fee ,
'result' => $result_v ,
'to_account_id' => $to_account_id ,
'from_account_id' => $from_account_id ,
);
if ( isset ( $info [ 'ori_amount' ])) $recInfo [ 'ori_amount' ] = floatval ( $info [ 'ori_amount' ]); //原价
$rechargeList [] = $recInfo ;
$updateInfo = $rechargeList ;
foreach ( $rechargeList as $key => $jinfo ) {
$jinfo [ 'user_id' ] = $uid ;
$jinfo [ 'record_datetime' ] = date ( " Y-m-d H:i:s " );
$jinfo [ 'result' ] = '1' ;
$isql = '' ;
foreach ( $jinfo as $key2 => $value ) {
if ( $isql != '' ) $isql .= " , " ;
$isql .= " ` { $key2 } `=' " . addslashes ( $value ) . " ' " ;
}
gpay_query ( " insert into tb_recharge set " . $isql );
$rid = gpay_insert_id ();
$rechargeList [ $key ][ 'id' ] = $rid ;
gpay_query ( " update tb_account set account=' " . $jinfo [ 'new_amount' ] . " ' where id= " . $jinfo [ 'account_id' ]);
}
$updateInfo = $rechargeList ;
gpay_unlock ( " account " , $uid );
return " suc " ;
}
gpay_unlock ( " account " , $uid );
} else {
gpay_unlock ( " account " , $uid );
return " yebz " ;
}
}
$info [ 'ac' ] = $ac ;
return gpay_pay_user_account ( $uid , array ( $info ), $updateInfo );
}
function gpay_get_account_by_id ( $account_id )
{
return gpay_fetchOne ( " select * from tb_account where id= { $account_id } " );
}
function gpay_pay_user_account ( $uid , $rList , & $updateInfo = '' )
{
$row = gpay_get_user_by_id ( $uid );
if ( ! $row ) {
return " nouser " ;
}
$company_id = intval ( $row [ 'company_id' ]);
$companyInfo = gpay_fetchOne ( " select * from tb_company where id = ' { $company_id } ' " );
foreach ( $rList as $item ) {
$ac = $item [ 'ac' ];
if ( $ac < 0 ) return 'err' ;
}
if ( ! gpay_lock ( " account " , $uid )) return " lockfail " ;
$xjList = gpay_get_user_account_list ( $uid , 2 );
2024-04-02 11:22:43 +08:00
if ( ! $xjList ) { //如果没有就取余额为0的
$xjList = gpay_get_user_account_list ( $uid , 2 , - 1 , 0 , false );
}
2024-04-01 15:54:27 +08:00
if ( $company_id == 54 ) $jlList = gpay_get_user_account_list ( $uid , 3 ); //交通局激励账户
$spList = array ();
foreach ( $rList as $key => $item ) {
$supplier_id = $item [ 'supplier_id' ];
$ftid = $item [ 'ftid' ];
if ( $supplier_id == - 1 || ! isset ( $item [ 'supplier_id' ])) { //根据饭堂ID自动获取
$ftrow = gpay_fetchOne ( " select * from tb_dining_hall where id = " . intval ( $ftid ));
if ( $ftrow ) $supplier_id = $ftrow [ 'supplier_id' ];
else $supplier_id = 0 ;
$rList [ $key ][ 'supplier_id' ] = $supplier_id ;
}
if ( $supplier_id > 0 ) {
2024-04-02 11:22:43 +08:00
$t1 = gpay_get_user_account_list ( $uid , 1 , $supplier_id );
if ( ! $t1 ) { //如果没有就取余额为0的
$t1 = gpay_get_user_account_list ( $uid , 1 , $supplier_id , 0 , false );
}
$spList [ $supplier_id ] = $t1 ;
2024-04-01 15:54:27 +08:00
}
}
$rechargeList = array ();
foreach ( $rList as $item ) {
$ac = $item [ 'ac' ];
$ac_r = $item [ 'ac' ];
$rtype = intval ( $item [ 'rtype' ]);
$ftid = intval ( $item [ 'ftid' ]);
$supplier_id = intval ( $item [ 'supplier_id' ]);
$oid = intval ( $item [ 'oid' ]);
$channelid = intval ( $item [ 'channelid' ]);
$kkfrom = $item [ 'kkfrom' ];
$admin_id = intval ( $item [ 'admin_id' ]);
$recharge_fund_type = intval ( $item [ 'recharge_fund_type' ]);
$extra_fee = floatval ( $item [ 'extra_fee' ]);
if ( isset ( $item [ 'result' ])) $result_v = intval ( $item [ 'result' ]);
else $result_v = 1 ;
$to_account_id = intval ( $item [ 'to_account_id' ]);
$from_account_id = intval ( $item [ 'from_account_id' ]);
if ( ! $spList [ $supplier_id ]) $spList [ $supplier_id ] = array ();
if ( $kkfrom == 'xj' ) {
if ( $supplier_id == 0 || $supplier_id == 97 ) { //97 韶关小岛生日账户特殊处理
$yeList = $xjList ;
} else {
$yeList = array ();
foreach ( $xjList as $item2 ) {
if ( $item2 [ 'supplier_id' ] == 0 || $item2 [ 'supplier_id' ] == $supplier_id ) $yeList [] = $item2 ;
}
}
} else if ( $kkfrom == 'fl' ) {
$yeList = $spList [ $supplier_id ];
} else if ( $kkfrom == 'jl' ) { //交通局激励账户
$yeList = $jlList ;
} else {
$flAccount = $spList [ $supplier_id ];
$xjAccount = array ();
if ( $supplier_id == 0 || $supplier_id == 97 ) { //97 韶关小岛生日账户特殊处理
foreach ( $xjList as $item2 ) {
$xjAccount [] = $item2 ;
}
} else {
foreach ( $xjList as $item2 ) {
if ( $item2 [ 'supplier_id' ] == 0 || $item2 [ 'supplier_id' ] == $supplier_id ) $xjAccount [] = $item2 ;
}
}
if ( $companyInfo [ 'kk_first' ] == 'xj' ) {
$yeList = array_merge ( $xjAccount , $flAccount );
} else {
$yeList = array_merge ( $flAccount , $xjAccount );
}
}
/* if ( $recharge_fund_type != 0 ) {
$yeList2 = array ();
foreach ( $yeList as $key => $item ) {
if ( $item [ 'recharge_fund_type' ] == $recharge_fund_type ) $yeList2 [] = $item ;
}
$yeList = $yeList2 ;
} */
$rechargeList_t = array ();
foreach ( $yeList as $key => $item2 ) {
2024-04-02 11:22:43 +08:00
$is_zero = false ;
if ( round ( $item2 [ 'account' ] * 100 ) <= 0 ) {
if ( $key == 0 && round ( $ac * 100 ) == 0 ) { //0元消费情况, 记录流水
$is_zero = true ;
} else {
continue ;
}
}
if ( $is_zero || round ( $item2 [ 'account' ] * 100 ) >= round ( $ac * 100 )) {
2024-04-01 15:54:27 +08:00
$old_amount = $item2 [ 'account' ];
$recharge_amount = $ac ;
$yeList [ $key ][ 'account' ] -= $ac ;
$ac = 0 ;
$recInfo = array (
'account_id' => $item2 [ 'id' ],
'recharge_amount' => " - " . $recharge_amount ,
'new_amount' => $yeList [ $key ][ 'account' ],
'old_amount' => $old_amount ,
'order_id' => intval ( $oid ),
'type' => intval ( $rtype ),
'ftid' => intval ( $ftid ),
'channel_id' => intval ( $channelid ),
'admin_id' => intval ( $admin_id ),
'recharge_fund_type' => intval ( $recharge_fund_type ),
'extra_fee' => $extra_fee ,
'result' => $result_v ,
'to_account_id' => $to_account_id ,
'from_account_id' => $from_account_id ,
);
if ( isset ( $item [ 'ori_amount' ])) $recInfo [ 'ori_amount' ] = floatval ( $item [ 'ori_amount' ]); //原价
$rechargeList_t [] = $recInfo ;
break ;
} else {
$old_amount = $item2 [ 'account' ];
$ac -= $item2 [ 'account' ];
$yeList [ $key ][ 'account' ] = 0 ;
$recInfo = array (
'account_id' => $item2 [ 'id' ],
'recharge_amount' => " - " . $old_amount ,
'new_amount' => 0 ,
'old_amount' => $old_amount ,
'order_id' => intval ( $oid ),
'type' => intval ( $rtype ),
'ftid' => intval ( $ftid ),
'channel_id' => intval ( $channelid ),
'admin_id' => intval ( $admin_id ),
'recharge_fund_type' => intval ( $recharge_fund_type ),
'extra_fee' => $extra_fee ,
'result' => $result_v ,
'to_account_id' => $to_account_id ,
'from_account_id' => $from_account_id ,
);
if ( isset ( $item [ 'ori_amount' ])) $recInfo [ 'ori_amount' ] = floatval ( $item [ 'ori_amount' ]); //原价
$rechargeList_t [] = $recInfo ;
}
}
2024-04-02 14:44:16 +08:00
if ( round ( $ac * 100 ) > 0 ) { //余额不足 || !$rechargeList_t
2024-04-01 15:54:27 +08:00
gpay_unlock ( " account " , $uid );
return " yebz " ;
}
if ( isset ( $item [ 'ori_amount' ]) && count ( $rechargeList_t ) > 1 ) { //按比例分配ori_amount
$ori_amount = floatval ( $item [ 'ori_amount' ]);
$ac_r_b = round ( $ac_r * 100 );
$ori_amount_b = $ori_amount_b2 = round ( $ori_amount * 100 );
foreach ( $rechargeList_t as $key2 => $item2 ) {
if ( $key2 == count ( $rechargeList_t ) - 1 ) {
$rechargeList_t [ $key2 ][ 'ori_amount' ] = $ori_amount_b2 / 100 ;
} else {
$recharge_amount_b = round ( abs ( $item2 [ 'recharge_amount' ]) * 100 );
$ori_amount_b3 = round ( $recharge_amount_b / $ac_r_b * $ori_amount_b );
$ori_amount_b2 -= $ori_amount_b3 ;
$rechargeList_t [ $key2 ][ 'ori_amount' ] = $ori_amount_b3 / 100 ;
}
}
}
foreach ( $rechargeList_t as $item2 ) {
$rechargeList [] = $item2 ;
}
$xjList = array ();
if ( $supplier_id != 0 ) $spList [ $supplier_id ] = array ();
foreach ( $yeList as $item2 ) {
if ( $item2 [ 'type_id' ] == 2 ) $xjList [] = $item2 ;
else if ( $supplier_id != 0 ) $spList [ $supplier_id ][] = $item2 ;
}
}
foreach ( $rechargeList as $key => $jinfo ) {
$jinfo [ 'user_id' ] = $uid ;
$jinfo [ 'record_datetime' ] = date ( " Y-m-d H:i:s " );
$jinfo [ 'result' ] = '1' ;
$isql = '' ;
foreach ( $jinfo as $key2 => $value ) {
if ( $isql != '' ) $isql .= " , " ;
$isql .= " ` { $key2 } `=' " . addslashes ( $value ) . " ' " ;
}
gpay_query ( " insert into tb_recharge set " . $isql );
$rid = gpay_insert_id ();
$rechargeList [ $key ][ 'id' ] = $rid ;
gpay_query ( " update tb_account set account=' " . $jinfo [ 'new_amount' ] . " ' where id= " . $jinfo [ 'account_id' ]);
}
$updateInfo = $rechargeList ;
gpay_unlock ( " account " , $uid );
return " suc " ;
}
function gpay_get_or_add_user_account ( $uid , $type_id , $supplier_id , $recharge_fund_type = 0 )
{
$data = gpay_get_user_account_list ( $uid , $type_id , $supplier_id , $recharge_fund_type , false );
if ( $data ) return $data ;
$jinfo = array (
'user_id' => $uid ,
'supplier_id' => $supplier_id ,
'account' => 0 ,
'type_id' => $type_id ,
'recharge_fund_type' => $recharge_fund_type ,
'addtime' => date ( " Y-m-d H:i:s " ),
);
$isql = '' ;
foreach ( $jinfo as $key => $value ) {
if ( $isql != '' ) $isql .= " , " ;
$isql .= " ` { $key } `=' " . addslashes ( $value ) . " ' " ;
}
gpay_query ( " insert into tb_account set " . $isql );
return gpay_get_user_account_list ( $uid , $type_id , $supplier_id , $recharge_fund_type , false );
}
/**
* 加钱操作
* @ param $userid
* @ param $ac
* @ param int $admin_id
* @ param int $oid
* @ param int $recharge_fund_type 资金来源(描述)
* @ param int $rtype 操作类型( tb_recharge_type 描述)
* @ param int $ac_type 1 : 福利 2 : 现金
* @ param int $supplier_id
* @ return string
*/
//gpay_add_user_account_uid($userid, $ac, $admin_id = 1, $oid = 0, $recharge_fund_type = 2, $rtype = 1, $ac_type = 2, $supplier_id = 0) {
function gpay_add_user_account_uid ( $userid , $ac , $info , & $updateInfo = '' )
{
$supplier_id = intval ( $info [ 'supplier_id' ]);
$recharge_fund_type = intval ( $info [ 'recharge_fund_type' ]);
$ac_type = intval ( $info [ 'ac_type' ]);
if ( $ac < 0 ) return 'err' ;
$user = gpay_get_user_by_id ( $userid );
if ( ! $user ) {
return " nouser " ;
}
$aList = gpay_get_or_add_user_account ( $userid , $ac_type , $supplier_id , $recharge_fund_type );
if ( ! $aList ) {
return " syserr " ;
}
return gpay_add_user_account ( $aList [ 0 ][ 'id' ], $ac , $info , $updateInfo );
}
//function gpay_add_user_account($account_id, $ac, $ftid, $rtype, $oid, $admin_id = 0, $recharge_fund_type = 0, $extra_fee = 0) {
function gpay_add_user_account ( $account_id , $ac , $info , & $updateInfo = '' )
{
$ftid = intval ( $info [ 'ftid' ]);
$rtype = intval ( $info [ 'rtype' ]);
$oid = intval ( $info [ 'oid' ]);
$admin_id = intval ( $info [ 'admin_id' ]);
$recharge_fund_type = intval ( $info [ 'recharge_fund_type' ]);
$extra_fee = intval ( $info [ 'extra_fee' ]);
$to_account_id = intval ( $info [ 'to_account_id' ]);
$from_account_id = intval ( $info [ 'from_account_id' ]);
2024-06-05 09:51:44 +08:00
$notes = addslashes ( $info [ 'notes' ]) ? : null ;
2024-04-01 15:54:27 +08:00
if ( $ac < 0 ) return 'err' ;
$aInfo = gpay_get_account_by_id ( $account_id );
if ( ! $aInfo ) {
return " noaccount " ;
}
$uid = $aInfo [ 'user_id' ];
if ( ! gpay_lock ( " account " , $uid )) return " lockfail " ;
$aInfo = gpay_get_account_by_id ( $account_id );
$old_amount = $aInfo [ 'account' ];
$new_amount = $aInfo [ 'account' ] + $ac ;
gpay_query ( " update tb_account set account=' " . $new_amount . " ' where id= " . $account_id );
$jinfo = array (
'account_id' => $account_id ,
'recharge_amount' => $ac ,
'new_amount' => $new_amount ,
'old_amount' => $old_amount ,
'order_id' => intval ( $oid ),
'type' => intval ( $rtype ),
'ftid' => intval ( $ftid ),
'user_id' => $uid ,
'record_datetime' => date ( " Y-m-d H:i:s " ),
'result' => 1 ,
'admin_id' => $admin_id ,
'recharge_fund_type' => $recharge_fund_type ,
'extra_fee' => $extra_fee ,
'to_account_id' => $to_account_id ,
'from_account_id' => $from_account_id ,
2024-06-05 09:51:44 +08:00
'notes' => $notes ,
2024-04-01 15:54:27 +08:00
);
$isql = '' ;
foreach ( $jinfo as $key => $value ) {
if ( $isql != '' ) $isql .= " , " ;
$isql .= " ` { $key } `=' " . addslashes ( $value ) . " ' " ;
}
gpay_query ( " insert into tb_recharge set " . $isql );
$rid = gpay_insert_id ();
$jinfo [ 'id' ] = $rid ;
$updateInfo = $jinfo ;
gpay_unlock ( " account " , $uid );
return " suc " ;
}
function gpay_refund_user_account ( $type , $oid , $rtype , $admin_id = 0 )
{ //退款
$getsql = " select * from tb_recharge where order_id= { $oid } and type= { $type } order by id " ;
if ( $type == - 1 ) $getsql = " select * from tb_recharge where id= " . $oid ;
$rList = gpay_fetchAll ( $getsql );
if ( ! $rList ) {
return " norecord " ;
}
foreach ( $rList as $item ) {
$ac = $item [ 'recharge_amount' ];
if ( $ac > 0 ) {
return " err " ;
}
}
$uid = $rList [ 0 ][ 'user_id' ];
if ( ! gpay_lock ( " account " , $uid ) || ! gpay_lock ( " refund " , $type . " _ " . $oid )) {
gpay_unlock ( " account " , $uid );
gpay_unlock ( " refund " , $type . " _ " . $oid );
return " lockfail " ;
}
$rfList = gpay_fetchAll ( " select * from tb_recharge where order_id= { $oid } and type= { $rtype } order by id " );
if ( $rfList ) {
gpay_unlock ( " account " , $uid );
gpay_unlock ( " refund " , $type . " _ " . $oid );
return " hasrefund " ;
}
foreach ( $rList as $item ) {
$account_id = $item [ 'account_id' ];
$ac = abs ( $item [ 'recharge_amount' ]);
$ftid = $item [ 'ftid' ];
$aInfo = gpay_get_account_by_id ( $account_id );
$old_amount = $aInfo [ 'account' ];
$new_amount = $aInfo [ 'account' ] + $ac ;
gpay_query ( " update tb_account set account=' " . $new_amount . " ' where id= " . $account_id );
$jinfo = array (
'account_id' => $account_id ,
'recharge_amount' => $ac ,
'new_amount' => $new_amount ,
'old_amount' => $old_amount ,
'order_id' => intval ( $oid ),
'type' => intval ( $rtype ),
'ftid' => intval ( $ftid ),
'user_id' => $uid ,
'record_datetime' => date ( " Y-m-d H:i:s " ),
'result' => 1 ,
'admin_id' => $admin_id ,
);
$isql = '' ;
foreach ( $jinfo as $key => $value ) {
if ( $isql != '' ) $isql .= " , " ;
$isql .= " ` { $key } `=' " . addslashes ( $value ) . " ' " ;
}
gpay_query ( " insert into tb_recharge set " . $isql );
}
gpay_unlock ( " account " , $uid );
gpay_unlock ( " refund " , $type . " _ " . $oid );
return " suc " ;
}
function gpay_cancel_dc ( $order_id , $admin_id , $rtype = 3 )
{
$row = gpay_fetchOne ( " select * from tb_order where id=' { $order_id } ' or code=' { $order_id } ' " );
if ( ! $row ) return 'no order' ;
if ( $row [ 'state_id' ] != 6 && $row [ 'state_id' ] != 3 ) {
return 'state error' ;
}
$oid = $row [ 'id' ];
$r = gpay_refund_user_account ( 2 , $oid , $rtype , $admin_id );
if ( $r != 'suc' ) return $r ;
gpay_query ( " update tb_order set state_id=4,cancel_time=now() where id= " . $oid );
return 'suc' ;
}
function gpay_cancel_pay_account ( $type , $oid , $reason = '' , $cancel_admin_id = 0 )
{ //取消支付
$getsql = " select * from tb_recharge where order_id= { $oid } and type= { $type } order by record_datetime " ;
if ( $type == - 1 ) $getsql = " select * from tb_recharge where id= " . $oid ;
$rList = gpay_fetchAll ( $getsql );
if ( ! $rList ) {
return " norecord " ;
}
$uid = $rList [ 0 ][ 'user_id' ];
if ( ! gpay_lock ( " account " , $uid )) {
gpay_unlock ( " account " , $uid );
return " lockfail " ;
}
$rList = gpay_fetchAll ( $getsql );
if ( ! $rList ) {
gpay_unlock ( " account " , $uid );
return " syserr " ;
}
//记录
$tdata = array ();
$trow = gpay_fetchOne ( " show tables like 'tb_recharge_cancel_log' " );
if ( $trow ) {
$tdata = gpay_fetchAll ( " desc tb_recharge_cancel_log " );
}
foreach ( $rList as $item ) {
$account_id = $item [ 'account_id' ];
$ac = $item [ 'recharge_amount' ];
$time = $item [ 'record_datetime' ];
$rid = $item [ 'id' ];
$aInfo = gpay_get_account_by_id ( $account_id );
$old_amount = $aInfo [ 'account' ];
$new_amount = $aInfo [ 'account' ] - $ac ;
gpay_query ( " update tb_account set account=' " . $new_amount . " ' where id= " . $account_id );
gpay_query ( " delete from tb_recharge where id= " . $rid );
$row = gpay_fetchOne ( " select * from tb_recharge where account_id= " . $account_id . " and id< { $rid } order by id desc limit 1 " );
if ( $row ) {
$old_amount = $row [ 'new_amount' ];
} else {
$old_amount = 0 ;
}
$data = gpay_fetchAll ( " select * from tb_recharge where account_id= " . $account_id . " and id> { $rid } order by id " );
foreach ( $data as $item2 ) {
$new_amount = $old_amount + $item2 [ 'recharge_amount' ];
//echo $old_amount."\t".$item2->recharge_amount."\t".$new_amount."\n";
gpay_query ( " update tb_recharge set old_amount= { $old_amount } ,new_amount= { $new_amount } where id= " . $item2 [ 'id' ]);
$old_amount = $new_amount ;
}
//记录
if ( $tdata ) {
$sql = '' ;
foreach ( $tdata as $titem ) {
$Field = $titem [ 'Field' ];
if ( $Field == 'id' || $Field == 'addtime' || $Field == 'rid' || $Field == 'reason' || $Field == 'cancel_admin_id' ) continue ;
$v = $item [ $Field ];
if ( $v === null ) $sql .= " ` { $Field } `=null, " ;
else $sql .= " ` { $Field } `=' " . addslashes ( $v ) . " ', " ;
}
$sql = " insert into tb_recharge_cancel_log set { $sql } rid= " . $item [ 'id' ] . " ,reason=' " . addslashes ( $reason ) . " ',cancel_admin_id= " . intval ( $cancel_admin_id ) . " ,addtime=now() " ;
gpay_query ( $sql );
}
}
gpay_unlock ( " account " , $uid );
return " suc " ;
}
function gpay_modify_pay_account ( $recharge_id , $new_recharge_amount , $reason = '' , $modify_admin_id = 0 ) { //修改支付金额
if ( $new_recharge_amount == 0 ) {
return gpay_cancel_pay_account ( - 1 , $recharge_id , $reason , $modify_admin_id );
}
if ( $new_recharge_amount > 0 ) {
return 'new_recharge_amount_err' ;
}
$getsql = " select * from tb_recharge where id= " . $recharge_id ;
$item = gpay_fetchOne ( $getsql );
if ( ! $item ) {
return " norecord " ;
}
if ( $item [ 'recharge_amount' ] >= 0 ) {
return 'notpay' ;
}
$modify_value = $new_recharge_amount - $item [ 'recharge_amount' ]; //退款金额
if ( $modify_value <= 0 ) {
return 'new_recharge_amount_err' ;
}
$uid = $rInfo [ 'user_id' ];
if ( ! gpay_lock ( " account " , $uid )) {
gpay_unlock ( " account " , $uid );
return " lockfail " ;
}
$item = gpay_fetchOne ( $getsql );
if ( ! $item ) {
gpay_unlock ( " account " , $uid );
return " syserr " ;
}
$old_recharge_amount = $item [ 'recharge_amount' ];
$old_amount_1 = $item [ 'old_amount' ];
$new_amount_1 = $old_amount_1 + $new_recharge_amount ;
//记录
$trow = gpay_fetchOne ( " show tables like 'tb_recharge_modify_log' " );
$account_id = $item [ 'account_id' ];
$ac = $item [ 'recharge_amount' ];
$time = $item [ 'record_datetime' ];
$rid = $item [ 'id' ];
$aInfo = gpay_get_account_by_id ( $account_id );
$old_amount = $aInfo [ 'account' ];
$new_amount = $aInfo [ 'account' ] + $modify_value ;
$update_ori_amount = " " ;
if ( isset ( $item [ 'ori_amount' ]) && ! empty ( $item [ 'ori_amount' ] )){
$ori_amount = $item [ 'ori_amount' ] - abs ( $modify_value ); // 原价
$update_ori_amount = " , ori_amount= { $ori_amount } " ;
}
gpay_query ( " update tb_account set account=' " . $new_amount . " ' where id= " . $account_id );
gpay_query ( " update tb_recharge set recharge_amount = { $new_recharge_amount } , new_amount= { $new_amount_1 } { $update_ori_amount } where id= " . $rid );
$old_amount = $new_amount_1 ;
$data = gpay_fetchAll ( " select * from tb_recharge where account_id= " . $account_id . " and id> { $rid } order by id " );
foreach ( $data as $item2 ) {
$new_amount = $old_amount + $item2 [ 'recharge_amount' ];
//echo $old_amount."\t".$item2->recharge_amount."\t".$new_amount."\n";
gpay_query ( " update tb_recharge set old_amount= { $old_amount } ,new_amount= { $new_amount } where id= " . $item2 [ 'id' ]);
$old_amount = $new_amount ;
}
//记录
if ( $trow ) {
gpay_query ( " insert into tb_recharge_modify_log set rid = { $recharge_id } , old_recharge_amount = { $old_recharge_amount } , new_recharge_amount = { $new_recharge_amount } ,reason=' " . addslashes ( $reason ) . " ',modify_admin_id= " . intval ( $modify_admin_id ) . " , `addtime` = now() " );
}
gpay_unlock ( " account " , $uid );
return " suc " ;
}
//$d = gpay_fetchAll("select * from tb_user limit 2");
//print_r($d);
//$r = gpay_refund_user_account(3, 123, 9);
//$r = add_user_account_uid(3, 4.56, 1, 0, 2, 7, 1, 9) ;
//var_dump($r);
//exit;