Commit d0be1061 authored by hangjun83's avatar hangjun83

openapi

parent db73c39f
......@@ -21,7 +21,7 @@ class BhProductsService
*/
public function storeBideProducts($bideProducts)
{
if (!is_array($bideProducts) || empty($bideProducts)) {
if (!is_array($bideProducts) || empty($bideProducts)) {
return false;
}
......@@ -107,9 +107,110 @@ class BhProductsService
$dbConnect->commit();
app(SysLogService::class)->addApiLogs($id.' 更新成功,更新时间:'.date('Y-m-d H:i:s',time()));
app(SysLogService::class)->addApiLogs('p_id : '.$id.' cas : '.$chemicalsRow->c_cas. ' 更新成功,更新时间:'.date('Y-m-d H:i:s',time()));
return true;
}catch(\Exception $exception){
$dbConnect->rollback();
throw $exception;
}
}
/**
* 保存bide平台的产品数据
*/
public function storeBjsProducts($bideProducts)
{
if (!is_array($bideProducts) || empty($bideProducts)) {
return false;
}
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', 175)
->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', 175)
->where('c_id', $chemicalsRow->c_id)
->where('p_code', 'like', $p_code)
->get()->toArray();
$pro_data = [];
if (empty($productRow)) {
$pro_data['b_id'] = 175;
$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['PriceB'];
$pro_data['p_tod'] = $p_tod;
$pro_data['p_weight'] = '0.01';
$pro_data['p_status'] = 1;
$pro_data['p_show'] = 1;
$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'] = 1;
$pro_data['p_price'] = $product['PriceB'];
$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();
app(SysLogService::class)->addApiLogs('p_id : '.$id.' cas : '.$chemicalsRow->c_cas. ' 更新成功,更新时间:'.date('Y-m-d H:i:s',time()));
return $id;
return true;
}catch(\Exception $exception){
$dbConnect->rollback();
......
<?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;
use App\Repositories\Contracts\ThirdApiPlatformRepository;
use App\Services\Kafka\KafkaService;
use App\Services\ThirdPlatform\Api\BjsApiService;
use App\Services\ThirdPlatform\PlatformAbstractService;
use App\Support\Traits\Helpers;
class BjsService extends PlatformAbstractService
{
public function __construct(ThirdApiPlatformRepository $thirdApiPlatformRepository)
{
$this->setPlatformName('bjs');
parent::__construct($thirdApiPlatformRepository);
}
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();
app(KafkaService::class)->produerSend(
[
'params' => ['cas' => current($content)],
'consumer' => __CLASS__,
'method' => 'batchUpdateApi'
]
);
$generator->next();
}
//$generator->rewind();
}
public function batchUpdateApi($params)
{
$result = app(BjsApiService::class)->pushBatchUpdateProduct($params['cas']);
if(!$result){
return ;
}
try{
$result = app(BhProductsService::class)->storeBjsProducts($result);
}catch(\Exception $exception){
app(SysLogService::class)->addApiLogs(['error' => $exception->getMessage()]);
}
}
}
<?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\Support\Traits\HttpClientHelpers;
class BjsApiService
{
use HttpClientHelpers;
protected $platformInfo = null;
public function __construct()
{
$this->platformInfo = app(ThirdApiPlatformRepository::class)->getPlatformInfoByPlatformName('bjs');
$this->platformInfo['platform_params'] = json_decode($this->platformInfo['platform_params'], true);
}
/**
* 批量更新商品信息
* @param $product
* @return bool
*/
public function pushBatchUpdateProduct($product){
if(!$this->platformInfo){
throw new \LogicException('第三方平台信息无法获取!',500);
}
try{
$params = [];
$params['CASNumber'] = $this->mergeApiMetaData($product);
$uri = $this->platformInfo['platform_url'].'/api/BJS/GetStockPriceDataThree';
$response = $this->getPostClient($uri,$params,null,[
'Client_id' => $this->platformInfo['platform_params']['Client_id'],
'Client_secret' => $this->platformInfo['platform_params']['Client_secret']
],false);
return $this->apiResponse($response);
}catch(\Exception $e){
return $this->requestError($e);
}
}
/**
* 整理组装 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;
}
}
......@@ -32,7 +32,7 @@ class ThridApiPlatformSeeder extends Seeder
'platform_status' => '1',
'platform_token' => '',
],*/
[
/*[
'platform_name' => 'bide',
'platform_title' => 'bide',
'platform_url' => 'http://220.248.71.110:9000',
......@@ -42,6 +42,17 @@ class ThridApiPlatformSeeder extends Seeder
'platform_type' => 'third',
'platform_status' => '1',
'platform_token' => '',
],*/
[
'platform_name' => 'bjs',
'platform_title' => 'bjs',
'platform_url' => 'http://220.248.71.110:9000',
'platform_desc' => 'bjs 接口',
'platform_icon' => '',
'platform_params' => '',
'platform_type' => 'third',
'platform_status' => '1',
'platform_token' => '',
],
];
......@@ -76,6 +87,12 @@ class ThridApiPlatformSeeder extends Seeder
'Client_secret' => '83643588AE356CBFF84227C178DAE900',
]);
break;
case "bjs" :
$platform['platform_params'] = json_encode([
'Client_id' => 'Picasso_bssw',
'Client_secret' => 'C35E61644B63D0C34BEC729028BBFF08',
]);
break;
}
$platform['platform_type'] = 'third';
$platform['platform_status'] = '1';
......
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