package com.chinacreator.process.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.process.bean.ActivityConfigBean;
import com.chinacreator.process.bean.NetOrderBean;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.dao.ActivityConfigDao;
import com.chinacreator.process.dao.AqiyActiveDao;

@Component
public class AqiyOrderService {

	@Autowired
	private AqiyActiveDao aqiyActiveDao;
	
	@Autowired
	private ActivityConfigDao activityConfigDao;

	public void handle(OrderBean orderBean) throws Exception {
		OrderBean activityOrderBean = aqiyActiveDao.existUser(orderBean.getUserid(), orderBean.getCpid(),
				orderBean.getSpid(),orderBean.getActiveType()+"");
		 if(activityOrderBean == null){//活动表无数据
			 orderBean = getOrderInfo(orderBean);
			 aqiyActiveDao.insertBeans(orderBean);
		 }else{
			 orderBean = getOrderInfo(orderBean);
			 OrderBean vipOrderBean = getVipInfo(activityOrderBean);
			 orderBean.setVipstatus(vipOrderBean.getVipstatus());
			 orderBean.setViptime(vipOrderBean.getViptime());
			 orderBean.setId(activityOrderBean.getId());
			 aqiyActiveDao.update(orderBean);
		 }
	}
	
	public OrderBean getVipInfo(OrderBean activityOrderBean) throws BusinessException{
		OrderBean orderBean = new OrderBean();
		 String vipstatus=activityOrderBean.getVipstatus();
		 String viptime = activityOrderBean.getViptime();
		 if(StringUtils.isEmpty(viptime)){
			 vipstatus = activityOrderBean.getVipstatus();
			 viptime = activityOrderBean.getViptime();
		 }else{
			 if(NumberUtils.toLong(viptime.substring(0, 6)) ==NumberUtils.toLong(DateFormatUtils.format(new Date(), "yyyyMM"))){
				 if(activityOrderBean.getVipcontime() != null && NumberUtils.toLong(activityOrderBean.getVipcontime().substring(0, 6))  == NumberUtils.toLong(DateFormatUtils.format(new Date(), "yyyyMM"))){
					 vipstatus ="0";
					 viptime = activityOrderBean.getVipcontime();
				 }else{
					 vipstatus = activityOrderBean.getVipstatus();
					 viptime = activityOrderBean.getViptime();
				 }
			 }else if(NumberUtils.toLong(viptime.substring(0, 6)) < NumberUtils.toLong(DateFormatUtils.format(new Date(), "yyyyMM"))){
				 if(activityOrderBean.getVipcontime() != null && NumberUtils.toLong(activityOrderBean.getVipcontime().substring(0, 6))  == NumberUtils.toLong(DateFormatUtils.format(new Date(), "yyyyMM"))){
					 vipstatus = activityOrderBean.getVipstatus();
					 viptime = activityOrderBean.getViptime();
				 }else{
					 vipstatus ="2";
					 viptime ="";
				 }
			 }else{
				 throw new BusinessException("8010", "viptime数据异常");
			 }
		 }
		 orderBean.setVipstatus(vipstatus);
		 orderBean.setViptime(viptime);
		return orderBean;
	}
	
	public OrderBean getOrderInfo(OrderBean orderBean) throws Exception{
		List<NetOrderBean> list = aqiyActiveDao.findOrder(orderBean.getCpid(), orderBean.getUserid(),orderBean.getSpid());
		 List<NetOrderBean> orderlist = new ArrayList<NetOrderBean>();
		 List<NetOrderBean> cancellist = new ArrayList<NetOrderBean>();
		 ActivityConfigBean activityConfigBean = activityConfigDao.findByCallerheadtype(orderBean.getCpid(), orderBean.getSpid(), orderBean.getProvince(),orderBean.getActiveType()+"");
		 if(list != null && list.size()>0){
			 for (NetOrderBean netOrderBean : list) {
				 if(NumberUtils.toLong(netOrderBean.getOrdertimestr()) >= NumberUtils.toLong(activityConfigBean.getBegintime()) && NumberUtils.toLong(netOrderBean.getOrdertimestr()) <= NumberUtils.toLong(activityConfigBean.getEndtime())){
					 if(netOrderBean.getStatus().equals("0")){
							orderlist.add(netOrderBean);
						}else if(netOrderBean.getStatus().equals("1")){
							cancellist.add(netOrderBean);
						}
				 }
			}
		 }
		 String ordertime ="";
		 String endtime="";
		 String canceltime="";
		 String orderstatus= "";
		 String orderchannel ="";
		 String cancelchannel="";
		 if(orderlist.size()>0 && cancellist.size()==0){//全是订购，取最早订购时间
			 orderstatus = "0";
			 for (NetOrderBean netOrderBean : orderlist) {
				if(StringUtils.isEmpty(ordertime)){
					ordertime = netOrderBean.getOrdertimestr();
				}
				if(NumberUtils.toLong(netOrderBean.getOrdertimestr()) <= NumberUtils.toLong(ordertime)){
					ordertime = netOrderBean.getOrdertimestr();
					orderchannel = netOrderBean.getOrderchannel();
				}
			}
		 }else if(cancellist.size() >0 && orderlist.size()==0){
			 orderstatus ="1";
			 for (NetOrderBean netOrderBean : cancellist) {
					if(StringUtils.isEmpty(ordertime)){
						ordertime = netOrderBean.getOrdertimestr();
						canceltime = netOrderBean.getCanceltimestr();
						cancelchannel = netOrderBean.getCancelchannel();
						orderchannel = netOrderBean.getOrderchannel();
						endtime = netOrderBean.getEndtimestr();
					}
					if(NumberUtils.toLong(netOrderBean.getOrdertimestr()) <= NumberUtils.toLong(ordertime)){
						ordertime = netOrderBean.getOrdertimestr();
						orderchannel = netOrderBean.getOrderchannel();
					}
					if(NumberUtils.toLong(netOrderBean.getCanceltimestr()) >= NumberUtils.toLong(canceltime)){
						canceltime = netOrderBean.getCanceltimestr();
						endtime = netOrderBean.getEndtimestr();
						cancelchannel = netOrderBean.getCancelchannel();
					}
				}
		 }else if(cancellist.size() >0 && orderlist.size()>0){
			 orderstatus ="0";
			 orderBean.setCancelchannel(cancelchannel);
			 for (NetOrderBean netOrderBean : list) {
				 if(netOrderBean.getStatus().equals("0") || netOrderBean.getStatus().equals("1")){
					 if(StringUtils.isEmpty(ordertime)){
							ordertime = netOrderBean.getOrdertimestr();
						}
					 if(NumberUtils.toLong(netOrderBean.getOrdertimestr()) <= NumberUtils.toLong(ordertime)){
							ordertime = netOrderBean.getOrdertimestr();
							orderchannel=netOrderBean.getOrderchannel();
						}
				 }
			 }
		 }else{
			 throw new BusinessException("8010", "order数据异常");
		 }
		 orderBean.setEndtime(endtime);
		 orderBean.setCanceltime(canceltime);
		 orderBean.setOrderstatus(orderstatus);
		 orderBean.setOrdertime(ordertime);
		 orderBean.setOrderchannel(orderchannel);
		 orderBean.setCancelchannel(cancelchannel);
		 return orderBean;
	} 
}
