package com.zhiwei.manage.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.zhiwei.manage.bean.Channel;
import com.zhiwei.manage.bean.Data;
import com.zhiwei.manage.dao.DataDaoImpl;
import com.zhiwei.manage.service.ServerServiceImpl;

@RunWith(SpringJUnit4ClassRunner.class) // 使用junit4进行测试
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class Tests {

	@Resource
	private DataDaoImpl dd;
	@Resource
	private ServerServiceImpl sd;

	@Test
	public void test1() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -5);
		List<Channel> list = sd.findDisChannel();
		long count = 0;
		Map<String, Long> map = new HashMap<>();
		for (; cal.getTime().before(new Date()); cal.add(Calendar.HOUR_OF_DAY, 1)) {
			for (Channel ch : list) {
				if (cal.get(Calendar.HOUR_OF_DAY) == 0) {
					count = 0;
					map.put(ch.getPt(), count);
				}
				if (map.get(ch.getPt()) == null) {
					map.put(ch.getPt(), (long) (Math.random() * 3000));
				} else
					map.put(ch.getPt(), map.get(ch.getPt()) + (int) (Math.random() * 3000));
				Data d = new Data();
				d.setCount(map.get(ch.getPt()));
				d.setPt(ch.getPt());
				try {
					d.setTime(sdf.parse(sdf.format(cal.getTime())));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				System.out.println(ch.getPt() + ":" + map.get(ch.getPt()));
				 dd.insertTest(d);
			}
		}

	}

}
