Commit 2317f863 authored by hangjun83's avatar hangjun83

乐妍

parent 3cbc7c48
...@@ -28,52 +28,52 @@ class LeyanService ...@@ -28,52 +28,52 @@ class LeyanService
public function updateProductsByEveryDay() public function updateProductsByEveryDay()
{ {
$currentPage = 1; $currentPage = 1;
while(true){ while (true) {
$result = $this->apiService->pushBatchUpdateProductByPage($currentPage,1000,$this->getToken()); $result = $this->apiService->pushBatchUpdateProductByPage($currentPage, 1000, $this->getToken());
if(!$result || !$result['productInfo']){ if (!$result || !$result['productInfo']) {
return null; return null;
} }
try{ try {
$this->storeLeyanProducts($result['productInfo']); $this->storeLeyanProducts($result['productInfo']);
if($result['allPage'] == $currentPage){ if ($result['allPage'] == $currentPage) {
break; break;
} }
$currentPage ++; $currentPage++;
}catch(\Exception $exception){ } catch (\Exception $exception) {
SimpleLogs::writeLog($exception->getMessage(),__CLASS__.':updateProductsByEveryDay', 'error'); SimpleLogs::writeLog($exception->getMessage(), __CLASS__ . ':updateProductsByEveryDay', 'error');
$currentPage ++; $currentPage++;
} }
} }
} }
public function batchUpdateProducts($params = null) public function batchUpdateProducts($params = null)
{ {
ini_set('memory_limit','3072M'); ini_set('memory_limit', '3072M');
$casList = app(BhuaProductService::class)->getProductCasByBidToGroupByCas($params); $casList = app(BhuaProductService::class)->getProductCasByBidToGroupByCas($params);
if($casList){ if ($casList) {
foreach($casList as $cas){ foreach ($casList as $cas) {
if(empty($cas['c_cas'])) continue; if (empty($cas['c_cas'])) continue;
$this->apiService->pushQueue([ $this->apiService->pushQueue([
'params' => ['cas' => $cas['c_cas']], 'params' => ['cas' => $cas['c_cas']],
'consumer' => __CLASS__, 'consumer' => __CLASS__,
'method' => 'updateProductByCas' 'method' => 'updateProductByCas'
],'leyan'); ], 'leyan');
} }
} }
} }
public function updateProductByCas($params) public function updateProductByCas($params)
{ {
if(!$params['cas']) return null; if (!$params['cas']) return null;
$result = $this->apiService->pushUpdateProductByCas($params['cas'],$this->getToken()); $result = $this->apiService->pushUpdateProductByCas($params['cas'], $this->getToken());
if(!$result){ if (!$result) {
return null; return null;
} }
try{ try {
$result = $this->storeLeyanProducts($result); $result = $this->storeLeyanProducts($result);
}catch(\Exception $exception){ } catch (\Exception $exception) {
SimpleLogs::writeLog($exception->getMessage(),__CLASS__.':updateProductByCas', 'error'); SimpleLogs::writeLog($exception->getMessage(), __CLASS__ . ':updateProductByCas', 'error');
} }
} }
...@@ -81,34 +81,35 @@ class LeyanService ...@@ -81,34 +81,35 @@ class LeyanService
* @ 获取api token * @ 获取api token
* @return false|mixed * @return false|mixed
*/ */
public function getToken(){ public function getToken()
try{ {
$token = $this->apiService->getPlatformDataEntries('leyan_api_token','data_values'); try {
if(empty($token)){ $token = $this->apiService->getPlatformDataEntries('leyan_api_token', 'data_values');
if (empty($token)) {
$apiToken = $this->apiService->getToken(); $apiToken = $this->apiService->getToken();
if($apiToken){ if ($apiToken) {
$storeToken = []; $storeToken = [];
$storeToken['token'] = $apiToken; $storeToken['token'] = $apiToken;
$storeToken['currentTime'] = time(); $storeToken['currentTime'] = time();
$this->apiService->storePlatformDataEntries('leyan_api_token',$storeToken); $this->apiService->storePlatformDataEntries('leyan_api_token', $storeToken);
return $storeToken['token']; return $storeToken['token'];
} }
}else{ } else {
if((time() - $token['currentTime']) >= 86400){ if ((time() - $token['currentTime']) >= 86400) {
$apiToken = $this->apiService->getToken(); $apiToken = $this->apiService->getToken();
if($apiToken){ if ($apiToken) {
$storeToken = []; $storeToken = [];
$storeToken['token'] = $apiToken; $storeToken['token'] = $apiToken;
$storeToken['currentTime'] = time(); $storeToken['currentTime'] = time();
$this->apiService->storePlatformDataEntries('leyan_api_token',$storeToken); $this->apiService->storePlatformDataEntries('leyan_api_token', $storeToken);
return $storeToken['token']; return $storeToken['token'];
} }
}else{ } else {
return $token['token']; return $token['token'];
} }
} }
}catch(\Exception $exception){ } catch (\Exception $exception) {
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':token error', 'error'); SimpleLogs::writeLog($exception->getMessage(), __CLASS__ . ':token error', 'error');
return false; return false;
} }
} }
...@@ -124,22 +125,24 @@ class LeyanService ...@@ -124,22 +125,24 @@ class LeyanService
} }
$bId = 108; $bId = 108;
try {
$dbConnect = DB::connection('bh_mysql'); $dbConnect = DB::connection('bh_mysql');
$dbConnect->beginTransaction();
$errorProduct = []; $errorProduct = [];
foreach ($products as $product) { foreach ($products as $product) {
try {
$id = 0; $id = 0;
$dbConnect->beginTransaction();
$chemicalsRow = $dbConnect->table('chemicals') $chemicalsRow = $dbConnect->table('chemicals')
->where('c_cas', $product['casNo']) ->where('c_cas', $product['casNo'])
->get()->toArray(); ->get()->toArray();
if (empty($chemicalsRow)) { if (empty($chemicalsRow)) {
array_push($errorProduct,$product['casNo']); array_push($errorProduct, $product['casNo']);
continue; continue;
} }
$chemicalsRow = current($chemicalsRow); $chemicalsRow = current($chemicalsRow);
...@@ -149,12 +152,12 @@ class LeyanService ...@@ -149,12 +152,12 @@ class LeyanService
->where('b_id', $bId) ->where('b_id', $bId)
->update(['p_status' => 0, 'p_show' => 0, 'modifytime' => time()]); ->update(['p_status' => 0, 'p_show' => 0, 'modifytime' => time()]);
foreach($product['productStockPrices'] as $package){ foreach ($product['productStockPrices'] as $package) {
$p_tod = '无货,请咨询'; $p_tod = '无货,请咨询';
$status = 0; $status = 0;
if(!empty($package['productStockInfos'])){ if (!empty($package['productStockInfos'])) {
foreach($package['productStockInfos'] as $stock){ foreach ($package['productStockInfos'] as $stock) {
if(isset($stock['city'])){ if (isset($stock['city'])) {
if ($stock['city'] == '上海' && isset($stock['number']) && $stock['number'] > 0) { if ($stock['city'] == '上海' && isset($stock['number']) && $stock['number'] > 0) {
$p_tod = '现货'; $p_tod = '现货';
$status = 1; $status = 1;
...@@ -175,8 +178,8 @@ class LeyanService ...@@ -175,8 +178,8 @@ class LeyanService
$pro_data = []; $pro_data = [];
if (empty($productRow)) { if (empty($productRow)) {
if($status == 0){ if ($status == 0) {
SimpleLogs::writeLog('Leyan cas : '.$chemicalsRow->c_cas. '为下架状态不做更新',__CLASS__.':storeLeyanProducts'); SimpleLogs::writeLog('Leyan cas : ' . $chemicalsRow->c_cas . '为下架状态不做更新', __CLASS__ . ':storeLeyanProducts');
continue; continue;
} }
...@@ -193,7 +196,7 @@ class LeyanService ...@@ -193,7 +196,7 @@ class LeyanService
} else { } else {
$pro_data['p_en_name'] = $product['productNameEn']; $pro_data['p_en_name'] = $product['productNameEn'];
} }
if(isset($product['purity']) && !empty($product['purity'])){ if (isset($product['purity']) && !empty($product['purity'])) {
$pro_data['p_level'] = $product['purity']; $pro_data['p_level'] = $product['purity'];
} }
$pro_data['p_pack'] = $package['size']; $pro_data['p_pack'] = $package['size'];
...@@ -214,7 +217,7 @@ class LeyanService ...@@ -214,7 +217,7 @@ class LeyanService
$pro_data['p_status'] = $status; $pro_data['p_status'] = $status;
$pro_data['p_show'] = 1; $pro_data['p_show'] = 1;
if($status == 1){ if ($status == 1) {
$pro_data['p_price'] = $package['price']; $pro_data['p_price'] = $package['price'];
} }
$pro_data['p_tod'] = $p_tod; $pro_data['p_tod'] = $p_tod;
...@@ -227,19 +230,17 @@ class LeyanService ...@@ -227,19 +230,17 @@ class LeyanService
} }
} }
if($id > 0){ if ($id > 0) {
SimpleLogs::writeLog('Leyan p_id : '.$id.' cas : '.$chemicalsRow->c_cas. ' 更新成功,更新时间:'.date('Y-m-d H:i:s',time()),__CLASS__.':storeLeyanProducts'); SimpleLogs::writeLog('Leyan p_id : ' . $id . ' cas : ' . $chemicalsRow->c_cas . ' 更新成功,更新时间:' . date('Y-m-d H:i:s', time()), __CLASS__ . ':storeLeyanProducts');
}
} }
$dbConnect->commit(); $dbConnect->commit();
} catch (\Exception $exception) {
return true; SimpleLogs::writeLog($exception->getMessage(), __CLASS__ . ':storeLeyanProducts', 'error');
}catch(\Exception $exception){
SimpleLogs::writeLog($exception->getMessage(),__CLASS__.':storeLeyanProducts', 'error');
$dbConnect->rollback(); $dbConnect->rollback();
throw $exception; throw $exception;
} }
} }
return true;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment