package com.gyee.alarm.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.gyee.alarm.mapper.auto.AlarmTsMapper;
import com.gyee.alarm.model.auto.ProEconElectricaltestPoint;
import com.gyee.alarm.model.vo.AlarmSuperTalbeType;
import com.gyee.alarm.model.vo.AlarmTsVo;
import com.gyee.alarm.model.vo.AlarmTypeValue;
import com.gyee.alarm.model.vo.AlarmVo;
import com.gyee.common.model.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service

public class AlarmHistoryService   {

    @Resource
    private AlarmTsMapper alarmTsMapper;


    /**
     * 查询报警信息接口
     * @param begin 开始时间
     * @param end 结束时间
     * @param alarmType 报警类型
     * @param stationid 场站
     * @param description 描述
     * @param pageNum 页数
     * @param pageSize 每页多少条
     * @return
     */
    public Map<String,Object> findAlarmlist(String modelId, long begin,long end ,String alarmType,String stationid,String deviceid,String description, int pageNum, int pageSize) {

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

        List<AlarmVo> vos =new ArrayList<>();
        String superTableName= null;
        StringBuilder descriptionSql=new StringBuilder();
        if(StringUtils.notEmp(alarmType) && StringUtils.notEmp(begin) && StringUtils.notEmp(end) )
        {

//            whereSql.append(" where  ts>=").append(begin).append("  and ts<= ").append(end);
//
//            whereSql.append(" and alarmtype=CONCAT('',").append(alarmType).append(",'')");

           if(AlarmTypeValue.BT.getCode().equals(alarmType))
           {
               superTableName= AlarmSuperTalbeType.BT.getCode();

           }else if(AlarmTypeValue.WT.getCode().equals(alarmType))
           {
               superTableName= AlarmSuperTalbeType.WT.getCode();

           }else if(AlarmTypeValue.CT.getCode().equals(alarmType))
           {
               superTableName= AlarmSuperTalbeType.CT.getCode();

           }
        }

//        if(StringUtils.notEmp(stationid)  )
//        {
//            whereSql.append(" and stationid=CONCAT('',").append(stationid).append(",'')");
//
//        }
//        if(StringUtils.notEmp(deviceid)  )
//        {
//            whereSql.append(" and deviceid= CONCAT('',").append(deviceid).append(",'')");
//
//        }
//
        if(StringUtils.notEmp(description)  )
        {
            descriptionSql.append("%").append(description).append("%");
            description=String.valueOf(descriptionSql);
        }
        if(StringUtils.empty(deviceid)  )
        {
            deviceid=null;
        }

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

        if(StringUtils.empty(description)  )
        {
            description=null;
        }
        vos = alarmTsMapper.selectByWtLimit(begin,end,modelId,superTableName, alarmType, stationid, deviceid, description, pageNum*pageSize, (pageNum*pageSize+pageSize));
        map.put("ls",vos);
        int total=alarmTsMapper.selectByWtCount(begin,end,modelId,superTableName, alarmType, stationid, deviceid, description);
        map.put("total",total);
        return map;
    }

}
