- 新增聚水潭回调接口及事件处理机制 - 添加聚水潭商品、订单、售后同步任务 - 实现聚水潭订单推送及售后状态同步 - 新增手动同步聚水潭商品功能 - 在各路由组添加聚水潭中间件支持
116 lines
4.8 KiB
PHP
116 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace app\haichao\sync;
|
|
|
|
use app\haichao\HaichaoJstServices;
|
|
use app\jobs\product\ProductSyncErp;
|
|
use app\services\product\product\StoreProductServices;
|
|
use app\services\product\sku\StoreProductAttrServices;
|
|
use app\services\product\sku\StoreProductAttrValueServices;
|
|
use app\services\store\SystemStoreServices;
|
|
use crmeb\exceptions\AdminException;
|
|
use think\facade\Log;
|
|
use Exception;
|
|
|
|
class JstStorkSync extends HaichaoJstServices
|
|
{
|
|
public function syncStorkFromInneropenErp(): void
|
|
{
|
|
/** @var StoreProductAttrValueServices $storeProductAttrValueServices */
|
|
$storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
|
|
Log::info('jst.stork.sync.start');
|
|
$inventorySkuIds = [];
|
|
$skuIds = '';
|
|
$index = 1;
|
|
$count = 0;
|
|
foreach ($storeProductAttrValueServices->getList(['is_del' => 0]) as $item) {
|
|
if ($item['code']) {
|
|
$skuIds .= $item['code'] . ',';
|
|
$count++;
|
|
$inventorySkuIds[$index] = [
|
|
'length' => $count,
|
|
'ids' => $skuIds,
|
|
];
|
|
if ($count >= 500) {
|
|
$skuIds = '';
|
|
$index++;
|
|
$count = 0;
|
|
}
|
|
}
|
|
}
|
|
$productStock = [];
|
|
foreach ($inventorySkuIds as $inventorySkuId) {
|
|
foreach ($this->getInneropenStockAndDetailList($inventorySkuId['ids']) as $product) {
|
|
$productStock[] = [
|
|
'code' => $product['item_code'],
|
|
'stock' => $product['stock']
|
|
];
|
|
}
|
|
}
|
|
Log::info('jst.stork.sync.merge ' . count($productStock));
|
|
foreach ($productStock as $item) {
|
|
$storeProductAttrValueServices->update(['code' => $item['code']], [
|
|
'stock' => $item['stock'],
|
|
'sum_stock' => $item['stock']
|
|
]);
|
|
}
|
|
Log::info('jst.stork.sync.end');
|
|
}
|
|
|
|
public function syncStockFromErp(array $ids): bool
|
|
{
|
|
try {
|
|
/** @var StoreProductServices $storeProductServices */
|
|
$storeProductServices = app()->make(StoreProductServices::class);
|
|
/** @var StoreProductAttrValueServices $storeProductAttrValueServices */
|
|
$storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
|
|
$list = $storeProductAttrValueServices->getSkuArray(['product_id' => $ids, 'type' => 0], 'code', 'id');
|
|
$values = array_filter(array_values($list));
|
|
if (empty($values)) {
|
|
throw new AdminException('没有符合同步库存的商品');
|
|
}
|
|
$skuData = $skuMap = [];
|
|
$basic = 20;
|
|
$num = count($values);
|
|
$rate = ceil($num / $basic);
|
|
for ($i = 0; $i < $rate; $i++) {
|
|
$code = array_slice($values, $i * $basic, $basic);
|
|
$codeStr = implode(',', $code);
|
|
$result = $this->getStork($codeStr)['data'];
|
|
if (!empty($result['inventorys'])) {
|
|
foreach ($result['inventorys'] as $inventory) {
|
|
$skuMap[$inventory['sku_id']] = $inventory['qty'] - $inventory['order_lock'];
|
|
}
|
|
}
|
|
}
|
|
if (!empty($skuMap)) {
|
|
foreach ($skuMap as $key => $item) {
|
|
if ($id = array_search($key, $list)) {
|
|
$skuData[] = ['id' => $id, 'stock' => $item, 'sum_stock' => $item];
|
|
}
|
|
}
|
|
}
|
|
$storeProductServices->transaction(function () use ($ids, $skuData, $storeProductAttrValueServices, $storeProductServices) {
|
|
$storeProductAttrValueServices->saveAll($skuData);
|
|
$productData = $storeProductAttrValueServices->getProductStockByValues($ids);
|
|
$storeProductServices->saveAll($productData);
|
|
});
|
|
/** @var SystemStoreServices $systemStoreServices */
|
|
$systemStoreServices = app()->make(SystemStoreServices::class);
|
|
$systemStoreList = $systemStoreServices->getErpStore([['erp_shop_id', '>', 0]]);
|
|
if (!empty($systemStoreList)) {
|
|
foreach ($systemStoreList as $store) {
|
|
ProductSyncErp::dispatchDo('syncBranchProductStock', [$ids[0], $skuMap, $store['id']]);
|
|
}
|
|
}
|
|
$storeProductServices->cacheTag()->clear();
|
|
/** @var StoreProductAttrServices $attrService */
|
|
$attrService = app()->make(StoreProductAttrServices::class);
|
|
$attrService->cacheTag()->clear();
|
|
} catch (Exception $e) {
|
|
Log::error('jst.stork.sync.error ' . $e->getMessage());
|
|
}
|
|
return true;
|
|
}
|
|
}
|