package com.czg.service.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.exception.CzgException;
import com.czg.service.system.mapper.SysDevicesMapper;
import com.czg.system.dto.SysDevicesDTO;
import com.czg.system.dto.SysDevicesPageDTO;
import com.czg.system.entity.SysDevices;
import com.czg.system.service.SysDevicesService;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 设备管理 服务层实现。
 *
 * @author ww
 * @since 2025-11-24
 */
@Slf4j
@Service
public class SysDevicesServiceImpl extends ServiceImpl<SysDevicesMapper, SysDevices> implements SysDevicesService {

    @Override
    public Long addDevice(SysDevicesDTO reqDTO) {
        // 检查 sn 是否存在
        if (exists(query().eq(SysDevices::getDeviceSn, reqDTO.getDeviceSn()))) {
            throw new CzgException("设备sn已存在");
        }

        SysDevices devices = BeanUtil.toBean(reqDTO, SysDevices.class);

        save(devices);
        return devices.getId();
    }

    @Override
    public Long updateDevice(SysDevicesDTO reqDTO) {
        // 检查 sn 存在
        if (exists(query().ne(SysDevices::getId, reqDTO.getId()).eq(SysDevices::getDeviceSn, reqDTO.getDeviceSn()))) {
            throw new CzgException("设备sn已存在");
        }

        SysDevices devices = getById(reqDTO.getId());
        if (devices == null) {
            throw new CzgException("设备不存在");
        }

        devices.setDeviceSn(reqDTO.getDeviceSn());
        devices.setDeviceType(reqDTO.getDeviceType());
        devices.setConnType(reqDTO.getConnType());
        devices.setOnlineStatus(reqDTO.getOnlineStatus());

        updateById(devices);
        return devices.getId();
    }

    @Override
    public Long deleteDevice(Long id) {
        SysDevices devices = getById(id);
        if (devices == null) {
            throw new CzgException("设备不存在");
        }

        removeById(id);
        return id;
    }

    @Override
    public Page<SysDevicesDTO> queryDevicePage(SysDevicesPageDTO reqDTO) {
        QueryWrapper wrapper = new QueryWrapper();
        if (StrUtil.isNotBlank(reqDTO.getDeviceSn())) {
            wrapper.like(SysDevices::getDeviceSn, reqDTO.getDeviceSn());
        }
        if (StrUtil.isNotBlank(reqDTO.getDeviceType())) {
            wrapper.eq(SysDevices::getDeviceType, reqDTO.getDeviceType());
        }
        if (StrUtil.isNotBlank(reqDTO.getConnType())) {
            wrapper.eq(SysDevices::getConnType, reqDTO.getConnType());
        }
        if (reqDTO.getOnlineStatus() != null) {
            wrapper.eq(SysDevices::getOnlineStatus, reqDTO.getOnlineStatus());
        }

        wrapper.orderBy(SysDevices::getOnlineStatus, false);
        wrapper.orderBy(SysDevices::getId, false);

        return page(PageUtil.buildPage(), wrapper).map(devices -> BeanUtil.toBean(devices, SysDevicesDTO.class));
    }

    @Override
    public void updateDeviceOnlineStatus(String sn, Integer online) {
        SysDevices devices = getOne(query().eq(SysDevices::getDeviceSn, sn));
        if (devices == null) {
            log.info("心跳设备不存在：sn={}", sn);
            return;
        }

        if (online == 0) {
            devices.setOnlineStatus(0);
            devices.setOfflineTime(LocalDateTime.now());
        } else {
            devices.setOnlineStatus(1);
            devices.setLastUnlineTime(LocalDateTime.now());
            devices.setOfflineTime(null);
        }
        updateById(devices);
    }
}
