package com.steerinfo.ems.zghbjkbzlog.service.impl;

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.model.IUpdatePO;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.ems.zghbjkbzlog.model.ZghbjkbzLog;
import com.steerinfo.auth.utils.JwtUtil;
import com.steerinfo.ems.zghbjkbz.controller.ZghbjkbzController;
import com.steerinfo.ems.zghbjkbz.mapper.ZghbjkbzMapper;
import com.steerinfo.ems.zghbjkbz.model.Zghbjkbz;
import com.steerinfo.ems.zghbjkbzlog.mapper.ZghbjkbzLogMapper;
import com.steerinfo.ems.zghbjkbzlog.service.IZghbjkbzLogService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigDecimal;

/**
 * ZghbjkbzLog服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-01-15 02:38
 * 类描述
 * 修订历史：
 * 日期：2020-01-15
 * 作者：generator
 * 参考：
 * 描述：Zghbjkbz服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "zghbjkbzLogService")
public class ZghbjkbzLogServiceImpl extends BaseServiceImpl<ZghbjkbzLog, String> implements IZghbjkbzLogService {
	
	 private static final Logger logger = LoggerFactory.getLogger(ZghbjkbzLogServiceImpl.class);

    @Autowired
    private ZghbjkbzLogMapper zghbjkbzLogMapper;
    
    @Autowired
    private ZghbjkbzMapper zghbjkbzMapper;
    
    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Override
    protected IBaseMapper<ZghbjkbzLog, String> getMapper() {
        return zghbjkbzLogMapper;
    }

	@Override
	public void record() {
		// logger.info("记录超出监控标准的数据--执行开始--");
		String sql = "select * from TASK_CONF where id = '114'";
		List<Map<String, Object>> l = jdbcTemplate.queryForList(sql);
		// 获取监控频率
		int intervals = 0;
		if (l!=null && l.size()>0){
			Map<String, Object> m = (Map<String, Object>)l.get(0);
			String cron = m.get("CRON") == null ? "" : m.get("CRON").toString();
			String[] s = cron.split(" ");
			if(s.length>2){
				String s1 = s[0];
				if(s1.indexOf("/") == -1){
					intervals = 60;
				}else{
					intervals = Integer.valueOf(s1.split("/")[1]);
				}
			}
		}
		List<Zghbjkbz> list  = zghbjkbzMapper.getAll();
		for(Zghbjkbz z : list){
			String cname = z.getcName();
			String zid = z.getId();
			if(cname!=null && !cname.trim().isEmpty()){
				// 判断EMS_HBSJ表是否有该字段
				sql = "select COLUMN_NAME from user_tab_cols where table_name='EMS_HBSJ' and COLUMN_NAME = ?";
				List lc = jdbcTemplate.queryForList(sql, new Object[]{cname.toUpperCase().trim()});
				if(lc == null || lc.size()<1){
					logger.error("创建日志出错，字段：" + cname + ", zid:" + zid);
					continue;
				}
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("zid", zid);
				m.put("cname", cname);
				// 如果超过标准,创建日志
				try {
					ZghbjkbzLog zg = zghbjkbzLogMapper.createZghbjkbzLog(m);
					if(zg!=null){
						// 获取最近1小时的记录
						ZghbjkbzLog zlog = zghbjkbzLogMapper.getHourZghbjkbzLog(zid);
						if(zlog == null){ // 如果最近1小时没有数据新增
							zg.setState("0");
							zg.setIntervals(intervals);
							this.add(zg);
						} else if(zlog!=null && zlog.getState().equals("0")){ // 如果有数据并且为状态为非连续数据，更新为连续数据.
							zlog.setState("1");
							zghbjkbzLogMapper.updateByPrimaryKeySelective(zlog);
						}
					}
	            } catch(Exception e){
	            	e.printStackTrace();
	            	logger.error("创建日志出错，字段：" + cname + ", zid:" + zid);
	            }
			}
		}
		logger.info("记录超出监控标准的数据--执行完毕--");
	}

}
