1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- #佣金结算脚本
-
- require_once 'DB_PDO.class.php';
- require_once 'confv2.class.php';
-
- #连接数据库
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
-
- #排除黑名单用户
- $black_users = [];
- $sql = "SELECT value FROM global_variable WHERE variable_key ='relation_blacklist' ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $black = $stmt->fetch(PDO::FETCH_ASSOC);
- if( !empty($black) ){
- $blacklist = trim($black['value']);
-
- $sql = " SELECT user_id from user_special_relation where relation_id in (".$blacklist.") ";
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $black_user = $stmt->fetchAll(PDO::FETCH_ASSOC);
- if( !empty($black_user) ){
- $black_users = array_column($black_user, 'user_id');
- }
- }
-
- #查询上月份结算信息
- $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;
- $blackcount = 0;
- foreach( $rebateInfo as $k=>$v){
- if( in_array($v['user_id'], $black_users) ){
- //黑名单用户跳过
- $blackcount++;
- echo "黑名单用户跳过user_id:".$v['user_id']."\n";
- continue;
- }
-
- $sql = "SELECT * from account_detail where type=:type and user_id=:user_id and create_at>:etime and is_red=0";
- $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{
- $sql = "INSERT INTO account_detail(user_id,type,rebate) values(:user_id,:type,:rebate)";
- $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){
- $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";
- echo "黑名单用户数:".$blackcount."\n";
-
-
|