zxl
2026-03-25 0b39edb68acc67ed01fbfe5d31bfa776a1b17de1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.tievd.jyz.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tievd.cube.commons.base.Result;
import com.tievd.jyz.cache.ResourceCache;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.dto.IssueDTO;
import com.tievd.jyz.entity.Device;
import com.tievd.jyz.entity.Upgrade;
import com.tievd.jyz.entity.UpgradeRecord;
import com.tievd.jyz.mapper.DeviceMapper;
import com.tievd.jyz.mapper.UpgradeMapper;
import com.tievd.jyz.mapper.UpgradeRecordMapper;
import com.tievd.jyz.mqtt.command.MqttCommandReceiver;
import com.tievd.jyz.mqtt.command.UpgradeCommand;
import com.tievd.jyz.service.IUpgradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.Serializable;
import java.util.Date;
import java.util.List;
 
/**
 * Upgrade
 * @author      cube
 * @since       2023-02-27
 * @version     V2.0.0
 */
@Service
public class UpgradeServiceImpl extends ServiceImpl<UpgradeMapper, Upgrade> implements IUpgradeService {
 
    @Autowired
    private DeviceMapper deviceMapper;
 
    @Autowired
    private UpgradeRecordMapper upgradeRecordMapper;
 
    @Autowired
    private MqttCommandReceiver mqttCommandReceiver;
    @Value("${init.sendCommandTopic:/ty/aibox/command/}")
    private String sendCommandTopic;
 
    @Override
    public Result issue(IssueDTO dto) {
        Date now = new Date();
        Upgrade upgrade = baseMapper.selectById(dto.getUpgradeId());
        if (upgrade == null){
            return Result.error("该版本不存在");
        }
        List<String> deviceIds = dto.getDeviceId();
        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Device::getId, deviceIds);
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        for (Device device: devices) {
            //下发升级指令只设备
            UpgradeCommand upgradeCommand = new UpgradeCommand(mqttCommandReceiver,upgrade.getVersion(),upgrade.getPath(),sendCommandTopic,device.getSn());
            upgradeCommand.init();
            upgradeCommand.execute();
            UpgradeRecord record = new UpgradeRecord();
            record.setUpgradeId(upgrade.getId());
            record.setCreateTime(new Date());
            record.setDeviceSn(device.getSn());
            record.setStatus(SystemConstant.ISSUE_UNDERWAY);
            record.setCreateTime(now);
            record.setIssueTime(now);
            upgradeRecordMapper.insert(record);
            ResourceCache.addUpgradeRecord(record);
        }
        return Result.ok();
    }
 
    @Override
    public void remove(Serializable id) {
        baseMapper.deleteById(id);
        QueryWrapper<UpgradeRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("upgrade_id", id);
        upgradeRecordMapper.delete(wrapper);
    }
}