RepeatBookOrderRecover.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. class RepeatBookOrderRecover extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'book:bor {--type=}';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '单本订购重复订单恢复';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $options = $this->option('type');
  36. if($options){
  37. echo $this->stats();
  38. }else{
  39. echo $this->start();
  40. }
  41. return;
  42. }
  43. public function start(){
  44. $sql = "SELECT uid,bid,COUNT(*) FROM book_orders where uid>0 and bid>0 GROUP BY uid,bid HAVING COUNT(*) >=2";
  45. $res = DB::select($sql);
  46. $i = 0;
  47. foreach ($res as $v){
  48. $order = DB::table('book_orders')->where('bid',$v->bid)->where('uid',$v->uid)->first();
  49. $update_user_sql = "UPDATE users set balance = balance+{$order->fee},reward_balance=reward_balance+{$order->reward_balance},charge_balance=charge_balance+{$order->charge_balance} where id = {$order->uid}";
  50. DB::update($update_user_sql);
  51. DB::table('book_orders')->where('id',$order->id)->update(['uid'=>-$order->uid]);
  52. $i++;
  53. }
  54. return '修改了'.$i.'个用户';
  55. }
  56. public function stats(){
  57. $sql = "SELECT id,bid,fee,date(created_at) as days,send_order_id,charge_balance,reward_balance FROM book_orders WHERE uid<0 and fee >0";
  58. $res = DB::select($sql);
  59. $i = 0;
  60. foreach ($res as $v){
  61. $update_sql = "update book_order_statistical set fee=fee-{$v->fee}, reward_balance=reward_balance-{$v->reward_balance}, charge_balance=charge_balance-{$v->charge_balance} where bid={$v->bid} and `day`='{$v->days}'";
  62. DB::update($update_sql);
  63. DB::table('book_orders')->where('id',$v->id)->update(['fee'=>0]);
  64. //DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->days)->
  65. $i++;
  66. }
  67. return '修改了'.$i.'个记录';
  68. }
  69. }