package com.zksy.base.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.base.domain.AlarmAssignDetail;
import com.zksy.base.domain.AlarmAssignList;
import com.zksy.base.dto.AlarmAssignListPageInDTO;
import com.zksy.base.mapper.AlarmAssignDetailMapper;
import com.zksy.base.mapper.AlarmAssignListMapper;
import com.zksy.base.service.IAlarmAssignListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 报警清单Service业务层处理
 * 
 * @author zksy
 * @date 2026-05-06
 */
@Service
public class AlarmAssignListServiceImpl extends ServiceImpl<AlarmAssignListMapper, AlarmAssignList> implements IAlarmAssignListService
{
    @Autowired
    private AlarmAssignDetailMapper alarmAssignDetailMapper;

    /**
     * 查询报警清单
     * 
     * @param alarmAssignId 报警清单主键
     * @return 报警清单
     */
    @Override
    public AlarmAssignList selectAlarmAssignListByAlarmAssignId(Long alarmAssignId)
    {
        AlarmAssignList alarmAssignList = this.baseMapper.selectAlarmAssignListByAlarmAssignId(alarmAssignId);
        if(alarmAssignList == null){
            return alarmAssignList;
        }
        LambdaQueryWrapper<AlarmAssignDetail> lambdaQueryWrapper = new LambdaQueryWrapper<AlarmAssignDetail>()
                .eq(AlarmAssignDetail::getAlarmAssignId, alarmAssignId)
                .orderByDesc(AlarmAssignDetail::getAlarmAssignDetailId);
        List<AlarmAssignDetail> detailList = alarmAssignDetailMapper.selectList(lambdaQueryWrapper);
        if(CollUtil.isNotEmpty(detailList)){

        }
        return alarmAssignList;
    }

    /**
     * 查询报警清单列表
     * 
     * @param alarmAssignList 报警清单
     * @return 报警清单
     */
    @Override
    public List<AlarmAssignList> selectAlarmAssignListList(AlarmAssignList alarmAssignList)
    {
        return this.baseMapper.selectAlarmAssignListList(alarmAssignList);
    }

    /**
     * 新增报警清单
     * 
     * @param alarmAssignList 报警清单
     * @return 结果
     */
    @Override
    public int insertAlarmAssignList(AlarmAssignList alarmAssignList)
    {
        alarmAssignList.setAlarmAssignId(IdWorker.getId());
        return this.baseMapper.insertAlarmAssignList(alarmAssignList);
    }

    /**
     * 修改报警清单
     * 
     * @param alarmAssignList 报警清单
     * @return 结果
     */
    @Override
    public int updateAlarmAssignList(AlarmAssignList alarmAssignList)
    {
        return this.baseMapper.updateAlarmAssignList(alarmAssignList);
    }

    /**
     * 批量删除报警清单
     * 
     * @param alarmAssignIds 需要删除的报警清单主键
     * @return 结果
     */
    @Override
    public int deleteAlarmAssignListByAlarmAssignIds(Long[] alarmAssignIds)
    {
        return this.baseMapper.deleteAlarmAssignListByAlarmAssignIds(alarmAssignIds);
    }

    /**
     * 删除报警清单信息
     * 
     * @param alarmAssignId 报警清单主键
     * @return 结果
     */
    @Override
    public int deleteAlarmAssignListByAlarmAssignId(Long alarmAssignId)
    {
        return this.baseMapper.deleteAlarmAssignListByAlarmAssignId(alarmAssignId);
    }

    /**
     * 分页查询报警清单列表
     * @param pageInDTO
     * @return
     */
    @Override
    public Page<AlarmAssignList> findByPage(AlarmAssignListPageInDTO pageInDTO) {
        Page<AlarmAssignList> page = new Page<>(pageInDTO.getPageNum(), pageInDTO.getPageSize());
        LambdaQueryWrapper<AlarmAssignList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(pageInDTO.getDeviceCode()), AlarmAssignList::getDeviceCode, pageInDTO.getDeviceCode())
                .eq(pageInDTO.getAlarmStatus() != null, AlarmAssignList::getAlarmStatus, pageInDTO.getAlarmStatus())
                .eq(pageInDTO.getAssignStatus() != null, AlarmAssignList::getAssignStatus, pageInDTO.getAssignStatus())
                .like(StringUtils.isNotEmpty(pageInDTO.getDeviceName()), AlarmAssignList::getDeviceName, pageInDTO.getDeviceName())
                .like(StringUtils.isNotEmpty(pageInDTO.getManufacturer()), AlarmAssignList::getManufacturer, pageInDTO.getManufacturer());
        queryWrapper.orderByDesc(AlarmAssignList::getAlarmTime);
        return this.page(page, queryWrapper);
    }
}
