优惠券订单及其他脚本

order_balance_day.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: hy
  5. * Date: 2019-04-12
  6. * Time: 13:54
  7. */
  8. #佣金结算脚本
  9. require_once 'DB_PDO.class.php';
  10. require_once 'conf.class.php';
  11. #连接数据库
  12. $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
  13. date_default_timezone_set('PRC');
  14. set_time_limit(0);
  15. ini_set('memory_limit','512M');
  16. $before_day = 1; //结算几天前的数据
  17. $rebate = 0.5; //结算比率
  18. $today = date('Y-m-d 00:00:00');
  19. #查询上次结算信息
  20. $stime = date('Y-m-d', strtotime(-$before_day.' day')).' 00:00:00';
  21. $etime = date('Y-m-d', strtotime(-$before_day.' day')).' 23:59:59';
  22. $sql = "SELECT user_id,sum(rebate) as money,count(1) as total_count,sum(`money`) as total_price from `order_rebate` where order_create_at>=:stime and order_create_at<=:etime and status>0 and maintain_power<3 group by user_id ";
  23. echo "beginTime:".$stime." -> endTime:".$etime."\n";
  24. $stmt = $_PDO->prepare($sql);
  25. $stmt->execute(array(':stime'=>$stime,':etime'=>$etime));
  26. $rebateInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
  27. if( empty($rebateInfo) ){
  28. exit('未获取到'.$stime.'~'.$etime.'结算信息'."\n");
  29. }
  30. $totalcount = count($rebateInfo);
  31. $success = 0;
  32. $fail = 0;
  33. $already = 0;
  34. foreach( $rebateInfo as $k=>$v){
  35. $sql = "SELECT * from account_detail where type=:type and user_id=:user_id and balance_at=:stime ";
  36. $stmt = $_PDO->prepare($sql);
  37. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>3,':stime'=>$stime));
  38. $res = $stmt->fetch(PDO::FETCH_ASSOC);
  39. if( !empty($res) ){
  40. $already++;
  41. echo "用户已结算过user_id:".$v['user_id']."\n";
  42. continue;
  43. }
  44. //查询欠款
  45. $debt_money = 0;
  46. $if_debt = 0;
  47. //1.获取最近月结
  48. $sql = "SELECT rebate,create_at from account_detail where type=:type and user_id=:user_id order by id desc limit 1";
  49. $stmt = $_PDO->prepare($sql);
  50. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>1));
  51. $last_info = $stmt->fetch(PDO::FETCH_ASSOC);
  52. $last_balance = 0;
  53. if( !empty($last_info) ){
  54. $last_balance = $last_info['rebate'];
  55. }
  56. if($last_balance<0){
  57. //上次月结欠款 ,日结还款
  58. //1.计算已还
  59. $y_money = 0;
  60. $sql = "SELECT sum(rebate) as y_money from account_detail where type=:type and user_id=:user_id and create_at>=:create_at and if_debt=1";
  61. $stmt = $_PDO->prepare($sql);
  62. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>3,':create_at'=>$last_info['create_at']));
  63. $y_info = $stmt->fetch(PDO::FETCH_ASSOC);
  64. if(!empty($y_info)){
  65. $y_money = $y_info['y_money'];
  66. }
  67. if($y_money + $last_balance >= 0){
  68. //1.已经还完
  69. }else{
  70. //2.未还完,欠金额
  71. $debt_money = $y_money + $last_balance;
  72. $if_debt = 1;
  73. }
  74. }
  75. try{
  76. $sql = "INSERT INTO account_detail(user_id,type,rebate,balance_at,order_count,order_total_price,order_total_rebate,day_debt_money,if_debt) values(:user_id,:type,:rebate,:balance_at,:count,:total_price,:total_rebate,:day_debt_money,:if_debt)";
  77. $stmt = $_PDO->prepare($sql);
  78. //结算预估金额的一半
  79. $money = (int)($v['money']*$rebate);
  80. $stmt->execute(array(':user_id'=>$v['user_id'],':type'=>3,':rebate'=>$money,':balance_at' => $stime,':count'=>$v['total_count'],':total_price'=>$v['total_price'],':total_rebate'=>$v['money'], ':day_debt_money'=>$debt_money, ':if_debt'=>$if_debt ));
  81. $res = $_PDO->lastinsertid();
  82. //echo "插入结果:".$res."\n";
  83. if($res){
  84. $success++;
  85. }else{
  86. $fail++;
  87. }
  88. }catch(Exception $e){
  89. echo "err_msg:".$e->getMessage()." user_id".$v['user_id']."\n";
  90. $fail++;
  91. continue;
  92. }
  93. }
  94. echo "成功结算笔数:".$success."\n";
  95. echo "已结算过笔数:".$already."\n";
  96. echo "失败结算笔数:".$fail."\n";