ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace JR\NetSuite\Model\ResourceModel\OrderQueue;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
|
||||
use JR\NetSuite\Model\OrderQueue;
|
||||
use JR\NetSuite\Model\ResourceModel\OrderQueue as OrderQueueResource;
|
||||
use JR\NetSuite\Helper\Data as Helper;
|
||||
|
||||
class Collection extends AbstractCollection {
|
||||
|
||||
protected function _construct() {
|
||||
$this->_init(OrderQueue::class, OrderQueueResource::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get next items in the queue based on batch size
|
||||
*
|
||||
* @param int $type_id
|
||||
* @param int $batchSize
|
||||
* @return \Magento\Framework\Data\Collection\AbstractDb
|
||||
*/
|
||||
public function getNext($batchSize = null) {
|
||||
$collection = $this->addFieldToSelect('*')
|
||||
->addFieldToFilter('status', ['eq' => 'pending'])
|
||||
->setOrder('id', 'asc');
|
||||
|
||||
if ($batchSize) {
|
||||
$collection->getSelect()->limit($batchSize);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update based on UNIQUE combo 'key-processing'
|
||||
* @param $key
|
||||
* @param $value
|
||||
*/
|
||||
public function insertOrUpdate($item) {
|
||||
$connection = $this->getConnection();
|
||||
try {
|
||||
// Start a transaction
|
||||
// $connection->beginTransaction();
|
||||
|
||||
// echo json_encode($item, JSON_PRETTY_PRINT) . "\n";
|
||||
|
||||
// update the existing item with same netsuite_order_id
|
||||
$select = $connection->select()
|
||||
->from($this->getMainTable())
|
||||
->where('netsuite_order_id = ?', $item['netsuite_order_id']);
|
||||
$existingItem = $connection->fetchRow($select);
|
||||
|
||||
if (!$existingItem) {
|
||||
$existingItem = [];
|
||||
}
|
||||
|
||||
// if the existing item is already success and the data is the same, skip
|
||||
if (isset($existingItem['status']) && $existingItem['status'] == 'success'
|
||||
&& isset($existingItem['data']) && $existingItem['data'] == json_encode($item)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$recorded_at = \DateTime::createFromFormat('m/d/Y g:i a', $item['last_modified_date'], new \DateTimeZone('America/Los_Angeles'));
|
||||
|
||||
$recorded_at->setTimezone(new \DateTimeZone('UTC'));
|
||||
|
||||
$data = [
|
||||
'netsuite_order_id' => $item['netsuite_order_id'],
|
||||
'data' => json_encode($item),
|
||||
'messages' => '',
|
||||
'recorded_at' => $recorded_at->format('Y-m-d H:i:s'),
|
||||
// 'created_at' => (new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT),
|
||||
'status' => 'pending',
|
||||
];
|
||||
|
||||
// update the existing item with same netsuite_order_id
|
||||
if (isset($existingItem['id'])) {
|
||||
$data['id'] = $existingItem['id'];
|
||||
}
|
||||
|
||||
// Insert new item or update existing one
|
||||
$connection->insertOnDuplicate($this->getMainTable(), $data, ['data', 'status', 'messages', 'recorded_at']);
|
||||
|
||||
// Commit the transaction
|
||||
// $connection->commit();
|
||||
} catch (\Exception $e) {
|
||||
// Rollback the transaction in case of an error
|
||||
$connection->rollBack();
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Error in insertOrUpdate: %1', $e->getMessage()),
|
||||
$e
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last sync netsuite order id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLastSyncRecordDate() {
|
||||
$select = $this->getConnection()->select()
|
||||
->from($this->getMainTable(), ['recorded_at'])
|
||||
->order('recorded_at DESC')
|
||||
->limit(1);
|
||||
|
||||
return $this->getConnection()->fetchOne($select);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user