RecordUserScoreChangeStatistics.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Facade\TableName;
  4. use Carbon\Carbon;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Facades\DB;
  8. class RecordUserScoreChangeStatistics extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'RecordUserScoreChangeStatistics';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '用户金额变化明细按天按用户汇总';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $carBob = Carbon::yesterday()->subDays(0);
  39. $date = $carBob->format('Y-m-d');
  40. $dateID = $carBob->format('Ymd');
  41. $this->RecordUserScoreChangeStatistics($date, $dateID);
  42. $this->RecordUserScoreChangeTotalStatistics($date, $dateID);
  43. return true;
  44. }
  45. // 按用户,按天分,按类型分
  46. public function RecordUserScoreChangeStatistics($date, $dateID)
  47. {
  48. DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordUserScoreChange as a')
  49. ->whereDate('UpdateTime', $date)
  50. ->selectRaw('sum(ChangeScore) Score,count(UserID) ScoreCount,Reason as ScoreType,UserID')
  51. ->orderBy('UserID')
  52. ->groupBy('UserID', 'Reason')
  53. // ->get()->each(function ($query) use ($dateID) {
  54. // return $query->DateID = $dateID;
  55. // })->map(function ($value) {
  56. // return (array)$value;
  57. // })
  58. ->chunk(100, function ($res) use ($dateID) {
  59. $list = $res->map(function ($val) use ($dateID) {
  60. $array = (array)$val;
  61. Arr::pull($array, 'row_num');
  62. $array['DateID'] = $dateID;
  63. return $array;
  64. })->toArray();
  65. DB::table(TableName::QPRecordDB() . 'RecordUserScoreChangeStatistics')->insert($list);
  66. });
  67. }
  68. // 按天、按类型分
  69. public function RecordUserScoreChangeTotalStatistics($date, $dateID)
  70. {
  71. DB::connection('sqlsrv')->table(TableName::QPRecordDB() . 'RecordUserScoreChange as a')
  72. ->whereDate('UpdateTime', $date)
  73. ->selectRaw('sum(ChangeScore) Score,count(UserID) ScoreCount,Reason as ScoreType')
  74. ->orderBy('Reason')
  75. ->groupBy('Reason')
  76. ->chunk(500, function ($res) use ($dateID) {
  77. $list = $res->map(function ($val) use ($dateID) {
  78. $array = (array)$val;
  79. Arr::pull($array, 'row_num');
  80. $array['DateID'] = $dateID;
  81. return $array;
  82. })->toArray();
  83. DB::table(TableName::QPRecordDB() . 'RecordUserScoreChangeTotalStatistics')->insert($list);
  84. });
  85. // ->get()->each(function ($query) use ($dateID) {
  86. // return $query->DateID = $dateID;
  87. // })->map(function ($value) {
  88. // return (array)$value;
  89. // })->toArray();
  90. // DB::table(TableName::QPRecordDB() . 'RecordUserScoreChangeTotalStatistics')
  91. // ->insert($list);
  92. }
  93. }