Commit 044ea405 authored by hj's avatar hj

更新提交

parent bbb50c62
<?php
/*
* This file is part of the Jiannei/lumen-api-starter.
*
* (c) Jiannei <longjian.huang@foxmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace App\Services\ThirdPlatform\Api;
use App\ImportExport\WuxiLab\WuxiLabExport;
use App\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Services\ThirdPlatform\PlatformAbstractService;
use App\Support\Facades\SimpleLogs;
use App\Support\Traits\HttpClientHelpers;
class WuxiApiNewService extends PlatformAbstractService
{
use HttpClientHelpers;
public function __construct(ThirdApiPlatformRepository $thirdApiPlatformRepository)
{
parent::__construct($thirdApiPlatformRepository);
$this->setPlatformName('wuxi_appTec');
$this->getPlatformInfo();
}
/**
* 批量更新商品信息
* @param $product
* @return bool
*/
public function pushBatchUpdateProduct($product)
{
try{
if($this->checkPlatformStatus()){
$params = [];
$params['chemicalProducts'] = $this->mergeApiMetaData($product);
$response = $this->getPostClient($this->platformInfo['platform_url'].'/api/v2/updateChamical',$params, null,
[
'headers' => [
'authorization' => $this->getAuthHeader()
]
]
);
return $this->apiResponse($response);
}
}catch(\Exception $e){
$this->requestError($e);
}
}
/**
* @更新包装规格
* @param $packages
* @param $token
* @return false|mixed|void
*/
public function pushBatchUpdatePackages($packages,$token)
{
try{
if($this->checkPlatformStatus()){
$params = [];
$params['company_id'] = $this->platformInfo['platform_params']['companyId'];
$params['product_type'] = 'CHAMICAL';
$params = array_merge($params,$packages);
$response = $this->getPostClient($this->platformInfo['platform_params']['adminUrl'].'/package/specification/save',$params,null, [
//'cookie' => $this->getCookie($token)
]);
return $this->apiResponse($response);
}
}catch(\Exception $e){
$this->requestError($e);
}
}
public function getPackageList($token)
{
try{
if($this->checkPlatformStatus()){
$params = [];
$params['companyId'] = $this->platformInfo['platform_params']['companyId'];
$params['productType'] = 'CHAMICAL';
$urlParams = [];
foreach($params as $key => $param){
$urlParams[] = $key.'='.$param;
}
$response = $this->clientRequest('get',
$this->platformInfo['platform_params']['adminUrl'].'/package/specification/count?'.implode('&',$urlParams),
[
'headers' => [
//'cookie' => $this->getCookie($token),
'content-type' => 'application/json;charset=UTF-8'
]
]);
if($response && $response > 0){
$page = 1;
$count = ceil($response / 30);
$packageList = [];
while(true){
$params['page'] = $page;
$params['rows'] = 30;
$urlParams = [];
foreach($params as $key => $param){
$urlParams[] = $key.'='.$param;
}
$packageListResponse = $this->clientRequest('get',
$this->platformInfo['platform_params']['adminUrl'].'/package/specification/list?'.implode('&',$urlParams),
[
'headers' => [
//'cookie' => $this->getCookie($token),
'content-type' => 'application/json;charset=UTF-8'
]
]);
$result = $this->apiResponse($packageListResponse);
if($result){
$packageList = array_merge($packageList,$result);
}
$page ++;
if($page > $count){
break;
}
sleep(2);
}
return $packageList;
}
return $this->apiResponse($response);
}
}catch(\Exception $e){
$this->requestError($e);
}
}
/**
* 整理组装 api 数据
* @param $metaData
* @return array
*/
public function mergeApiMetaData($metaData)
{
if(empty($metaData)){
return [];
}
$mergeData = [];
foreach($metaData['rawList'] as $datas){
$tempData['supplier_product_id'] = $datas['r_code'];
$tempData['mdl'] = '';
$tempData['cas'] = $datas['c_cas'];
$tempData['nu'] = '';
$tempData['chinese_name'] = $datas['r_cn_name'];
$tempData['english_name'] = $datas['r_en_name'];
$tempData['chemicalformula'] = $datas['c_fzs'] ?? '';
$tempData['purity'] = $datas['r_level'];
/*$tempData['usually_ships_days_min'] = '';
$tempData['usually_ships_within_days'] = '';
$tempData['backorder_lead_time'] = '';*/
//储存条件
switch($datas['c_cctj']){
case 1: $tempData['storage_condition'] = 0;break;
case 2: $tempData['storage_condition'] = 1;break;
case 3: $tempData['storage_condition'] = 2;break;
}
//运输条件
switch ($tempData['storage_condition']){
case 0: $tempData['transportation_condition'] = 0;break;
case 1: $tempData['transportation_condition'] = 2;break;
case 2: $tempData['transportation_condition'] = 1;break;
}
/*产品种类
1: Templates/Scaffolds,
2: Amino Acids &
Peptides,
3:Building Blocks,
4:Screening Library
Compounds,
5:Fragment Library
Compounds,
6:Reagent Services,
7:Natural Products,
8:Impurity
*/
$tempData['group_id'] = 6;
//是否进口
$tempData['is_imported'] = false;
//是否推送电商
$tempData['is_push_ds'] = false;
//$tempData['status'] = $datas['r_status'] == '1' ? 'ACTIVE' : 'INACTIVE';
$tempData['inventorys'][] = [
'type' => $datas['r_unit'],
'quantity' => floatval($datas['r_stock']),
'country' => 'CN',
'stock_status' => $datas['r_stock'] > 0 ? 'INSTOCK' : 'OUTSTOCK',
];
$tempData['packages'] = [];
if(isset($metaData['packageList'][$datas['r_code']])){
$package = $metaData['packageList'][$datas['r_code']];
$pack['unit_description'] = $package['p_pack_unit'];
//$pack['package_status'] = $package['p_status'] == '1' && $package['p_show'] == '1' && $package['p_if_big'] == 0 ? 'ACTIVE' : 'INACTIVE';
//$tempData['status'] = $package['p_status'] == '1' && $package['p_show'] == '1' && $package['p_if_big'] == 0 ? 'ACTIVE' : 'INACTIVE';
$pack['inventorys'] = [
'package_quantity' => $package['p_stock'],
//'stock_status' => $package['p_stock'] > 0 ? 'INSTOCK' : 'OUTSTOK',
'country' => 'CN'
];
$pack['prices'] = [
'price' => $package['p_price'],
'currency' => 'RMB',
//'status' => 'ACTIVE'
];
/*$pack['costs'] = [
'In_cost' => $package['p_avg_cost'],
'In_cost_currency' => 'RMB',
'status' => $package['p_stock'] > 0 ? 'ACTIVE' : 'INACTVIE',
];*/
$pack['wuxiPackagePricings'] = [
'discount'=> 0.00,
'currency' => 'RMB',
//'status' => 'INACTIVE'
];
$tempData['packages'][] = $pack;
}
$mergeData[] = $tempData;
unset($tempData);
}
return $mergeData;
}
public function getAuthHeader()
{
$auth = $this->dataEntriesService->getEntriesValues('wuxi_api_auth');
if(!$auth){
$response = $this->getPostClient($this->platformInfo['platform_url'].'/token/fetch', [
'username' => '790087059@qq.com',
'password' => 'YXH1992xb!!'
]
);
$result = $this->apiResponse($response);
if($result){
if($result['code'] == '200' && $result['success'] == 'true'){
$this->dataEntriesService->storeEntries(6, 'wuxi_api_auth',
['authorization' => $result['data']]
);
}
}
return $result['data'];
}
return $auth['authorization'];
}
protected function apiResponse($response)
{
$decodeResponse = json_decode($response,true);
if($decodeResponse){
return $decodeResponse;
}
return false;
}
protected function requestError($response)
{
SimpleLogs::writeLog($response->getMessage(), 'WuxiApi error', 'error');
throw $response;
}
}
<?php
/*
* This file is part of the Jiannei/lumen-api-starter.
*
* (c) Jiannei <longjian.huang@foxmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace App\Services\ThirdPlatform;
use App\ImportExport\WuxiLab\WuxiLabExport;
use App\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Rhawn\Services\RhawnProductService;
use App\Services\ThirdPlatform\Api\WuxiApiService;
use App\Services\ThirdPlatform\Api\WuxiApiNewService;
use App\Support\Facades\SimpleLogs;
class WuxiLabNewService
......@@ -22,7 +13,7 @@ class WuxiLabNewService
public function __construct()
{
$this->apiService = (new WuxiApiService(app(ThirdApiPlatformRepository::class)));
$this->apiService = (new WuxiApiNewService(app(ThirdApiPlatformRepository::class)));
$this->rhawnChemicalsService = app(RhawnProductService::class);
}
......@@ -67,7 +58,7 @@ class WuxiLabNewService
$this->apiService->pushQueue([
'params' => ['rawList' => $rawList,'packageList' => $newPackages],
'consumer' => __CLASS__,
'method' => 'batchUpdateToWuxiLab'
'method' => 'batchUpdateTo'
],'wuxilab');
}
......@@ -122,50 +113,6 @@ class WuxiLabNewService
}
}
public function test($rawCode)
{
try{
$rawsList = $this->rhawnChemicalsService->getChemicalRawThroughtCode($rawCode);
if($rawsList){
foreach ($rawsList as $raw) {
$rawIdList[] = $raw['r_id'];
}
$packages = $this->rhawnChemicalsService->getChemicalPackage($rawIdList);
$newPackages = [];
if($packages){
foreach($packages as $pack){
$newPackages[$pack['r_code']] = $pack;
}
}
/*$path = app(WuxiLabExport::class)->saveExcel(
['rawList' => $rawsList,'packageList' => $newPackages],
'package'
);
if($path){
list($file,$expend) = explode('.',$path);
$newPath = $file.'.'.strtolower($expend);
$cmd = 'mv '.storage_path('app/'.$path).' '.storage_path('app/'.$newPath);
trim(shell_exec("$cmd 2>&1"));
$filePathList = explode('/',$file);
$fileName = $filePathList[count($filePathList) - 1];
$result = $this->apiService->pushBatchUploadFile('package',$fileName, $this->apiService->backgroundLogin());
if($result){
$cmd = 'rm -rf '.storage_path('app/'.$newPath);
trim(shell_exec("$cmd 2>&1"));
SimpleLogs::writeLog('文件名为:'.$fileName.'路径:'.$newPath.'药明康德后台上传完成', __CLASS__.':processNotExistProductsUpdate');
}
}*/
$result = $this->apiService->pushBatchUpdateProduct(['rawList' => $rawsList,'packageList' => $newPackages]);
var_dump($result);
}
}catch(\Throwable $exception){
var_dump($exception->getMessage());
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':processNotExistProductsUpdate', 'error');
}
}
/**
* 批量更新新的商品
*/
......@@ -399,19 +346,19 @@ class WuxiLabNewService
* @批量更新商品
* @param $updateData
*/
public function batchUpdateToWuxiLab($updateData)
public function batchUpdateTo($updateData)
{
if(!isset($updateData['rawList']) || empty($updateData['rawList'])){
if(empty($updateData['rawList'])){
return false;
}
if(!isset($updateData['packageList']) || empty($updateData['packageList'])){
if(empty($updateData['packageList'])){
return false;
}
try{
$result = $this->apiService->pushBatchUpdateProduct($updateData);
if($result){
//处理更新不成功的商品
if(isset($result['errorProduct']) && !empty($result['errorProduct'])){
if(!empty($result['errorProduct'])){
$storeEntries = $this->apiService->getPlatformDataEntries('wuxiLab_not_exist_products','data_values');
if(isset($storeEntries) && !empty($storeEntries)){
if(count($storeEntries['products']) >= $this->apiService->getPlatformInfo('platform_params')['excelCreateLimit']){
......
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