|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Service;
|
|
|
|
|
|
+use App\Jobs\ProcessDataJob;
|
|
|
use App\Model\BasicType;
|
|
|
use App\Model\Depart;
|
|
|
use App\Model\Employee;
|
|
@@ -17,6 +18,7 @@ use App\Model\Role;
|
|
|
use App\Model\RoleMenuButton;
|
|
|
use App\Model\SeeRange;
|
|
|
use App\Model\Storehouse;
|
|
|
+use App\Model\U8Job;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
/**
|
|
@@ -1311,25 +1313,108 @@ class ProductService extends Service
|
|
|
}
|
|
|
|
|
|
public function updateTopStock($data, $user){
|
|
|
- $service = new U8ServerService(true);
|
|
|
- if(! empty($service->error)) return [false, $service->error];
|
|
|
-
|
|
|
$limit_key = "updateTopStockFromYy";
|
|
|
- list($status, $msg) = $this->limitingSendRequestBackgExpire($limit_key, 20);
|
|
|
- if(! $status) return [false, '正在获取T9杭州总仓库存货库存,请勿重复操作'];
|
|
|
+// list($status, $msg) = $this->limitingSendRequestBackg($limit_key);
|
|
|
+// if(! $status) return [false, '正在获取T9杭州总仓库存货库存,请勿重复操作'];
|
|
|
+
|
|
|
+ //总店
|
|
|
+ $head = $user['head']['id'] ?? 0;
|
|
|
+ $storehouse_id = Storehouse::where('top_depart_id', $head)->where('del_time',0)->value('id') ?? 0;
|
|
|
+
|
|
|
+ $time = time();
|
|
|
+ DB::table('product')
|
|
|
+ ->where('del_time', 0)
|
|
|
+ ->where('top_depart_id', $head)
|
|
|
+ ->select('id','code')
|
|
|
+ ->orderBy('id')
|
|
|
+ ->chunk(200, function ($data) use($head,$storehouse_id,$time){
|
|
|
+ $data = Collect($data)->map(function ($object) {
|
|
|
+ return (array)$object;
|
|
|
+ })->toArray();
|
|
|
+
|
|
|
+ $map = array_column($data,'id','code');
|
|
|
+ $id = array_unique(array_column($data,'id'));
|
|
|
+ $code = array_unique(array_column($data,'code'));
|
|
|
+ $post = [
|
|
|
+ 'urlFromT9' => 'getStock',
|
|
|
+ 'code' => $code,
|
|
|
+ 'warehouse' => '001',
|
|
|
+ ];
|
|
|
+ $post = json_encode($post);
|
|
|
+ $header = ['Content-Type:application/json'];
|
|
|
+ list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
|
|
|
+ if($msg['code'] != 200) return [false, $msg['msg']];
|
|
|
+ $msg = $msg['data'] ?? [];
|
|
|
+
|
|
|
+ $insert = [];
|
|
|
+ foreach ($msg as $value){
|
|
|
+ $insert[] = [
|
|
|
+ 'product_id' => $map[$value['product_no']] ?? 0,
|
|
|
+ 'storehouse_id' => $storehouse_id,
|
|
|
+ 'top_depart_id' => $head,
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'crt_time' => $time,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ ProductInventoryOfTop::where('del_time',0)
|
|
|
+ ->whereIn('product_id',$id)
|
|
|
+ ->update(['del_time' => $time]);
|
|
|
+
|
|
|
+ if(! empty($insert)) ProductInventoryOfTop::insert($insert);
|
|
|
+ });
|
|
|
+
|
|
|
+ return [true, ''];
|
|
|
+
|
|
|
+ try{
|
|
|
+ $job = ProcessDataJob::dispatch(['head' => $head, 'storehouse_id' => $storehouse_id, 'type' => U8Job::three,'limit_key' => $limit_key])->onQueue(U8Job::job3);
|
|
|
+ if(! $job) {
|
|
|
+ $this->dellimitingSendRequestBackg($limit_key);
|
|
|
+ return [false,'T9杭州总仓库存货库存任务没有进入队列!'];
|
|
|
+ }
|
|
|
+ }catch (\Throwable $e){
|
|
|
+ $this->dellimitingSendRequestBackg($limit_key);
|
|
|
+ return [false,$e->getMessage()];
|
|
|
+ }
|
|
|
+
|
|
|
+ return [true, 'T9杭州总仓库存货库存任务正在后台执行,请等待'];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function returnStock($stock, $value){
|
|
|
+ if(isset($stock[$value['id']])){
|
|
|
+ $number = $stock[$value['id']];
|
|
|
+ if($number <= 0){
|
|
|
+ $stock = "售罄";
|
|
|
+ } elseif($number <= 15){
|
|
|
+ $stock = " 库存紧张";
|
|
|
+ }else{
|
|
|
+ $stock = " 库存充足";
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ $stock = "联系客服";
|
|
|
+ }
|
|
|
|
|
|
+ return $stock;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function writeStock($data){
|
|
|
try {
|
|
|
+ $head = $data['head'];
|
|
|
+ $storehouse_id = $data['storehouse_id'];
|
|
|
+ $limit_key = $data['limit_key'];
|
|
|
+
|
|
|
DB::enableQueryLog();
|
|
|
- //总店
|
|
|
- $head = $user['head']['id'] ?? 0;
|
|
|
- $storehouse_id = Storehouse::where('top_depart_id', $head)->where('del_time',0)->value('id') ?? 0;
|
|
|
+
|
|
|
+ $service = new U8ServerService(true);
|
|
|
+ if(! empty($service->error)) return [false, $service->error];
|
|
|
+
|
|
|
$time = time();
|
|
|
DB::table('product')
|
|
|
->where('del_time', 0)
|
|
|
->where('top_depart_id', $head)
|
|
|
->select('id','code')
|
|
|
->orderBy('id')
|
|
|
- ->chunk(200, function ($data) use($service,$head,$storehouse_id,$time){
|
|
|
+ ->chunk(200, function ($data) use($head,$storehouse_id,$time){
|
|
|
$data = Collect($data)->map(function ($object) {
|
|
|
return (array)$object;
|
|
|
})->toArray();
|
|
@@ -1337,8 +1422,16 @@ class ProductService extends Service
|
|
|
$map = array_column($data,'id','code');
|
|
|
$id = array_unique(array_column($data,'id'));
|
|
|
$code = array_unique(array_column($data,'code'));
|
|
|
- list($status, $msg) = $service->getStock($code, "001");
|
|
|
- if(! $status) return [false, $msg];
|
|
|
+ $post = [
|
|
|
+ 'urlFromT9' => 'getStock',
|
|
|
+ 'code' => $code,
|
|
|
+ 'warehouse' => '001',
|
|
|
+ ];
|
|
|
+ $post = json_encode($post);
|
|
|
+ $header = ['Content-Type:application/json'];
|
|
|
+ list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
|
|
|
+ if($msg['code'] != 200) return [false, $msg['msg']];
|
|
|
+ $msg = $msg['data'] ?? [];
|
|
|
|
|
|
$insert = [];
|
|
|
foreach ($msg as $value){
|
|
@@ -1355,32 +1448,45 @@ class ProductService extends Service
|
|
|
->whereIn('product_id',$id)
|
|
|
->update(['del_time' => $time]);
|
|
|
|
|
|
- ProductInventoryOfTop::insert($insert);
|
|
|
+ if(! empty($insert)) ProductInventoryOfTop::insert($insert);
|
|
|
});
|
|
|
|
|
|
DB::commit();
|
|
|
+ $this->dellimitingSendRequestBackg($limit_key);
|
|
|
}catch (\Throwable $exception){
|
|
|
DB::rollBack();
|
|
|
+ $this->dellimitingSendRequestBackg($limit_key);
|
|
|
return [false, $exception->getMessage()];
|
|
|
}
|
|
|
|
|
|
return [true, ''];
|
|
|
}
|
|
|
|
|
|
- private function returnStock($stock, $value){
|
|
|
- if(isset($stock[$value['id']])){
|
|
|
- $number = $stock[$value['id']];
|
|
|
- if($number <= 0){
|
|
|
- $stock = "售罄";
|
|
|
- } elseif($number <= 15){
|
|
|
- $stock = " 库存紧张";
|
|
|
- }else{
|
|
|
- $stock = " 库存充足";
|
|
|
- }
|
|
|
- }else{
|
|
|
- $stock = "联系客服";
|
|
|
+ public function post_helper($url, $data, $header = [], $timeout = 20){
|
|
|
+ $ch = curl_init();
|
|
|
+ curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
+ curl_setopt($ch, CURLOPT_ENCODING, '');
|
|
|
+ curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
|
|
|
+
|
|
|
+ if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
+ $r = curl_exec($ch);
|
|
|
+
|
|
|
+ if ($r === false) {
|
|
|
+ // 获取错误号
|
|
|
+ $errorNumber = curl_errno($ch);
|
|
|
+ // 获取错误信息
|
|
|
+ $errorMessage = curl_error($ch);
|
|
|
+ $message = "cURL Error #{$errorNumber}: {$errorMessage}";
|
|
|
+
|
|
|
+ return [false, $message];
|
|
|
}
|
|
|
+ curl_close($ch);
|
|
|
|
|
|
- return $stock;
|
|
|
+ return [true, json_decode($r, true)];
|
|
|
}
|
|
|
}
|