$bouquetId = $row['bouquet_id'];
-
$fromAdj = (clone $fromDate)->modify('-1 second')->format('Y-m-d H:i:s');
- $toAdj = (clone $toDate)->modify('+1 second')->format('Y-m-d H:i:s');
+ $toAdj = (clone $toDate)->modify('+1 second')->format('Y-m-d H:i:s');
- /** @var MatrixBouquetActuality[] $hits */
- $hits = MatrixBouquetActuality::find()
+ /** @var MatrixBouquetActuality[] $actualities */
+ $actualities = MatrixBouquetActuality::find()
->where(['bouquet_id' => $bouquetId])
->andWhere('date_to >= :fromAdj', [':fromAdj' => $fromAdj])
- ->andWhere('date_from <= :toAdj', [':toAdj' => $toAdj])
+ ->andWhere('date_from <= :toAdj', [':toAdj' => $toAdj])
->orderBy(['date_from' => SORT_ASC])
->all();
- if (empty($hits)) {
+ if (empty($actualities)) {
$new = new MatrixBouquetActuality([
'guid' => $guid,
'bouquet_id' => $bouquetId,
continue;
}
- $minFrom = $from;
- $maxTo = $to;
- foreach ($hits as $h) {
- if ($h->date_from < $minFrom) { $minFrom = $h->date_from; }
- if ($h->date_to > $maxTo) { $maxTo = $h->date_to; }
- }
-
- $master = array_shift($hits);
- $master->date_from = $minFrom;
- $master->date_to = $maxTo;
+ $master = array_shift($actualities);
+ $master->date_from = $from;
+ $master->date_to = $to;
$master->updated_at = $now;
$master->updated_by = $userId;
Yii::error("Ошибка обновления GUID={$guid}: " . json_encode($master->getErrors(), JSON_UNESCAPED_UNICODE));
}
- foreach ($hits as $dup) {
- $dup->delete();
- }
-
- while (true) {
- $leftBound = (new \DateTime($master->date_from))->modify('-1 second')->format('Y-m-d H:i:s');
- $rightBound = (new \DateTime($master->date_to))->modify('+1 second')->format('Y-m-d H:i:s');
-
- /** @var MatrixBouquetActuality[] $neighbors */
- $neighbors = MatrixBouquetActuality::find()
- ->where(['guid' => $guid])
- ->andWhere(['<>', 'id', $master->id])
- ->andWhere('date_to >= :leftBound', [':leftBound' => $leftBound])
- ->andWhere('date_from <= :rightBound', [':rightBound' => $rightBound])
- ->orderBy(['date_from' => SORT_ASC])
- ->all();
-
- if (empty($neighbors)) {
- break;
- }
-
- foreach ($neighbors as $nei) {
- if ($nei->date_from < $master->date_from)
- {
- $master->date_from = $nei->date_from;
- }
- if ($nei->date_to > $master->date_to)
- {
- $master->date_to = $nei->date_to;
- }
- }
- $master->updated_at = $now;
- $master->updated_by = $userId;
-
- if (!$master->save()) {
- Yii::error("Ошибка повторного обновления GUID={$guid}: " . json_encode($master->getErrors(), JSON_UNESCAPED_UNICODE));
- break;
- }
-
- foreach ($neighbors as $nei) {
- $nei->delete();
- }
+ foreach ($actualities as $actuality) {
+ /** @var MatrixBouquetActuality $actuality */
+ $actuality->delete();
}
}
}