|
|
@@ -11,6 +11,7 @@ use App\Services\common\BatchUpdateService;
|
|
|
use App\Services\common\DataHandlerService;
|
|
|
use App\ValueStore;
|
|
|
use Carbon\Carbon;
|
|
|
+use Doctrine\DBAL\Query\QueryBuilder;
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
@@ -273,7 +274,7 @@ Class CommodityService
|
|
|
// if ($commodities->first()){
|
|
|
// $commodity=$commodities->first();
|
|
|
// }else{
|
|
|
- $commodity = $this->firstOrCreate(['owner_id' => $ownerId, 'sku' => $sku]);
|
|
|
+ $commodity = $this->firstOrCreate(['owner_id' => $ownerId, 'sku' => $sku]);
|
|
|
// }
|
|
|
$commodityBarcodes = $commodity['barcodes'] ?? new Collection();
|
|
|
|
|
|
@@ -431,10 +432,10 @@ Class CommodityService
|
|
|
$last_time = $basSkus->first()['addtime'];
|
|
|
$last_records = $basSkus->where('addtime', $last_time);
|
|
|
if (!$basSkus) return;
|
|
|
- $this->syncCreateCommodity($basSkus);
|
|
|
- $this->deleteCacheKey($create_set, $create_keys);
|
|
|
- $this->setLastRecordsByRedis($create_key, $create_set, $create_keys, $last_records);
|
|
|
- $this->setAsnLastSyncAt($created_at, $last_time);
|
|
|
+ $this->syncCreateCommodity($basSkus);
|
|
|
+ $this->deleteCacheKey($create_set, $create_keys);
|
|
|
+ $this->setLastRecordsByRedis($create_key, $create_set, $create_keys, $last_records);
|
|
|
+ $this->setAsnLastSyncAt($created_at, $last_time);
|
|
|
}
|
|
|
|
|
|
public function syncCommodityUpdated()
|
|
|
@@ -450,10 +451,10 @@ Class CommodityService
|
|
|
$last_time = $basSkus->first()['edittime'];
|
|
|
$last_records = $basSkus->where('edittime', $last_time);
|
|
|
if (!$basSkus) return;
|
|
|
- $this->syncUpdateCommodity($basSkus);
|
|
|
- $this->deleteCacheKey($update_set, $update_keys);
|
|
|
- $this->setLastRecordsByRedis($update_key, $update_set, $update_keys, $last_records);
|
|
|
- $this->setAsnLastSyncAt($updated_at, $last_time);
|
|
|
+ $this->syncUpdateCommodity($basSkus);
|
|
|
+ $this->deleteCacheKey($update_set, $update_keys);
|
|
|
+ $this->setLastRecordsByRedis($update_key, $update_set, $update_keys, $last_records);
|
|
|
+ $this->setAsnLastSyncAt($updated_at, $last_time);
|
|
|
}
|
|
|
|
|
|
public function syncCreateCommodity($addBasSkus)
|
|
|
@@ -464,7 +465,8 @@ Class CommodityService
|
|
|
$this->insertCommodities($insert_params);
|
|
|
/** @var CommodityBarcodeService $commodityBarcodeService */
|
|
|
$commodityBarcodeService = app(CommodityBarcodeService::class);
|
|
|
- $commodityBarcodeService->createBarcodeByWms($addBasSkus);
|
|
|
+ if (count($insert_params) > 0) $commodityBarcodeService->createBarcodeByWms($addBasSkus);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
public function insertCommodities($insert_params)
|
|
|
@@ -475,13 +477,13 @@ Class CommodityService
|
|
|
$skus = array_unique(data_get($insert_params, '*.sku'));
|
|
|
sort($skus);
|
|
|
try {
|
|
|
- $bool = Commodity::query()->insert($insert_params);
|
|
|
+ $bool = $this->insert($insert_params);
|
|
|
if ($bool) {
|
|
|
app('LogService')->log(__METHOD__, __FUNCTION__, "批量添加 Commodity Success " . count($insert_params) . ' || ' . json_encode($insert_params));
|
|
|
- $commodities = Commodity::query()->with('owner')->whereIn('owner_id', $ownerIds)->whereIn('sku', $skus)->get();
|
|
|
+ $commodities = Commodity::query()->whereIn('owner_id', $ownerIds)->whereIn('sku', $skus)->get();
|
|
|
$md5 = md5(json_encode([$skus, $ownerIds]));
|
|
|
if (Cache::has('commodity_' . $md5)) Cache::forget('commodity_' . $md5);
|
|
|
- $this->pushToCache($commodities);
|
|
|
+ if(count($commodities)>0) $this->pushToCache($commodities);
|
|
|
} else app('LogService')->log(__METHOD__, __FUNCTION__, "批量添加 Commodity FAILED " . ' || ' . json_encode($insert_params));
|
|
|
} catch (\Exception $e) {
|
|
|
app('LogService')->log(__METHOD__, __FUNCTION__, "批量添加 Commodity ERROR " . ' || ' . json_encode($insert_params) . ' || ' . json_encode($e->getMessage()) . ' || ' . json_encode($e->getTraceAsString()));
|
|
|
@@ -611,11 +613,13 @@ Class CommodityService
|
|
|
'id', 'name', 'sku', 'owner_id', 'length', 'width', 'height', 'volumn', 'pack_spec', 'updated_at', 'created_at'
|
|
|
]];
|
|
|
$insert_params = [];
|
|
|
+ $updateBasSkus=collect();
|
|
|
foreach ($addBasSkus as $basSku) {
|
|
|
$commodity = Cache::get("owner_code_{$basSku['customerid']}_sku_{$basSku['sku']}");
|
|
|
if (!$commodity) {
|
|
|
$commodity = $dataHandlerService->getKeyValue(['owner_id' => $owner_map[$basSku['customerid']], 'sku' => $basSku['sku']], $commodities_map);
|
|
|
if (!$commodity) {
|
|
|
+ $updateBasSkus->add($basSku);
|
|
|
$insert_params[] = [
|
|
|
'owner_id' => $owner_map[$basSku['customerid']] ?? '',
|
|
|
'sku' => $basSku->sku,
|
|
|
@@ -631,15 +635,18 @@ Class CommodityService
|
|
|
continue;
|
|
|
};
|
|
|
}
|
|
|
- if ($commodity->sku != $basSku->sku ||
|
|
|
- $commodity->name != $basSku->descr_c ||
|
|
|
- $commodity->length != $basSku->skulength ||
|
|
|
- $commodity->width != $basSku->skuwidth ||
|
|
|
- $commodity->height != $basSku->skuhigh ||
|
|
|
- $commodity->volumn != $basSku->cube ||
|
|
|
- $commodity->created_at != $basSku->addtime ||
|
|
|
- $commodity->updated_at != $basSku->edittime ||
|
|
|
- $commodity->pack_spec != $basSku->pickid) {
|
|
|
+ if (
|
|
|
+ /* $commodity->sku != $basSku->sku ||
|
|
|
+ $commodity->name != $basSku->descr_c ||
|
|
|
+ $commodity->length != $basSku->skulength ||
|
|
|
+ $commodity->width != $basSku->skuwidth ||
|
|
|
+ $commodity->height != $basSku->skuhigh ||
|
|
|
+ $commodity->volumn != $basSku->cube ||
|
|
|
+ $commodity->created_at != $basSku->addtime ||
|
|
|
+ $commodity->pack_spec != $basSku->pickid*/
|
|
|
+ $commodity->updated_at != $basSku->edittime
|
|
|
+ ) {
|
|
|
+ $updateBasSkus->add($basSku);
|
|
|
$updateParams[] = [
|
|
|
'id' => $commodity->id,
|
|
|
'owner_id' => $owner_map[$basSku['customerid']] ?? '',
|
|
|
@@ -659,14 +666,19 @@ Class CommodityService
|
|
|
if (count($updateParams) > 0) $this->updateCommodities($updateParams);
|
|
|
/** @var CommodityBarcodeService $commodityBarcodeService */
|
|
|
$commodityBarcodeService = app(CommodityBarcodeService::class);
|
|
|
- $commodityBarcodeService->updateBarcodeByWms($addBasSkus);
|
|
|
+ if (count($updateParams) > 0) $commodityBarcodeService->updateBarcodeByWms($updateBasSkus);
|
|
|
}
|
|
|
|
|
|
private function pushToCache($commodities)
|
|
|
{
|
|
|
+ $owners=Owner::query()->whereIn('id',array_unique(data_get($commodities,'*.owner_id')))->get();
|
|
|
+ $owner_map=[];
|
|
|
+ foreach ($owners as $owner){
|
|
|
+ $owner_map[$owner->id]=$owner->code;
|
|
|
+ }
|
|
|
if (count($commodities) < 1) return null;
|
|
|
foreach ($commodities as $commodity) {
|
|
|
- $commodity_key = "owner_code_{$commodity['owner']['code']}_sku_{$commodity['sku']}";
|
|
|
+ $commodity_key = "owner_code_{$owner_map[$commodity['owner_id']]}_sku_{$commodity['sku']}";
|
|
|
Cache::put($commodity_key, $commodity);
|
|
|
}
|
|
|
}
|
|
|
@@ -696,6 +708,7 @@ Class CommodityService
|
|
|
LogService::log(__METHOD__, __FUNCTION__, '修改或更新' . $key . json_encode($asnLastSyncAt));
|
|
|
return $asnLastSyncAt;
|
|
|
}
|
|
|
+
|
|
|
public function deleteCacheKey($set, $keys)
|
|
|
{
|
|
|
if (Cache::get($set)) {
|
|
|
@@ -729,8 +742,8 @@ Class CommodityService
|
|
|
$this->batchUpdate($updateParams);
|
|
|
$md5 = md5(json_encode([$skus, $ownerIds]));
|
|
|
if (Cache::has('commodity_' . $md5)) Cache::forget('commodity_' . $md5);
|
|
|
- $commodities = Commodity::query()->with('owner')->whereIn('owner_id', $ownerIds)->whereIn('sku', $skus)->get();
|
|
|
- $this->pushToCache($commodities);
|
|
|
+ $commodities = Commodity::query()->whereIn('owner_id', $ownerIds)->whereIn('sku', $skus)->get();
|
|
|
+ if(count($commodities)>0) $this->pushToCache($commodities);
|
|
|
}
|
|
|
|
|
|
// TODO
|
|
|
@@ -740,7 +753,7 @@ Class CommodityService
|
|
|
* @param array $skus
|
|
|
* @param array $barcodes
|
|
|
* @param bool $isSyncWms 是否开启同步wms数据 开启则必须给定 $ownerIds 和 $skus
|
|
|
- * @param int $paginate 分页只对 货主$ownerIds 条件
|
|
|
+ * @param int $paginate 分页只对 货主$ownerIds 条件
|
|
|
* @param int $page
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed|null
|
|
|
*/
|
|
|
@@ -759,28 +772,17 @@ Class CommodityService
|
|
|
case $status == '有SKU':
|
|
|
if ($isSyncWms) {
|
|
|
if (!$ownerIds) return null;
|
|
|
- $owners = Owner::query()->whereIn('id', $ownerIds)->select('id', 'code')->get();
|
|
|
- if (!$owners) return null;
|
|
|
- foreach ($owners as $owner) {
|
|
|
- $ownerCodes[] = $owner->code;
|
|
|
- }
|
|
|
- $bas_skus = OracleBasSKU::query()
|
|
|
- ->select('customerid', 'sku', 'descr_c', 'alternate_sku1', 'alternate_sku2', 'alternate_sku3', 'skulength', 'skuwidth', 'skuhigh', 'cube', 'packid', 'addtime', 'edittime')
|
|
|
- ->whereIn('customerid', $ownerCodes)->whereIn('sku', $skus)
|
|
|
- ->get();
|
|
|
- if (!$bas_skus) return null;
|
|
|
- $this->syncUpdateCommodity($bas_skus);
|
|
|
+ return $this->getCommoditiesByOwnerIdsAndSKu($ownerIds,$skus);
|
|
|
}
|
|
|
sort($skus);
|
|
|
//在取出的记录用 $ownerIds和$barcodes筛选
|
|
|
$commodities = Commodity::query()
|
|
|
- ->with(['barcodes', 'owner'])
|
|
|
->whereIn('sku', $skus)->get();
|
|
|
if ($ownerIds) {
|
|
|
sort($ownerIds);
|
|
|
// $md5 = md5(json_encode([$skus, $ownerIds]));
|
|
|
// return Cache::remember('commodity_' . $md5, $time, function () use ($skus, $ownerIds, $commodities) {
|
|
|
- return $commodities->whereIn('owner_id', $ownerIds);
|
|
|
+ return $commodities->whereIn('owner_id', $ownerIds);
|
|
|
// });
|
|
|
}
|
|
|
if ($barcodes && !$ownerIds) {
|
|
|
@@ -788,7 +790,6 @@ Class CommodityService
|
|
|
$md5 = md5(json_encode([$skus, $barcodes]));
|
|
|
return Cache::remember('commodity_' . $md5, $time, function () use ($skus, $barcodes) {
|
|
|
return Commodity::query()
|
|
|
- ->with(['barcodes', 'owner'])
|
|
|
->whereIn('id', function ($query) use ($barcodes) {
|
|
|
$query->from('commodity_barcodes')->select('commodity_id')->whereIn('code', $barcodes);
|
|
|
})
|
|
|
@@ -800,7 +801,6 @@ Class CommodityService
|
|
|
case $status == '有条码没SKU':
|
|
|
sort($barcodes);
|
|
|
$commodities = Commodity::query()
|
|
|
- ->with(['barcodes', 'owner'])
|
|
|
->whereIn('id', function ($query) use ($barcodes) {
|
|
|
$query->from('commodity_barcodes')->select('commodity_id')->whereIn('code', $barcodes);
|
|
|
})->get();
|
|
|
@@ -821,7 +821,6 @@ Class CommodityService
|
|
|
if (is_null($commodities))//如果缓存已失效或者无缓存则重新缓存
|
|
|
{
|
|
|
$commodities = Commodity::query()
|
|
|
- ->with(['barcodes', 'owner'])
|
|
|
->whereIn('owner_id', $ownerIds)
|
|
|
->orderBy('owner_id', 'asc')
|
|
|
->paginate($paginate, '*', 'page', $page);
|
|
|
@@ -831,4 +830,114 @@ Class CommodityService
|
|
|
return $commodities;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public function getCommoditiesByOwnerIdsAndSKu($owner_ids, $skus)
|
|
|
+ {
|
|
|
+ $owner_codes = Owner::query()->whereIn('id', $owner_ids)->get()->map(function ($owner) {return $owner->code;});
|
|
|
+ $basSkus = OracleBasSKU::query()->whereIn('sku', $skus)->whereIn('customerid', $owner_codes)
|
|
|
+ ->select('customerid', 'sku', 'descr_c', 'alternate_sku1', 'alternate_sku2', 'alternate_sku3', 'skulength', 'skuwidth', 'skuhigh', 'cube', 'packid', 'addtime', 'edittime')->get();
|
|
|
+ $maps = [];
|
|
|
+ foreach ($basSkus as $basSku) {
|
|
|
+ $value = [
|
|
|
+ 'owner_code' => $basSku['customerid'],
|
|
|
+ 'sku' => $basSku['sku']
|
|
|
+ ];
|
|
|
+ $maps[json_encode($value)] = $value;
|
|
|
+ }
|
|
|
+ return $this->getCommoditiesByMaps($maps, $basSkus);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $maps
|
|
|
+ * @param null $basSkus
|
|
|
+ * @return \Illuminate\Support\Collection|\Tightenco\Collect\Support\Collection
|
|
|
+ * 参数maps格式 $maps[json_encode(['owner_code' => $basSku['customerid'],'sku' => $basSku['sku']])]=['owner_code' => $basSku['customerid'],'sku' => $basSku['sku']];
|
|
|
+ */
|
|
|
+ public function getCommoditiesByMaps($maps, $basSkus = null)
|
|
|
+ {
|
|
|
+ $commodities = $this->getCommodityByCacheMaps($maps);
|
|
|
+ if (count($maps) == 0) return $commodities;
|
|
|
+ $commoditiesInDB=$this->getCommodityByMaps($maps);
|
|
|
+ $commodities = $commodities->concat($commoditiesInDB);
|
|
|
+ if (count($maps) == 0) return $commodities;
|
|
|
+ $owners = Owner::query()->whereIn('code', array_unique(data_get($maps, '*.owner_code')))->get();
|
|
|
+ $owner_map = [];
|
|
|
+ $owner_id_map = [];
|
|
|
+ foreach ($owners as $owner) {
|
|
|
+ $owner_map[$owner->id] = $owner->code;
|
|
|
+ $owner_id_map[$owner->code] = $owner->id;
|
|
|
+ }
|
|
|
+ $mapOwnerCodes = [];
|
|
|
+ $mapSkus = [];
|
|
|
+ foreach ($maps as $map) {
|
|
|
+ $mapSkus[] = $map['sku'];
|
|
|
+ $mapOwnerCodes[] = $map['owner_code'];
|
|
|
+ }
|
|
|
+ $basSkus = OracleBasSKU::query()->whereIn('sku', $mapSkus)->whereIn('customerid', $mapOwnerCodes)
|
|
|
+ ->select('customerid', 'sku', 'descr_c', 'alternate_sku1', 'alternate_sku2', 'alternate_sku3', 'skulength', 'skuwidth', 'skuhigh', 'cube', 'packid', 'addtime', 'edittime')->get();
|
|
|
+ if (count($basSkus) < 1) return $commodities;
|
|
|
+ $insert_params = [];
|
|
|
+ $owner_ids = [];
|
|
|
+ $skus = [];
|
|
|
+ foreach ($basSkus as $basSku) {
|
|
|
+ $commodity = Cache::get("owner_code_{$basSku['customerid']}_sku_{$basSku['sku']}");
|
|
|
+ if ($commodity) continue;
|
|
|
+ if ($basSku->sku=='NOSKU')continue;
|
|
|
+ $owner_id = $owner_id_map[$basSku['customerid']] ?? '';
|
|
|
+ $owner_ids[] = $owner_id;
|
|
|
+ $skus[] = $basSku->sku;
|
|
|
+ $insert_params[] = [
|
|
|
+ 'owner_id' => $owner_id,
|
|
|
+ 'sku' => $basSku->sku,
|
|
|
+ 'name' => $basSku->descr_c,
|
|
|
+ 'length' => $basSku->skulength,
|
|
|
+ 'width' => $basSku->skuwidth,
|
|
|
+ 'height' => $basSku->skuhigh,
|
|
|
+ 'volumn' => $basSku->cube,
|
|
|
+ 'created_at' => $basSku->addtime,
|
|
|
+ 'updated_at' => $basSku->edittime,
|
|
|
+ 'pack_spec' => $basSku->packid == 'STANDARD' ? 0 : explode("/", $basSku->packid)[1],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $this->insert($insert_params);
|
|
|
+ app('LogService')->log(__METHOD__, __FUNCTION__, "批量添加 Commodity Success " . count($insert_params) . ' || ' . json_encode($insert_params));
|
|
|
+ $insertCommodities = Commodity::query()->whereIn('owner_id',array_unique($owner_ids))->whereIn('sku',array_unique($skus))->get();
|
|
|
+ if(count($insertCommodities)>0) $this->pushToCache($insertCommodities);
|
|
|
+ $commodities = $commodities->concat($insertCommodities);
|
|
|
+ return $commodities;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getCommodityByCacheMaps(&$maps)
|
|
|
+ {
|
|
|
+ $commodities = collect();
|
|
|
+ foreach ($maps as $key => $map) {
|
|
|
+ $commodity = Cache::get("owner_code_{$map['owner_code']}_sku_{$map['sku']}");
|
|
|
+ if (isset($commodity)) {
|
|
|
+ $commodities->push($commodity);
|
|
|
+ unset($maps[$key]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $commodities;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getCommodityByMaps(&$maps, $owners = null)
|
|
|
+ {
|
|
|
+ $count=count($maps);
|
|
|
+ if ($owners == null) $owners = Owner::query()->whereIn('code', array_unique(data_get($maps, '*.owner_code')))->get();
|
|
|
+ $owner_map=[];
|
|
|
+ foreach ($owners as $owner){
|
|
|
+ $owner_map[$owner->id]=$owner->code;
|
|
|
+ }
|
|
|
+ $commodities = Commodity::query()
|
|
|
+ ->whereIn('owner_id', data_get($owners, '*.id'))
|
|
|
+ ->whereIn('sku', array_unique(data_get($maps, '*.sku')))
|
|
|
+ ->get();
|
|
|
+ foreach ($commodities as $commodity) {
|
|
|
+ $key = json_encode(['owner_code'=>$owner_map[$commodity->owner_id],'sku'=>$commodity->sku]);
|
|
|
+ if(isset($maps[$key]))unset($maps[$key]);
|
|
|
+ }
|
|
|
+ if(count($maps)!=$count) $this->pushToCache($commodities);
|
|
|
+ return $commodities;
|
|
|
+ }
|
|
|
}
|