Commit f5d51340 authored by hangjun83's avatar hangjun83

乐妍

parent 85c5f324
<?php
namespace App\Console\Commands;
use App\Jobs\BideJob;
use App\Services\ThirdPlatform\BideService;
use App\Services\ThirdPlatform\LeyanService;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
class LeyanJobCommand extends Command
{
use ConfirmableTrait;
/**
* 命令行的名称及用法。
*
* @var string
*/
protected $signature = 'leyan:job
{--action_type= : 任务操作类型}
{--params= : 任务参数}';
/**
* 命令行的概述。
*
* @var string
*/
protected $description = 'leyan更新cas数据';
/**
* 创建新的命令实例。
*
* @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(LeyanService::class);
switch($action_type){
case 'updateProductByCas' :
default:
$service->updateProductByCas($params);
break;
}
}
}
public function help()
{
$helpStr = "参数帮助说明";
$this->comment($this->setHelp($helpStr)->getProcessedHelp());
$this->line("action_type: 具体动作参数\n\n initProductToExcel => 初始化批量商品 \n batchUpdateProduct => 批量更新商品\n\nparams : 操作需要传入的参数.非必填项");
}
}
......@@ -73,6 +73,7 @@ class AppServiceProvider extends ServiceProvider
\App\Console\Commands\BideJobCommand::class,
\App\Console\Commands\BjsJobCommand::class,
\App\Console\Commands\ZhenkhJobCommand::class,
\App\Console\Commands\LeyanJobCommand::class,
\App\Console\Commands\ThirdPlatformJobCommand::class,
\App\Console\Commands\KafkaConsumerCommand::class,
\App\Console\Commands\TestJobCommand::class
......
<?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\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Services\ThirdPlatform\PlatformAbstractService;
use App\Support\Traits\HttpClientHelpers;
class LeyanApiService extends PlatformAbstractService
{
use HttpClientHelpers;
public function __construct(ThirdApiPlatformRepository $thirdApiPlatformRepository)
{
parent::__construct($thirdApiPlatformRepository);
$this->setPlatformName('leyan');
$this->getPlatformInfo();
}
/**
* 批量更新商品信息
* @param $product
* @return bool
*/
public function pushUpdateProductByCas($product,$token)
{
try{
if($this->checkplatformstatus()){
$params = [];
$params['casNo'] = $this->mergeapimetadata($product);
$uri = $this->platforminfo['platform_url'].'/ProductDate/findProductDataPoolByCatalogNoOrCas';
$response = $this->getpostclient(
$uri, $params,null,
[
'token' => $token,
'content-type' => 'application/json;charset=utf-8'
]
);
return $this->apiresponse($response);
}
}catch(\exception $e){
return $this->requesterror($e);
}
}
public function pushBatchUpdateProduct($token){
}
public function getToken(){
try{
if($this->checkPlatformStatus()){
$tokenParams = [
'username' => $this->platformInfo['platform_params']['username'],
'password' => $this->platformInfo['platform_params']['password']
];
$response = $this->getPostClient($this->getPlatformApiUrl().'/ProductDate/getToken',$tokenParams);
$token = $this->apiResponse($response);
return $token['data'];
}
}catch(\Exception $exception){
$this->requestError($exception);
}
}
/**
* 整理组装 api 数据
* @param $metaData
* @return array
*/
public function mergeApiMetaData($metaData)
{
return $metaData;
}
protected function apiResponse($response)
{
$decodeResponse = json_decode($response,true);
if($decodeResponse['status'] == '1' && $decodeResponse['code'] == '0'){
if(!empty($decodeResponse['data'])){
return $decodeResponse['data'];
}
}elseif($decodeResponse['status'] == '0'){
if(!is_array($decodeResponse['data'])){
return false;
}
}
return false;
}
protected function requestError($err)
{
return false;
}
}
<?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\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Services\ThirdPlatform\Api\LeyanApiService;
use App\Support\Facades\SimpleLogs;
use App\Support\Traits\Helpers;
use Illuminate\Support\Facades\DB;
class LeyanService
{
public function __construct()
{
$this->apiService = (new LeyanApiService(app(ThirdApiPlatformRepository::class)));
}
public function batchUpdateProducts($params = null)
{
$path = config('filesystems.disks.local.root').'/upload/'.$params;
$generator = Helpers::yieldLoadCsv($path);
// yield 循环逐行读取csv内容
while ($generator->valid()) {
$content = $generator->current();
$this->apiService->pushQueue([
'params' => ['cas' => current($content)],
'consumer' => __CLASS__,
'method' => 'batchUpdateApi'
],'bjs');
$generator->next();
}
//$generator->rewind();
}
public function updateProductByCas($params)
{
$result = $this->apiService->pushUpdateProductByCas($params['cas'],$this->getToken());
if(!$result){
return null;
}
try{
$result = $this->storeLeyanProducts($result);
}catch(\Exception $exception){
SimpleLogs::writeLog($exception->getMessage(),__CLASS__.':batchUpdateApi', 'error');
}
}
/**
* @ 获取api token
* @return false|mixed
*/
public function getToken(){
try{
$token = $this->apiService->getPlatformDataEntries('leyan_api_token','data_values');
if(empty($token)){
$apiToken = $this->apiService->getToken();
if($apiToken){
$apiToken['currentTime'] = time();
$this->apiService->storePlatformDataEntries('leyan_api_token',$apiToken);
return $apiToken['token'];
}
}
}catch(\Exception $exception){
SimpleLogs::writeLog($exception->getMessage(), __CLASS__.':token error', 'error');
return false;
}
}
/**
* 保存bjs平台的产品数据
*/
public function storeLeyanProducts($bideProducts)
{
if (!is_array($bideProducts) || empty($bideProducts)) {
return false;
}
$bId = 108;
try {
$id = 0;
$dbConnect = DB::connection('bh_mysql');
$dbConnect->beginTransaction();
$chemicalsRow = $dbConnect->table('chemicals')
->where('c_cas', $bideProducts[0]['CASNumber'])
->get()->toArray();
if (empty($chemicalsRow)) {
return false;
}
$chemicalsRow = current($chemicalsRow);
$dbConnect->table('products')
->where('c_id', $chemicalsRow->c_id)
->where('b_id', $bId)
->update(['p_status' => 0, 'p_show' => 0, 'modifytime' => time()]);
foreach ($bideProducts as $product) {
if ($product['Weight'] > 0) {
$p_tod = '现货';
} else {
$p_tod = '无货,请咨询';
}
$p_code = $product['ProductNum'] . '-' . $product['PackSize'];
$productRow = $dbConnect->table('products')
->where('b_id', $bId)
->where('c_id', $chemicalsRow->c_id)
->where('p_code', 'like', $p_code)
->get()->toArray();
$pro_data = [];
if (empty($productRow)) {
$pro_data['b_id'] = $bId;
$pro_data['c_id'] = $chemicalsRow->c_id;
$pro_data['p_code'] = $p_code;
if (!$product['ProductCName']) {
$pro_data['p_cn_name'] = '';
} else {
$pro_data['p_cn_name'] = $product['ProductCName'];
}
if (!$product['ProductEName']) {
$pro_data['p_en_name'] = '';
} else {
$pro_data['p_en_name'] = $product['ProductEName'];
}
$pro_data['p_level'] = $product['Purity'];
$pro_data['p_pack'] = $product['PackSize'];
$pro_data['p_price'] = $product['Price'];
$pro_data['p_tod'] = $p_tod;
$pro_data['p_weight'] = '0.01';
$pro_data['p_status'] = 1;
$pro_data['p_show'] = 0;
$pro_data['prom_from'] = 0;
$pro_data['prom_to'] = 0;
$id = $dbConnect->table('products')->insertGetId($pro_data);
} else {
$productRow = current($productRow);
$pro_data['p_status'] = 1;
$pro_data['p_show'] = 0;
$pro_data['p_price'] = $product['Price'];
$pro_data['p_tod'] = $p_tod;
$pro_data['modifytime'] = time();
$dbConnect->table('products')
->where('p_id', $productRow->p_id)
->update($pro_data);
$id = $productRow->p_id;
}
}
$dbConnect->commit();
SimpleLogs::writeLog('Leyan p_id : '.$id.' cas : '.$chemicalsRow->c_cas. ' 更新成功,更新时间:'.date('Y-m-d H:i:s',time()),__CLASS__.':storeLeyanProducts');
return true;
}catch(\Exception $exception){
SimpleLogs::writeLog($exception->getMessage(),__CLASS__.':storeLeyanProducts', 'error');
$dbConnect->rollback();
throw $exception;
}
}
}
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