package com.pqs9900.serviceImpl.DataInfo;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.websocket.EncodeException;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.utils.SendMail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.njcn.mapper.configuration.DicDataMapper;
import com.njcn.pojo.advanced.MessageObj;
import com.njcn.pojo.configuration.DicData;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.PubUtils;
import com.njcn.utils.WebSocketUtil;
import com.pqs9900.mapper.DataInfo.AlarmInfoMapper;
import com.pqs9900.pojo.DataInfo.AlarmInfo;
/** @Author denghuajun
 * @Description //TODO 告警信息impl
 * @Date  2019/3/15 13:49
 **/
import com.pqs9900.service.DataInfo.AlarmInfoService;

@Service
public class AlarmInfoServiceImpl implements AlarmInfoService {
    private static final Logger logger = LoggerFactory.getLogger(AlarmInfoServiceImpl.class);
	@Resource
	private AlarmInfoMapper alarmInfoMapper;
	@Resource
	private DicDataMapper dicDataMapper;

    private WebSocketUtil webSocketUtil = new WebSocketUtil();

    @Resource
	private SendMail sendMail;

	@Override
	public void alarmTip(String alarmIndex){
		AlarmInfo info = new AlarmInfo();
		info = alarmInfoMapper.selectByPrimaryKey(alarmIndex);

		if(info == null){
			return;
		}

		try {
			DicData dic = new DicData();
			dic = dicDataMapper.selectId(info.getType());
			info.setType(dic.getDicName());

			info.setTimeid(PubUtils.date2String(info.getOccurredTime(), "yyyy-MM-dd HH:mm:ss"));

	        MessageObj messageObj = new MessageObj();
	        messageObj.setMsgBody("alarm");
	        messageObj.setMsgbody(info);

            webSocketUtil.sendAll(messageObj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
            logger.error(e.getMessage());
		}

	}

	@Override
	public void alarmForSeverity(String alarmIndex){
		System.out.println("调用邮件功能");
		AlarmInfo info = alarmInfoMapper.selectByPrimaryKey(alarmIndex);

		if(info == null){
			logger.error("告警信息推送出错,告警信息为空");
			return;
		}
		try {
			DicData dic = dicDataMapper.selectId(info.getType());
			if(Objects.nonNull(dic)){
				String msg = PubUtils.date2String(info.getOccurredTime(), "yyyy-MM-dd HH:mm:ss")+" "+info.getDescrpiption()+",告警类型:"+dic.getDicName()+"!";
				sendMail.sendmail("2719161629@qq.com",msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public List<AlarmInfo> getAlarmInfo(Date startTime,Date endTime,String state,String type,Integer alarmLevel,Integer lineGrade){
		List<AlarmInfo> result = new ArrayList<>();
		result = alarmInfoMapper.getAlarmInfo(startTime, endTime,state,type,alarmLevel,lineGrade);

		return result;
	}

	@Override
	public List<AlarmInfo> getAlarmInfoDetail(String state,String type,Integer typeIndex,Integer devLineType,String processTime){
		List<AlarmInfo> result = new ArrayList<>();
		result = alarmInfoMapper.getAlarmInfoDetail(state,type,typeIndex,devLineType,processTime);

		return result;
	}

	@Override
	public List<AlarmInfo> unAlarmInfo(){
		List<AlarmInfo> result = new ArrayList<>();
		result = alarmInfoMapper.unAlarmInfo();

		return result;
	}

	@Override
	public void updateAlarmInfo(String type,Integer typeIndex,Integer devLineType){
        User user = TokenManager.getToken();
		alarmInfoMapper.updateAlarmInfo(type,typeIndex,devLineType,new Date(),user.getUserIndex());
	}
}
