InOutOptionService.php 3.9 KB

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