优惠券订单及其他脚本

order_balance.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. #佣金结算脚本
  3. define( "ROOT_PATH", dirname(dirname(__FILE__)) );
  4. require_once ROOT_PATH.'/DB_PDO.class.php';
  5. require_once ROOT_PATH.'/confv2.class.php';
  6. #连接数据库
  7. $_PDO=DB_PDO::getInstance( conf::$DB_ONLINE_CONF );
  8. #排除黑名单用户
  9. $black_users = [];
  10. /*
  11. $sql = "SELECT value FROM global_variable WHERE variable_key ='relation_blacklist' ";
  12. $stmt = $_PDO->prepare($sql);
  13. $stmt->execute();
  14. $black = $stmt->fetch(PDO::FETCH_ASSOC);
  15. if( !empty($black) ){
  16. $blacklist = trim($black['value']);
  17. $sql = " SELECT user_id from user_special_relation where relation_id in (".$blacklist.") ";
  18. $stmt = $_PDO->prepare($sql);
  19. $stmt->execute();
  20. $black_user = $stmt->fetchAll(PDO::FETCH_ASSOC);
  21. if( !empty($black_user) ){
  22. $black_users = array_column($black_user, 'user_id');
  23. }
  24. }
  25. */
  26. #查询上月份结算信息
  27. $stime = date('2019-12-01').' 00:00:00';
  28. $etime = date('2020-01-01').' 00:00:00';
  29. $ntime = date('2020-01-26').' 00:00:00';
  30. $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 ";
  31. echo "beginTime:".$stime." -> endTime:".$etime."\n";
  32. $stmt = $_PDO->prepare($sql);
  33. $stmt->execute(array(':stime'=>$stime,':etime'=>$etime));
  34. $rebateInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
  35. if( empty($rebateInfo) ){
  36. exit('未获取到上月结算信息'."\n");
  37. }
  38. $totalcount = count($rebateInfo);
  39. $success = 0;
  40. $fail = 0;
  41. $already = 0;
  42. $blackcount = 0;
  43. foreach( $rebateInfo as $k=>$v){
  44. if( in_array($v['user_id'], $black_users) ){
  45. //黑名单用户跳过
  46. $blackcount++;
  47. echo "黑名单用户跳过user_id:".$v['user_id']."\n";
  48. continue;
  49. }
  50. $sql = "SELECT id from account_detail where type=:type and user_id=:user_id and create_at>:etime and create_at<:ntime and is_red=0";
  51. $stmt = $_PDO->prepare($sql);
  52. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':etime'=>$etime, ':ntime'=>$ntime));
  53. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  54. if( empty($res) ){
  55. echo "多多!!!用户跳过user_id:".$v['user_id']."\n";
  56. continue;
  57. }
  58. try{
  59. $sql = "UPDATE account_detail set rebate=:rebate where id=:id";
  60. $stmt = $_PDO->prepare($sql);
  61. $stmt->execute(array(':id'=>$res['id'],':rebate'=>$v['money']));
  62. $res = $stmt->rowCount();
  63. echo "更新结果:".$res."\n";
  64. if($res){
  65. $success++;
  66. }else{
  67. $fail++;
  68. }
  69. }catch(Exception $e){
  70. echo "err_msg:".$e->getMessage()." user_id".$v['user_id']."\n";
  71. $fail++;
  72. continue;
  73. }
  74. }
  75. echo "成功结算笔数:".$success."\n";
  76. echo "失败结算笔数:".$fail."\n";
  77. echo "黑名单用户数:".$blackcount."\n";