优惠券订单及其他脚本

profit_balance.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_profit` where order_balance_at>=:stime and order_balance_at<:etime and status=2 and maintain_power<3 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_profit where type=:type and user_id=:user_id and create_at>:etime and create_at<:ntime";
  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. $id = $res['id'];
  59. try{
  60. $sql = "UPDATE account_detail_profit set rebate=:rebate where id=:id";
  61. $stmt = $_PDO->prepare($sql);
  62. $stmt->execute(array(':id'=>$res['id'],':rebate'=>$v['money']));
  63. $res = $stmt->rowCount();
  64. echo "更新结果:".$res."\n";
  65. if($res){
  66. $success++;
  67. }else{
  68. echo "失败".$id.' '.$v['money']."\n";
  69. $fail++;
  70. }
  71. }catch(Exception $e){
  72. echo "未知异常 user_id".$v['user_id']."\n";
  73. $fail++;
  74. continue;
  75. }
  76. }
  77. echo "成功结算笔数:".$success."\n";
  78. echo "失败结算笔数:".$fail."\n";
  79. echo "黑名单用户数:".$blackcount."\n";