Explorar o código

巡检设备同步

459242451@qq.com %!s(int64=3) %!d(string=hai) anos
pai
achega
947fb8db62

+ 6 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/qdtl/TlCommonController.java

@@ -9,6 +9,7 @@ import cn.hutool.json.JSONUtil;
 import com.ruoyi.common.annotation.Log;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.common.enums.BusinessType;
 import com.ruoyi.common.utils.poi.ExcelUtil;
 import com.ruoyi.qdtl.domain.TlArea;
@@ -43,6 +44,10 @@ import java.util.stream.Collectors;
 @RequestMapping("/qdtl/common")
 public class TlCommonController extends BaseController
 {
+
+    @Autowired
+    private RedisCache redisCache;
+
     /**
      * 查询监控设备管理列表
      */
@@ -57,6 +62,7 @@ public class TlCommonController extends BaseController
         String result = "";
         if ("post".equals(method)) {
             result = HttpUtil.post(url, params);
+
         } else if ("get".equals(method)) {
             result = HttpUtil.get(url);
         }

+ 56 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/XunTask.java

@@ -0,0 +1,56 @@
+package com.ruoyi.web.controller.task;
+
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.ruoyi.qdtl.domain.TlInspectionDevice;
+import com.ruoyi.qdtl.domain.XunDevice;
+import com.ruoyi.qdtl.service.IQdCommonService;
+import com.ruoyi.qdtl.service.ITlInspectionDeviceService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+/**
+ * @Description: TODO
+ * @Author: huangcheng
+ * @Date: 2022/3/7
+ * @Version V1.0
+ */
+@Component("xunTask")
+public class XunTask {
+
+    @Autowired
+    private IQdCommonService qdCommonService;
+    @Autowired
+    private ITlInspectionDeviceService tlInspectionDeviceService;
+
+    /**
+     * 同步设备列表
+     */
+    public void syncDevice() {
+        JSONArray deviceList = qdCommonService.getDevice();
+        if (deviceList != null && deviceList.size() > 0) {
+            for (Object o : deviceList) {
+                JSONObject jsonObject = JSONUtil.parseObj(o);
+                String thirdId = jsonObject.getStr("id");
+                String name = jsonObject.getStr("name");
+                String code = jsonObject.getStr("code");
+                // 根据thirdId查询是否存在
+                TlInspectionDevice tlInspectionDevice = tlInspectionDeviceService.queryByThirdId(thirdId);
+                if (tlInspectionDevice != null) {
+                    tlInspectionDevice.setDeviceCode(code);
+                    tlInspectionDevice.setDeviceName(name);
+                    tlInspectionDeviceService.updateTlInspectionDevice(tlInspectionDevice);
+                } else {
+                    tlInspectionDevice = new TlInspectionDevice();
+                    tlInspectionDevice.setDeviceCode(code);
+                    tlInspectionDevice.setDeviceName(name);
+                    tlInspectionDevice.setThirdId(thirdId);
+                    tlInspectionDeviceService.insertTlInspectionDevice(tlInspectionDevice);
+                }
+            }
+        }
+    }
+}

+ 8 - 0
ruoyi-admin/src/main/resources/application.yml

@@ -121,3 +121,11 @@ xss:
   excludes: /system/notice
   # 匹配链接
   urlPatterns: /system/*,/monitor/*,/tool/*
+
+# 第三方接口
+third:
+  xuntoken: https://api.5ixun.com/api/v1/login # 获取token
+  xundevice: https://api.5ixun.com/api/v1/device # 设备列表
+  xuncheckpointLog: https://api.5ixun.com/api/v1/checkpointLog # 设备巡检历史
+  xuntrail: https://api.5ixun.com/api/v1/trail # 设备轨迹
+

+ 5 - 0
ruoyi-common/pom.xml

@@ -130,6 +130,11 @@
             <artifactId>hutool-all</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 14 - 0
ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java

@@ -13,6 +13,8 @@ import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
 import org.springframework.security.web.authentication.logout.LogoutFilter;
+import org.springframework.security.web.firewall.HttpFirewall;
+import org.springframework.security.web.firewall.StrictHttpFirewall;
 import org.springframework.web.filter.CorsFilter;
 import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
 import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
@@ -141,4 +143,16 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
     {
         auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
     }
+
+    /**
+     * 配置地址栏不能识别 // 的情况
+     * @return
+     */
+    @Bean
+    public HttpFirewall allowUrlEncodedSlashHttpFirewall() {
+        StrictHttpFirewall firewall = new StrictHttpFirewall();
+        //此处可添加别的规则,目前只设置 允许双 //
+        firewall.setAllowUrlEncodedDoubleSlash(true);
+        return firewall;
+    }
 }

