package com.chinacreator.process.service;

import java.util.List;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.dao.NewYearActiveDao;

@Component
public class ComOrderService {

	private static Logger log = Logger.getLogger(ComOrderService.class);

	@Autowired
	private OrderService orderService;

	@Autowired
	private NewYearActiveDao newYearctiveDao;

	public void order(OrderBean orderBean) throws Exception {
		List<OrderBean> oldOrderBeans = newYearctiveDao.existUser(orderBean.getUserid(), orderBean.getCpid(),
				orderBean.getSpid());
		log.info("oldOrderBeans数目：" + oldOrderBeans.size());
		if (oldOrderBeans == null || oldOrderBeans.size() == 0) {
			orderBean.setFlowstatus("1");
			orderBean.setVipstatus("2");
			newYearctiveDao.insertBeans(orderBean);
		} else if (oldOrderBeans.size() == 1 && oldOrderBeans.get(0).getActiveType() == orderBean.getActiveType()) {
			OrderBean oldOrderBean = oldOrderBeans.get(0);
			if (oldOrderBean.getOrderType().equals("0")) {
				throw new BusinessException("9040", "队列入库失败，重复订购");
			} else {
				throw new BusinessException("9044", "队列入库失败，已参加过活动");
			}
		} else {
			for (OrderBean oldOrderBean : oldOrderBeans) {
				if ((oldOrderBean.getEndtime() == null && !oldOrderBean.getOrderType().equals("3")) || ((oldOrderBean.getEndtime() != null)
						&& (NumberUtils.toLong(oldOrderBean.getEndtime()) > NumberUtils
								.toLong(orderBean.getUpdateTime().substring(0, 6))))) {
					throw new BusinessException("9045", "队列入库失败，活动互斥");
				}
				if (oldOrderBean.getActiveType() == orderBean.getActiveType()) {
					throw new BusinessException("9045", "队列入库失败，已参加活动");
				}
				if (oldOrderBean.getOrderType().equals("3")) {
					if(oldOrderBean.getActiveType() == orderBean.getActiveType()){
						newYearctiveDao.UpdateRelations(oldOrderBean);
					} 
				}
			}
				orderBean.setFlowstatus("1");
				orderBean.setVipstatus("2");
				newYearctiveDao.insertBeans(orderBean);
		}
	}

	public void cancel(OrderBean orderBean) throws Exception {
		List<OrderBean> oldOrderBeans = newYearctiveDao.existUser(orderBean.getUserid(), orderBean.getCpid(),
				orderBean.getSpid());
		if (oldOrderBeans == null || oldOrderBeans.size() == 0) {
			throw new BusinessException("9041", "队列入库失败，活动无该退订用户");
		} else {
			if (oldOrderBeans.size() == 1) {
				OrderBean oldOrderBean = oldOrderBeans.get(0);
				if (oldOrderBean.getActiveType() == 0) {
					orderService.cancel(orderBean);
				} else {
					if (!oldOrderBean.getOrderType().equals("0")) {
						throw new BusinessException("9042", "队列入库失败，重复退订");
					}
					if (NumberUtils.toLong(orderBean.getUpdateTime()) <= NumberUtils
							.toLong(oldOrderBean.getUpdateTime())) {
						throw new BusinessException("9033", "队列入库失败，退订时间晚于订购时间");
					}
					orderBean.setId(oldOrderBean.getId());
					newYearctiveDao.cancelOrder(orderBean);
				}
			} else if (oldOrderBeans.size() == 2) {
				OrderBean oldOrderBean = null;
				for (OrderBean orderBean2 : oldOrderBeans) {
					if (orderBean2.getActiveType() == 1) {
						oldOrderBean = orderBean2;
					}
				}
				if (!oldOrderBean.getOrderType().equals("0")) {
					throw new BusinessException("9043", "队列入库失败，重复退订");
				}
				if (NumberUtils.toLong(orderBean.getUpdateTime()) <= NumberUtils.toLong(oldOrderBean.getUpdateTime())) {
					throw new BusinessException("9033", "队列入库失败，退订时间晚于订购时间");
				}
				orderBean.setId(oldOrderBean.getId());
				newYearctiveDao.cancelOrder(orderBean);
			}
		}
	}
}
