package com.chinacreator.process.service;

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 AqiyMonthOrderService {

	@Autowired
	private AqiyActiveDao aqiyActiveDao;

	@Autowired
	private ActivityConfigDao activityConfigDao;
	
	@Autowired
	private AqiyOrderService aqiyOrderService;
	
	public void handle(OrderBean orderBean) throws Exception {
		int i =0;
		orderBean = getOrder(orderBean);
		ActivityConfigBean activityConfigBean = activityConfigDao.findByCallerheadtype(orderBean.getCpid(),orderBean.getSpid(), orderBean.getProvince(),orderBean.getActiveType()+"");
		if(NumberUtils.toLong(orderBean.getOrdertime())>=NumberUtils.toLong(activityConfigBean.getBegintime()) &&
				NumberUtils.toLong(orderBean.getOrdertime())<=NumberUtils.toLong(activityConfigBean.getEndtime())){
			OrderBean activityOrderBean = aqiyActiveDao.existUser(orderBean.getUserid(), orderBean.getCpid(),orderBean.getSpid(), orderBean.getActiveType() + "");
			OrderBean otherActivityOrderBean = aqiyActiveDao.existUser(orderBean.getUserid(), orderBean.getCpid(),orderBean.getSpid(), "9");
			if (otherActivityOrderBean != null) {
			if ((otherActivityOrderBean.getVipstatus().equals("0") && (NumberUtils
					.toLong(otherActivityOrderBean.getViptime().substring(0, 6)) == NumberUtils.toLong("201809")))
					|| (StringUtils.isNotEmpty(otherActivityOrderBean.getVipconstatus())
							&& NumberUtils.toLong(otherActivityOrderBean.getVipcontime().substring(0, 6)) == NumberUtils
									.toLong("201809"))) {
				orderBean.setActiveType(9);
				aqiyOrderService.handle(orderBean);
				i =1;
			}
		}
			if(i ==0){
				if (activityOrderBean == null) {// 活动表无数据
					aqiyActiveDao.insertBeans(orderBean);
					if (otherActivityOrderBean != null && otherActivityOrderBean.getVipstatus().equals("2")) {// 用户自己未领取
						aqiyActiveDao.updatestatus(otherActivityOrderBean.getId());
					}
				}else{
					OrderBean vipOrderBean = getVipInfo(activityOrderBean);
					orderBean.setVipstatus(vipOrderBean.getVipstatus());
					orderBean.setViptime(vipOrderBean.getViptime());
					orderBean.setId(activityOrderBean.getId());
					aqiyActiveDao.update(orderBean);
				}
			}
		}else{
			orderBean.setActiveType(9);
			aqiyOrderService.handle(orderBean);
		}
	}
	
	public OrderBean getOrder(OrderBean orderBean) throws Exception{
		List<NetOrderBean> list = aqiyActiveDao.findOrder(orderBean.getCpid(), orderBean.getUserid(),
				orderBean.getSpid());
		if(list != null && list.size()>0){
			NetOrderBean NetOrderBean = list.get(0);
			orderBean.setEndtime(NetOrderBean.getEndtimestr());
			orderBean.setCanceltime(NetOrderBean.getCanceltimestr());
			orderBean.setOrderstatus(NetOrderBean.getStatus()+"");
			orderBean.setOrdertime(NetOrderBean.getOrdertimestr());
			orderBean.setOrderchannel(NetOrderBean.getOrderchannel());
			orderBean.setCancelchannel(NetOrderBean.getCancelchannel());
		}
		return 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.getActiveType() == 18) {
					vipstatus = activityOrderBean.getVipstatus();
					viptime = activityOrderBean.getViptime();
				} else {
					vipstatus = "2";
					viptime = "";
				}
			} else {
				throw new BusinessException("8010", "viptime数据异常");
			}
		}
		orderBean.setVipstatus(vipstatus);
		orderBean.setViptime(viptime);
		return orderBean;
	}
}
