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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.alarm.domain.WarningThreshold;
import com.zksy.base.alarm.mapper.WarningThresholdMapper;
import com.zksy.base.alarm.service.WarningThresholdService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Administrator
* @description 针对表【warning_threshold(预警阈值)】的数据库操作Service实现
* @createDate 2025-09-01 09:59:43
*/
@Service
public class WarningThresholdServiceImpl extends ServiceImpl<WarningThresholdMapper, WarningThreshold>
    implements WarningThresholdService {

    @Override
    public Page<WarningThreshold> findByPage(long pageNum, long pageSize, String deviceCode, String warningType, String warningCode) {
        Page<WarningThreshold> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<WarningThreshold> queryWrapper = new LambdaQueryWrapper<>();
        if (deviceCode != null && !deviceCode.isEmpty()) {
            queryWrapper.apply("CONCAT(',', device_code, ',') LIKE CONCAT('%,', {0}, ',%')", deviceCode);
        }
        queryWrapper.like(WarningThreshold::getWarningType,warningType)
                .like(WarningThreshold::getWarningCode,warningCode);
        queryWrapper.orderByDesc(WarningThreshold::getUpdateTime);
        return this.page(page, queryWrapper);
    }

    @Override
    public List<WarningThreshold> getWarningThresholdList(String deviceCode, String warningType, String warningCode) {
        LambdaQueryWrapper<WarningThreshold> queryWrapper = new LambdaQueryWrapper<>();
        if (deviceCode != null && !deviceCode.isEmpty()) {
            queryWrapper.apply("CONCAT(',', device_code, ',') LIKE CONCAT('%,', {0}, ',%')", deviceCode);
        }
        queryWrapper.like(WarningThreshold::getWarningType,warningType)
                .like(WarningThreshold::getWarningCode,warningCode);
        queryWrapper.orderByDesc(WarningThreshold::getUpdateTime);
        return this.list(queryWrapper);
    }

    /**
     * 根据设备编号查询该设备所有预警值
     * device_code 字段支持多个设备编号，用逗号分隔
     */
    @Override
    public WarningThreshold getWarningThresholdByDeviceAndCode(String deviceCode, String warningCodes) {
        LambdaQueryWrapper<WarningThreshold> lambdaQueryWrapper = Wrappers.lambdaQuery(WarningThreshold.class)
                .apply("CONCAT(',', device_code, ',') LIKE CONCAT('%,', {0}, ',%')", deviceCode)
                .eq(WarningThreshold::getWarningCode,warningCodes);

        return baseMapper.selectOne(lambdaQueryWrapper);
    }
}




