Skip to content

Commit

Permalink
Merge pull request #131 from JayWenStar/delete_devices
Browse files Browse the repository at this point in the history
⭐️: 支持删除离线的设备
  • Loading branch information
ZhouYixun authored Feb 27, 2022
2 parents 8daa4c0 + 8c46899 commit 04e7c67
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,11 @@ public RespModel<Devices> findById(@RequestParam(name = "id") int id) {
public RespModel<Integer> findTemper() {
return new RespModel<>(RespEnum.SEARCH_OK, devicesService.findTemper());
}

@WebAspect
@ApiOperation(value = "删除设备", notes = "设备必须离线才能删除,会删除设备与套件绑定关系")
@DeleteMapping()
public RespModel<String> delete(@RequestParam(name = "id") int id) {
return devicesService.delete(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,24 @@ public class Devices implements Serializable, TypeConverter<Devices, DevicesDTO>
@TableField
@Column(defaultValue = "0", comment = "设备温度")
Integer temperature;

public static Devices newDeletedDevice(int id) {
String tips = "设备已删除";
return new Devices()
.setAgentId(0)
.setStatus("DISCONNECTED")
.setPlatform(0)
.setId(id)
.setVersion("未知")
.setSize("未知")
.setCpu("未知")
.setManufacturer("未知")
.setName(tips)
.setModel(tips)
.setChiName(tips)
.setNickName(tips)
.setName(tips)
.setUser(tips)
.setUdId(tips);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import org.cloud.sonic.common.http.RespModel;
import org.cloud.sonic.controller.models.domain.Devices;
import org.cloud.sonic.controller.models.http.DeviceDetailChange;
import org.cloud.sonic.controller.models.http.UpdateDeviceImg;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -46,4 +48,6 @@ Page<Devices> findAll(List<String> iOSVersion, List<String> androidVersion, List
void refreshDevicesTemper(JSONObject jsonObject);

Integer findTemper();

RespModel<String> delete(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.cloud.sonic.common.http.RespModel;
import org.cloud.sonic.controller.mapper.DevicesMapper;
import org.cloud.sonic.controller.mapper.TestSuitesDevicesMapper;
import org.cloud.sonic.controller.models.domain.Devices;
import org.cloud.sonic.controller.models.domain.TestSuitesDevices;
import org.cloud.sonic.controller.models.domain.Users;
import org.cloud.sonic.controller.models.http.DeviceDetailChange;
import org.cloud.sonic.controller.models.http.UpdateDeviceImg;
Expand All @@ -15,12 +19,18 @@
import org.cloud.sonic.controller.services.impl.base.SonicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.cloud.sonic.common.http.RespEnum.DELETE_ERROR;
import static org.cloud.sonic.common.http.RespEnum.DELETE_OK;

/**
* @author ZhouYiXun
* @des 设备逻辑层实现
Expand All @@ -29,11 +39,10 @@
@Service
public class DevicesServiceImpl extends SonicServiceImpl<DevicesMapper, Devices> implements DevicesService {

@Autowired
private DevicesMapper devicesMapper;
@Autowired private DevicesMapper devicesMapper;

@Autowired
private UsersService usersService;
@Autowired private UsersService usersService;
@Autowired private TestSuitesDevicesMapper testSuitesDevicesMapper;

@Override
public boolean saveDetail(DeviceDetailChange deviceDetailChange) {
Expand Down Expand Up @@ -89,7 +98,19 @@ public List<Devices> findAll(int platform) {

@Override
public List<Devices> findByIdIn(List<Integer> ids) {
return lambdaQuery().in(Devices::getId, ids).list();
List<Devices> realDevices = lambdaQuery().in(Devices::getId, ids).list();
List<Devices> devices = new ArrayList<>();
for (int i = 0; i < ids.size(); i++) {
// 如果存在则直接加入,不存在则生成一个标记设备已删除的对象
if (!realDevices.isEmpty() && (realDevices.get(i) != null)
&& ids.get(i).equals(realDevices.get(i).getId())
) {
devices.add(realDevices.get(i));
} else {
devices.add(Devices.newDeletedDevice(ids.get(i)));
}
}
return devices;
}

@Override
Expand Down Expand Up @@ -220,4 +241,22 @@ public Integer findTemper() {
,DeviceStatus.DEBUGGING,DeviceStatus.TESTING));
}

@Override
@Transactional(rollbackFor = Exception.class)
public RespModel<String> delete(int id) {
Devices devices = devicesMapper.selectById(id);
if (ObjectUtils.isEmpty(devices)) {
return new RespModel<>(DELETE_ERROR, "设备已被删除过");
}
if (devices.getStatus().equals(DeviceStatus.OFFLINE) || devices.getStatus().equals(DeviceStatus.DISCONNECTED)) {
devicesMapper.deleteById(id);
testSuitesDevicesMapper.delete(
new LambdaQueryWrapper<TestSuitesDevices>().eq(TestSuitesDevices::getDevicesId, id)
);
} else {
return new RespModel<>(DELETE_ERROR, "设备不处于离线状态");
}
return new RespModel<>(DELETE_OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public JSONArray findCaseStatus(int id) {
}
List<JSONObject> device = new ArrayList<>();
for (int i = statusList.size() - 1; i >= 0; i--) {
if (statusList.get(i).getInteger("case_id") == testCases.getId()) {
if (statusList.get(i).getInteger("case_id").equals(testCases.getId())) {
JSONObject deviceIdAndStatus = new JSONObject();
deviceIdAndStatus.put("deviceId", statusList.get(i).getInteger("device_id"));
deviceIdAndStatus.put("status", statusList.get(i).getInteger("status"));
Expand Down

0 comments on commit 04e7c67

Please sign in to comment.