<?php
namespace App\Services\Base;
class Batch
{
const BATCH_CAPACITY = 49;
const BATCH_COUNT = 50;
public function listBatch($crest, $method, $params, $next_count = 4, $col = null)
{
$resultBatch = $crest->call($method, $params);
if (isset($resultBatch['total']) && isset($resultBatch['next']) && ($resultBatch['total'] - $resultBatch['next']) >= 0) {
$count = 0;
while ($resultBatch['next'] < $resultBatch['total'] && $count < $next_count) {
$count++;
$params['start'] = $resultBatch['next'];
$batch = [];
if (($resultBatch['total'] - $params['start']) >= self::BATCH_CAPACITY) {
$batchTotal = ceil(($resultBatch['total'] - $params['start']) / self::BATCH_COUNT); // 1=54/50
$batchCount = 0;
$params2 = $params;
while ($batchCount < $batchTotal && $batchCount < ($col ? $col : self::BATCH_COUNT)) {
$params2['start'] = $params['start'] + ($batchCount * (self::BATCH_COUNT));
$batch[] = [
'method' => $method,
'params' => $params2
];
$batchCount++;
}
} else {
$batch[] = [
'method' => $method,
'params' => $params
];
}
// dump($batch);
$result = $crest->callBatch($batch);
// dump($result);exit();
if (isset($result['result']['result'])) {
foreach ($result['result']['result'] as $key => $value) {
foreach ($value as $val) {
$resultBatch['result'][] = $val;
}
if (array_key_exists($key, $result['result']['result_next'])) {
$resultBatch['next'] = $result['result']['result_next'][$key];
} else {
$resultBatch['next'] = $resultBatch['total'];
}
}
}
}
}
return $resultBatch;
}
public function updateBatch($crest, $quantity, $dateId, $method, $fields) //$date должны обязательно лежать ID чтобы вызвать их можно было $date['ID']
{
// if (is_array($dateId)) {
if ($quantity > self::BATCH_CAPACITY) {
$batchTotal = $quantity / self::BATCH_CAPACITY + 1;
$batchCount = 1;
while ($batchCount <= $batchTotal) {
$batchCount++;
$batch = [];
$i = 1;
while ($i <= self::BATCH_CAPACITY) {
$batch[$i] = [
'method' => $method,
'params' => [
'id' => array_shift($dateId)['ID'], //$deals['result']
'fields' => $fields
]
];
$i++;
if (!$dateId) {
break;
}
}
$crest->callBatch($batch);
}
} else {
$batch = [];
foreach ($dateId as $key => $value) {
$batch[$key + 1] = [
'method' => $method,
'params' => [
'id' => $value['ID'],
'fields' => $fields
]
];
}
$crest->callBatch($batch);
}
// } else {
// dump('не массив');
// $res = $crest->call($method, [
// 'id' => $dateId,
// 'fields' => $fields
// ]
// );
// }
return true;
}
}