Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

109 rindas
2.3KB

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Email\Members\TerminateOrderComplete;
  4. use App\Kintone\Models\SeasonTicketContract;
  5. use App\Logic\EmailManager;
  6. use App\Util\DateUtil;
  7. use App\Util\DBUtil;
  8. use Exception;
  9. use Illuminate\Support\Collection;
  10. class SeasonTicketContractTerminateComplete extends BaseCommand
  11. {
  12. const COMMAND = "season-ticket-contract:terminate-complete";
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = self::COMMAND;
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '定期契約の解約完了通知を行う';
  25. static public function getCommand()
  26. {
  27. return self::COMMAND;
  28. }
  29. /**
  30. * @var Collection<int, EmailManager>
  31. */
  32. private Collection $managers;
  33. /**
  34. * Create a new command instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct()
  39. {
  40. parent::__construct();
  41. $this->managers = collect();
  42. }
  43. /**
  44. * Execute the console command.
  45. *
  46. * @return int
  47. */
  48. public function service(): int
  49. {
  50. try {
  51. $db = DBUtil::instance();
  52. $db->beginTransaction();
  53. $targets = $this->getTargets();
  54. $this->outputInfo(sprintf("取得対象 %d件", $targets->count()));
  55. // データハンドリング
  56. foreach ($targets as $data) {
  57. $this->handleData($data);
  58. }
  59. // コミット
  60. $this->commit();
  61. $db->commit();
  62. } catch (Exception $e) {
  63. $db->rollBack();
  64. throw $e;
  65. }
  66. return self::RESULTCODE_SUCCESS;
  67. }
  68. private function getTargets()
  69. {
  70. $access = SeasonTicketContract::getAccess();
  71. $query = SeasonTicketContract::getQuery()
  72. ->whereDate(SeasonTicketContract::FIELD_CONTRACT_END_DATE, DateUtil::now()->setTime(0, 0));
  73. return $access->all($query);
  74. }
  75. private function handleData(SeasonTicketContract $data)
  76. {
  77. $email = new TerminateOrderComplete($data);
  78. $manager = new EmailManager($email);
  79. $this->managers->push($manager);
  80. }
  81. private function commit()
  82. {
  83. foreach ($this->managers as $manager) {
  84. $manager->confirm();
  85. }
  86. }
  87. }