InOutOptionService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. public function setOrderNumber($data){
  12. if(! empty($data['data']['tagList'])){
  13. $in = $out = [];
  14. $device_id = $data['data']['id'] ?? 0; //设备id
  15. $site = $data['data']['devMark'] ?? ""; //站点
  16. foreach ($data['data']['tagList'] as $value){
  17. if($value['direction'] == '1'){
  18. //(判断到有发货出库的入库标识 存入缓存)
  19. if(Redis::exists($device_id . self::Key)){
  20. Redis::lpush($device_id . self::Key . self::OrderKeyQueueInOut, json_encode($value));
  21. }else{
  22. //入库 直接生成入库单
  23. if(! in_array($value['epc'],$in)) $in[] = $value['epc'];
  24. }
  25. }elseif ($value['direction'] == '2'){
  26. //(判断到有发货出库的出库标识 存入缓存)
  27. if(Redis::exists($device_id . self::Key)){
  28. Redis::lpush($device_id . self::Key . self::OrderKeyQueueInOut, json_encode($value));
  29. }else{
  30. //出库 直接生成出库单
  31. if(! in_array($value['epc'],$out)) $out[] = $value['epc'];
  32. }
  33. }
  34. }
  35. if(! empty($in)) dispatch(new DoorDeviceJob($in,1,$site,$device_id))->onQueue('door_device');
  36. if(! empty($out)) dispatch(new DoorDeviceJob($out,2,$site,$device_id))->onQueue('door_device');
  37. }
  38. }
  39. public function getOrderNumber($data,$site){
  40. //获取某个站点的数据
  41. $tmp = [];
  42. $result = [];
  43. //发货出库
  44. $key = $data['device_id'] . self::Key . self::OrderKeyQueueInOut;
  45. if(Redis::exists($key)) {
  46. //发货
  47. while ($item = Redis::lpop($key)) {
  48. $order = json_decode($item, true);
  49. $order_it = $order['epc'] . $order['direction'];
  50. if(! in_array($order_it, $tmp)){
  51. $str = @hex2bin($order['epc']);
  52. $str = ltrim($str, "\x00");
  53. if(! empty($str) && substr($str, 0, 2) === "BZ"){
  54. $order['epc'] = $str;
  55. $order['site'] = $site;
  56. $result[] = $order;
  57. }
  58. }
  59. }
  60. return [true, $result];
  61. }
  62. //入库
  63. $key = $data['device_id'] . self::OrderKeyQueueIn;
  64. $res = Redis::get($key);
  65. if(! empty($res)){
  66. $return = json_decode($res,true);
  67. foreach ($return as $value){
  68. $result[] = [
  69. 'epc' => $value,
  70. 'direction' => '1',
  71. ];
  72. }
  73. Redis::del($key);
  74. return [true, $result];
  75. }
  76. //发货
  77. $key = $data['device_id'] . self::OrderKeyQueueOut;
  78. $res = Redis::get($key);
  79. if(! empty($res)){
  80. $return = json_decode($res,true);
  81. foreach ($return as $value){
  82. $result[] = [
  83. 'epc' => $value,
  84. 'direction' => '2',
  85. ];
  86. }
  87. Redis::del($key);
  88. return [true, $result];
  89. }
  90. return [true, $result];
  91. }
  92. //设置为发货出库
  93. public function setFhMessage($data,$site){
  94. $key = $data['device_id'] . self::Key;
  95. if(empty($data['fh'])){
  96. //通道门处在出入库屏 删除通道门在发货出库屏下的标识(如果存在)
  97. if(Redis::exists($key)) Redis::del($key);
  98. }else{
  99. //通道门处在发货出库屏 设置55秒过期 前端50秒请求一次 更新标识时间
  100. if (Redis::setnx($key, 1)) Redis::expire($key, 55); //多少秒后过期
  101. }
  102. }
  103. }