|
@@ -1,5 +1,8 @@
|
|
|
package com.xy.service;
|
|
|
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
@@ -7,12 +10,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.xy.collections.list.JArrayList;
|
|
|
import com.xy.collections.list.JList;
|
|
|
import com.xy.collections.map.JMap;
|
|
|
+import com.xy.dto.CommandMqtt;
|
|
|
import com.xy.dto.DeviceQualityDto;
|
|
|
import com.xy.dto.DeviceQualityValueDto;
|
|
|
+import com.xy.dto.PaterDto;
|
|
|
import com.xy.entity.DeviceInfo;
|
|
|
import com.xy.entity.DeviceQuality;
|
|
|
import com.xy.entity.DeviceQualityValue;
|
|
|
import com.xy.entity.SysDictRedis;
|
|
|
+import com.xy.error.CommRuntimeException;
|
|
|
import com.xy.mapper.DeviceInfoMapper;
|
|
|
import com.xy.mapper.DeviceQualityMapper;
|
|
|
import com.xy.utils.*;
|
|
@@ -22,13 +28,14 @@ import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
import static com.xy.utils.PlusBeans.*;
|
|
|
|
|
@@ -49,6 +56,8 @@ public class DeviceQualityServiceImpl extends ServiceImpl<DeviceQualityMapper, D
|
|
|
|
|
|
private DeviceQualityValueServiceImpl deviceQualityValueService;
|
|
|
|
|
|
+ private MqttService mqttService;
|
|
|
+
|
|
|
private void queryDeviceQualityValue(List<DeviceQualityDto.Vo> list) {
|
|
|
if (Emptys.check(list)) {
|
|
|
JList<Integer> qualityIds = new JArrayList<>(list).getProperty(DeviceQualityDto.Vo::getQualityId);
|
|
@@ -90,7 +99,25 @@ public class DeviceQualityServiceImpl extends ServiceImpl<DeviceQualityMapper, D
|
|
|
List<DeviceQuality> deviceQualities = list(lambdaQueryWrapper);
|
|
|
List<DeviceQualityDto.Vo> list = copy(DeviceQualityDto.Vo.class, deviceQualities);
|
|
|
queryDeviceQualityValue(list);
|
|
|
- return R.ok(copy(DeviceQualityDto.Vo.class, deviceQualities));
|
|
|
+ return R.ok(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ @PostMapping("obj")
|
|
|
+ @ApiOperation("对象查询")
|
|
|
+ public R<DeviceQualityDto.Vo> obj(@RequestBody DeviceQualityDto.Obj obj) {
|
|
|
+ LambdaQueryWrapper<DeviceQuality> lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(obj, DeviceQuality.class).build();
|
|
|
+ List<DeviceQuality> deviceQualities = list(lambdaQueryWrapper);
|
|
|
+ List<DeviceQualityDto.Vo> list = copy(DeviceQualityDto.Vo.class, deviceQualities);
|
|
|
+ if (!Emptys.check(list)) {
|
|
|
+ if (Emptys.check(obj.getDeviceId())) {
|
|
|
+ save(Arrays.asList(new DeviceQualityDto.Save().setDeviceId(obj.getDeviceId())));
|
|
|
+ return obj(obj);
|
|
|
+ }
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+ DeviceQualityDto.Vo vo = copy(DeviceQualityDto.Vo.class, list.get(0));
|
|
|
+ queryDeviceQualityValue(Arrays.asList(vo));
|
|
|
+ return R.ok(vo);
|
|
|
}
|
|
|
|
|
|
@PostMapping("update")
|
|
@@ -114,6 +141,10 @@ public class DeviceQualityServiceImpl extends ServiceImpl<DeviceQualityMapper, D
|
|
|
@PostMapping("rest")
|
|
|
@ApiOperation("重置某项质检")
|
|
|
public R rest(@RequestBody DeviceQualityDto.Rest rest) {
|
|
|
+ SysDictRedis sysDictRedis = SysDictUtils.get(DeviceEnum.MQTT_CMD_TEMPLET_QUALITY.getKey(), rest.getType());
|
|
|
+ if (!Emptys.check(sysDictRedis)) {
|
|
|
+ return R.fail("未配置" + rest.getType() + "指令模板");
|
|
|
+ }
|
|
|
SysDictRedis sysDictRedis1 = SysDictUtils.get(DeviceEnum.DEVICE_QUALITY_STATUS.getKey(), DeviceSonEnum.DEVICE_QUALITY_STATUS_QUALIFIED.getKey());
|
|
|
SysDictRedis sysDictRedis2 = SysDictUtils.get(DeviceEnum.DEVICE_FACTORY_STATUS.getKey(), DeviceSonEnum.DEVICE_FACTORY_STATUS_CAN_SHIPPED.getKey());
|
|
|
DeviceQuality deviceQuality = getById(rest.getQualityId());
|
|
@@ -122,8 +153,8 @@ public class DeviceQualityServiceImpl extends ServiceImpl<DeviceQualityMapper, D
|
|
|
}
|
|
|
//修改质检
|
|
|
UpdateWrapper<DeviceQuality> updateWrapper = new UpdateWrapper<DeviceQuality>()
|
|
|
- .set(rest.getType() + "_begin_time", LocalDateTime.now())
|
|
|
- .set(rest.getType() + "_end_time", null)
|
|
|
+ .set(rest.getType() + "_state_begin_time", LocalDateTime.now())
|
|
|
+ .set(rest.getType() + "_state_end_time", null)
|
|
|
.eq(StringTools.humpToLine(LambdaUtils.getProperty(DeviceQuality::getQualityId)), rest.getQualityId());
|
|
|
update(updateWrapper);
|
|
|
//修改质检属性
|
|
@@ -131,29 +162,42 @@ public class DeviceQualityServiceImpl extends ServiceImpl<DeviceQualityMapper, D
|
|
|
.setType(rest.getType());
|
|
|
update.setQualityId(rest.getQualityId());
|
|
|
deviceQualityValueService.update(update);
|
|
|
+ //发送mqtt指令
|
|
|
+ JSONObject obj;
|
|
|
+ try {
|
|
|
+ obj = JSONUtil.parseObj(sysDictRedis.getValue());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+ CommandMqtt commandMqtt = new CommandMqtt()
|
|
|
+ .setDeviceId(deviceQuality.getDeviceId())
|
|
|
+ .setTemplet(obj.toString());
|
|
|
+ mqttService.senCommand(Arrays.asList(commandMqtt));
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
@PostMapping("save")
|
|
|
@ApiOperation("添加")
|
|
|
- @Transactional(rollbackFor = Exception.class)
|
|
|
- public R save(@RequestBody List<DeviceQualityDto.Vo> vos) {
|
|
|
+ public R save(@RequestBody List<DeviceQualityDto.Save> deviceQualitySaves) {
|
|
|
List<DeviceQualityDto.Vo> updates = new ArrayList<>();
|
|
|
List<DeviceQuality> saves = new ArrayList<>();
|
|
|
- JList<Long> deviceIds = new JArrayList<>(vos).getProperty(DeviceQualityDto.Vo::getDeviceId);
|
|
|
+ JList<Long> deviceIds = new JArrayList<>(deviceQualitySaves).getProperty(DeviceQualityDto.Save::getDeviceId);
|
|
|
//获取设备信息
|
|
|
List<DeviceInfo> deviceInfos = deviceInfoMapper.selectList(new LambdaQueryWrapper<DeviceInfo>().in(DeviceInfo::getDeviceId, deviceIds));
|
|
|
JMap<Long, DeviceInfo> deviceInfoJMap = new JArrayList<>(deviceInfos).toMap(DeviceInfo::getDeviceId).cover();
|
|
|
//获取已有质检信息
|
|
|
- List<DeviceQuality> deviceQualities = list(new LambdaQueryWrapper<DeviceQuality>().in(DeviceQuality::getDeviceId, deviceIds));
|
|
|
+ List<DeviceQuality> deviceQualities = list(new LambdaQueryWrapper<DeviceQuality>()
|
|
|
+ .in(DeviceQuality::getDeviceId, deviceIds)
|
|
|
+ .eq(DeviceQuality::getShowStatus, true)
|
|
|
+ );
|
|
|
JMap<Long, List<DeviceQuality>> deviceQualitieJMap = new JArrayList<>(deviceQualities).toMap(DeviceQuality::getDeviceId).group();
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
//封装修改和添加对象
|
|
|
- for (DeviceQualityDto.Vo save : vos) {
|
|
|
+ for (DeviceQualityDto.Save save : deviceQualitySaves) {
|
|
|
DeviceQuality deviceQuality = new DeviceQuality().setDeviceId(save.getDeviceId());
|
|
|
DeviceInfo deviceInfo = deviceInfoJMap.get(deviceQuality.getDeviceId());
|
|
|
if (deviceInfo == null) {
|
|
|
- return R.fail(save.getDeviceId() + "设备不存在");
|
|
|
+ throw new CommRuntimeException(save.getDeviceId() + "设备不存在");
|
|
|
}
|
|
|
deviceQuality.setDeviceType(deviceInfo.getDeviceType())
|
|
|
.setCreateTime(now)
|
|
@@ -182,6 +226,35 @@ public class DeviceQualityServiceImpl extends ServiceImpl<DeviceQualityMapper, D
|
|
|
List<DeviceQualityValueDto.Vo> deviceQualityValueDtoVos = new ArrayList<>(saves.size());
|
|
|
saves.forEach(deviceQuality -> deviceQualityValueDtoVos.add(new DeviceQualityValueDto.Vo().setQualityId(deviceQuality.getQualityId())));
|
|
|
deviceQualityValueService.save(deviceQualityValueDtoVos);
|
|
|
+ //发送mqtt指令
|
|
|
+ Map<String, SysDictRedis> sysDictRedisMap = SysDictUtils.get(DeviceEnum.MQTT_CMD_TEMPLET_QUALITY.getKey());
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+ String wkSn = LambdaUtils.getProperty(PaterDto::getWkSn);
|
|
|
+ String actionType = LambdaUtils.getProperty(PaterDto::getActionType);
|
|
|
+ String cmdType = LambdaUtils.getProperty(PaterDto::getCmdType);
|
|
|
+ String data = LambdaUtils.getProperty(PaterDto::getData);
|
|
|
+ sysDictRedisMap.forEach((s, sysDictRedis) -> {
|
|
|
+ JSONObject obj;
|
|
|
+ try {
|
|
|
+ obj = JSONUtil.parseObj(sysDictRedis.getValue());
|
|
|
+ } catch (Exception e) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ jsonObject.set(wkSn, obj.get(wkSn))
|
|
|
+ .set(cmdType, obj.get(cmdType))
|
|
|
+ .set(actionType, obj.get(actionType));
|
|
|
+ jsonArray.add(obj.get(data));
|
|
|
+ });
|
|
|
+ jsonObject.set(data, jsonArray.toString());
|
|
|
+ List<CommandMqtt> commandMqtts = new ArrayList<>(saves.size());
|
|
|
+ saves.forEach(deviceQuality -> {
|
|
|
+ CommandMqtt commandMqtt = new CommandMqtt()
|
|
|
+ .setDeviceId(deviceQuality.getDeviceId())
|
|
|
+ .setTemplet(jsonObject.toString());
|
|
|
+ commandMqtts.add(commandMqtt);
|
|
|
+ });
|
|
|
+ mqttService.senCommand(commandMqtts);
|
|
|
return R.ok();
|
|
|
}
|
|
|
|