|
@@ -0,0 +1,249 @@
|
|
|
+package com.fjhx.rabbitmq;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.fjhx.business.service.*;
|
|
|
+import com.fjhx.constant.ApiConstant;
|
|
|
+import com.fjhx.entity.rfid.QrCode;
|
|
|
+import com.fjhx.entity.rfid.business.*;
|
|
|
+import com.fjhx.rfid.r2000.common.service.IRFIDService;
|
|
|
+import com.fjhx.utils.DateUtils;
|
|
|
+import com.fjhx.utils.HttpUtils;
|
|
|
+import com.fjhx.utils.MapUtil;
|
|
|
+import com.google.common.collect.Interner;
|
|
|
+import com.google.common.collect.Interners;
|
|
|
+import com.rabbitmq.client.Channel;
|
|
|
+import org.springframework.amqp.core.Message;
|
|
|
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
|
|
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
+import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.retry.annotation.Backoff;
|
|
|
+import org.springframework.retry.annotation.EnableRetry;
|
|
|
+import org.springframework.retry.annotation.Retryable;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * rabbitmq队列监听工具
|
|
|
+ */
|
|
|
+@EnableRetry
|
|
|
+@Component
|
|
|
+public class RabbitMQListener implements ChannelAwareMessageListener {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IRFIDService irfidService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IRfidHasauService iRfidHasauService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IRfidNoauService iRfidNoauService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IProductionLineRouteService iProductionLineRouteService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IWorkService iWorkService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IQrCodeService qrCodeService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IWorkFollowService iWorkFollowService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ITaskStepService taskStepService;
|
|
|
+
|
|
|
+ //弱引用
|
|
|
+ private static Interner<String> lock = Interners.newWeakInterner();
|
|
|
+
|
|
|
+ @RabbitHandler
|
|
|
+ @RabbitListener(
|
|
|
+ queues = {
|
|
|
+ "rfid_to_sd_queue"
|
|
|
+ },
|
|
|
+ concurrency = "8"
|
|
|
+ )
|
|
|
+ @Retryable(value = {Exception.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000, multiplier = 1))
|
|
|
+ @Override
|
|
|
+ public void onMessage(Message message, Channel channel) throws Exception {
|
|
|
+ try {
|
|
|
+ String msg = new String(message.getBody(), "UTF-8");
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(msg);
|
|
|
+ rfid(jsonObject);
|
|
|
+ System.err.println(jsonObject.toJSONString());
|
|
|
+ // 手动签收消息,通知mq服务器端删除该消息
|
|
|
+ channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ // 丢弃该消息
|
|
|
+ channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
|
|
|
+ throw e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void rfid(JSONObject jsonObject){
|
|
|
+ System.out.println(System.currentTimeMillis());
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ list.add("1");
|
|
|
+ list.add("2");
|
|
|
+ list.add("3");
|
|
|
+ //如果返回数据存在
|
|
|
+ if(jsonObject.containsKey("rfidMsg")) {
|
|
|
+ String antno = jsonObject.getJSONObject("rfidMsg").get("ANTNO").toString();
|
|
|
+ String ip = jsonObject.getJSONObject("rfidMsg").get("IP").toString();
|
|
|
+ String rfid = jsonObject.getJSONObject("rfidMsg").get("RFID").toString();
|
|
|
+ synchronized (lock.intern(rfid)) {
|
|
|
+ if(StringUtils.equals("16",antno)){
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ map.put("rfid",rfid);
|
|
|
+ map.put("antno",antno);
|
|
|
+ HttpUtils.sendPost(ApiConstant.PUSH_ANTNO, JSON.toJSONString(map));
|
|
|
+ List<ProductionLineRoute> productionLineRoutes = iProductionLineRouteService.getWorkmanshipId(ip, antno);
|
|
|
+ //工序肯定只有一条
|
|
|
+ if (productionLineRoutes.size() == 1) {
|
|
|
+ //取出工序数据
|
|
|
+ ProductionLineRoute route = productionLineRoutes.get(0);
|
|
|
+ //判断当前rfid是否已经绑定过工单
|
|
|
+ long workCount = iWorkService.count(Wrappers.<Work>query().
|
|
|
+ lambda()
|
|
|
+ .eq(Work::getRfid, rfid)
|
|
|
+ .ne(Work::getStatus, 30));
|
|
|
+ if (workCount ==1) {
|
|
|
+ //取出这条工单
|
|
|
+ Work work = iWorkService.getOne(Wrappers.<Work>query().
|
|
|
+ lambda()
|
|
|
+ .eq(Work::getRfid, rfid)
|
|
|
+ .ne(Work::getStatus, 30)
|
|
|
+ .last("limit 1"));
|
|
|
+ if (ObjectUtil.isNotEmpty(work)){
|
|
|
+ //判断当前这条工单工序是否存在
|
|
|
+ long count = iWorkFollowService.count(Wrappers.<WorkFollow>query().lambda().
|
|
|
+ eq(WorkFollow::getWorkId,work.getId()).
|
|
|
+ eq(WorkFollow::getWorkmanshipId,route.getWorkmanshipId()));
|
|
|
+ //不存在
|
|
|
+ if(count==0){
|
|
|
+ //添加工序流程
|
|
|
+ iWorkFollowService.saveFollow(work.getId(), route.getWorkmanshipId());
|
|
|
+ //添加任务流程
|
|
|
+ taskStepService.saveTaskStep(work.getTaskId(), route.getWorkmanshipId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ //取出工序ID
|
|
|
+ List<ProductionLineRoute> productionLineRoutes = iProductionLineRouteService.getWorkmanshipId(ip, antno);
|
|
|
+ //工序肯定只有一条
|
|
|
+ if (productionLineRoutes.size() == 1) {
|
|
|
+ //取出工序数据
|
|
|
+ ProductionLineRoute route = productionLineRoutes.get(0);
|
|
|
+ List<String> geways = Arrays.asList(route.getRfidPassageway().split(","));
|
|
|
+ //判断当前通道是不是第一道工序
|
|
|
+ if (list.contains(antno)) {
|
|
|
+ //判断当前rfid是否已经绑定过工单
|
|
|
+ long workCount = iWorkService.count(Wrappers.<Work>query().
|
|
|
+ lambda()
|
|
|
+ .eq(Work::getRfid, rfid)
|
|
|
+ .ne(Work::getStatus, 30));
|
|
|
+ if (workCount == 0) {
|
|
|
+ //取出最新的那一条二维码工单
|
|
|
+ QrCode qrCode = qrCodeService.getOne(Wrappers.<QrCode>query().
|
|
|
+ lambda().eq(QrCode::getVeId, 1).
|
|
|
+ orderByDesc(QrCode::getCreateTime).
|
|
|
+ last("limit 1"));
|
|
|
+// String[] code = qrCode.getQrcode().split("-");
|
|
|
+ if (ObjectUtil.isNotEmpty(qrCode)) {
|
|
|
+ //绑定工单
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ map.put("rfid",rfid);
|
|
|
+ map.put("qrCode",qrCode.getQrcode());
|
|
|
+ map.put("workShipId",route.getWorkmanshipId());
|
|
|
+ HttpUtils.sendPost(ApiConstant.BIND_WORK, JSON.toJSONString(map));
|
|
|
+// //取出这条工单
|
|
|
+// Work work = iWorkService.getOne(Wrappers.<Work>query().
|
|
|
+// lambda()
|
|
|
+// .eq(Work::getTaskId, code[0])
|
|
|
+// .eq(Work::getRfid, "0")
|
|
|
+// .eq(Work::getUuid,"0")
|
|
|
+// .orderByAsc(Work::getCode)
|
|
|
+// .ne(Work::getStatus, 30)
|
|
|
+// .last("limit 1"));
|
|
|
+// if (ObjectUtil.isNotEmpty(work) && StringUtils.equals(work.getRfid(),"0")&& StringUtils.equals(work.getUuid(),"0")) {
|
|
|
+// //绑定rfid
|
|
|
+// work.setRfid(rfid);
|
|
|
+// work.setUuid(code[1]);
|
|
|
+// work.setPutTime(DateUtils.getTime());
|
|
|
+// work.setStatus(10);//把状态改成生产中
|
|
|
+// iWorkService.updateById(work);
|
|
|
+// //添加工序流程
|
|
|
+// iWorkFollowService.saveFollow(work.getId(), route.getWorkmanshipId());
|
|
|
+// //添加任务流程
|
|
|
+// taskStepService.saveTaskStep(work.getTaskId(), route.getWorkmanshipId());
|
|
|
+// }
|
|
|
+ //清理当前这条数据之前得qrcode
|
|
|
+ qrCodeService.remove(Wrappers.<QrCode>query().lambda().
|
|
|
+ eq(QrCode::getVeId, 1).le(QrCode::getCreateTime, qrCode.getCreateTime()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {//其他通道的数据
|
|
|
+ //判断当前rfid是否绑定了工单
|
|
|
+ List<Work> workList = iWorkService.getWorkList(rfid);
|
|
|
+ if (workList.size() == 1) {//有绑定工单
|
|
|
+ Work work = workList.get(0);
|
|
|
+ //判断数据库是否存在这条通道得数据,不是:添加数据 是:直接略过
|
|
|
+ long count = iRfidHasauService.count(Wrappers.<RfidHasau>query().lambda().
|
|
|
+ in(RfidHasau::getAntno, geways).eq(RfidHasau::getIp, ip).
|
|
|
+ eq(RfidHasau::getDelFlag, 0)
|
|
|
+ .eq(RfidHasau::getRfid, rfid));
|
|
|
+ if (count == 0) {
|
|
|
+ List<String> workAntno = Arrays.asList(work.getAntnos().split(","));
|
|
|
+ if(!workAntno.containsAll(geways)){
|
|
|
+ // //判断当前这条工单工序是否存在
|
|
|
+// long workFollowCount = iWorkFollowService.count(Wrappers.<WorkFollow>query().lambda().
|
|
|
+// eq(WorkFollow::getWorkId,work.getId()).
|
|
|
+// eq(WorkFollow::getWorkmanshipId,route.getWorkmanshipId()));
|
|
|
+// //不存在
|
|
|
+// if(workFollowCount==0){
|
|
|
+ RfidHasau hasau = new RfidHasau();
|
|
|
+ hasau.setCreateTime(new Date());
|
|
|
+ hasau.setIp(ip);
|
|
|
+ hasau.setRfid(rfid);
|
|
|
+ hasau.setAntno(antno);
|
|
|
+ hasau.setWorkmanshipId(route.getWorkmanshipId());
|
|
|
+ hasau.setContractId(work.getContractId());
|
|
|
+ hasau.setContractProductId(work.getTaskId());
|
|
|
+ hasau.setBomColorId(work.getBomColorId());
|
|
|
+ hasau.setProductLineId(work.getProcessLineId());
|
|
|
+ hasau.setProductColorId(work.getProductColorId());
|
|
|
+ hasau.setWorkId(work.getId());
|
|
|
+ iRfidHasauService.save(hasau);
|
|
|
+ //处理work
|
|
|
+ if(workAntno.size()!=1){
|
|
|
+ geways.addAll(workAntno);
|
|
|
+ }
|
|
|
+ work.setAntnos(String.join(",", geways));
|
|
|
+ iWorkService.updateById(work);
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|