|
|
@@ -8,7 +8,7 @@ use App\CommodityBarcode;
|
|
|
use App\OracleBasSKU;
|
|
|
use App\Owner;
|
|
|
use App\Services\common\BatchUpdateService;
|
|
|
-use App\Shop;
|
|
|
+use App\ValueStore;
|
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
@@ -16,94 +16,102 @@ use Illuminate\Support\Facades\Cache;
|
|
|
|
|
|
Class CommodityService
|
|
|
{
|
|
|
- /** @var CacheService $cacheService */
|
|
|
- private $cacheService;
|
|
|
- function __construct(){
|
|
|
- $this->cacheService=app('CacheService');
|
|
|
- }
|
|
|
-
|
|
|
- public function firstOrCreate($param,$column = null):Commodity{
|
|
|
- if ($column) return Commodity::query()->firstOrCreate($param,$column);
|
|
|
+ public function firstOrCreate($param, $column = null): Commodity
|
|
|
+ {
|
|
|
+ if ($column) return Commodity::query()->firstOrCreate($param, $column);
|
|
|
return Commodity::query()->firstOrCreate($param);
|
|
|
}
|
|
|
- public function updateOrCreate($param,$column = null){
|
|
|
- if ($column) return Commodity::query()->updateOrCreate($param,$column);
|
|
|
+
|
|
|
+ public function updateOrCreate($param, $column = null)
|
|
|
+ {
|
|
|
+ if ($column) return Commodity::query()->updateOrCreate($param, $column);
|
|
|
return Commodity::query()->updateOrCreate($param);
|
|
|
}
|
|
|
|
|
|
- public function first(array $params, $with = null){
|
|
|
+ public function first(array $params, $with = null)
|
|
|
+ {
|
|
|
$commodity = Commodity::query();
|
|
|
- if ($with)$commodity->with($with);
|
|
|
- foreach ($params as $column => $value){
|
|
|
- if (!is_array($value))$commodity->where($column,$value);
|
|
|
- else $commodity->whereIn($column,$value);
|
|
|
+ if ($with) $commodity->with($with);
|
|
|
+ foreach ($params as $column => $value) {
|
|
|
+ if (!is_array($value)) $commodity->where($column, $value);
|
|
|
+ else $commodity->whereIn($column, $value);
|
|
|
}
|
|
|
return $commodity->first();
|
|
|
}
|
|
|
|
|
|
- public function get(array $params){
|
|
|
+ public function get(array $params)
|
|
|
+ {
|
|
|
$query = Commodity::query()->with('barcodes');
|
|
|
- if ($params["owner_id"] ?? false){
|
|
|
+ if ($params["owner_id"] ?? false) {
|
|
|
$query->where("owner_id", $params["owner_id"]);
|
|
|
}
|
|
|
- if ($params["sku"] ?? false){
|
|
|
- if (!is_array($params["sku"]))$params["sku"] = [$params["sku"]];
|
|
|
+ if ($params["sku"] ?? false) {
|
|
|
+ if (!is_array($params["sku"])) $params["sku"] = [$params["sku"]];
|
|
|
$query->whereIn('sku', $params["sku"]);
|
|
|
}
|
|
|
return $query->get();
|
|
|
}
|
|
|
|
|
|
- public function insert(array $params){
|
|
|
+ public function insert(array $params)
|
|
|
+ {
|
|
|
return Commodity::query()->insert($params);
|
|
|
}
|
|
|
|
|
|
|
|
|
- public function getOwnerCommodities(array $params){
|
|
|
+ public function getOwnerCommodities(array $params)
|
|
|
+ {
|
|
|
$query = Commodity::query();
|
|
|
- foreach ($params as $column => $value){
|
|
|
- if (!is_array($value)) $query->where($column,$value);
|
|
|
- else $query->whereIn($column,$value);
|
|
|
+ foreach ($params as $column => $value) {
|
|
|
+ if (!is_array($value)) $query->where($column, $value);
|
|
|
+ else $query->whereIn($column, $value);
|
|
|
}
|
|
|
return $query->get();
|
|
|
}
|
|
|
|
|
|
/* 批量更新 */
|
|
|
- public function batchUpdate(array $params){
|
|
|
+ public function batchUpdate(array $params)
|
|
|
+ {
|
|
|
return app(BatchUpdateService::class)->batchUpdate('commodities', $params);
|
|
|
}
|
|
|
|
|
|
/* 根据货主条形码查找商品 */
|
|
|
- private function ownerBarcodeSeekCommodityQuery(Builder $query, array $ownerParam, $barcode){
|
|
|
- $query->whereHas('owner',function ($builder)use($ownerParam){
|
|
|
- foreach ($ownerParam as $column => $param){
|
|
|
+ private function ownerBarcodeSeekCommodityQuery(Builder $query, array $ownerParam, $barcode)
|
|
|
+ {
|
|
|
+ $query->whereHas('owner', function ($builder) use ($ownerParam) {
|
|
|
+ foreach ($ownerParam as $column => $param) {
|
|
|
$builder->where($column, $param);
|
|
|
}
|
|
|
});
|
|
|
- $query->whereHas('barcodes',function ($builder)use($barcode){
|
|
|
- if (is_array($barcode))$builder->whereIn('code',$barcode);
|
|
|
- else $builder->where('code',$barcode);
|
|
|
+ $query->whereHas('barcodes', function ($builder) use ($barcode) {
|
|
|
+ if (is_array($barcode)) $builder->whereIn('code', $barcode);
|
|
|
+ else $builder->where('code', $barcode);
|
|
|
});
|
|
|
return $query;
|
|
|
}
|
|
|
- public function ownerBarcodeSeekCommodityFirst(array $ownerParam, $barcode, $with = null){
|
|
|
+
|
|
|
+ public function ownerBarcodeSeekCommodityFirst(array $ownerParam, $barcode, $with = null)
|
|
|
+ {
|
|
|
$commodity = Commodity::query();
|
|
|
if ($with) $commodity->with($with);
|
|
|
$commodity = $this->ownerBarcodeSeekCommodityQuery($commodity, $ownerParam, $barcode);
|
|
|
return $commodity->first();
|
|
|
}
|
|
|
- public function ownerBarcodeSeekCommodityGet(array $ownerParam, $barcode, $isNullSku = false){
|
|
|
+
|
|
|
+ public function ownerBarcodeSeekCommodityGet(array $ownerParam, $barcode, $isNullSku = false)
|
|
|
+ {
|
|
|
$commodities = Commodity::query()->with('barcodes');
|
|
|
- if ($isNullSku)$commodities->whereNull('sku');
|
|
|
+ if ($isNullSku) $commodities->whereNull('sku');
|
|
|
$commodities = $this->ownerBarcodeSeekCommodityQuery($commodities, $ownerParam, $barcode);
|
|
|
return $commodities->get();
|
|
|
}
|
|
|
|
|
|
/* 通过货主代码与条形码寻找FLUX商品补充至WMS 单条*/
|
|
|
- public function ownerAndBarcodeFirstOrCreate(Owner $owner,$barcode){
|
|
|
- $wmsCommodity = app('OracleBasSkuService')->first(['customerid'=>$owner->code, 'barcode'=>$barcode]);
|
|
|
+ public function ownerAndBarcodeFirstOrCreate(Owner $owner, $barcode)
|
|
|
+ {
|
|
|
+ $wmsCommodity = app('OracleBasSkuService')->first(['customerid' => $owner->code, 'barcode' => $barcode]);
|
|
|
if (!$wmsCommodity) return null;
|
|
|
|
|
|
- $commodity = $this->firstOrCreate(['owner_id'=>$owner->id, 'sku'=>$wmsCommodity->sku],[
|
|
|
+ $commodity = $this->firstOrCreate(['owner_id' => $owner->id, 'sku' => $wmsCommodity->sku], [
|
|
|
"name" => $wmsCommodity->descr_c,
|
|
|
"sku" => $wmsCommodity->sku,
|
|
|
"owner_id" => $owner->id,
|
|
|
@@ -112,41 +120,43 @@ Class CommodityService
|
|
|
"height" => $wmsCommodity->skuhigh,
|
|
|
"volumn" => $wmsCommodity->cube,
|
|
|
]);
|
|
|
- if ($wmsCommodity->alternate_sku1)app('CommodityBarcodeService')->first([
|
|
|
+ if ($wmsCommodity->alternate_sku1) app('CommodityBarcodeService')->first([
|
|
|
'commodity_id' => $commodity->id,
|
|
|
'code' => $wmsCommodity->alternate_sku1,
|
|
|
]);
|
|
|
- if ($wmsCommodity->alternate_sku2)app('CommodityBarcodeService')->first([
|
|
|
+ if ($wmsCommodity->alternate_sku2) app('CommodityBarcodeService')->first([
|
|
|
'commodity_id' => $commodity->id,
|
|
|
'code' => $wmsCommodity->alternate_sku2,
|
|
|
]);
|
|
|
return $commodity;
|
|
|
}
|
|
|
|
|
|
- public function create(array $params){
|
|
|
+ public function create(array $params)
|
|
|
+ {
|
|
|
return Commodity::query()->create($params);
|
|
|
}
|
|
|
|
|
|
- public function getByWmsOrders($orderHeaders){
|
|
|
- if(!$orderHeaders) return null;
|
|
|
+ public function getByWmsOrders($orderHeaders)
|
|
|
+ {
|
|
|
+ if (!$orderHeaders) return null;
|
|
|
$customerId_sku_map = [];
|
|
|
foreach ($orderHeaders as $orderHeader) {
|
|
|
$oracleDOCOrderDetails = $orderHeader->oracleDOCOrderDetails;
|
|
|
foreach ($oracleDOCOrderDetails as $oracleDOCOrderDetail) {
|
|
|
$value = [
|
|
|
'owner_code' => $oracleDOCOrderDetail->customerid,
|
|
|
- 'sku'=> $oracleDOCOrderDetail->sku
|
|
|
+ 'sku' => $oracleDOCOrderDetail->sku
|
|
|
];
|
|
|
- if(!in_array($value,$customerId_sku_map)){
|
|
|
+ if (!in_array($value, $customerId_sku_map)) {
|
|
|
$customerId_sku_map[] = $value;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- $owner_codes = array_diff(array_unique(data_get($customerId_sku_map,'*.owner_code')),['','*',null]) ;
|
|
|
- if(!$owner_codes) return null;
|
|
|
+ $owner_codes = array_diff(array_unique(data_get($customerId_sku_map, '*.owner_code')), ['', '*', null]);
|
|
|
+ if (!$owner_codes) return null;
|
|
|
|
|
|
- $owners = Owner::query()->whereIn('code',$owner_codes)->get();
|
|
|
+ $owners = Owner::query()->whereIn('code', $owner_codes)->get();
|
|
|
|
|
|
$owners_code_map = [];
|
|
|
$owners_id_map = [];
|
|
|
@@ -154,70 +164,70 @@ Class CommodityService
|
|
|
$owners_code_map[$owner->code] = $owner;
|
|
|
$owners_id_map[$owner->id] = $owner;
|
|
|
}
|
|
|
- $orderHeader_sku = array_diff(array_unique(data_get($customerId_sku_map,'*.sku')),['','*',null]) ;
|
|
|
+ $orderHeader_sku = array_diff(array_unique(data_get($customerId_sku_map, '*.sku')), ['', '*', null]);
|
|
|
$commodities = Commodity::query()
|
|
|
- ->whereIn('owner_id',data_get($owners,'*.id'))
|
|
|
- ->whereIn('sku',$orderHeader_sku)
|
|
|
- ->groupBy('owner_id','sku') //*!!!!!!!!
|
|
|
+ ->whereIn('owner_id', data_get($owners, '*.id'))
|
|
|
+ ->whereIn('sku', $orderHeader_sku)
|
|
|
+ ->groupBy('owner_id', 'sku') //*!!!!!!!!
|
|
|
->get();
|
|
|
|
|
|
- if($commodities->count() < count($customerId_sku_map)){
|
|
|
+ if ($commodities->count() < count($customerId_sku_map)) {
|
|
|
$commoditiesInWAS索引_sku = [];
|
|
|
foreach ($commodities as $commodityInWms) {
|
|
|
$owner = $owners_id_map[$commodityInWms->owner_id] ?? '';
|
|
|
- if(!$owner)continue;
|
|
|
- $key ='owner_cod='.$owner['code'].' sku='.$commodityInWms->sku;
|
|
|
- $commoditiesInWAS索引_sku[$key]=$commodityInWms;
|
|
|
+ if (!$owner) continue;
|
|
|
+ $key = 'owner_cod=' . $owner['code'] . ' sku=' . $commodityInWms->sku;
|
|
|
+ $commoditiesInWAS索引_sku[$key] = $commodityInWms;
|
|
|
}
|
|
|
- $commodities需要新增=[];
|
|
|
+ $commodities需要新增 = [];
|
|
|
foreach ($customerId_sku_map as $commodityInWms) {
|
|
|
- $key ='owner_cod='.$commodityInWms['owner_code'].' sku='.$commodityInWms['sku'];
|
|
|
- if($commoditiesInWAS索引_sku[$key] ?? false) continue;
|
|
|
+ $key = 'owner_cod=' . $commodityInWms['owner_code'] . ' sku=' . $commodityInWms['sku'];
|
|
|
+ if ($commoditiesInWAS索引_sku[$key] ?? false) continue;
|
|
|
$commodities需要新增[$key] = $commodityInWms;
|
|
|
}
|
|
|
- $commodity_set = $this->createCommodities($commodities需要新增,$owners_code_map);
|
|
|
+ $commodity_set = $this->createCommodities($commodities需要新增, $owners_code_map);
|
|
|
$commodities = $commodities->concat($commodity_set);
|
|
|
}
|
|
|
- return $commodities;
|
|
|
+ return $commodities;
|
|
|
}
|
|
|
|
|
|
- public function createCommodities($params,$owners_code_map)
|
|
|
+ public function createCommodities($params, $owners_code_map)
|
|
|
{
|
|
|
- if(!$params) return [];
|
|
|
+ if (!$params) return [];
|
|
|
$bas_sku_arr = OracleBasSKU::query()
|
|
|
->selectRaw('customerid,sku,descr_c,skulength,skuwidth,skuhigh,cube')
|
|
|
- ->whereIn('CustomerID',data_get($params,'*.owner_code'))
|
|
|
- ->whereIn('Sku',data_get($params,'*.sku'))
|
|
|
+ ->whereIn('CustomerID', data_get($params, '*.owner_code'))
|
|
|
+ ->whereIn('Sku', data_get($params, '*.sku'))
|
|
|
->get();
|
|
|
$insert_params = [];
|
|
|
$created_at = Carbon::now()->format('Y-m-d H:i:s');
|
|
|
foreach ($bas_sku_arr as $bas_sku) {
|
|
|
$owner = $owners_code_map[$bas_sku->customerid] ?? '';
|
|
|
- if(!$owner)continue;
|
|
|
- if($bas_sku->sku==null)continue;
|
|
|
- if($bas_sku->descr_c=='')continue;
|
|
|
+ if (!$owner) continue;
|
|
|
+ if ($bas_sku->sku == null) continue;
|
|
|
+ if ($bas_sku->descr_c == '') continue;
|
|
|
$insert_params[] = [
|
|
|
'owner_id' => $owner->id,
|
|
|
'sku' => $bas_sku->sku,
|
|
|
- 'name' =>$bas_sku->descr_c,
|
|
|
+ 'name' => $bas_sku->descr_c,
|
|
|
'created_at' => $created_at,
|
|
|
- 'length' =>$bas_sku->skulength,
|
|
|
+ 'length' => $bas_sku->skulength,
|
|
|
'width' => $bas_sku->skuwidth,
|
|
|
'height' => $bas_sku->skuhigh,
|
|
|
'volumn' => $bas_sku->cube
|
|
|
];
|
|
|
}
|
|
|
- if(count($insert_params) > 0){
|
|
|
+ if (count($insert_params) > 0) {
|
|
|
try {
|
|
|
$this->insert($insert_params);
|
|
|
- app('LogService')->log(__METHOD__,__FUNCTION__,'批量添加 commodity ' . count($insert_params) .json_encode($insert_params) );
|
|
|
+ app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 commodity ' . count($insert_params) . json_encode($insert_params));
|
|
|
} catch (\Exception $e) {
|
|
|
- app('LogService')->log(__METHOD__,__FUNCTION__,'批量添加 commodity error' .json_encode($insert_params) ."||".$e->getMessage().'||'.$e->getTraceAsString());
|
|
|
+ app('LogService')->log(__METHOD__, __FUNCTION__, '批量添加 commodity error' . json_encode($insert_params) . "||" . $e->getMessage() . '||' . $e->getTraceAsString());
|
|
|
}
|
|
|
}
|
|
|
return Commodity::query()
|
|
|
- ->whereIn('owner_id',data_get($owners_code_map,'*.id'))
|
|
|
- ->whereIn('sku',data_get($params,'*.sku'))
|
|
|
+ ->whereIn('owner_id', data_get($owners_code_map, '*.id'))
|
|
|
+ ->whereIn('sku', data_get($params, '*.sku'))
|
|
|
->get();
|
|
|
}
|
|
|
|
|
|
@@ -228,93 +238,94 @@ Class CommodityService
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- public function syncBarcodes($barcodesStr,$ownerId,$sku):Commodity
|
|
|
+ public function syncBarcodes($barcodesStr, $ownerId, $sku): Commodity
|
|
|
{
|
|
|
- $barcodes=(function()use($barcodesStr){
|
|
|
- $barcodes=rtrim($barcodesStr,',');
|
|
|
- $barcodes=explode(',',$barcodes);
|
|
|
- foreach ($barcodes as $k=>$barcode){
|
|
|
- if(!trim($barcode)) unset($barcodes[$k]);
|
|
|
+ $barcodes = (function () use ($barcodesStr) {
|
|
|
+ $barcodes = rtrim($barcodesStr, ',');
|
|
|
+ $barcodes = explode(',', $barcodes);
|
|
|
+ foreach ($barcodes as $k => $barcode) {
|
|
|
+ if (!trim($barcode)) unset($barcodes[$k]);
|
|
|
}
|
|
|
return $barcodes;
|
|
|
})();
|
|
|
- $commodity=$this->firstOrCreate(['owner_id'=>$ownerId,'sku'=>$sku]);
|
|
|
- $commodityBarcodes=$commodity['barcodes']??new Collection();
|
|
|
+ $commodity = $this->firstOrCreate(['owner_id' => $ownerId, 'sku' => $sku]);
|
|
|
+ $commodityBarcodes = $commodity['barcodes'] ?? new Collection();
|
|
|
|
|
|
|
|
|
/** @var CommodityBarcodeService $commodityBarcodeService */
|
|
|
- $commodityBarcodeService=app('CommodityBarcodeService');
|
|
|
- $redundantCommodityBarcodes=new Collection();
|
|
|
- foreach($commodityBarcodes as $commodityBarcode){//清除数组中 已经在商品有的条码,清除商品条码中,不在数组中的条码
|
|
|
- $hasMatch=false;
|
|
|
- foreach($barcodes as $key=>$barcode){
|
|
|
- if($barcodes[$key]==$commodityBarcode['code']){
|
|
|
- $hasMatch=true;
|
|
|
+ $commodityBarcodeService = app('CommodityBarcodeService');
|
|
|
+ $redundantCommodityBarcodes = new Collection();
|
|
|
+ foreach ($commodityBarcodes as $commodityBarcode) {//清除数组中 已经在商品有的条码,清除商品条码中,不在数组中的条码
|
|
|
+ $hasMatch = false;
|
|
|
+ foreach ($barcodes as $key => $barcode) {
|
|
|
+ if ($barcodes[$key] == $commodityBarcode['code']) {
|
|
|
+ $hasMatch = true;
|
|
|
unset($barcodes[$key]);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
- if(!$hasMatch){
|
|
|
+ if (!$hasMatch) {
|
|
|
$redundantCommodityBarcodes->push($commodityBarcode);
|
|
|
}
|
|
|
}
|
|
|
- if(!empty($redundantCommodityBarcodes)){
|
|
|
+ if (!empty($redundantCommodityBarcodes)) {
|
|
|
$commodityBarcodeService->destroyCollections($redundantCommodityBarcodes);
|
|
|
}
|
|
|
- if(!empty($barcodes)){
|
|
|
- $commodityBarcodeService->insertMany_onCommodities([['commodity_id'=>$commodity['id'],'barcodes'=>$barcodes]]);
|
|
|
+ if (!empty($barcodes)) {
|
|
|
+ $commodityBarcodeService->insertMany_onCommodities([['commodity_id' => $commodity['id'], 'barcodes' => $barcodes]]);
|
|
|
}
|
|
|
return $commodity;
|
|
|
}
|
|
|
+
|
|
|
public function destroyWithOffspring(Commodity $commodity)
|
|
|
{
|
|
|
- $barcodesIds=$commodity->barcodes->map(function ($barcode){
|
|
|
+ $barcodesIds = $commodity->barcodes->map(function ($barcode) {
|
|
|
return $barcode['id'];
|
|
|
});
|
|
|
CommodityBarcode::destroy($barcodesIds);
|
|
|
$commodity->delete();
|
|
|
}
|
|
|
|
|
|
- public function syncWMSOrderCode($owner,$skus)
|
|
|
+ public function syncWMSOrderCode($owner, $skus)
|
|
|
{
|
|
|
- $basSku = OracleBasSKU::query()->whereIn('SKU',$skus)->where('CustomerID',$owner->code)->get();
|
|
|
+ $basSku = OracleBasSKU::query()->whereIn('SKU', $skus)->where('CustomerID', $owner->code)->get();
|
|
|
$inner_params = [];
|
|
|
$created_at = new Carbon();
|
|
|
- $basSku->each(function($bas_sku)use(&$inner_params,$owner,$created_at){
|
|
|
+ $basSku->each(function ($bas_sku) use (&$inner_params, $owner, $created_at) {
|
|
|
$inner_params = [
|
|
|
'owner_id' => $owner->id,
|
|
|
'sku' => $bas_sku->sku,
|
|
|
- 'name' =>$bas_sku->descr_c,
|
|
|
+ 'name' => $bas_sku->descr_c,
|
|
|
'created_at' => $created_at,
|
|
|
- 'length' =>$bas_sku->skulength,
|
|
|
+ 'length' => $bas_sku->skulength,
|
|
|
'width' => $bas_sku->skuwidth,
|
|
|
'height' => $bas_sku->skuhigh,
|
|
|
'volumn' => $bas_sku->cube
|
|
|
];
|
|
|
});
|
|
|
- if(count($inner_params) > 0){
|
|
|
+ if (count($inner_params) > 0) {
|
|
|
try {
|
|
|
$this->insert($inner_params);
|
|
|
app('LogService')->log(__METHOD__, __FUNCTION__, json_encode($inner_params));
|
|
|
} catch (\Exception $e) {
|
|
|
- app('LogService')->log(__METHOD__, 'Error '.__FUNCTION__, json_encode($inner_params).' || '.$e->getMessage());
|
|
|
+ app('LogService')->log(__METHOD__, 'Error ' . __FUNCTION__, json_encode($inner_params) . ' || ' . $e->getMessage());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
//获取箱规
|
|
|
public function getPack($owner_id, $sku)
|
|
|
{
|
|
|
$that = $this;
|
|
|
- return app("CacheService")->getOrExecute("pack_{$owner_id}_{$sku}",function ()use($owner_id,$sku,$that){
|
|
|
+ return app("CacheService")->getOrExecute("pack_{$owner_id}_{$sku}", function () use ($owner_id, $sku, $that) {
|
|
|
$commodity = $that->first([
|
|
|
"owner_id" => $owner_id,
|
|
|
"sku" => $sku
|
|
|
]);
|
|
|
- if (!$commodity || $commodity->pack_spec === null){
|
|
|
+ if (!$commodity || $commodity->pack_spec === null) {
|
|
|
$owner = app("OwnerService")->find($owner_id);
|
|
|
$action = new CommodityController();
|
|
|
- $action->syncOwnerCommodities($owner->id,$owner->code,$sku);
|
|
|
+ $action->syncOwnerCommodities($owner->id, $owner->code, $sku);
|
|
|
}
|
|
|
return $that->first([
|
|
|
"owner_id" => $owner_id,
|
|
|
@@ -328,102 +339,40 @@ Class CommodityService
|
|
|
{
|
|
|
$query = Commodity::query();
|
|
|
if ($params["barcode"] ?? false) {
|
|
|
- $query->whereHas("barcodes",function ($query)use($params){
|
|
|
+ $query->whereHas("barcodes", function ($query) use ($params) {
|
|
|
/** @var Builder $query */
|
|
|
- $query->where("code",$params["barcode"]);
|
|
|
+ $query->where("code", $params["barcode"]);
|
|
|
});
|
|
|
unset($params["barcode"]);
|
|
|
}
|
|
|
- foreach ($params as $column => $param){
|
|
|
- $query->where($column,$param);
|
|
|
+ foreach ($params as $column => $param) {
|
|
|
+ $query->where($column, $param);
|
|
|
}
|
|
|
- if ($query->count() > 0)return true;
|
|
|
+ if ($query->count() > 0) return true;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- public function getCommoditiesByMap($map)
|
|
|
+ public function pushCommodityToCache()
|
|
|
{
|
|
|
- /** @var OwnerService $ownerService */
|
|
|
- $ownerService = app('OwnerService');
|
|
|
-
|
|
|
- $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 = [];
|
|
|
- foreach ($map as $item) {
|
|
|
- $owner = Cache::get("owner_code_{$item['owner_code']}_sku_{$item['sku']}");
|
|
|
- if($owner){
|
|
|
- $collect->push($owner);
|
|
|
- continue;
|
|
|
- }
|
|
|
- $items = [
|
|
|
- 'owner_code' => $item['owner_code'],
|
|
|
- 'sku' => $item['sku']
|
|
|
- ];
|
|
|
- $unexists[json_encode($items)] = true;
|
|
|
- $sku[] = $item['sku'];
|
|
|
- $owner_code[] = $item['owner_code'];
|
|
|
+ $amount = 1000;
|
|
|
+ $commodity = Commodity::query()->orderByDesc('id')->first();
|
|
|
+ $sum=$commodity->id;
|
|
|
+ $number = ceil($sum / $amount);
|
|
|
+ for ($i = 0; $i < $number; $i++) {
|
|
|
+ if ($i<41) continue;
|
|
|
+ $commodities = $this->getPiece(($i * $amount), $amount);
|
|
|
+ if (count($commodities)<1) continue;
|
|
|
+ $this->pushToCache($commodities);
|
|
|
}
|
|
|
- if(count($unexists) == 0)return $collect;
|
|
|
- $BasSKUs = OracleBasSKU::query()->whereIn('SKU',$sku)->whereIn('CustomerID',$owner_code)->get();
|
|
|
- $BasSKUs = $BasSKUs->filter(function($bas_sku)use($unexists){
|
|
|
- $arr = [
|
|
|
- 'owner_code' => $bas_sku['customerid'],
|
|
|
- 'sku' => $bas_sku['sku']
|
|
|
- ];
|
|
|
- return $unexists[json_encode($arr)] ?? false;
|
|
|
- });
|
|
|
- $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()));
|
|
|
- }
|
|
|
- }
|
|
|
- return $collect;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getPiece(int $start, int $amount)
|
|
|
+ {
|
|
|
+ $commodities = Commodity::query()->with(['owner', 'barcodes'])
|
|
|
+ ->where('id', '>=', $start)
|
|
|
+ ->where('id', '<', ($start+$amount))
|
|
|
+ ->get();
|
|
|
+ return $commodities;
|
|
|
}
|
|
|
|
|
|
private function pushToCache($commodities)
|
|
|
@@ -436,4 +385,140 @@ Class CommodityService
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public function syncCommodityCreated()
|
|
|
+ {
|
|
|
+ $created_at = config('sync.commodity_sync.created_at');
|
|
|
+ $create_set = config('sync.commodity_sync.cache_prefix.create_set');
|
|
|
+ $create_keys = config('sync.commodity_sync.cache_prefix.create_keys');
|
|
|
+ $create_key = config('sync.commodity_sync.cache_prefix.create');
|
|
|
+ /** @var OracleBasSkuService $oracleBasSkuService */
|
|
|
+ $oracleBasSkuService = app(OracleBasSkuService::class);
|
|
|
+ $last_time = $this->getAsnLastSyncAt($created_at, 'create');
|
|
|
+ $basSkus = $oracleBasSkuService->getWmsCreatedCommodities($last_time);
|
|
|
+ $last_time=$basSkus->first()['addtime'];
|
|
|
+ $last_records = $basSkus->where('addtime', $last_time);
|
|
|
+ if (!$basSkus) return;
|
|
|
+ $addBasSkus = $this->getLastRecordsByRedis($create_set, $create_key, $basSkus);
|
|
|
+ //if (count($addBasSkus) > 0) $addBasSkus=$this->filterByCommodityCache($addBasSkus);
|
|
|
+ if (count($addBasSkus) > 0) {
|
|
|
+ $this->syncCreateCommodity($addBasSkus);
|
|
|
+ $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()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ // TODO
|
|
|
+ public function syncCreateCommodity($addBasSkus)
|
|
|
+ {
|
|
|
+ $owner_codes = array_diff(array_unique(data_get($addBasSkus, '*.customerid')), ['', '*', null]);
|
|
|
+ if (count($owner_codes)<1) return null;
|
|
|
+ $owners = Owner::query()->whereIn('code', $owner_codes)->get();
|
|
|
+ $owners_code_map = [];
|
|
|
+ foreach ($owners as $owner) {
|
|
|
+ $owners_code_map[$owner->code] = $owner;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // TODO
|
|
|
+ public function getParamsByBasSku($addBasSkus, $owners_code_map)
|
|
|
+ {
|
|
|
+ $params = [];
|
|
|
+ $commodities = Commodity::query()->with(['owner' => function ($query) use ($addBasSkus) {
|
|
|
+ return $query->whereIn('code', data_get($addBasSkus, '*.customerid'));
|
|
|
+ }])
|
|
|
+ ->whereIn('sku', data_get($addBasSkus, '*.sku'))
|
|
|
+ ->get();
|
|
|
+ $commodity_map = [];
|
|
|
+ foreach ($commodities as $commodity) {
|
|
|
+ $commodity_map[$commodity['owner']['code'].$commodity['sku']] = $commodity;
|
|
|
+ }
|
|
|
+ foreach ($addBasSkus as $addBasSku) {
|
|
|
+ if ($store_asn_code_map[$addBasSku->customerid.$addBasSku->sku] ?? false) continue;
|
|
|
+ $owner = $owners_code_map[$addBasSku->customerid] ?? null;
|
|
|
+
|
|
|
+ $params[] = [
|
|
|
+
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ return $params;
|
|
|
+ }
|
|
|
+ public function getAsnLastSyncAt($key, $type)
|
|
|
+ {
|
|
|
+ $last_time = ValueStore::query()->where('name', $key)->value('value');
|
|
|
+ if ($last_time) return $last_time;
|
|
|
+ if ($type == 'create') {
|
|
|
+ $store = Commodity::query()->orderByDesc('created_at')->first();
|
|
|
+ if ($store) return $store->created_at;
|
|
|
+ } else {
|
|
|
+ $store = Commodity::query()->orderByDesc('updated_at')->first();
|
|
|
+ if ($store) return $store->updated_at;
|
|
|
+ }
|
|
|
+ return Carbon::now()->subSeconds(65);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setAsnLastSyncAt($key, $last_time)
|
|
|
+ {
|
|
|
+ $asnLastSyncAt = ValueStore::query()->updateOrCreate([
|
|
|
+ 'name' => $key,
|
|
|
+ ], [
|
|
|
+ 'name' => $key,
|
|
|
+ 'value' => $last_time,
|
|
|
+ ]);
|
|
|
+ LogService::log(__METHOD__, __FUNCTION__, '修改或更新' . $key . json_encode($asnLastSyncAt));
|
|
|
+ return $asnLastSyncAt;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getLastRecordsByRedis($set, $prefixKey, $basSkus)
|
|
|
+ {
|
|
|
+ if (Cache::get($set)) {
|
|
|
+ $addBasSkus = collect();
|
|
|
+ foreach ($basSkus as $basSku) {
|
|
|
+ if (Cache::get($prefixKey . $basSkus->customerid . '_' . $basSkus->sku)) continue;
|
|
|
+ $addBasSkus->add($basSku);
|
|
|
+ }
|
|
|
+ return $addBasSkus;
|
|
|
+ }
|
|
|
+ return $basSkus;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function deleteCacheKey($set, $keys)
|
|
|
+ {
|
|
|
+ if (Cache::get($set)) {
|
|
|
+ $cacheKeys = Cache::get($keys);
|
|
|
+ if (!$cacheKeys) return;
|
|
|
+ foreach ($cacheKeys as $cacheKey) {
|
|
|
+ Cache::forget($cacheKey);
|
|
|
+ }
|
|
|
+ Cache::forget($keys);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setLastRecordsByRedis($prefixKey, $set, $keys, $last_records)
|
|
|
+ {
|
|
|
+ if (Cache::get($set)) {
|
|
|
+ $cacheKeys = [];
|
|
|
+ foreach ($last_records as $last_record) {
|
|
|
+ Cache::put($prefixKey . $last_record->customerid . '_' . $last_record->sku, true);
|
|
|
+ array_push($cacheKeys, $prefixKey . $last_record->customerid . '_' . $last_record->sku);
|
|
|
+ }
|
|
|
+ Cache::put($keys, $cacheKeys);
|
|
|
+ Cache::put($set, true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public function filterByCommodityCache($basSkus)
|
|
|
+ {
|
|
|
+ if (count($basSkus)<1)return null;
|
|
|
+ $addBasSkus=collect();
|
|
|
+ foreach ($basSkus as $basSku){
|
|
|
+ if (Cache::get("owner_code_{$basSku['customerid']}_sku_{$basSku['sku']}")) continue;
|
|
|
+ $addBasSkus->add($basSku);
|
|
|
+ }
|
|
|
+ return $addBasSkus;
|
|
|
+ }
|
|
|
+
|
|
|
}
|