1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- #佣金结算脚本
-
- require_once 'DB_PDO.class.php';
- require_once 'confv2.class.php';
-
- #连接数据库
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
-
- #查询上月份结算信息
- $stime = date('Y-m-01', strtotime('-1 month')).' 00:00:00';
- $etime = date('Y-m-01').' 00:00:00';
- $sql = "SELECT user_id,sum(rebate) as money from `order_rebate` where order_balance_at>=:stime and order_balance_at<:etime and status=2 and maintain_power<3 and type=7 group by user_id ";
- echo "beginTime:".$stime." -> endTime:".$etime."\n";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':stime'=>$stime,':etime'=>$etime));
- $rebateInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
-
- if( empty($rebateInfo) ){
- exit('未获取到上月结算信息'."\n");
- }
- $totalcount = count($rebateInfo);
- $success = 0;
- $fail = 0;
- $already = 0;
- foreach( $rebateInfo as $k=>$v){
- $sql = "SELECT * from account_detail where type=:type and user_id=:user_id and create_at>:etime and is_red=1";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':etime'=>$etime));
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if( !empty($res) ){
- $already++;
- echo "用户已结算过user_id:".$v['user_id']."\n";
- continue;
- }
- try{
- //判断用户balance
- $last_balance = getUserBalance($v['user_id']);
- if($last_balance <0 ){
- //特殊处理
- # 优化处理2019-03-26
- if($v['money'] < abs($last_balance*100)){
- $v['money'] = 0;
- $new_balance = $v['money']/100 + $last_balance;
- $last_balance = $v['money']/100;
- }else{
- $v['money'] = $v['money'] + $last_balance*100;
- $new_balance = 0;
- }
- }
- $sql = "INSERT INTO account_detail(user_id,type,rebate,is_red) values(:user_id,:type,:rebate,1)";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':rebate'=>$v['money']));
- $res = $_PDO->lastinsertid();
- //echo "插入结果:".$res."\n";
- if($res){
- if($last_balance <0 ){
- //记入流水,balance置零
- $insertAccount = redAccountRecord($v['user_id'], 6, abs($last_balance), $res, $new_balance);//上月提现返回type:6
- }
- $success++;
- }else{
- $fail++;
- }
- }catch(Exception $e){
- echo "err_msg:".$e->getMessage()." user_id".$v['user_id']."\n";
- $fail++;
- continue;
- }
- }
-
- echo "成功结算笔数:".$success."\n";
- echo "已结算过笔数:".$already."\n";
- echo "失败结算笔数:".$fail."\n";
-
- function getUserBalance($user_id){
- $table = "red_account_record_".($user_id%10);
- $sql = "SELECT balance from {$table} where user_id={$user_id} order by id desc limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- return $res['balance'];
- }
-
- function redAccountRecord($user_id, $type, $money, $oh_id, $balance){
- $table = "red_account_record_".($user_id%10);
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $sql = "insert into `{$table}` (user_id, type, money, oh_id, balance) values ({$user_id},{$type},{$money},'{$oh_id}', {$balance})";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- return $_PDO->lastinsertid();
- }
-
|