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.dao.NewYearActiveDao;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.video.queue.MessageService;
import com.chinacreator.video.queue.bean.MessagePipe;

@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class UpdateChannelJob {
	
	private Logger log = Logger.getLogger("channelupdate");
	
	@Autowired
	private MessageService messageService;

	@Autowired
	private NewYearActiveDao newYearctiveDao;
	
	@Autowired
	private ActiveDao activeDao;
	
	@Autowired
	private CallerAreacodeDao callerAreacodeDao;
	
	@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("channelupdate", 500);
			for (MessagePipe messagePipe : list) {
				ValidateLogBean logbean = new ValidateLogBean();
				try{
					OrderBean orderBean = transBean(messagePipe.getBody());
					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", "非视频风暴活动产品或者活动结束");
					}
					 List<OrderBean>  oldOrderBeans = newYearctiveDao.existUser(orderBean.getUserid(),orderBean.getCpid(), orderBean.getSpid());
					if(oldOrderBeans == null || oldOrderBeans.size() == 0){
						throw new BusinessException("9038", "更改渠道失败，活动表无记录");
					}
					OrderBean oldOrderBean = null;
					for (OrderBean orderBean2 : oldOrderBeans) {
						if(activityConfigBean.getActiveType() ==orderBean2.getActiveType() ){
							oldOrderBean = orderBean2;
							break;
						}
					}
					if(oldOrderBean.getChannel().equals("spfbtx1") || oldOrderBean.getChannel().equals("spfbtx2")
							|| oldOrderBean.getChannel().equals("spfbtx3") || oldOrderBean.getChannel().equals("spfbtx4")
							 || oldOrderBean.getChannel().equals("spfbtx5")  || oldOrderBean.getChannel().equals("spfbtx14")
							 || oldOrderBean.getChannel().equals("spfbtx15") || oldOrderBean.getChannel().equals("spfbtx16") 
							 ||  oldOrderBean.getChannel().equals("spfbtx17")){
						throw new BusinessException("9039", "渠道已更改或者不能更改");
					}
					activeDao.updateChannel(oldOrderBean.getId(),orderBean.getChannel());
				}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);
	}
}
