package com.chinacreator.process.job;

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.common.exception.BusinessException;
import com.chinacreator.process.bean.ActivityConfigBean;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.bean.ValidateLogBean;
import com.chinacreator.process.dao.ActiveDao;
import com.chinacreator.process.dao.ActivityConfigDao;
import com.chinacreator.process.dao.CallerAreacodeDao;
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 ValidateJob {
	
	private Logger log = Logger.getLogger("validatemq");
	
	@Autowired
	private MessageService messageService;
	
	@Autowired
	private CallerAreacodeDao callerAreacodeDao;

	@Autowired
	private ActiveDao activeDao;
	
	@Autowired
	private ActivityConfigDao activityConfigDao;

	@Autowired
	private DictionaryDao dictionaryDao;
	
	public void doProcess() throws Exception {
		System.out.println("接收验证队列JOB启动");
		if(dictionaryDao.getValue("recivemq").equals("0")){
		List<MessagePipe> list = messageService
				.reciveBatchMessage("validate", 500);
		for (MessagePipe messagePipe : list) {
			ValidateLogBean logbean = new ValidateLogBean();
			try{
				OrderBean orderBean = transBean(messagePipe.getBody());
				System.out.println("验证日志===================="+JsonUtil.objectToJson(logbean));
				logbean.setUserid(orderBean.getUserid());
				logbean.setCpid(orderBean.getCpid());
				logbean.setSpid(orderBean.getSpid());
				String province = callerAreacodeDao.findByCallerhead(orderBean.getUserid().substring(0, 7)).getProvince();
				ActivityConfigBean activityConfigBean = activityConfigDao
						.findByCallerhead(orderBean.getCpid(),
								orderBean.getSpid(),province);
				if (activityConfigBean == null) {
					throw new BusinessException("9030", "非视频风暴活动产品或者活动结束");
				}
				OrderBean oldOrderBean = activeDao.existUser(orderBean.getUserid(),
						orderBean.getCpid(), orderBean.getSpid());
				if(oldOrderBean == null){
					throw new BusinessException("9036", "验证扣费失败，活动表无记录");
				}
				if(oldOrderBean.getIscharge().equals("2")){
					throw new BusinessException("9037", "验证扣费成功，已扣费");
				}
				activeDao.updateCharge(oldOrderBean.getId());
			}catch (Exception e) {
				e.printStackTrace();
				logbean.setResult("8000");
				logbean.setErrorinfo("系统忙！");
				if (e instanceof BusinessException) {
					logbean.setErrorinfo(e.getMessage());
					logbean.setResult(((BusinessException) e).getCode());
				}
			} finally {
				log.info(JsonUtil.objectToJson(logbean));
			}
		}
		}else{
			System.out.println("停止接收队列消息");
		}
	}
	
	public OrderBean transBean(Map<String, Object> body) {
		String jsonStr = JsonUtil.objectToJson(body);
		return (OrderBean) JsonUtil.jsonToBean(jsonStr, OrderBean.class);
	}
}
