package com.gyee.alarm.service.auto.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.mapper.auto.ProEconAlarmConfigurationMapper;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.model.auto.ProEconAlarmInfo;
import com.gyee.alarm.mapper.auto.ProEconAlarmInfoMapper;
import com.gyee.alarm.model.vo.AlarmSuperTalbeType;
import com.gyee.alarm.model.vo.AlarmTypeValue;
import com.gyee.alarm.model.vo.AlarmVo;
import com.gyee.alarm.service.auto.IProEconAlarmInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyee.common.model.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 报警缓存表 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2023-06-19
 */
@Service
public class ProEconAlarmInfoServiceImpl extends ServiceImpl<ProEconAlarmInfoMapper, ProEconAlarmInfo> implements IProEconAlarmInfoService {


    @Resource
    private ProEconAlarmInfoMapper proEconAlarmInfoMapper;


    /**
     * 查询报警信息接口
     * @param begin 开始时间
     * @param end 结束时间
     * @param alarmType 报警类型
     * @param stationid 场站
     * @param description 描述
     * @param pageNum 页数
     * @param pageSize 每页多少条
     * @return
     */
    public IPage<ProEconAlarmInfo> findAlarmlist(Page page, String modelId, Date begin, Date end , String alarmType, String stationid, String deviceid, String description, String deviceType, String components, String alarmId, Boolean isclose, int pageNum, int pageSize) {

        Map<String,Object> map=new HashMap<>();


        String superTableName= null;
        StringBuilder descriptionSql=new StringBuilder();


//
        if(StringUtils.notEmp(description)  )
        {
            descriptionSql.append("%").append(description).append("%");
            description=String.valueOf(descriptionSql);
        }
        if(StringUtils.empty(deviceid)  )
        {
            deviceid=null;
        }

        if(StringUtils.empty(stationid)  )
        {
            stationid=null;
        }
        if(StringUtils.empty(modelId)  )
        {
            modelId=null;
        }
        if(StringUtils.empty(description)  )
        {
            description=null;
        }
        if(StringUtils.empty(deviceType)  )
        {
            deviceType=null;
        }

        if(StringUtils.empty(components)  )
        {
            components=null;
        }
        if(StringUtils.empty(alarmId)  )
        {
            alarmId=null;
        }

        if(StringUtils.empty(isclose)  )
        {
            isclose=null;
        }
       IPage<ProEconAlarmInfo> vos =proEconAlarmInfoMapper.selectByWtLimit(page,begin,end,modelId,superTableName, alarmType, stationid, deviceid, description,deviceType,components, alarmId,isclose);

        for(ProEconAlarmInfo vo:vos.getRecords())
        {

            if(com.gyee.alarm.util.StringUtils.notEmp(vo.getDeviceId()) && CacheContext.wtmap.containsKey(vo.getDeviceId()))
            {
                vo.setDeviceName(CacheContext.wtmap.get(vo.getDeviceId()).getNemCode());
            }

            if(CacheContext.wpmap.containsKey(vo.getStationId()))
            {
                vo.setStationName(CacheContext.wpmap.get(vo.getStationId()).getName());
            }

            if(CacheContext.subwpmap.containsKey(vo.getStationId()))
            {
                vo.setStationName(CacheContext.subwpmap.get(vo.getStationId()).getName());
            }
        }




        return vos;
    }
}