+ 10 - 0
ruoyi-system/src/main/java/com/ruoyi/qdtl/domain/TlInspectionDevice.java

@@ -41,6 +41,8 @@ public class TlInspectionDevice extends BaseEntity
     /** 删除标志(0代表存在 2代表删除) */
     private String delFlag;
 
+    private String thirdId;
+
     public void setId(Long id) 
     {
         this.id = id;
@@ -122,4 +124,12 @@ public class TlInspectionDevice extends BaseEntity
             .append("updateTime", getUpdateTime())
             .toString();
     }
+
+    public String getThirdId() {
+        return thirdId;
+    }
+
+    public void setThirdId(String thirdId) {
+        this.thirdId = thirdId;
+    }
 }

+ 20 - 0
ruoyi-system/src/main/java/com/ruoyi/qdtl/domain/XunDevice.java

@@ -0,0 +1,20 @@
+package com.ruoyi.qdtl.domain;
+
+import lombok.Data;
+
+/**
+ * @Description: TODO
+ * @Author: huangcheng
+ * @Date: 2022/3/7
+ * @Version V1.0
+ */
+@Data
+public class XunDevice {
+    private Integer id;
+    private String name;
+    private String code;
+    private Integer areaId;
+    private Integer addCheckpoint;
+    private Integer model;
+    private String areaName;
+}

+ 3 - 0
ruoyi-system/src/main/java/com/ruoyi/qdtl/mapper/TlInspectionDeviceMapper.java

@@ -6,6 +6,7 @@ import java.util.Map;
 import com.alibaba.fastjson.JSONObject;
 import com.ruoyi.qdtl.domain.TlInspectionDevice;
 import org.apache.ibatis.annotations.MapKey;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * 巡检设备管理Mapper接口
@@ -66,4 +67,6 @@ public interface TlInspectionDeviceMapper
     int selectExist(TlInspectionDevice tlInspectionDevice);
 
     List<JSONObject> queryDeviceByArea();
+
+    TlInspectionDevice queryByThirdId(@Param("thirdId") String thirdId);
 }

+ 20 - 0
ruoyi-system/src/main/java/com/ruoyi/qdtl/service/IQdCommonService.java

@@ -0,0 +1,20 @@
+package com.ruoyi.qdtl.service;
+
+import cn.hutool.json.JSONArray;
+import com.ruoyi.qdtl.domain.XunDevice;
+
+import java.util.List;
+
+/**
+ * @Description: TODO
+ * @Author: huangcheng
+ * @Date: 2022/3/7
+ * @Version V1.0
+ */
+public interface IQdCommonService {
+
+    String getXunToken();
+
+    JSONArray getDevice();
+
+}

+ 2 - 0
ruoyi-system/src/main/java/com/ruoyi/qdtl/service/ITlInspectionDeviceService.java

@@ -63,4 +63,6 @@ public interface ITlInspectionDeviceService
     public int deleteTlInspectionDeviceById(Long id);
 
     Map<String,Integer> queryDeviceByArea();
+
+    TlInspectionDevice queryByThirdId(String thirdId);
 }

+ 82 - 0
ruoyi-system/src/main/java/com/ruoyi/qdtl/service/impl/QdCommonServiceImpl.java

