Browse Source

设备异常处理

LuoDLeo 2 months ago
parent
commit
fd78b2cadc
17 changed files with 174 additions and 118 deletions
  1. 19 0
      src/main/java/org/springblade/common/enums/VargoWayStatusEnum.java
  2. 26 18
      src/main/java/org/springblade/modules/api/controller/ApiVendingController.java
  3. 2 0
      src/main/java/org/springblade/modules/api/mapper/VendingOrderMapper.xml
  4. 1 1
      src/main/java/org/springblade/modules/api/request/VendingShipReq.java
  5. 0 51
      src/main/java/org/springblade/modules/business/entity/FacilityFault.java
  6. 11 0
      src/main/java/org/springblade/modules/business/entity/FacilityLog.java
  7. 0 8
      src/main/java/org/springblade/modules/business/mapper/FacilityFaultMapper.java
  8. 6 0
      src/main/java/org/springblade/modules/business/mapper/FacilityLogMapper.java
  9. 5 1
      src/main/java/org/springblade/modules/business/mapper/FacilityLogMapper.xml
  10. 14 10
      src/main/java/org/springblade/modules/business/service/IFacilityCargoWayService.java
  11. 0 8
      src/main/java/org/springblade/modules/business/service/IFacilityFaultService.java
  12. 5 0
      src/main/java/org/springblade/modules/business/service/IFacilityLogService.java
  13. 19 7
      src/main/java/org/springblade/modules/business/service/impl/FacilityCargoWayServiceImpl.java
  14. 0 12
      src/main/java/org/springblade/modules/business/service/impl/FacilityFaultServiceImpl.java
  15. 8 1
      src/main/java/org/springblade/modules/business/service/impl/FacilityLogServiceImpl.java
  16. 2 1
      src/main/java/org/springblade/modules/business/service/impl/FacilityServiceImpl.java
  17. 56 0
      src/main/java/org/springblade/modules/quartz/task/VendingTimeTask.java

+ 19 - 0
src/main/java/org/springblade/common/enums/VargoWayStatusEnum.java

@@ -0,0 +1,19 @@
+package org.springblade.common.enums;
+
+import lombok.Getter;
+
+/**
+ * 货道状态
+ */
+@Getter
+public enum VargoWayStatusEnum {
+    NORMAL("正常", 1),
+    FAULT("故障", 2);
+    private String name;
+    private Integer value;
+
+    VargoWayStatusEnum(String name, Integer value) {
+        this.name = name;
+        this.value = value;
+    }
+}

+ 26 - 18
src/main/java/org/springblade/modules/api/controller/ApiVendingController.java

@@ -13,6 +13,7 @@ import org.springblade.common.aspect.VendingLog;
 import org.springblade.common.constant.CommonConstant;
 import org.springblade.common.enums.FacilityStatusEnum;
 import org.springblade.common.enums.OrderStateEnum;
+import org.springblade.common.enums.VargoWayStatusEnum;
 import org.springblade.common.enums.VendingLogEnum;
 import org.springblade.modules.api.request.VendingHeartReq;
 import org.springblade.modules.api.request.VendingOrderReq;
@@ -21,9 +22,7 @@ import org.springblade.modules.api.response.VendingOrderGoodsRes;
 import org.springblade.modules.api.response.VendingOrderRes;
 import org.springblade.modules.api.service.VendingOrderService;
 import org.springblade.modules.business.entity.Facility;
-import org.springblade.modules.business.entity.FacilityFault;
-import org.springblade.modules.business.enums.FacilityFaultStatusEnum;
-import org.springblade.modules.business.service.IFacilityFaultService;
+import org.springblade.modules.business.service.IFacilityCargoWayService;
 import org.springblade.modules.business.service.IFacilityService;
 import org.springblade.modules.finance.entity.Order;
 import org.springblade.modules.finance.entity.OrderGoods;
