='{$stime}' order by id "; echo "beginTime:".$stime." -> endTime:now \n"; $stmt = $_PDO->prepare($sql); $stmt->execute(); $accountInfo = $stmt->fetchAll(PDO::FETCH_ASSOC); echo "共:".count($accountInfo)."\n"; sleep(1); if( empty($accountInfo) ){ exit('没有需要处理的数据'."\n"); } $x = 0; foreach($accountInfo as $account){ #查余额,对比 $user_id = $account['user_id']; $id = $account['id']; $balance = get_balance($user_id, $id); if($balance < $account['rebate']){ if($balance < 0 ){ echo "\n 负值意外!! id:".$id; continue; } //提现过多,清洗 if($balance>2){ $ideal = $balance - 2; //预留0.2元以防意外 } else { $ideal = $balance; } $result = acc_xi($id, $ideal, $account['rebate']); $x++; } } echo "\n 总共清洗:".$x; function acc_xi($id, $ideal, $old){ $_PDO=DB_PDO::getInstance( conf::$DB_ONLINE_CONF ); try{ $sql = "UPDATE account_detail set rebate=:rebate where id=:id"; $stmt = $_PDO->prepare($sql); $stmt->execute(array(':id'=>$id,':rebate'=>$ideal)); $res = $stmt->rowCount(); if(!$res){ echo "更新失败: iD:".$id. ' 结果:'.$res."\n"; } else { $money = $ideal/100; $sql = "UPDATE account_alipay_detail set money = {$money} where detail_id = {$id}"; $stmt = $_PDO->prepare($sql); $stmt->execute(); $res = $stmt->rowCount(); echo "更新成功:iD:".$id. ' 钱: '.$old. '=> '. $ideal.'<->'.$money.' 结果:'.$res."\n"; } }catch(Exception $e){ echo "err_msg:".$e->getMessage()." id:".$id."\n"; } } function get_balance($user_id, $id){ $_PDO=DB_PDO::getInstance( conf::$DB_ONLINE_CONF ); $sql = " select sum(rebate) as money from account_detail where user_id = {$user_id} and type in(1,3) and id< {$id} "; $stmt = $_PDO->prepare($sql); $stmt->execute(); $a1 = $stmt->fetch(PDO::FETCH_ASSOC); $sql = " select sum(rebate) as money from account_detail where user_id = {$user_id} and type=2 and state_of_embodiment in(0, 1) and id<{$id} "; $stmt = $_PDO->prepare($sql); $stmt->execute(); $a2 = $stmt->fetch(PDO::FETCH_ASSOC); $money2 = 0; if( isset($a2['money']) ){ $money2 = $a2['money']; } $balance = $a1['money'] - $money2; return $balance; }