package com.pqs9000.task;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.njcn.mapper.configuration.DeviceDetailMapper;
import com.njcn.mapper.data.LimitRateMapper;
import com.njcn.pojo.commons.device.DeviceDetail;
import com.njcn.pojo.data.LimitRate;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.SendMail;
import com.njcn.utils.UserUtil;
import com.pqs9000.mapper.data.DataVMapper;
import com.pqs9000.pojo.data.DataV;

@Component
public class WarningTask {
	private static final Logger logger = LoggerFactory.getLogger(WarningTask.class);
	@Autowired
	private UserUtil userUtil;

	@Resource
	SendMail sendmail;

	@Resource
	private DataVMapper dataVMapper;

	@Resource
	private LimitRateMapper limitRateMapper;

	@Resource
	private DeviceDetailMapper deviceDetailMapper;

	private String startTime;
	private String endTime;
	private String day;

	@Scheduled(cron = "0 0 10 * * ?")
	public void timer() {
		SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH),
				0, 0, 0);
		this.endTime = dateformat1.format(calendar.getTime());
			
		calendar.add(Calendar.DATE, -1);
		this.startTime = dateformat1.format(calendar.getTime());
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		this.day = sdf.format(calendar.getTime());
		
		mailWarning();
	}

	public void mailWarning() {
		List<User> users = userUtil.listMailUser();

		for (User user : users) {
			String address = user.getEmail();
			String stateMessage = "";
			String rateMessage = "";
			stateMessage = stateMessage + this.day + "通讯异常统计信息:" + "\r\n";
			rateMessage = rateMessage + this.day + "稳态指标合格率异常统计信息:" + "\r\n";
			List<Integer> indexs = userUtil.listLineByUserId(user.getUserIndex());

			if (indexs != null && indexs.size() > 0) {
				boolean flag = false;
				for (Integer index : indexs) {
					DeviceDetail tmp = new DeviceDetail();
					tmp = deviceDetailMapper.queryNameBylineId(index);

					if (judgeState(index, this.startTime, this.endTime)) {
						stateMessage = stateMessage + tmp.getSubName() + "_" + tmp.getName() + "_" + tmp.getPointName()
								+ "\r\n";
						flag = true;
					}
					if (judgeRate(index, this.startTime, this.endTime)) {
						rateMessage = rateMessage + tmp.getSubName() + "_" + tmp.getName() + "_" + tmp.getPointName()
								+ "\r\n";
						flag = true;
					}
				}

				if(flag){
					try {
						sendmail.sendmail(address, stateMessage + rateMessage);
					} catch (Exception e) {
						logger.error(e.getMessage());
					}
				}
			}
		}
	}

	public boolean judgeState(Integer lineIndex, String startTime, String endTime) {
		List<DataV> data = new ArrayList<>();
		data = dataVMapper.queryData(lineIndex, startTime, endTime);

		if (data == null) {
			return true;
		} else {
			return false;
		}
	}

	public boolean judgeRate(Integer lineIndex, String startTime, String endTime) {
		List<LimitRate> data = new ArrayList<>();
		data = limitRateMapper.queryData(lineIndex, startTime, endTime);

		if (data != null && data.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
}