@@ -58,7 +57,7 @@ public class ApiVendingController {
     private final IOrderGoodsService orderGoodsService;
     private final IOrderService orderService;
     private final IFacilityService facilityService;
-    private final IFacilityFaultService facilityFaultService;
+    private final IFacilityCargoWayService facilityCargoWayService;
 
 
     @VendingLog(VendingLogEnum.ORDER)
@@ -169,6 +168,10 @@ public class ApiVendingController {
                     .set(OrderGoods::getShipment, 0)
                     .eq(OrderGoods::getId, req.getOrderGoodsId())
                     .update();
+
+            //将货道改为异常,该货道不可购买
+            facilityCargoWayService.updateCargoWayStatus(req.getMachineId(), req.getChannel(), VargoWayStatusEnum.FAULT);
+
         }
 
     }
@@ -186,14 +189,23 @@ public class ApiVendingController {
         facility.setHeartbeatTime(now);
         if ("0".equals(req.getErrorNum()) || "OK".equals(req.getStatus())) {
             facility.setFacilityStatus(FacilityStatusEnum.ON_LINE.getValue());
+
+            //将异常货道恢复
+            facilityCargoWayService.updateCargoWayStatus(String.valueOf(facility.getId()), null, VargoWayStatusEnum.NORMAL);
+
+            //位置校验
+            //"location": "N,22.97637,E,113.37804"
+            String[] split = req.getLocation().split(",");
+
+            //TODO 2024/9/26 LuoDLeo 位置过大,发短信提醒
+            //double distance = DistanceUtils.getDistance(Double.parseDouble(facility.getLongitude()), Double.parseDouble(facility.getLatitude()), Double.parseDouble(split[3]), Double.parseDouble(split[1]));
+
+            //更新位置
+            facility.setLatitude(split[1]);//纬度N
+            facility.setLongitude(split[3]);//经度E
+
             facilityService.updateById(facility);
 
-            FacilityFault facilityFault = new FacilityFault();
-            facilityFault.setFacilityId(req.getMachineId());
-            facilityFault.setFaultDesc(FacilityFaultStatusEnum.getDesc("0"));
-            facilityFault.setFaultStatus("0");
-            facilityFault.setCreateTime(now);
-            facilityFaultService.save(facilityFault);
             return;
         } else {
             //设备故障
@@ -203,16 +215,12 @@ public class ApiVendingController {
             //录入故障信息
             String[] split = req.getErrorArray().replace("{", "").replace("}", "").split(",");
 
-            List<FacilityFault> facilityFaults = new ArrayList<>();
             for (String status : split) {
-                FacilityFault facilityFault = new FacilityFault();
-                facilityFault.setFacilityId(req.getMachineId());
-                facilityFault.setFaultDesc(FacilityFaultStatusEnum.getDesc(status));
-                facilityFault.setFaultStatus(status);
-                facilityFault.setCreateTime(now);
-                facilityFaults.add(facilityFault);
+                //判断是不是货道异常,是的话将该货道设置为不可购买
+                if (status.startsWith("10")) {
+                    facilityCargoWayService.updateCargoWayStatus(String.valueOf(facility.getId()), status.substring(2), VargoWayStatusEnum.FAULT);
+                }
             }
-            facilityFaultService.saveBatch(facilityFaults);
         }
     }
 

+ 2 - 0
src/main/java/org/springblade/modules/api/mapper/VendingOrderMapper.xml

@@ -6,7 +6,9 @@
         select a.*
         from t_order_goods a
                  left join t_order b on a.order_id = b.id
+                 left join t_facility_cargo_way c on a.cargo_way_id = c.id
         where b.order_state = 2
+          and c.cargo_status = 1
           and a.device_id = #{param.machineId}
         order by b.create_time asc
     </select>

+ 1 - 1
src/main/java/org/springblade/modules/api/request/VendingShipReq.java

@@ -22,7 +22,7 @@ public class VendingShipReq {
     @NotBlank(message = "订单号不能为空")
     private String saleSn;
 
-    @ApiModelProperty(value = "出货的货道编号")
+    @ApiModelProperty(value = "orderGoodsId")
     @NotBlank(message = "出货的货道编号不能为空")
     private String orderGoodsId;
 

+ 0 - 51
src/main/java/org/springblade/modules/business/entity/FacilityFault.java

@@ -1,51 +0,0 @@
-package org.springblade.modules.business.entity;
-
-import com.baomidou.mybatisplus.annotation.*;
-import com.fasterxml.jackson.annotation.JsonFormat;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
-import io.swagger.annotations.ApiModel;
-import io.swagger.annotations.ApiModelProperty;
-import lombok.Data;
-
-import java.util.Date;
-
-/**
- * 设备故障信息
- */
-@Data
-@TableName("t_facility_fault")
-@ApiModel(value = "FacilityFault对象", description = "设备故障表")
-public class FacilityFault {
-
-    private static final long serialVersionUID = 1L;
-
-    @JsonSerialize(using = ToStringSerializer.class)
-    @TableId(value = "id", type = IdType.ASSIGN_ID)
-    @ApiModelProperty(value = "主键")
-    protected Long id;
-
-    /**
-     * 设备id
-     */
-    @ApiModelProperty(value = "设备id")
-    private String facilityId;
-
-    /**
-     * 故障描述
-     */
-    @ApiModelProperty(value = "故障描述")
-    private String faultDesc;
-
-    /**
-     * 故障状态
-     */
-    @ApiModelProperty(value = "故障状态")
-    private String faultStatus;
-
-    @TableField(value = "create_time", fill = FieldFill.INSERT)
-    @ApiModelProperty(value = "创建时间", hidden = true)
-    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
-    protected Date createTime;
-
-}

+ 11 - 0
src/main/java/org/springblade/modules/business/entity/FacilityLog.java

@@ -1,7 +1,9 @@
 package org.springblade.modules.business.entity;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
 import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import org.springblade.core.log.model.LogAbstract;
@@ -15,11 +17,20 @@ public class FacilityLog extends LogAbstract {
     /**
      * 接口名
      */
+    @ApiModelProperty(value = "接口名")
     private String title;
 
     /**
      * 耗时
      */
+    @ApiModelProperty(value = "耗时")
     private String time;
 
+    /**
+     * 设备id
+     */
+    @ApiModelProperty(value = "设备id")
+    @TableField(exist = false)
+    private Long machineId;
+
 }

+ 0 - 8
src/main/java/org/springblade/modules/business/mapper/FacilityFaultMapper.java

@@ -1,8 +0,0 @@
-package org.springblade.modules.business.mapper;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import org.springblade.modules.business.entity.FacilityFault;
-
-public interface FacilityFaultMapper extends BaseMapper<FacilityFault> {
-
-}

+ 6 - 0
src/main/java/org/springblade/modules/business/mapper/FacilityLogMapper.java

@@ -1,8 +1,14 @@
 package org.springblade.modules.business.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import org.springblade.modules.business.entity.FacilityLog;
 
+import java.util.Date;
+import java.util.List;
+
 public interface FacilityLogMapper  extends BaseMapper<FacilityLog> {
 
+    List<FacilityLog> getFacilityLog(IPage<FacilityLog> page, Date now);
+
 }

+ 5 - 1
src/main/java/org/springblade/modules/business/mapper/FacilityFaultMapper.xml → src/main/java/org/springblade/modules/business/mapper/FacilityLogMapper.xml

@@ -1,5 +1,9 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-<mapper namespace="org.springblade.modules.business.mapper.FacilityFaultMapper">
+<mapper namespace="org.springblade.modules.business.mapper.FacilityLogMapper">
+
+    <select id="getFacilityLog" resultType="org.springblade.modules.business.entity.FacilityLog">
+
+    </select>
 
 </mapper>

+ 14 - 10
src/main/java/org/springblade/modules/business/service/IFacilityCargoWayService.java

@@ -16,10 +16,11 @@
  */
 package org.springblade.modules.business.service;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.service.IService;
+import org.springblade.common.enums.VargoWayStatusEnum;
 import org.springblade.modules.business.entity.FacilityCargoWay;
 import org.springblade.modules.business.vo.FacilityCargoWayVO;
-import com.baomidou.mybatisplus.extension.service.IService;
-import com.baomidou.mybatisplus.core.metadata.IPage;
 
 /**
  * 售货机货表 服务类
@@ -29,13 +30,16 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
  */
 public interface IFacilityCargoWayService extends IService<FacilityCargoWay> {
 
-	/**
-	 * 自定义分页
-	 *
-	 * @param page
-	 * @param facilityCargoWay
-	 * @return
-	 */
-	IPage<FacilityCargoWayVO> selectFacilityCargoWayPage(IPage<FacilityCargoWayVO> page, FacilityCargoWayVO facilityCargoWay);
+    /**
+     * 自定义分页
+     *
+     * @param page
+     * @param facilityCargoWay
+     * @return
+     */
+    IPage<FacilityCargoWayVO> selectFacilityCargoWayPage(IPage<FacilityCargoWayVO> page, FacilityCargoWayVO facilityCargoWay);
+
+
+    void updateCargoWayStatus(String facilityId, String cargoNo, VargoWayStatusEnum statusEnum);
 
 }

+ 0 - 8
src/main/java/org/springblade/modules/business/service/IFacilityFaultService.java

@@ -1,8 +0,0 @@
-package org.springblade.modules.business.service;
-
-import com.baomidou.mybatisplus.extension.service.IService;
-import org.springblade.modules.business.entity.FacilityFault;
-
-public interface IFacilityFaultService  extends IService<FacilityFault> {
-
-}

+ 5 - 0
src/main/java/org/springblade/modules/business/service/IFacilityLogService.java

@@ -1,8 +1,13 @@
 package org.springblade.modules.business.service;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.IService;
 import org.springblade.modules.business.entity.FacilityLog;
 
+import java.util.Date;
+
 public interface IFacilityLogService extends IService<FacilityLog> {
 
+    public IPage<FacilityLog> getFacilityLog(IPage<FacilityLog> page, Date now);
+
 }

+ 19 - 7
src/main/java/org/springblade/modules/business/service/impl/FacilityCargoWayServiceImpl.java

@@ -16,13 +16,15 @@
  */
 package org.springblade.modules.business.service.impl;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springblade.common.enums.VargoWayStatusEnum;
+import org.springblade.common.utils.StringUtils;
 import org.springblade.modules.business.entity.FacilityCargoWay;
-import org.springblade.modules.business.vo.FacilityCargoWayVO;
 import org.springblade.modules.business.mapper.FacilityCargoWayMapper;
 import org.springblade.modules.business.service.IFacilityCargoWayService;
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springblade.modules.business.vo.FacilityCargoWayVO;
 import org.springframework.stereotype.Service;
-import com.baomidou.mybatisplus.core.metadata.IPage;
 
 /**
  * 售货机货表 服务实现类
@@ -33,9 +35,19 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
 @Service
 public class FacilityCargoWayServiceImpl extends ServiceImpl<FacilityCargoWayMapper, FacilityCargoWay> implements IFacilityCargoWayService {
 
-	@Override
-	public IPage<FacilityCargoWayVO> selectFacilityCargoWayPage(IPage<FacilityCargoWayVO> page, FacilityCargoWayVO facilityCargoWay) {
-		return page.setRecords(baseMapper.selectFacilityCargoWayPage(page, facilityCargoWay));
-	}
+    @Override
+    public IPage<FacilityCargoWayVO> selectFacilityCargoWayPage(IPage<FacilityCargoWayVO> page, FacilityCargoWayVO facilityCargoWay) {
+        return page.setRecords(baseMapper.selectFacilityCargoWayPage(page, facilityCargoWay));
+    }
+
+    @Override
+    public void updateCargoWayStatus(String facilityId, String cargoNo, VargoWayStatusEnum statusEnum) {
+        this.lambdaUpdate()
+                .set(FacilityCargoWay::getCargoStatus, statusEnum.getValue())
+                .eq(FacilityCargoWay::getFacilityId, facilityId)
+                .eq(StringUtils.isNoneBlank(cargoNo), FacilityCargoWay::getCargoNo, cargoNo)
+                .eq(StringUtils.isBlank(cargoNo), FacilityCargoWay::getCargoStatus, VargoWayStatusEnum.FAULT.getValue())
+                .update();
+    }
 
 }

+ 0 - 12
src/main/java/org/springblade/modules/business/service/impl/FacilityFaultServiceImpl.java

@@ -1,12 +0,0 @@
-package org.springblade.modules.business.service.impl;
-
-import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import org.springblade.modules.business.entity.FacilityFault;
-import org.springblade.modules.business.mapper.FacilityFaultMapper;
-import org.springblade.modules.business.service.IFacilityFaultService;
-import org.springframework.stereotype.Service;
-
-@Service
-public class FacilityFaultServiceImpl extends ServiceImpl<FacilityFaultMapper, FacilityFault> implements IFacilityFaultService {
-
-}

+ 8 - 1
src/main/java/org/springblade/modules/business/service/impl/FacilityLogServiceImpl.java

@@ -1,12 +1,19 @@
 package org.springblade.modules.business.service.impl;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springblade.modules.business.entity.FacilityLog;
 import org.springblade.modules.business.mapper.FacilityLogMapper;
 import org.springblade.modules.business.service.IFacilityLogService;
 import org.springframework.stereotype.Service;
 
+import java.util.Date;
+
 @Service
-public class FacilityLogServiceImpl extends ServiceImpl<FacilityLogMapper, FacilityLog>  implements IFacilityLogService {
+public class FacilityLogServiceImpl extends ServiceImpl<FacilityLogMapper, FacilityLog> implements IFacilityLogService {
+
+    public IPage<FacilityLog> getFacilityLog(IPage<FacilityLog> page, Date now) {
+        return page.setRecords(baseMapper.getFacilityLog(page,now));
+    }
 
 }

+ 2 - 1
src/main/java/org/springblade/modules/business/service/impl/FacilityServiceImpl.java

@@ -19,6 +19,7 @@ package org.springblade.modules.business.service.impl;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springblade.common.enums.StatusEnum;
+import org.springblade.common.enums.VargoWayStatusEnum;
 import org.springblade.core.log.exception.ServiceException;
 import org.springblade.modules.business.entity.Facility;
 import org.springblade.modules.business.entity.FacilityCargoWay;
@@ -85,7 +86,7 @@ public class FacilityServiceImpl extends ServiceImpl<FacilityMapper, Facility> i
 			facilityCargoWay.setFacilityId(facility.getId());
 			facilityCargoWay.setCargoNo(i + "");
 			facilityCargoWay.setVolume(facilityModel.getChannelCapacity());
-			facilityCargoWay.setCargoStatus(StatusEnum.ENABLE.getValue());
+			facilityCargoWay.setCargoStatus(VargoWayStatusEnum.NORMAL.getValue());
 			facilityCargoWay.setEnable(StatusEnum.ENABLE.getValue());
 			facilityCargoWay.setDiscount(0.0);
 			list.add(facilityCargoWay);

+ 56 - 0
src/main/java/org/springblade/modules/quartz/task/VendingTimeTask.java

@@ -0,0 +1,56 @@
+package org.springblade.modules.quartz.task;
+
+import lombok.extern.slf4j.Slf4j;
+import org.redisson.api.RLock;
+import org.redisson.api.RedissonClient;
+import org.springblade.common.enums.FacilityStatusEnum;
+import org.springblade.modules.business.entity.Facility;
+import org.springblade.modules.business.service.IFacilityLogService;
+import org.springblade.modules.business.service.IFacilityService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.Calendar;
+import java.util.concurrent.TimeUnit;
+
+//设备心跳信息处理
+@Slf4j
+@Component
+public class VendingTimeTask {
+
+    @Resource
+    private RedissonClient redissonClient;
+    @Autowired
+    private IFacilityLogService facilityLogService;
+    @Autowired
+    private IFacilityService facilityService;
+
+    /**
+     * 离线校验
+     */
+    public void onlineTesting() {
+        RLock lock = redissonClient.getLock("vending:online:testing");
+        lock.lock(10, TimeUnit.SECONDS);
+        try {
+            //查询设备心跳,超过6分钟未发心跳则判断为离线(设备心跳为5分钟)
+            Calendar instance = Calendar.getInstance();
+            instance.set(Calendar.MINUTE, -6);
+
+            facilityService.lambdaUpdate()
+                    .lt(Facility::getHeartbeatTime, instance.getTime())
+                    .eq(Facility::getFacilityStatus, FacilityStatusEnum.ON_LINE.getValue())
+                    .set(Facility::getFacilityStatus, FacilityStatusEnum.OFF_LINE.getValue())
+                    .update();
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error(e.getMessage(), e);
+        } finally {
+            if (lock.isLocked()) {
+                lock.unlock();
+            }
+        }
+    }
+
+}