package com.chinacreator.process.job;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import com.chinacreator.process.bean.CsHisBean;
import com.chinacreator.process.dao.CsHisDao;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.video.queue.MessageService;
import com.chinacreator.video.queue.bean.MessagePipe;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class CSHisJob {

	private Logger logger = Logger.getLogger(CSHisJob.class);

	@Autowired
	private DictionaryDao dictionaryDao;

	@Autowired
	private MessageService messageService;
	
	@Autowired
	private CsHisDao csHisDao;

	public void doProcess() throws Exception {
		logger.info("接收畅视活动历史数据队列JOB启动");
		if (dictionaryDao.getValue("recivemq").equals("0")) {
			List<MessagePipe> list = messageService.reciveBatchMessage("cshis", 500);
			if(list != null && list.size()>0){
				List<CsHisBean> listdao = new ArrayList<CsHisBean>();
				for (MessagePipe messagePipe : list) {
					listdao.add(transBean(messagePipe.getBody()));
				}
				csHisDao.save(listdao);
				logger.info("畅视活动历史数据接收完成 size="+listdao.size());
			}
		}else{
			logger.info("停止接收畅视活动历史数据队列JOB启动");
		}
	}

	public  CsHisBean transBean(Map<String, Object> body) {
		String jsonStr = JsonUtil.objectToJson(body);
		return (CsHisBean) JsonUtil.jsonToBean(jsonStr, CsHisBean.class);
	}
}
