package com.zksy.base.alarm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.alarm.domain.AlarmData;
import com.zksy.base.alarm.mapper.AlarmDataMapper;
import com.zksy.base.alarm.service.AlarmDataService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class AlarmDataServiceImpl extends ServiceImpl<AlarmDataMapper, AlarmData>
    implements AlarmDataService {

    @Override
    public Page<AlarmData> findByPage(long pageNum, long pageSize, String deviceName, 
                                        String deviceCode, String warningType, String warningCode, 
                                        Integer alarmStatus) {
        Page<AlarmData> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<AlarmData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(deviceName != null, AlarmData::getDeviceName, deviceName)
                .like(deviceCode != null, AlarmData::getDeviceCode, deviceCode)
                .like(warningType != null, AlarmData::getWarningType, warningType)
                .like(warningCode != null, AlarmData::getWarningCode, warningCode)
                .eq(alarmStatus != null, AlarmData::getAlarmStatus, alarmStatus);
        queryWrapper.orderByDesc(AlarmData::getAlarmTime);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<AlarmData> getAlarmDataList(String deviceName, String deviceCode, 
                                              String warningType, String warningCode, 
                                              Integer alarmStatus) {
        LambdaQueryWrapper<AlarmData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(deviceName != null, AlarmData::getDeviceName, deviceName)
                .like(deviceCode != null, AlarmData::getDeviceCode, deviceCode)
                .like(warningType != null, AlarmData::getWarningType, warningType)
                .like(warningCode != null, AlarmData::getWarningCode, warningCode)
                .eq(alarmStatus != null, AlarmData::getAlarmStatus, alarmStatus);
        queryWrapper.orderByDesc(AlarmData::getAlarmTime);
        return this.list(queryWrapper);
    }

    @Override
    public boolean saveAlarmData(AlarmData alarmData) {
        if (alarmData.getAlarmTime() == null) {
            alarmData.setAlarmTime(LocalDateTime.now());
        }
        if (alarmData.getCreateTime() == null) {
            alarmData.setCreateTime(LocalDateTime.now());
        }
        if (alarmData.getAlarmStatus() == null) {
            alarmData.setAlarmStatus(0);
        }
        return this.save(alarmData);
    }
}
