package com.xy.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xy.dto.DeviceEventMsgDto;
import com.xy.dto.DeviceInfoDto;
import com.xy.entity.DeviceEventMsg;
import com.xy.entity.DeviceInfo;
import com.xy.entity.SysCodeConfigureRedis;
import com.xy.mapper.DeviceEventMsgMapper;
import com.xy.utils.*;
import com.xy.utils.enums.SysCodeConfigureEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
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.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.xy.utils.Beans.copy;
import static com.xy.utils.PlusBeans.toIPage;
import static com.xy.utils.PlusBeans.toPageBean;
/**
*
* 机器-消息表 服务实现类
*
*
* @author lijin
* @since 2023-01-09
*/
@Service
@Api(tags = "机器-消息表")
@AllArgsConstructor(onConstructor_ = @Lazy)
public class DeviceEventMsgServiceImpl extends ServiceImpl implements DeviceEventMsgService {
private DeviceInfoServiceImpl deviceInfoService;
@Override
@ApiOperation("添加")
public R save(DeviceEventMsgDto.Save save) {
DeviceInfoDto.Vo deviceInfo = deviceInfoService.obj(new DeviceInfoDto.Obj().setDeviceId(save.getDeviceId()).setIsSysinfo(true)).getData();
if (deviceInfo == null) {
return R.fail("设备不存在");
}
//查询消息编码信息
String paterCode = save.getCode().substring(0, save.getCode().length() - 1);
SysCodeConfigureRedis sysCodeConfigureRedis = SysCodeConfigureUtils.get(paterCode, save.getCode());
//添加消息记录
save.setMercId(deviceInfo.getMercId());
DeviceEventMsg deviceEventMsg = copy(DeviceEventMsg.class, save)
.setCreateTime(LocalDateTime.now());
save(deviceEventMsg);
//异常消息,修改设备当前故障等级
if (paterCode.equals(SysCodeConfigureEnum.D01.getCode())) {
if (Emptys.check(sysCodeConfigureRedis.getExpand())) {
DeviceInfo updateDeviceInfo = new DeviceInfo()
.setDeviceId(deviceInfo.getDeviceId())
.setFaultLevel(Integer.valueOf(sysCodeConfigureRedis.getExpand()));
deviceInfoService.updateById(updateDeviceInfo);
}
}
return R.ok();
}
@Override
@ApiOperation("分页查询")
public R> page(@RequestBody DeviceEventMsgDto.Page page) {
List codes = getCodes(page.getCode());
PageBean pageBean = page.getPage();
LambdaQueryWrapper lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(page, DeviceEventMsg.class)
.ge(DeviceEventMsg::getCreateTime, page.getBeginCreateTime())
.le(DeviceEventMsg::getCreateTime, page.getEndCreateTime())
.in(DeviceEventMsg::getCode, codes)
.build()
.orderByDesc(!Emptys.check(pageBean.getOrders()), DeviceEventMsg::getId);
IPage iPage = page(toIPage(pageBean), lambdaQueryWrapper);
PageBean voPageBean = toPageBean(DeviceEventMsgDto.Vo.class, iPage);
//反显设备名称
if (Emptys.check(voPageBean.getRecords())) {
List deviceIdList = voPageBean.getRecords().stream().map(DeviceEventMsgDto.Vo::getDeviceId).collect(Collectors.toList());
Map deviceMap = deviceInfoService.getDeviceNameList(new DeviceInfoDto.DeviceIdDto().setDeviceId(deviceIdList)).getData();
Map stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.get(SysCodeConfigureEnum.D01.getCode());
voPageBean.getRecords().forEach(i -> {
i.setDeviceName(deviceMap.get(i.getDeviceId()));
SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(i.getCode());
if (sysCodeConfigureRedis != null) {
i.setSysCodeConfigureRedis(sysCodeConfigureRedis);
}
});
}
return R.ok(voPageBean);
}
@PostMapping("list")
@ApiOperation("集合查询")
public R> list(@RequestBody DeviceEventMsgDto.SelectList selectList) {
List codes = getCodes(selectList.getCode());
LambdaQueryWrapper lambdaQueryWrapper = new MybatisPlusQuery().eqWrapper(selectList, DeviceEventMsg.class)
.in(DeviceEventMsg::getId, selectList.getIds())
.in(DeviceEventMsg::getDeviceId, selectList.getDeviceIds())
.in(DeviceEventMsg::getCode, codes)
.build();
List list = list(lambdaQueryWrapper);
Map stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.get(SysCodeConfigureEnum.D01.getCode());
List vos = copy(DeviceEventMsgDto.Vo.class, list);
vos.forEach(vo -> {
SysCodeConfigureRedis sysCodeConfigureRedis = stringSysCodeConfigureRedisMap.get(vo.getCode());
if (sysCodeConfigureRedis != null) {
vo.setSysCodeConfigureRedis(sysCodeConfigureRedis);
}
});
return R.ok(vos);
}
private List getCodes(String code) {
List codes = new ArrayList<>();
if (Emptys.check(code)) {
Map stringSysCodeConfigureRedisMap = SysCodeConfigureUtils.get(code);
if (Emptys.check(stringSysCodeConfigureRedisMap)) {
stringSysCodeConfigureRedisMap.forEach((code2, sysCodeConfigureRedis) -> codes.add(code2));
} else {
codes.add(code);
}
}
return codes;
}
}