SaveBookSettlementMode.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Console\Commands\ContentManage;
  3. use Dotenv\Store\StoreBuilder;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. class SaveBookSettlementMode extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'ContentManage:SaveBookSettlementMode {--bid= : 书籍id}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '保存书籍的结算模式,每天执行一次';
  20. /**
  21. * Execute the console command.
  22. */
  23. public function handle(): void
  24. {
  25. $maxBid = 0;
  26. $bid = $this->option('bid');
  27. while (true) {
  28. $books = DB::table('books')
  29. ->when($bid, function ($query, $bid) {
  30. return $query->where(['id' => $bid]);
  31. })
  32. ->select('id', 'settlement_type', 'bottomline')
  33. ->orderBy('id', 'asc')
  34. ->where('id', '>', $maxBid)
  35. ->limit(300)
  36. ->get();
  37. $maxBid = $books->last()->id ?? 0;
  38. if(0 == $maxBid) {
  39. break;
  40. }
  41. $this->dealBooks($books);
  42. }
  43. }
  44. private function dealBooks($books) {
  45. $nowDate = date('Y-m-d');
  46. $nowTime = date('Y-m-d H:i:s');
  47. $bids = $books->pluck('id')->unique();
  48. $bookLasteSettlementModeMap = $this->bookLasteSettlementModeMap($bids);
  49. foreach ($books as $book) {
  50. if($bookLasteSettlementModeMap->has($book->id)) {
  51. $bookLasteSettlementMode = $bookLasteSettlementModeMap->get($book->id);
  52. if($book->settlement_type == $bookLasteSettlementMode['settlement_mode'] &&
  53. $book->bottomline == $bookLasteSettlementMode['bottomline']) {
  54. continue;
  55. }
  56. }
  57. DB::table('book_settlement_mode_history')
  58. ->insert([
  59. 'bid' => $book->id,
  60. 'effective_date' => $nowDate,
  61. 'settlement_mode' => $book->settlement_type,
  62. 'bottomline' => $book->bottomline,
  63. 'change_reason' => 'manual',
  64. 'created_at' => $nowTime,
  65. 'updated_at' => $nowTime
  66. ]);
  67. }
  68. }
  69. private function bookLasteSettlementModeMap($bids) {
  70. $map = collect();
  71. $history = DB::table('book_settlement_mode_history')
  72. ->whereIn('bid', $bids)
  73. ->where([
  74. 'is_enabled' => 1, 'change_reason' => 'manual'
  75. ])->select('id', 'settlement_mode', 'bottomline', 'effective_date', 'bid')
  76. ->orderBy('id', 'desc')
  77. ->get();
  78. foreach ($history as $h) {
  79. if($map->has($h->bid)) {
  80. $settlementMode = $map->get($h->bid);
  81. if($settlementMode['effective_date'] >= $h->effective_date) {
  82. continue;
  83. }
  84. }
  85. $map->put($h->bid, [
  86. 'effective_date' => $h->effective_date,
  87. 'settlement_mode' => $h->settlement_mode,
  88. 'bottomline' => $h->bottomline
  89. ]);
  90. }
  91. return $map;
  92. }
  93. }