src/Services/Base/Batch.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Services\Base;
  3. class Batch
  4. {
  5.     const BATCH_CAPACITY 49;
  6.     const BATCH_COUNT 50;
  7.     public function listBatch($crest$method$params$next_count 4$col null)
  8.     {
  9.         $resultBatch $crest->call($method$params);
  10.         if (isset($resultBatch['total']) && isset($resultBatch['next']) && ($resultBatch['total'] - $resultBatch['next']) >= 0) {
  11.             $count 0;
  12.             while ($resultBatch['next'] < $resultBatch['total'] && $count $next_count) {
  13.                 $count++;
  14.                 $params['start'] = $resultBatch['next'];
  15.                 $batch = [];
  16.                 if (($resultBatch['total'] - $params['start']) >= self::BATCH_CAPACITY) {
  17.                     $batchTotal ceil(($resultBatch['total'] - $params['start']) / self::BATCH_COUNT);  // 1=54/50
  18.                     $batchCount 0;
  19.                     $params2 $params;
  20.                     while ($batchCount $batchTotal && $batchCount < ($col $col self::BATCH_COUNT)) {
  21.                         $params2['start'] = $params['start'] + ($batchCount * (self::BATCH_COUNT));
  22.                         $batch[] = [
  23.                             'method' => $method,
  24.                             'params' => $params2
  25.                         ];
  26.                         $batchCount++;
  27.                     }
  28.                 } else {
  29.                     $batch[] = [
  30.                         'method' => $method,
  31.                         'params' => $params
  32.                     ];
  33.                 }
  34. //                dump($batch);
  35.                 $result $crest->callBatch($batch);
  36. //                dump($result);exit();
  37.                 if (isset($result['result']['result'])) {
  38.                     foreach ($result['result']['result'] as $key => $value) {
  39.                         foreach ($value as $val) {
  40.                             $resultBatch['result'][] = $val;
  41.                         }
  42.                         if (array_key_exists($key$result['result']['result_next'])) {
  43.                             $resultBatch['next'] = $result['result']['result_next'][$key];
  44.                         } else {
  45.                             $resultBatch['next'] = $resultBatch['total'];
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.         return $resultBatch;
  52.     }
  53.     public function updateBatch($crest$quantity$dateId$method$fields//$date должны обязательно лежать ID чтобы вызвать их можно было $date['ID']
  54.     {
  55. //        if (is_array($dateId)) {
  56.         if ($quantity self::BATCH_CAPACITY) {
  57.             $batchTotal $quantity self::BATCH_CAPACITY 1;
  58.             $batchCount 1;
  59.             while ($batchCount <= $batchTotal) {
  60.                 $batchCount++;
  61.                 $batch = [];
  62.                 $i 1;
  63.                 while ($i <= self::BATCH_CAPACITY) {
  64.                     $batch[$i] = [
  65.                         'method' => $method,
  66.                         'params' => [
  67.                             'id' => array_shift($dateId)['ID'], //$deals['result']
  68.                             'fields' => $fields
  69.                         ]
  70.                     ];
  71.                     $i++;
  72.                     if (!$dateId) {
  73.                         break;
  74.                     }
  75.                 }
  76.                 $crest->callBatch($batch);
  77.             }
  78.         } else {
  79.             $batch = [];
  80.             foreach ($dateId as $key => $value) {
  81.                 $batch[$key 1] = [
  82.                     'method' => $method,
  83.                     'params' => [
  84.                         'id' => $value['ID'],
  85.                         'fields' => $fields
  86.                     ]
  87.                 ];
  88.             }
  89.             $crest->callBatch($batch);
  90.         }
  91. //        } else {
  92. //            dump('не массив');
  93. //            $res = $crest->call($method, [
  94. //                    'id' => $dateId,
  95. //                    'fields' => $fields
  96. //                ]
  97. //            );
  98. //        }
  99.         return true;
  100.     }
  101. }