InOutOptionService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\DoorDeviceJob;
  4. use Illuminate\Support\Facades\Redis;
  5. class InOutOptionService extends Service
  6. {
  7. const OrderKeyQueueIn = 'InOrderNumber';
  8. const OrderKeyQueueOut = 'OutOrderNumber';
  9. const OrderKeyQueueInOut = 'InOutOrderNumber';
  10. const Key = 'FHCK';
  11. //通道门传来的数据 正常出入库 直接生成单据 (放入队列生成)
  12. //发货出库数据 出库以后存到缓存 前端自取 发货出库会有可能这次不发了 所以也有入回来
  13. public function setOrderNumber($data){
  14. if(! empty($data['data']['tagList'])){
  15. $in = $out = [];
  16. $device_id = $data['data']['id'] ?? 0; //设备id
  17. $site = $data['data']['devMark'] ?? ""; //站点
  18. foreach ($data['data']['tagList'] as $value){
  19. if($value['direction'] == '1'){
  20. //(判断到有发货出库的入库标识 存入缓存)
  21. if(Redis::exists($device_id . self::Key)){
  22. Redis::lpush($device_id . self::Key . self::OrderKeyQueueInOut, json_encode($value));
  23. }else{
  24. //入库 直接生成入库单
  25. if(! in_array($value['epc'],$in)) $in[] = $value['epc'];
  26. }
  27. }elseif ($value['direction'] == '2'){
  28. //(判断到有发货出库的出库标识 存入缓存)
  29. if(Redis::exists($device_id . self::Key)){
  30. Redis::lpush($device_id . self::Key . self::OrderKeyQueueInOut, json_encode($value));
  31. }else{
  32. //出库 直接生成出库单
  33. if(! in_array($value['epc'],$out)) $out[] = $value['epc'];
  34. }
  35. }
  36. }
  37. if(! empty($in)) dispatch(new DoorDeviceJob($in,1,$site,$device_id))->onQueue('door_device');
  38. if(! empty($out)) dispatch(new DoorDeviceJob($out,2,$site,$device_id))->onQueue('door_device');
  39. }
  40. }
  41. //获取单号 包括成功的出入库数据(生成单据成功 返回成功的包装单号) 和 发货出库(包装单号)
  42. public function getOrderNumber($data,$site){
  43. //获取某个站点的数据
  44. $tmp = [];
  45. $result = [];
  46. //发货出库
  47. $key = $data['device_id'] . self::Key . self::OrderKeyQueueInOut;
  48. if(Redis::exists($key)) {
  49. //发货
  50. while ($item = Redis::lpop($key)) {
  51. $order = json_decode($item, true);
  52. $order_it = $order['epc'] . $order['direction'];
  53. if(! in_array($order_it, $tmp)){
  54. $str = @hex2bin($order['epc']);
  55. $str = ltrim($str, "\x00");
  56. $str = str_replace(chr(26), '', $str); //过滤CRTL-Z字符
  57. if(! empty($str) && substr($str, 0, 2) === "BZ"){
  58. $order['epc'] = $str;
  59. $order['site'] = $site;
  60. $result[] = $order;
  61. }
  62. }
  63. }
  64. return [true, $result];
  65. }
  66. //入库
  67. $key = $data['device_id'] . self::OrderKeyQueueIn;
  68. $res = Redis::get($key);
  69. if(! empty($res)){
  70. $return = json_decode($res,true);
  71. foreach ($return as $value){
  72. $str = str_replace(chr(26), '', $value); //过滤CRTL-Z字符
  73. $result[] = [
  74. 'epc' => $str,
  75. 'direction' => '1',
  76. ];
  77. }
  78. Redis::del($key);
  79. return [true, $result];
  80. }
  81. //发货
  82. $key = $data['device_id'] . self::OrderKeyQueueOut;
  83. $res = Redis::get($key);
  84. if(! empty($res)){
  85. $return = json_decode($res,true);
  86. foreach ($return as $value){
  87. $result[] = [
  88. 'epc' => $value,
  89. 'direction' => '2',
  90. ];
  91. }
  92. Redis::del($key);
  93. return [true, $result];
  94. }
  95. return [true, $result];
  96. }
  97. //设置通道门是 正常出入库 还是 发货出库
  98. public function setFhMessage($data,$site){
  99. $key = $data['device_id'] . self::Key;
  100. if(empty($data['fh'])){
  101. //通道门处在出入库屏 删除通道门在发货出库屏下的标识(如果存在)
  102. if(Redis::exists($key)) Redis::del($key);
  103. }else{
  104. //通道门处在发货出库屏 设置55秒过期 前端50秒请求一次 更新标识时间
  105. if (Redis::setnx($key, 1)) {
  106. Redis::expire($key, 55); //多少秒后过期
  107. }else{
  108. Redis::expire($key, 55); //多少秒后过期
  109. }
  110. }
  111. }
  112. }