优惠券订单及其他脚本

red_balance.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $stime = date('Y-m-01', strtotime('-1 month')).' 00:00:00';
  9. $etime = date('Y-m-01').' 00:00:00';
  10. $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 ";
  11. echo "beginTime:".$stime." -> endTime:".$etime."\n";
  12. $stmt = $_PDO->prepare($sql);
  13. $stmt->execute(array(':stime'=>$stime,':etime'=>$etime));
  14. $rebateInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
  15. if( empty($rebateInfo) ){
  16. exit('未获取到上月结算信息'."\n");
  17. }
  18. $totalcount = count($rebateInfo);
  19. $success = 0;
  20. $fail = 0;
  21. $already = 0;
  22. foreach( $rebateInfo as $k=>$v){
  23. $sql = "SELECT * from account_detail where type=:type and user_id=:user_id and create_at>:etime and is_red=1";
  24. $stmt = $_PDO->prepare($sql);
  25. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':etime'=>$etime));
  26. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  27. if( !empty($res) ){
  28. $already++;
  29. echo "用户已结算过user_id:".$v['user_id']."\n";
  30. continue;
  31. }
  32. try{
  33. //判断用户balance
  34. $last_balance = getUserBalance($v['user_id']);
  35. if($last_balance <0 ){
  36. //特殊处理
  37. # 优化处理2019-03-26
  38. if($v['money'] < abs($last_balance*100)){
  39. $v['money'] = 0;
  40. $new_balance = $v['money']/100 + $last_balance;
  41. $last_balance = $v['money']/100;
  42. }else{
  43. $v['money'] = $v['money'] + $last_balance*100;
  44. $new_balance = 0;
  45. }
  46. }
  47. $sql = "INSERT INTO account_detail(user_id,type,rebate,is_red) values(:user_id,:type,:rebate,1)";
  48. $stmt = $_PDO->prepare($sql);
  49. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1,':rebate'=>$v['money']));
  50. $res = $_PDO->lastinsertid();
  51. //echo "插入结果:".$res."\n";
  52. if($res){
  53. if($last_balance <0 ){
  54. //记入流水,balance置零
  55. $insertAccount = redAccountRecord($v['user_id'], 6, abs($last_balance), $res, $new_balance);//上月提现返回type:6
  56. }
  57. $success++;
  58. }else{
  59. $fail++;
  60. }
  61. }catch(Exception $e){
  62. echo "err_msg:".$e->getMessage()." user_id".$v['user_id']."\n";
  63. $fail++;
  64. continue;
  65. }
  66. }
  67. echo "成功结算笔数:".$success."\n";
  68. echo "已结算过笔数:".$already."\n";
  69. echo "失败结算笔数:".$fail."\n";
  70. function getUserBalance($user_id){
  71. $table = "red_account_record_".($user_id%10);
  72. $sql = "SELECT balance from {$table} where user_id={$user_id} order by id desc limit 1";
  73. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  74. $stmt = $_PDO->prepare($sql);
  75. $stmt->execute();
  76. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  77. return $res['balance'];
  78. }
  79. function redAccountRecord($user_id, $type, $money, $oh_id, $balance){
  80. $table = "red_account_record_".($user_id%10);
  81. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  82. $sql = "insert into `{$table}` (user_id, type, money, oh_id, balance) values ({$user_id},{$type},{$money},'{$oh_id}', {$balance})";
  83. $stmt = $_PDO->prepare($sql);
  84. $stmt->execute();
  85. return $_PDO->lastinsertid();
  86. }