|
|
@@ -16,10 +16,16 @@ use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
Class CommodityService
|
|
|
{
|
|
|
- /** @var CacheService $cacheService */
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var CacheService $cacheService
|
|
|
+ * @var OwnerService $ownerService
|
|
|
+ */
|
|
|
private $cacheService;
|
|
|
+ private $ownerService;
|
|
|
function __construct(){
|
|
|
$this->cacheService=app('CacheService');
|
|
|
+ $this->ownerService=app('OwnerService');
|
|
|
}
|
|
|
|
|
|
public function firstOrCreate($param,$column = null):Commodity{
|
|
|
@@ -343,107 +349,40 @@ Class CommodityService
|
|
|
|
|
|
public function getCommoditiesByMap($map)
|
|
|
{
|
|
|
- /**
|
|
|
- * @var OwnerService $ownerService
|
|
|
- * @var OracleBasSkuService $oracleBasSkuService
|
|
|
- */
|
|
|
- $ownerService = app('OwnerService');
|
|
|
- $oracleBasSkuService = app('OracleBasSkuService');
|
|
|
-
|
|
|
- $commodities_map = (function()use($map){
|
|
|
- $commodities_map = [];
|
|
|
- if(count($map)==0)return $commodities_map;
|
|
|
- Commodity::query()->with("owner")->whereIn('sku',array_unique(data_get($map,'*.sku')))->whereHas('owner',function($query)use($map){
|
|
|
- $query->whereIn('code',array_unique(data_get($map,'*.owner_code')));
|
|
|
- })->get()->each(function($commodity)use(&$commodities_map){
|
|
|
- $commodities_map[json_encode(['owner_code' => $commodity['owner']['code'], 'sku' => $commodity['sku']])] = $commodity;
|
|
|
- });
|
|
|
- return $commodities_map;
|
|
|
- })();
|
|
|
-
|
|
|
- $owner_codes = (function()use($map){
|
|
|
- $owner_codes = [];
|
|
|
- if(count($map) == 0)return $owner_codes;
|
|
|
- foreach ($map as $item) {
|
|
|
- $owner_codes[$item['owner_code']] = $item['owner_code'];
|
|
|
- }
|
|
|
- return $owner_codes;
|
|
|
- })();
|
|
|
-
|
|
|
- $owner_map = (function()use($ownerService,$owner_codes){
|
|
|
- $owners = $ownerService->getOwnerByCodes($owner_codes);
|
|
|
- $map = [];
|
|
|
- $owners->each(function ($owner)use(&$map){
|
|
|
- $map[$owner['code']] = $owner['id'];
|
|
|
- });
|
|
|
- return $map;
|
|
|
- })();
|
|
|
-
|
|
|
$collect = collect();
|
|
|
- if(count($map) == 0) return $collect;
|
|
|
- $unexists = [];$sku = [];$owner_code = [];
|
|
|
-
|
|
|
+ if(count($map)==0)return $collect;
|
|
|
foreach ($map as $item) {
|
|
|
- $owner = Cache::get("owner_code_{$item['owner_code']}_sku_{$item['sku']}");
|
|
|
- if($owner){
|
|
|
- $collect->push($owner);
|
|
|
- continue;
|
|
|
- }
|
|
|
- $key = json_encode( ['owner_code' => $item['owner_code'], 'sku' => $item['sku']]);
|
|
|
- if(!empty($commodities_map[json_encode($key)])){
|
|
|
- $commodities_map[$key];
|
|
|
- continue;
|
|
|
- }
|
|
|
- $unexists[json_encode($key)] = true;
|
|
|
- $sku[] = $item['sku'];
|
|
|
- $owner_code[] = $item['owner_code'];
|
|
|
+ $commodity = $this->getCommodityByOwnerCodeAndSKU($item['owner_code'],$item['sku']);
|
|
|
+ $collect->push($commodity);
|
|
|
}
|
|
|
+ return $collect;
|
|
|
+ }
|
|
|
|
|
|
- if(count($unexists) == 0)return $collect;
|
|
|
- $BasSKUs = $oracleBasSkuService->get( ['SKU'=>$sku,'CustomerID'=>$owner_code]);
|
|
|
- $BasSKUs = $BasSKUs->filter(function($bas_sku)use($unexists,$commodities_map){
|
|
|
- $arr = [
|
|
|
- 'owner_code' => $bas_sku['customerid'],
|
|
|
- 'sku' => $bas_sku['sku']
|
|
|
- ];
|
|
|
- $key = json_encode($arr);
|
|
|
- if(!empty($commodities_map($key)))return false;
|
|
|
- return $unexists[json_encode($arr)] ?? false;
|
|
|
+ public function getCommodityByOwnerCodeAndSKU($ownerCode,$sku){
|
|
|
+ $commodity_key = "owner_code_{$ownerCode}_sku_{$sku}";
|
|
|
+ return Cache::remember($commodity_key,300,function()use($ownerCode,$sku){
|
|
|
+ $commodity = Commodity::query()->where('sku',$sku)->whereHas('owner',function($query)use($ownerCode){
|
|
|
+ $query->where('code',$ownerCode);
|
|
|
+ })->first();
|
|
|
+ if(isset($commodity))return $commodity;
|
|
|
+ $basSKu = app('OracleBasSkuService')->first(['sku'=>$sku,'customerid'=>$ownerCode]);
|
|
|
+ return Commodity::query()->create($this->getParamsByBasSku($basSKu));
|
|
|
});
|
|
|
- $inner_params = (function()use($BasSKUs,$owner_map){
|
|
|
- $map = [];
|
|
|
- $date = Carbon::now();
|
|
|
- $BasSKUs->each(function($basSku)use(&$map,$owner_map,$date){
|
|
|
- $map[] = [
|
|
|
- 'owner_id' => $owner_map[$basSku['customerid']] ?? '',
|
|
|
- 'sku' => $basSku->sku,
|
|
|
- 'name' =>$basSku->descr_c,
|
|
|
- 'length' =>$basSku->skulength,
|
|
|
- 'width' => $basSku->skuwidth,
|
|
|
- 'height' => $basSku->skuhigh,
|
|
|
- 'volumn' => $basSku->cube,
|
|
|
- 'created_at' => $date,
|
|
|
- 'updated_at' => $date,
|
|
|
- ];
|
|
|
- });
|
|
|
- return $map;
|
|
|
- })();
|
|
|
- if(count($inner_params)==0)return $collect;
|
|
|
- foreach (array_chunk($inner_params,4000) as $item) {
|
|
|
- try {
|
|
|
- $bool = Commodity::query()->insert($item);
|
|
|
- if($bool){
|
|
|
- app('LogService')->log(__METHOD__,__FUNCTION__,"批量添加 Commodity Success ".count($inner_params).' || '.json_encode($inner_params));
|
|
|
- $commodities = Commodity::query()->with('owner')->whereIn('owner_id',data_get($item,'*.owner_id'))->whereIn('sku',data_get($item,'*.sku'))->get();
|
|
|
- $this->pushToCache($commodities);
|
|
|
- $collect = $collect->concat($commodities);
|
|
|
- }
|
|
|
- else app('LogService')->log(__METHOD__,__FUNCTION__,"批量添加 Commodity FAILED ".' || '.json_encode($inner_params));
|
|
|
- } catch (\Exception $e) {
|
|
|
- app('LogService')->log(__METHOD__,__FUNCTION__,"批量添加 Commodity ERROR ".' || '.json_encode($inner_params).' || '.json_encode($e->getMessage()).' || '.json_encode($e->getTraceAsString()));
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getParamsByBasSku($basSku,$owner = null){
|
|
|
+ if(empty($owner)){
|
|
|
+ $owner = app('OwnerService')->getOwnerByCode($basSku['customerid']);
|
|
|
}
|
|
|
- return $collect;
|
|
|
+ return [
|
|
|
+ 'owner_id' => $owner['id'] ?? '',
|
|
|
+ 'sku' => $basSku['sku'],
|
|
|
+ 'name' =>$basSku['descr_c'],
|
|
|
+ 'length' =>$basSku['skulength'],
|
|
|
+ 'width' => $basSku['skuwidth'],
|
|
|
+ 'height' => $basSku['skuhigh'],
|
|
|
+ 'volumn' => $basSku['cube']
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
private function pushToCache($commodities)
|