123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace App\Service;
- use App\Jobs\DoorDeviceJob;
- use Illuminate\Support\Facades\Redis;
- class InOutOptionService extends Service
- {
- const OrderKeyQueueIn = 'InOrderNumber';
- const OrderKeyQueueOut = 'OutOrderNumber';
- const OrderKeyQueueInOut = 'InOutOrderNumber';
- const Key = 'FHCK';
- //通道门传来的数据 正常出入库 直接生成单据 (放入队列生成)
- //发货出库数据 出库以后存到缓存 前端自取 发货出库会有可能这次不发了 所以也有入回来
- public function setOrderNumber($data){
- if(! empty($data['data']['tagList'])){
- $in = $out = [];
- $device_id = $data['data']['id'] ?? 0; //设备id
- $site = $data['data']['devMark'] ?? ""; //站点
- foreach ($data['data']['tagList'] as $value){
- if($value['direction'] == '1'){
- //(判断到有发货出库的入库标识 存入缓存)
- if(Redis::exists($device_id . self::Key)){
- Redis::lpush($device_id . self::Key . self::OrderKeyQueueInOut, json_encode($value));
- }else{
- //入库 直接生成入库单
- if(! in_array($value['epc'],$in)) $in[] = $value['epc'];
- }
- }elseif ($value['direction'] == '2'){
- //(判断到有发货出库的出库标识 存入缓存)
- if(Redis::exists($device_id . self::Key)){
- Redis::lpush($device_id . self::Key . self::OrderKeyQueueInOut, json_encode($value));
- }else{
- //出库 直接生成出库单
- if(! in_array($value['epc'],$out)) $out[] = $value['epc'];
- }
- }
- }
- if(! empty($in)) dispatch(new DoorDeviceJob($in,1,$site,$device_id))->onQueue('door_device');
- if(! empty($out)) dispatch(new DoorDeviceJob($out,2,$site,$device_id))->onQueue('door_device');
- }
- }
- //获取单号 包括成功的出入库数据(生成单据成功 返回成功的包装单号) 和 发货出库(包装单号)
- public function getOrderNumber($data,$site){
- //获取某个站点的数据
- $tmp = [];
- $result = [];
- //发货出库
- $key = $data['device_id'] . self::Key . self::OrderKeyQueueInOut;
- if(Redis::exists($key)) {
- //发货
- while ($item = Redis::lpop($key)) {
- $order = json_decode($item, true);
- $order_it = $order['epc'] . $order['direction'];
- if(! in_array($order_it, $tmp)){
- $str = @hex2bin($order['epc']);
- $str = ltrim($str, "\x00");
- $str = str_replace(chr(26), '', $str); //过滤CRTL-Z字符
- if(! empty($str) && substr($str, 0, 2) === "BZ"){
- $order['epc'] = $str;
- $order['site'] = $site;
- $result[] = $order;
- }
- }
- }
- return [true, $result];
- }
- //入库
- $key = $data['device_id'] . self::OrderKeyQueueIn;
- $res = Redis::get($key);
- if(! empty($res)){
- $return = json_decode($res,true);
- foreach ($return as $value){
- $str = str_replace(chr(26), '', $value); //过滤CRTL-Z字符
- $result[] = [
- 'epc' => $str,
- 'direction' => '1',
- ];
- }
- Redis::del($key);
- return [true, $result];
- }
- //发货
- $key = $data['device_id'] . self::OrderKeyQueueOut;
- $res = Redis::get($key);
- if(! empty($res)){
- $return = json_decode($res,true);
- foreach ($return as $value){
- $result[] = [
- 'epc' => $value,
- 'direction' => '2',
- ];
- }
- Redis::del($key);
- return [true, $result];
- }
- return [true, $result];
- }
- //设置通道门是 正常出入库 还是 发货出库
- public function setFhMessage($data,$site){
- $key = $data['device_id'] . self::Key;
- if(empty($data['fh'])){
- //通道门处在出入库屏 删除通道门在发货出库屏下的标识(如果存在)
- if(Redis::exists($key)) Redis::del($key);
- }else{
- //通道门处在发货出库屏 设置55秒过期 前端50秒请求一次 更新标识时间
- if (Redis::setnx($key, 1)) {
- Redis::expire($key, 55); //多少秒后过期
- }else{
- Redis::expire($key, 55); //多少秒后过期
- }
- }
- }
- }
|