package com.chinacreator.process.job;

import java.util.List;
import java.util.Map;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.process.bean.ActivityConfigBean;
import com.chinacreator.process.bean.LogBean;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.dao.ActivityConfigDao;
import com.chinacreator.process.dao.BlackWhiteDao;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.service.NewYearOrderService;
import com.chinacreator.process.service.OrderService;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.video.queue.MessageService;
import com.chinacreator.video.queue.bean.MessagePipe;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class ReciveMQJob2 {

	private Logger log = Logger.getLogger("recivemq");

	@Autowired
	private MessageService messageService;

	@Autowired
	private ActivityConfigDao activityConfigDao;

	@Autowired
	private BlackWhiteDao blackWhiteDao;
	
	@Autowired
	private DictionaryDao dictionaryDao;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private NewYearOrderService newYearOrderService;
	
	public void doProcess() throws Exception {
		System.out.println("接收订购队列JOB启动");
		if(dictionaryDao.getValue("recivemq").equals("0")){
		List<MessagePipe> list = messageService
				.reciveBatchMessage("order", 500);
		for (MessagePipe messagePipe : list) {
			LogBean logBean = new LogBean();
			try {
				OrderBean orderBean = transBean(messagePipe.getBody());
				logBean.setUserid(orderBean.getUserid());
				logBean.setInputParm(JsonUtil.objectToJson(orderBean));
				ActivityConfigBean activityConfigBean = activityConfigDao
						.findByCallerhead(orderBean.getCpid(),
								orderBean.getSpid(),"");/** =============>error<====================== */
				if (activityConfigBean == null) {
					throw new BusinessException("9030", "非视频风暴活动产品或者活动结束");
				}
				if(blackWhiteDao.isWhite(orderBean.getUserid())){
					orderBean.setIsWhite(0);
				}else{
					orderBean.setIsWhite(1);
				}
				if(orderBean.getOrderType().equals("0")){
					if ((NumberUtils.toLong(orderBean.getUpdateTime()) < NumberUtils
							.toLong(activityConfigBean.getBegintime()) || NumberUtils
							.toLong(orderBean.getUpdateTime()) > NumberUtils
							.toLong(activityConfigBean.getEndtime()))
							&& (orderBean.getIsWhite() == 1)) {
						throw new BusinessException("9030", "非视频风暴活动产品或者活动结束");
					}
				}else{
					if ((NumberUtils.toLong(orderBean.getUpdateTime()) < NumberUtils
							.toLong(activityConfigBean.getBegintime()) || NumberUtils
							.toLong(orderBean.getUpdateTime()) > NumberUtils
							.toLong(activityConfigBean.getCancelendtime()))
							&& (orderBean.getIsWhite() == 1)) {
						throw new BusinessException("9030", "非视频风暴活动产品或者活动结束");
					}
				}
//				if((orderBean.getProvince() != null && orderBean.getProvince().equals("浙江") && orderBean.getSpid().equals("6"))){
//					handle(orderBean);
//				}else{
					newYearHandle(orderBean);
//				}
			} catch (Exception e) {
				e.printStackTrace();
				logBean.setErrorInfo("系统错误");
				logBean.setResultCode("8000");
				if (e instanceof BusinessException) {
					logBean.setErrorInfo(e.getMessage());
					logBean.setResultCode(((BusinessException) e).getCode());
				}
			} finally {
				log.info(JsonUtil.objectToJson(logBean));
			}
		}
		}else{
			System.out.println("停止接收队列消息");
		}
	}

	public void handle(OrderBean orderBean) throws Exception {
		if (orderBean.getOrderType().equals("0")) {
			orderService.order(orderBean);
		} else {
			orderService.cancel(orderBean);
		}
	}
	
	public void newYearHandle(OrderBean orderBean) throws Exception {
		orderBean.setActiveType(1);
		if (orderBean.getOrderType().equals("0")) {
			newYearOrderService.order(orderBean);
		} else {
			newYearOrderService.cancel(orderBean);
		}
	}

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