*/ private Collection $settings; /** * 振込者名がキー * @var Collection **/ private Collection $settingMap; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function service(): int { $this->getSettings(); $targets = $this->getTargets(); $this->outputInfo(sprintf("総件数:%d件", $targets->count())); foreach ($targets as $target) { try { $this->handleData($target); } catch (SkipException $e) { $this->outputWarn($e->getMessage()); } } return self::RESULTCODE_SUCCESS; } public function handleData(YuchoPaymentResult $payment) { $this->resolve($payment); } private function getSettings() { // 設定をキャッシュする $this->settings = YuchoPaymentRelationSetting::getAccess()->all(); $ret = collect(); foreach ($this->settings as $setting) { $name = $setting->paymentName; $ret->put($name, $setting); } $this->settingMap = $ret; } private function findSetting(string $paymentName) { return $this->settingMap->get($paymentName); } private function resolve(YuchoPaymentResult $payment) { $setting = $this->findSetting($payment->paymentName); if ($setting) { $payment->customerCode = $setting->customerCode; $this->outputInfo(sprintf("割り当て from <%s> to <%s>(%d)", $payment->paymentName, $setting->customerName, $setting->customerCode)); $payment->save(); } } public function getTargets() { $limitDate = DateUtil::now()->addDays(-7); // 未充当の入金一覧を取得する // 直近7日分を対象とする $targets = YuchoPaymentResult::getAccess()->all(YuchoPaymentResult::getQuery() ->whereNull(YuchoPaymentResult::FIELD_CUSTOMER_CODE) ->whereDate(YuchoPaymentResult::FIELD_PAYMENT_DATE, $limitDate, KintoneRecordQueryOperator::GE)); return $targets; } }