package com.ycl.task; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.github.pagehelper.PageInfo; import com.ycl.platform.domain.entity.DeviceInfo; import com.ycl.platform.mapper.DeviceInfoMapper; import com.ycl.platform.wvp.Device; import com.ycl.platform.wvp.WVPResult; import com.ycl.utils.http.HttpUtils; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * "国标设备"同步 * * @author:xp * @date:2024/8/21 15:18 */ @RequiredArgsConstructor @Component("deviceSynTask") public class DeviceSynTask { private final DeviceInfoMapper deviceInfoMapper; @Value("${rtsp.server:http://127.0.0.1:7788}") private String rtspServer; public void run() { String result = HttpUtils.sendGet(rtspServer + "/api/device/query/devices?page=1&count=15"); WVPResult wvpResult = JSON.parseObject(result, WVPResult.class); if (0 == wvpResult.getCode()) { JSONObject data = (JSONObject) wvpResult.getData(); List list = data.getList("list", Device.class); if (! CollectionUtils.isEmpty(list)) { list.stream().forEach(item -> { DeviceInfo device = new LambdaQueryChainWrapper<>(deviceInfoMapper) .eq(DeviceInfo::getDeviceId, item.getDeviceId()) .one(); if (Objects.isNull(device)) { DeviceInfo deviceInfo = new DeviceInfo(); deviceInfo.setDeviceId(item.getDeviceId()); deviceInfo.setDeviceName(item.getName()); Date now = new Date(); deviceInfo.setCreateTime(now); deviceInfo.setUpdateTime(now); deviceInfo.setDeleted(0); deviceInfoMapper.insert(deviceInfo); } else { device.setDeviceName(item.getName()); deviceInfoMapper.updateById(device); } }); } } } }