Commit bbb50c62 authored by hj's avatar hj

更新提交

parent 226388d4
<?php
namespace App\Console\Commands;
use App\Jobs\WuxiLabJob;
use App\Services\ThirdPlatform\WuxiLabNewService;
use App\Services\ThirdPlatform\WuxiLabService;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
class WuxiLabNewJobCommand extends Command
{
use ConfirmableTrait;
/**
* 命令行的名称及用法。
*
* @var string
*/
protected $signature = 'wuxilabnew:job
{--action_type= : 任务操作类型}
{--params= : 任务参数}';
/**
* 命令行的概述。
*
* @var string
*/
protected $description = '药名康德任务命令行';
/**
* 创建新的命令实例。
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* 运行命令。
* @throws \Exception
*/
public function handle()
{
if (! $this->confirmToProceed()) {
return 1;
}
if($this->hasArgument('help')){
$this->help();
}else{
$action_type = $this->option('action_type');
$params = $this->option('params');
if(empty($action_type)){
$this->error('缺少命令参数,请输入具体的参数命令.如需帮助请输入 --help');
exit;
}
$service = app(WuxiLabNewService::class);
switch($action_type){
case 'initProductToExcel' :
if(!isset($params) || $params == 0){
$params['limit'] = 1000;
}
$service->initCreateProductsToExcel($params['limit']);
break;
case 'batchUpdatePackages' :
if(!isset($params) || $params == 0){
$params['limit'] = 1000;
}
$service->batchUpdatePackagesInfo($params);
break;
case 'batchUploadProducts' :
$service->batchUploadProducts();
break;
case 'batchUpdateToWuxiLab' :
$service->batchUpdateToWuxiLab();
break;
case 'batchUpdateProduct' :
$service->batchUpdateProducts();
break;
case 'test' :
$service->test($params);
break;
case 'test1' :
$service->test1();
break;
case 'batchUploadNewProducts' :
$service->batchUploadNewProducts();
break;
case 'batchUpdateProductPackages' :
$service->batchUploadPackages();
break;
case 'batchUpdateProductsShelves' :
$service->batchUpdateProductsShelves();
break;
case 'initToken' :
$service->initToken();
break;
default:
}
/*$productUpdateJob = (new WuxiLabJob($action_type,$params))->delay(100)->onQueue('slow');
app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($productUpdateJob);*/
}
}
public function help()
{
$helpStr = "参数帮助说明";
$this->comment($this->setHelp($helpStr)->getProcessedHelp());
$this->line("action_type: 具体动作参数\n\n initProductToExcel => 初始化批量商品 \n batchUpdateProduct => 批量更新商品\n\nparams : 操作需要传入的参数.非必填项");
}
}
...@@ -78,7 +78,8 @@ class AppServiceProvider extends ServiceProvider ...@@ -78,7 +78,8 @@ class AppServiceProvider extends ServiceProvider
\App\Console\Commands\KafkaConsumerCommand::class, \App\Console\Commands\KafkaConsumerCommand::class,
\App\Console\Commands\TestJobCommand::class, \App\Console\Commands\TestJobCommand::class,
\App\Console\Commands\RhawnToolsJobCommand::class, \App\Console\Commands\RhawnToolsJobCommand::class,
\App\Console\Commands\InvoiceJobCommand::class \App\Console\Commands\InvoiceJobCommand::class,
\App\Console\Commands\WuxiLabNewJobCommand::class
]); ]);
} }
......
...@@ -41,8 +41,6 @@ class WuxiApiService extends PlatformAbstractService ...@@ -41,8 +41,6 @@ class WuxiApiService extends PlatformAbstractService
$params = []; $params = [];
$params['chemicalProducts'] = $this->mergeApiMetaData($product); $params['chemicalProducts'] = $this->mergeApiMetaData($product);
$response = $this->getPostClient($this->platformInfo['platform_url'].'/api/v1/update',$params, [ $response = $this->getPostClient($this->platformInfo['platform_url'].'/api/v1/update',$params, [
$this->platformInfo['platform_params']['username'], $this->platformInfo['platform_params']['password'] $this->platformInfo['platform_params']['username'], $this->platformInfo['platform_params']['password']
]); ]);
......
<?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\Support\Facades\SimpleLogs;
class WuxiLabNewService
{
public function __construct()
{
$this->apiService = (new WuxiApiService(app(ThirdApiPlatformRepository::class)));
$this->rhawnChemicalsService = app(RhawnProductService::class);
}
public function initToken()
{
$this->getToken();
//$this->apiService->removePlatformDataEntries('wuxilab_backlogin_token');
}
/**
* 批量更新商品
*/
public function batchUpdateProducts($params = null)
{
$status = $this->apiService->checkPlatformStatus();
if($status){
$limit = $this->apiService->getPlatformInfo('platform_params')['batchNums'];
$page = 0;
ini_set('memory_limit','3072M');
while(true) {
$rawList = $this->rhawnChemicalsService->getRawsList($page * $limit, $limit);
if (!$rawList) {
break;
}
$rawIdList = [];
foreach ($rawList as $raw) {
$rawIdList[] = $raw['r_id'];
}
$packages = $this->rhawnChemicalsService->getChemicalPackage($rawIdList);
$newPackages = [];
if($packages){
foreach($packages as $pack){
//符合110状态才上架
if($pack['p_status'] == 1 && $pack['p_show'] == 1 && $pack['p_if_big'] == 0){
$newPackages[$pack['p_code']] = $pack;
}
//$newPackages[$pack['r_code']] = $pack;
}
}
if(!empty($rawList)){
$this->apiService->pushQueue([
'params' => ['rawList' => $rawList,'packageList' => $newPackages],
'consumer' => __CLASS__,
'method' => 'batchUpdateToWuxiLab'
],'wuxilab');
}
$page ++;
}
}
}
/**
* 商品批量下架
* @param null $params
* @throws \Exception
*/
public function batchUpdateProductsShelves($params = null)
{
$status = $this->apiService->checkPlatformStatus();
if($status){
$limit = $this->apiService->getPlatformInfo('platform_params')['batchNums'];
$page = 0;
ini_set('memory_limit','3072M');
$rawCodeList = [];
while(true) {
$rawList = $this->rhawnChemicalsService->getRawsList($page * $limit, $limit);
if (!$rawList) {
break;
}
foreach ($rawList as $raw) {
if(!in_array($raw['r_code'],$rawCodeList)){
array_push($rawCodeList,$raw['r_code']);
}
}
$page ++;
}
$path = app(WuxiLabExport::class)->saveExcel(
['rawCode' => $rawCodeList], 'product_shelves'
);
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($params['type'],$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');
}*/
}
}
}
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');
}
}
/**
* 批量更新新的商品
*/
public function batchUploadNewProducts()
{
$storeEntries = $this->apiService->getPlatformDataEntries('wuxiLab_not_exist_products','data_values');
if(isset($storeEntries['products']) && !empty($storeEntries['products'])){
$countNum = 0;
$products = [];
foreach($storeEntries['products'] as $product){
if(!in_array($product,$products) || empty($products)){
array_push($products,$product);
$countNum ++;
}
if($countNum == $this->apiService->getPlatformInfo('platform_params')['excelCreateLimit']){
$this->apiService->pushQueue([
'params' => ['products' => $products,'type' => 'all'],
'consumer' => __CLASS__,
'method' => 'processProductsUpdateThroughtExcel'
],'wuxilab_product');
$countNum = 0;
$products = [];
}
}
$this->apiService->removePlatformDataEntries('wuxiLab_not_exist_products');
}
}
/**
* @批量后台请求更新商品规格
* @param null $params
*/
public function batchUpdatePackagesInfo($params = null)
{
$token = $this->getToken();
$wuxiLabPackage = ['bottle','box','bp','case','ea','g','gal','hole','kg','kit','l','m','m2','mg','ml','mm','mmol','mu',
'ng','nmole','od','package','pair','piece','pmole','RL','sets','test', 'unit','vial','xa','µci','µg','µl','µmol'];
//$path = config('filesystems.disks.local.root').'/upload/'.$params;
//$generator = Helpers::yieldLoadCsv($path);
// yield 循环逐行读取csv内容
/*while ($generator->valid()) {
$content = $generator->current();
if(in_array($content[1],$wuxiLabPackage)){
$this->apiService->pushBatchUpdatePackages([
'unit_quantity' => $content[0], 'unit_type' => $content[1], 'unit_description' => $content[0].$content[1]
],$token);
}
usleep(30000);
$generator->next();
}*/
$packageList = $this->apiService->getPackageList($token);
$packagesUnit = $this->rhawnChemicalsService->getProductPackagesThroughGroupByPackUnit();
foreach($packagesUnit as $package){
if(!empty($packageList)){
$exist = 0;
foreach($packageList as $p){
$name = $package['p_pack'].$package['p_pack_unit'];
if($name == $p['unitDescription']){
$exist = 1;
}
}
if(!$exist){
//转换
$package['p_pack_unit'] = strtolower($package['p_pack_unit']);
switch($package['p_pack_unit']){
case 'ul' : $package['p_pack_unit'] = 'µl';break;
case 'ug' : $package['p_pack_unit'] = 'µg';break;
}
if(!in_array($package['p_pack_unit'],$wuxiLabPackage)){
SimpleLogs::writeLog('unit_description:'.$package['p_pack'].strtolower($package['p_pack_unit']).'规格不在范围内,跳过', __CLASS__.':batchUpdatePackagesInfo');
continue;
}
try{
//如果包装数量不是数字,跳过
if(is_numeric($package['p_pack'])){
SimpleLogs::writeLog('unit_quantity:'.$package['p_pack'].' unit_type:'.strtolower($package['p_pack_unit']).' unit_description:'.$package['p_pack'].strtolower($package['p_pack_unit']), __CLASS__.':batchUpdatePackagesInfo');
$this->apiService->pushBatchUpdatePackages([
'unit_quantity' => $package['p_pack'], 'unit_type' => strtolower($package['p_pack_unit']), 'unit_description' => $package['p_pack'].strtolower($package['p_pack_unit'])
],$token);
}else{
SimpleLogs::writeLog('unit_description:'.$package['p_pack'].strtolower($package['p_pack_unit']).'数量不是数字,跳过', __CLASS__.':batchUpdatePackagesInfo');
continue;
}
}catch(\Throwable $exception){
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':batchUpdatePackagesInfo', 'error');
}
}
}
}
}
/**
* @处理不存在商品的更新
* @param $params
* @return false|void
*/
public function processProductsUpdateThroughtExcel($params)
{
ini_set('memory_limit','3072M');
if(!isset($params['products']) || empty($params['products'])){
return false;
}
try{
$rawsList = $newPackages = [];
$slice_array_length= 500;
$current_length = 0;
while(true){
$list = [];
$product = array_slice($params['products'],$current_length,$slice_array_length);
$list = $this->rhawnChemicalsService->getChemicalRawThroughtCode($product);
if($list){
$rawIdList = [];
foreach ($list as $raw){
$rawIdList[] = $raw['r_id'];
}
$rawsList = array_merge($rawsList,$list);
$packages = $this->rhawnChemicalsService->getChemicalPackage($rawIdList);
if($packages){
foreach($packages as $pack){
//符合110状态才上架
if($pack['p_status'] == 1 && $pack['p_show'] == 1 && $pack['p_if_big'] == 0){
$newPackages[$pack['p_code']] = $pack;
}
}
}
}
if($current_length >= $this->apiService->getPlatformInfo('platform_params')['excelCreateLimit']){
break;
}
$current_length += $slice_array_length;
}
if($params['type'] == 'all'){
$productPath = app(WuxiLabExport::class)->saveExcel(
['rawList' => $rawsList,'packageList' => $newPackages],
'product'
);
if($productPath){
list($file,$expend) = explode('.',$productPath);
$newPath = $file.'.'.strtolower($expend);
$cmd = 'mv '.storage_path('app/'.$productPath).' '.storage_path('app/'.$newPath);
trim(shell_exec("$cmd 2>&1"));
$filePathList = explode('/',$file);
$fileName = $filePathList[count($filePathList) - 1];
$result = $this->apiService->pushBatchUploadFile('product',$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');
}
}
$packagePath = app(WuxiLabExport::class)->saveExcel(
['rawList' => $rawsList,'packageList' => $newPackages],
'package'
);
if($packagePath){
list($file,$expend) = explode('.',$packagePath);
$newPath = $file.'.'.strtolower($expend);
$cmd = 'mv '.storage_path('app/'.$packagePath).' '.storage_path('app/'.$newPath);
trim(shell_exec("$cmd 2>&1"));
$filePathList = explode('/',$file);
$fileName = $filePathList[count($filePathList) - 1];
$result = $this->apiService->pushBatchUploadFile('product',$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');
}
}
}else{
$path = app(WuxiLabExport::class)->saveExcel(
['rawList' => $rawsList,'packageList' => $newPackages],
$params['type']
);
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($params['type'],$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');
}
}
}
usleep(rand(1000000,5000000));
}catch(\Throwable $exception){
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':processNotExistProductsUpdate', 'error');
}
}
/**
* 批量更新商品包装
*/
public function batchUploadPackages()
{
$storeEntries = [];
$storeEntries = $this->apiService->getPlatformDataEntries('wuxiLab_do_not_update_products','data_values');
if(isset($storeEntries['products']) && !empty($storeEntries['products'])){
$countNum = 0;
$products = [];
foreach($storeEntries['products'] as $product){
if(!in_array($product,$products) || empty($products)){
array_push($products,$product);
$countNum ++;
}
if($countNum >= $this->apiService->getPlatformInfo('platform_params')['excelCreateLimit']){
$this->apiService->pushQueue([
'params' => ['products' => $products,'type' => 'package'],
'consumer' => __CLASS__,
'method' => 'processProductsUpdateThroughtExcel'
],'wuxilab_package');
$countNum = 0;
$products = [];
}
}
$this->apiService->removePlatformDataEntries('wuxiLab_do_not_update_products');
}
}
/**
* @批量更新商品
* @param $updateData
*/
public function batchUpdateToWuxiLab($updateData)
{
if(!isset($updateData['rawList']) || empty($updateData['rawList'])){
return false;
}
if(!isset($updateData['packageList']) || empty($updateData['packageList'])){
return false;
}
try{
$result = $this->apiService->pushBatchUpdateProduct($updateData);
if($result){
//处理更新不成功的商品
if(isset($result['errorProduct']) && !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']){
$this->apiService->pushQueue([
'params' => ['products' => $storeEntries['products'],'type' => 'product'],
'consumer' => __CLASS__,
'method' => 'processProductsUpdateThroughtExcel'
],'wuxilab_product');
/*$this->apiService->pushQueue([
'params' => ['products' => $storeEntries['products'],'type' => 'package'],
'consumer' => __CLASS__,
'method' => 'processProductsUpdateThroughtExcel'
],'wuxilab_package');*/
$this->apiService->removePlatformDataEntries('wuxiLab_not_exist_products');
/*SimpleKafka::produerSend(
[
'params' => ['products' => $storeEntries['products'],'type' => 'product'],
'consumer' => __CLASS__,
'method' => 'processProductsUpdateThroughtExcel'
]
);*/
$storeEntries = [];
}
}
foreach($result['errorProduct'] as $errorProduct){
if($errorProduct['reason'] == 'PRODUCT_NOT_EXIST'){
if(!is_null($storeEntries) && !empty($storeEntries['products'])){
if(!in_array($errorProduct['data'],$storeEntries['products'])){
array_push($storeEntries['products'],$errorProduct['data']);
}
}else{
$storeEntries['products'][] = $errorProduct['data'];
}
}
}
$this->apiService->storePlatformDataEntries('wuxiLab_not_exist_products',['products' => $storeEntries['products']]);
}
//更新成功,批量更新包装规格
$storeEntries = $this->apiService->getPlatformDataEntries('wuxiLab_do_not_update_products','data_values');
if(isset($storeEntries) && !empty($storeEntries)){
if(!is_null($storeEntries) && count($storeEntries['products']) >= $this->apiService->getPlatformInfo('platform_params')['excelCreateLimit']){
$this->apiService->pushQueue([
'params' => ['products' => $storeEntries['products'],'type' => 'package'],
'consumer' => __CLASS__,
'method' => 'processProductsUpdateThroughtExcel'
],'wuxilab_package');
$this->apiService->removePlatformDataEntries('wuxiLab_do_not_update_products');
$storeEntries = [];
}
}
$updateSuccessStoreEntries = $this->apiService->getPlatformDataEntries('wuxiLab_update_success_products','data_values');
//更新成功的商品
foreach($result['successProduct'] as $successProduct){
if(!is_null($storeEntries) && !empty($storeEntries['products'])){
if(!in_array($successProduct,$storeEntries['products'])){
array_push($storeEntries['products'],$successProduct);
}
}else{
$storeEntries['products'][] = $successProduct;
}
if(!is_null($updateSuccessStoreEntries) && !empty($updateSuccessStoreEntries['products'])){
if(!in_array($successProduct,$updateSuccessStoreEntries['products'])){
array_push($updateSuccessStoreEntries['products'],$successProduct);
}
}else{
$updateSuccessStoreEntries['products'][] = $successProduct;
}
}
$this->apiService->storePlatformDataEntries('wuxiLab_do_not_update_products',['products' => $storeEntries['products']]);
$this->apiService->storePlatformDataEntries('wuxiLab_update_success_products',['products' => $updateSuccessStoreEntries['products']]);
}
}catch(\Throwable $exception){
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':batchUpdateToWuxiLab', 'error');
var_dump($exception->getMessage());
}
}
/**
* @ 获取api token
* @return false|mixed
*/
public function getToken(){
try{
$token = $this->apiService->getPlatformDataEntries('wuxilab_backlogin_token','data_values');
if(empty($token)){
$apiToken = $this->apiService->backgroundLogin();
if($apiToken){
$backloginToken = [];
$backloginToken['currentTime'] = time();
$backloginToken['token'] = $apiToken;
$this->apiService->storePlatformDataEntries('wuxilab_backlogin_token',$backloginToken);
return $apiToken;
}
}else{
//超过有效期,重新请求获取
if(intval(gmdate('i',time() - intval($token['currentTime']))) > 30){
$apiToken = $this->apiService->backgroundLogin();
if($apiToken){
$apiToken['currentTime'] = time();
$this->apiService->storePlatformDataEntries('wuxilab_backlogin_token',$apiToken);
return $apiToken['token'];
}
}
return $token['token'];
}
}catch(\Exception $exception){
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':token error', 'error');
return false;
}
}
/**
* @初始化生成产品excel文档
* @param $exportLimit
*/
public function initCreateProductsToExcel($exportLimit)
{
$limit = $exportLimit;
$page = 0;
while(true) {
$rawList = $this->rhawnChemicalsService->getRawsList($page * $limit, $limit);
if (!$rawList) {
break;
}
$rawIdList = [];
foreach ($rawList as $raw) {
$rawIdList[] = $raw['r_id'];
}
$packages = $this->rhawnChemicalsService->getChemicalPackage($rawIdList);
$newPackages = [];
if($packages){
foreach($packages as $pack){
$newPackages[$pack['r_code']] = $pack;
}
}
if(!empty($rawList)){
app(WuxiLabExport::class)->saveExcel(
['rawList' => $rawList,'packageList' => $newPackages]
);
}
break;
}
}
}
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