优惠券订单及其他脚本

profit_balance.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. # 津贴结算脚本
  3. require_once 'DB_PDO.class.php';
  4. require_once 'confv2.class.php';
  5. #连接数据库
  6. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  7. #排除黑名单用户
  8. $black_users = [];
  9. $sql = "SELECT value FROM global_variable WHERE variable_key ='relation_blacklist' ";
  10. $stmt = $_PDO->prepare($sql);
  11. $stmt->execute();
  12. $black = $stmt->fetch(PDO::FETCH_ASSOC);
  13. if( !empty($black) ){
  14. $blacklist = trim($black['value']);
  15. $sql = " SELECT user_id from user_special_relation where relation_id in (".$blacklist.") ";
  16. $stmt = $_PDO->prepare($sql);
  17. $stmt->execute();
  18. $black_user = $stmt->fetchAll(PDO::FETCH_ASSOC);
  19. if( !empty($black_user) ){
  20. $black_users = array_column($black_user, 'user_id');
  21. }
  22. }
  23. #查询上月份结算信息
  24. $stime = date('Y-m-01', strtotime('-1 month')).' 00:00:00';
  25. $etime = date('Y-m-01').' 00:00:00';
  26. $sql = "SELECT user_id,sum(rebate) as money from `order_rebate_profit` where order_balance_at>=:stime and order_balance_at<:etime and status=2 and maintain_power<3 group by user_id ";
  27. echo "beginTime:".$stime." -> endTime:".$etime."\n";
  28. $stmt = $_PDO->prepare($sql);
  29. $stmt->execute(array(':stime'=>$stime,':etime'=>$etime));
  30. $rebateInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
  31. if( empty($rebateInfo) ){
  32. exit('未获取到上月结算信息'."\n");
  33. }
  34. $totalcount = count($rebateInfo);
  35. $success = 0;
  36. $fail = 0;
  37. $already = 0;
  38. $blackcount = 0;
  39. foreach( $rebateInfo as $k=>$v){
  40. if( in_array($v['user_id'], $black_users) ){
  41. //黑名单用户跳过
  42. $blackcount++;
  43. echo "黑名单用户跳过user_id:".$v['user_id']."\n";
  44. continue;
  45. }
  46. $sql = "SELECT * from account_detail_profit where type=:type and user_id=:user_id and create_at>:etime ";
  47. $stmt = $_PDO->prepare($sql);
  48. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':etime'=>$etime));
  49. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  50. if( !empty($res) ){
  51. $already++;
  52. echo "用户已结算过user_id:".$v['user_id']."\n";
  53. continue;
  54. }
  55. try{
  56. $sql = "INSERT INTO account_detail_profit(user_id,type,rebate) values(:user_id,:type,:rebate)";
  57. $stmt = $_PDO->prepare($sql);
  58. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':rebate'=>$v['money']));
  59. $res = $_PDO->lastinsertid();
  60. //echo "插入结果:".$res."\n";
  61. if($res){
  62. $success++;
  63. }else{
  64. $fail++;
  65. }
  66. }catch(Exception $e){
  67. echo "未知异常 user_id".$v['user_id']."\n";
  68. $fail++;
  69. continue;
  70. }
  71. }
  72. echo "成功结算笔数:".$success."\n";
  73. echo "已结算过笔数:".$already."\n";
  74. echo "失败结算笔数:".$fail."\n";
  75. echo "黑名单用户数:".$blackcount."\n";