优惠券订单及其他脚本

user_account_xi.php 2.6KB

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