package com.chinacreator.process.service;

import java.util.Date;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
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.ActiveDao;
import com.chinacreator.process.dao.SysDao;

@Component
public class OrderService {
	
	@Autowired
	private ActiveDao activeDao;
	
	@Autowired
	private SysDao sysDao;

	public void order(OrderBean orderBean) throws Exception {
		OrderBean oldOrderBean = activeDao.existUser(orderBean.getUserid(),
				orderBean.getCpid(), orderBean.getSpid());
		if (oldOrderBean != null
				&& !(oldOrderBean.getUsertype().equals("2") && oldOrderBean
						.getOrderType().equals("3"))) {
			throw new BusinessException("9030", "队列入库失败，已参加过活动");
		}
			orderBean.setIscharge("2");
			String lastDay = sysDao.getLastDay();
		if (oldOrderBean == null) {
			if(orderBean.getUpdateTime().substring(0, 8).equals(lastDay)){
				orderBean.setFlowstatus("4");
			}else{
				orderBean.setFlowstatus("1");
			}
			activeDao.insertBeans(orderBean);
		} else {
			orderBean.setVipstatus("2");
			if (compareTime(DateUtils.parseDate(orderBean.getUpdateTime(),
					new String[] { "yyyyMMddHHmmss" }), DateUtils.parseDate(
					oldOrderBean.getOldcanceltime(),
					new String[] { "yyyyMMddHHmmss" }))) {
				orderBean.setFlowstatus("5");
			} else {
				if(orderBean.getUpdateTime().substring(0, 6).equals(lastDay)){
					orderBean.setFlowstatus("4");
				}else{
					orderBean.setFlowstatus("1");
				}
			}
			orderBean.setId(oldOrderBean.getId());
			orderBean.setOldcanceltime(oldOrderBean.getOldcanceltime());
			activeDao.updateOldUser(orderBean);
		}
	}
	
	public void cancel(OrderBean orderBean) throws Exception {
		OrderBean oldOrderBean = activeDao.existUser(orderBean.getUserid(),
				orderBean.getCpid(), orderBean.getSpid());
		if (oldOrderBean == null) {
			activeDao.insertOldUserBeans(orderBean);
		} else {
			if (oldOrderBean.getOrderType().equals("3")) {
				throw new BusinessException("9034", "队列入库失败，老用户重复退订");
			}
			if (oldOrderBean.getOrderType().equals("1")) {
				throw new BusinessException("9032", "队列入库失败，重复退订");
			}
			if (NumberUtils.toLong(orderBean.getUpdateTime()) <= NumberUtils
					.toLong(oldOrderBean.getUpdateTime())) {
				throw new BusinessException("9033", "队列入库失败，退订时间晚于订购时间");
			}
			orderBean.setOldcanceltime(oldOrderBean.getOldcanceltime());
			orderBean.setOrdertime(oldOrderBean.getUpdateTime());
			orderBean.setId(oldOrderBean.getId());
			orderBean.setUsertype(oldOrderBean.getUsertype());
			activeDao.cancelOrder(orderBean);
		}
	}
	
	private boolean compareTime(Date date1, Date date2) {
		String date1Str = DateFormatUtils.format(date1, "yyyyMM");
		String date2Str = DateFormatUtils.format(date2, "yyyyMM");
		if (date1Str.equals(date2Str)) {
			return true;
		}
		return false;
	}
}
