优惠券订单及其他脚本

user_profit_xi.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. $stime = !empty($argv[1]) ? $argv[1] : date('Y-m-d H:i:s', strtotime('-1 hour'));
  9. $sql = "SELECT id, user_id, rebate from `account_detail_profit` where type=2 and state_of_embodiment in(0,1) and update_at>='{$stime}' order by id ";
  10. echo "beginTime:".$stime." -> endTime:now"."\n";
  11. $stmt = $_PDO->prepare($sql);
  12. $stmt->execute();
  13. $accountInfo = $stmt->fetchAll(PDO::FETCH_ASSOC);
  14. echo "共:".count($accountInfo)."\n";
  15. sleep(1);
  16. if( empty($accountInfo) ){
  17. exit('未获取到需要清洗数据'."\n");
  18. }
  19. $x = 0;
  20. foreach($accountInfo as $account){
  21. #查余额,对比
  22. $user_id = $account['user_id'];
  23. $id = $account['id'];
  24. $balance = get_balance($user_id, $id);
  25. if($balance < $account['rebate']){
  26. if($balance < 0 ){
  27. echo "\n 负值意外!! id:".$id;
  28. continue;
  29. }
  30. //提现过多,清洗
  31. if($balance>2){
  32. $ideal = $balance - 2; //预留0.2元以防意外
  33. } else {
  34. $ideal = $balance;
  35. }
  36. $result = acc_xi($id, $ideal, $account['rebate']);
  37. $x++;
  38. }
  39. }
  40. echo "\n 总共清洗:".$x;
  41. function acc_xi($id, $ideal, $old){
  42. $_PDO=DB_PDO::getInstance( conf::$DB_ONLINE_CONF );
  43. try{
  44. $sql = "UPDATE account_detail_profit set rebate=:rebate where id=:id";
  45. $stmt = $_PDO->prepare($sql);
  46. $stmt->execute(array(':id'=>$id,':rebate'=>$ideal));
  47. $res = $stmt->rowCount();
  48. if(!$res){
  49. echo "更新失败: iD:".$id. ' 结果:'.$res."\n";
  50. } else {
  51. $money = $ideal/100;
  52. $sql = "UPDATE account_alipay_detail_profit set money = {$money} where detail_id = {$id}";
  53. $stmt = $_PDO->prepare($sql);
  54. $stmt->execute();
  55. $res = $stmt->rowCount();
  56. echo "更新成功:iD:".$id. ' 钱: '.$old. '=> '. $ideal.'<->'.$money.' 结果:'.$res."\n";
  57. }
  58. }catch(Exception $e){
  59. echo "err_msg:".$e->getMessage()." id:".$id."\n";
  60. }
  61. }
  62. function get_balance($user_id, $id){
  63. $_PDO=DB_PDO::getInstance( conf::$DB_ONLINE_CONF );
  64. $sql = " select sum(rebate) as money from account_detail_profit where user_id = {$user_id} and type=1 and id< {$id} ";
  65. $stmt = $_PDO->prepare($sql);
  66. $stmt->execute();
  67. $a1 = $stmt->fetch(PDO::FETCH_ASSOC);
  68. $sql = "SELECT sum(rebate) as money from account_detail_profit where user_id = {$user_id} and type=2 and state_of_embodiment in(0,1) and id<{$id} ";
  69. $stmt = $_PDO->prepare($sql);
  70. $stmt->execute();
  71. $a2 = $stmt->fetch(PDO::FETCH_ASSOC);
  72. $money2 = 0;
  73. if( isset($a2['money']) ){
  74. $money2 = $a2['money'];
  75. }
  76. $balance = $a1['money'] - $money2;
  77. return $balance;
  78. }