@@ -0,0 +1,82 @@
+package com.ruoyi.qdtl.service.impl;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONArray;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.qdtl.domain.XunDevice;
+import com.ruoyi.qdtl.service.IQdCommonService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @Description: TODO
+ * @Author: huangcheng
+ * @Date: 2022/3/7
+ * @Version V1.0
+ */
+@Service
+@Slf4j
+public class QdCommonServiceImpl implements IQdCommonService {
+
+    @Value("${third.xuntoken}")
+    private String xuntokenUrl;
+    @Value("${third.xundevice}")
+    private String xundeviceUrl;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    /**
+     * 获取巡检接口的token
+     * @return
+     */
+    @Override
+    public String getXunToken() {
+        String token = redisCache.getCacheObject("xun:token");
+        if (StrUtil.isNotBlank(token)) {
+            return token;
+        }
+        Map<String, Object> params = new HashMap<>();
+        params.put("username", "15190837633");
+        params.put("password", "e10adc3949ba59abbe56e057f20f883e");
+        String post = HttpUtil.post(xuntokenUrl, params);
+        JSONObject postJson = JSONUtil.parseObj(post);
+        if (200 == postJson.getInt("code")) {
+            token = postJson.getStr("data");
+            // 存入缓存。暂定为6小时过期
+            redisCache.setCacheObject("xun:token", token, 6, TimeUnit.HOURS);
+        } else {
+            log.error("获取xun接口的token失败:{}",postJson);
+            throw new ServiceException("获取xun接口的token失败");
+        }
+        return token;
+    }
+
+    /**
+     * 设备列表
+     * @return
+     */
+    @Override
+    public JSONArray getDevice() {
+        String get = HttpRequest.get(xundeviceUrl).header("Authorization", this.getXunToken()).execute().body();
+        JSONObject getJson = JSONUtil.parseObj(get);
+        if (200 == getJson.getInt("code")) {
+            JSONArray data = getJson.getJSONArray("data");
+            return data;
+        }
+        return new JSONArray();
+    }
+}

+ 5 - 0
ruoyi-system/src/main/java/com/ruoyi/qdtl/service/impl/TlInspectionDeviceServiceImpl.java

@@ -139,4 +139,9 @@ public class TlInspectionDeviceServiceImpl implements ITlInspectionDeviceService
         List<JSONObject> jsonObjects = tlInspectionDeviceMapper.queryDeviceByArea();
         return jsonObjects.stream().collect(Collectors.toMap(a -> a.getString("areaId"), b -> b.getInteger("cnt")));
     }
+
+    @Override
+    public TlInspectionDevice queryByThirdId(String thirdId) {
+        return tlInspectionDeviceMapper.queryByThirdId(thirdId);
+    }
 }

+ 11 - 1
ruoyi-system/src/main/resources/mapper/qdtl/TlInspectionDeviceMapper.xml

@@ -17,10 +17,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <result property="createTime"    column="create_time"    />
         <result property="updateBy"    column="update_by"    />
         <result property="updateTime"    column="update_time"    />
+        <result property="thirdId"    column="third_id"    />
     </resultMap>
 
     <sql id="selectTlInspectionDeviceVo">
-        select id, device_code, device_name, device_type, lnglat, area_id, remark, del_flag, create_by, create_time, update_by, update_time from tl_inspection_device
+        select id, device_code, device_name, device_type, lnglat, area_id, remark, del_flag, create_by, create_time, update_by, update_time, third_id from tl_inspection_device
     </sql>
 
     <select id="selectTlInspectionDeviceList" parameterType="TlInspectionDevice" resultMap="TlInspectionDeviceResult">
@@ -55,6 +56,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         group by area_id
     </select>
 
+    <select id="queryByThirdId" resultType="com.ruoyi.qdtl.domain.TlInspectionDevice">
+        <include refid="selectTlInspectionDeviceVo"/>
+        where third_id = #{thirdId}
+        and del_flag = '0'
+    </select>
+
     <insert id="insertTlInspectionDevice" parameterType="TlInspectionDevice" useGeneratedKeys="true" keyProperty="id">
         insert into tl_inspection_device
         <trim prefix="(" suffix=")" suffixOverrides=",">
@@ -69,6 +76,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">create_time,</if>
             <if test="updateBy != null">update_by,</if>
             <if test="updateTime != null">update_time,</if>
+            <if test="thirdId != null">third_id,</if>
          </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
@@ -82,6 +90,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">#{createTime},</if>
             <if test="updateBy != null">#{updateBy},</if>
             <if test="updateTime != null">#{updateTime},</if>
+            <if test="thirdId != null">#{thirdId},</if>
          </trim>
     </insert>
 
@@ -99,6 +108,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             <if test="createTime != null">create_time = #{createTime},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
+            <if test="thirdId != null">third_id = #{thirdId},</if>
         </trim>
         where id = #{id}
     </update>

+ 6 - 1
sql/ry_20210908.sql

@@ -685,4 +685,9 @@ create table gen_table_column (
   update_by         varchar(64)     default ''                 comment '更新者',
   update_time       datetime                                   comment '更新时间',
   primary key (column_id)
-) engine=innodb auto_increment=1 comment = '代码生成业务表字段';
+) engine=innodb auto_increment=1 comment = '代码生成业务表字段';
+
+
+alter table tl_inspection_device
+    add third_id varchar(20) null comment '第三方id';
+