package com.zksy.manhole.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zksy.base.domain.EquipmentStatus;
import com.zksy.common.exception.ServiceException;
import com.zksy.manhole.domain.BaseDevicesManhole;
import com.zksy.manhole.domain.EquipmentStatusManhole;
import com.zksy.manhole.mapper.BaseDevicesManholeMapper;
import com.zksy.manhole.mapper.EquipmentStatusManholeMapper;
import com.zksy.manhole.service.BaseDevicesManholeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author Administrator
* @description 针对表【base_devices(设备基础信息)】的数据库操作Service实现
* @createDate 2025-03-13 11:50:13
*/
@Service
public class BaseDevicesManholeServiceImpl extends ServiceImpl<BaseDevicesManholeMapper, BaseDevicesManhole> implements BaseDevicesManholeService {

    @Autowired
    private BaseDevicesManholeMapper baseDevicesBaseDevicesMapper;

    @Autowired
    private EquipmentStatusManholeMapper equipmentStatusBaseDevicesMapper;

    @Override
    public void getByDeviceNumberStatus(String deviceNumber,Integer queryStatus,Integer updateStatus) {
        LambdaQueryWrapper<BaseDevicesManhole> wrapper = new LambdaQueryWrapper<>();
        //设备编号
        wrapper.eq(BaseDevicesManhole::getEquipmentCode, deviceNumber);
        //根据设备编号获取到设备id
        BaseDevicesManhole device = baseDevicesBaseDevicesMapper.selectOne(wrapper);
        if(device==null){
            throw new ServiceException("设备不存在");
        }
        //在线状态，从状态表中获取
        String deviceEquipmentId = device.getEquipmentId();
        LambdaQueryWrapper<EquipmentStatusManhole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentStatusManhole::getEquipmentId,deviceEquipmentId);
        queryWrapper.eq(EquipmentStatusManhole::getOnlineStatus,queryStatus);

        EquipmentStatusManhole equipmentStatus = equipmentStatusBaseDevicesMapper.selectOne(queryWrapper);

        //修改状态表
        if(equipmentStatus != null){
            equipmentStatus.setOnlineStatus(updateStatus);
            equipmentStatusBaseDevicesMapper.updateById(equipmentStatus);
        }
    }
}




