From 037213b1e3c277a414e76646d45c1e55122dbd10 Mon Sep 17 00:00:00 2001 From: Vladimir Fomichev Date: Fri, 15 Aug 2025 15:37:25 +0300 Subject: [PATCH] =?utf8?q?=D0=A1=D0=BB=D0=B8=D1=8F=D0=BD=D0=B8=D0=B5=20?= =?utf8?q?=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/api2/controllers/DataController.php | 123 ++++++++++++---------- 1 file changed, 69 insertions(+), 54 deletions(-) diff --git a/erp24/api2/controllers/DataController.php b/erp24/api2/controllers/DataController.php index ec992b0a..ffbe7d81 100644 --- a/erp24/api2/controllers/DataController.php +++ b/erp24/api2/controllers/DataController.php @@ -228,52 +228,46 @@ class DataController extends BaseController file_put_contents(self::OUT_DIR . '/request_error.txt', PHP_EOL . date("d.m.Y H:i:s", time()) . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine(), FILE_APPEND); LogService::apiDataLogs(1, $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine(), time()); } finally { - $jsonArray = []; + $jsonId = null; + $json = null; + try { + $apiCron = ApiCron::find()->where(['status' => 0])->orderBy(['date' => SORT_ASC])->one(); - $apiCron = ApiCron::find()->where(['status' => 0])->orderBy(['date' => SORT_ASC])->one(); - - $mess['debug'] = [ - 'is_object' => is_object($apiCron), - 'class' => get_class($apiCron), - 'attributes' => $apiCron ? $apiCron->attributes : null, - 'errors' => $apiCron ? $apiCron->errors : null - ]; - if (!empty($apiCron) && empty($mess['create_orders'])) { - $mess['line'][] = __LINE__; - $jsonId = $apiCron->request_id; -// $apiCron2 = ApiCron::find()->where(['id' => $apiCron->id])->andWhere(['request_id' => ''])->one(); -// if ($apiCron2) { -// $apiCron2->status = 1; -// $apiCron2->date_up = date('Y-m-d H:i:s'); -// $apiCron2->request_id = $mess['request_id']; -// $apiCron2->save(); -// if ($apiCron2->getErrors()) { -// LogService::apiErrorLog(json_encode(["error_id" => 1, "error" => $apiCron2->getErrors()], JSON_UNESCAPED_UNICODE)); -// } -// $mess['request_id'] = $apiCron2->request_id; -// } -// if (!empty($mess['request_id'])) { -// $apiCron->status = 1; -// $apiCron->date_up = date('Y-m-d H:i:s'); -// $apiCron->save(); -// if ($apiCron->getErrors()) { -// LogService::apiErrorLog(json_encode(["error_id" => 3, "error" => $apiCron->getErrors()], JSON_UNESCAPED_UNICODE)); -// } -// } - $mess['line'][] = __LINE__; - $jsonDecoded = json_decode($apiCron->json_post, true); - $mess['line'][] = __LINE__; - $jsonDecoded['mess'] = $mess; + $payload = $mess; + + if ($apiCron) { + $claimed = ApiCron::updateAll( + [ + 'status' => 1, + 'date_up' => date('Y-m-d H:i:s'), + 'request_id' => $mess['request_id'] ?? (string) time(), + ], + [ + 'and', + ['id' => $apiCron->id], + ['status' => 0], + ] + ); + + if ($claimed) { + $cronDecoded = json_decode($apiCron->json_post, true) ?: []; + + if (!empty($cronDecoded['request_id'])) { + $jsonId = $cronDecoded['request_id']; + } else { + $jsonId = $apiCron->request_id ?: ($mess['request_id'] ?? (string) time()); + } + + $payload = $this->mergePayloads($payload, $cronDecoded); - $createEmployeeArray = $this->getCreateEmployee(); - if (!empty($createEmployeeArray)) { - $jsonDecoded['create_employee'] = $createEmployeeArray; + $createEmployeeArray = $this->getCreateEmployee(); + if (!empty($createEmployeeArray)) { + $payload['create_employee'] = $createEmployeeArray; + } + } } - $json = json_encode($jsonDecoded, JSON_UNESCAPED_UNICODE); - } else { - $mess['line'][] = __LINE__; if (!empty($writeOffIds) && !empty($writeOff)) { $this->getWriteOffsSetStatusSend($writeOffIds); } @@ -286,25 +280,22 @@ class DataController extends BaseController if (!empty($replacementInvoiceIds) && !empty($replacementInvoice)) { $this->getReplacementInvoiceSetStatusSend($replacementInvoiceIds); } - $json = json_encode($mess, JSON_UNESCAPED_UNICODE); - } - } catch (Exception $e) { - $mess['line'][] = ' error ' . __LINE__ . ' ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine(); - } -// -// if (!empty($mess)) { -// $jsonArray = array_merge($jsonArray, $mess); -// $json = json_encode($jsonArray, JSON_UNESCAPED_UNICODE); -// } + if (empty($jsonId)) { + $jsonId = $mess['request_id'] ?? (string) time(); + } - if (empty($jsonId)) { - $mess['line'][] = __LINE__; - $jsonId = time(); + $json = json_encode($payload, JSON_UNESCAPED_UNICODE); + + } catch (\Throwable $e) { + $mess['line'][] = ' error ' . __LINE__ . ' ' . $e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine(); + $jsonId = $jsonId ?? (string) time(); + $json = $json ?? json_encode($mess, JSON_UNESCAPED_UNICODE); } file_put_contents(self::OUT_DIR . '/request_' . $jsonId . '.json', PHP_EOL . $json); file_put_contents(self::getPathJson() . '/request_' . $jsonId . '.json', PHP_EOL . $json); + $mess['line'][] = __LINE__; LogService::apiDataLogs(1, json_encode($mess, JSON_UNESCAPED_UNICODE), $jsonId); Yii::error('Логи запроса ' . $jsonId . ' ' . json_encode($mess, JSON_UNESCAPED_UNICODE)); @@ -313,6 +304,30 @@ class DataController extends BaseController return $this->asJson(json_decode($json, true)); } + private function mergePayloads(array $base, array $override): array + { + foreach ($override as $key => $val) { + if (!array_key_exists($key, $base)) { + $base[$key] = $val; + continue; + } + + if (is_array($base[$key]) && is_array($val) && array_is_list($base[$key]) && array_is_list($val)) { + $base[$key] = array_values(array_merge($base[$key], $val)); + continue; + } + + if (is_array($base[$key]) && is_array($val)) { + $base[$key] = mergePayloads($base[$key], $val); + continue; + } + + $base[$key] = $val; + } + return $base; + } + + private function getWriteOffsSetStatusSend($writeOffIds) { try { -- 2.39.5