package com.chinacreator.process.service;


import java.text.SimpleDateFormat;
import java.util.Calendar;
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.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.NetOrderBean;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.dao.FhGoodsHandleDao;


/**
 * 复合产品处理流程
 * @author xu.zhou
 * @date 20190312
 */
@Component
public class FhGoodsHandleService {
	
	private Logger logger = Logger.getLogger(FhGoodsHandleService.class);
	
	@Autowired
	private FhGoodsHandleDao fhGoodsHandelDao;
	
	public void handle(OrderBean orderBean) throws Exception {
		//System.out.println("查询条件："+orderBean.getUserid()+">>>"+orderBean.getCpid()+">>>"+orderBean.getSpid()+">>>"+orderBean.getActiveType()+">>>"+orderBean.getFhcpid()+">>>"+orderBean.getFhspid());		 
		if(orderBean.getOrderType().equals("0")) {//订购
			order(orderBean);
		}else{//退订
			cancel(orderBean);
		}
	}
	
	/**
	 * 订购处理
	 * @param orderBean
	 * @throws Exception
	 */
	private void order(OrderBean orderBean) throws Exception {
		OrderBean oldOrderBean = fhGoodsHandelDao.existUserByFh(orderBean.getUserid(), orderBean.getCpid(),
				orderBean.getSpid(),orderBean.getActiveType()+"", orderBean.getFhcpid(), orderBean.getFhspid());
		//活动表无数据
		if (oldOrderBean == null){
			orderBean.setFlowstatus("8");//流量赠送状态 ： 8、不处理 
			orderBean.setVipstatus("2");//腾讯会员赠送状态 ：2未赠送 
			setVipConflag(orderBean,oldOrderBean);
			fhGoodsHandelDao.insertBeans(orderBean);
		//已退订
		} else if (oldOrderBean.getCanceltime() != null && !"".equals(oldOrderBean.getCanceltime().trim())){
			 OrderBean vipOrderBean = getVipInfo(oldOrderBean);
			 orderBean.setVipstatus(vipOrderBean.getVipstatus());
			 orderBean.setViptime(vipOrderBean.getViptime());
			 orderBean.setId(oldOrderBean.getId());
			 setVipConflag(orderBean,oldOrderBean);
			 fhGoodsHandelDao.UpdateOrder(orderBean);
		}else{
			//已是订购状态，暂时不做处理
			 logger.info("订购处理：已是订购状态，暂时不做处理");
		}
	}
	
	/**
	 * 退购处理
	 * @param orderBean
	 * @throws Exception
	 */
	private void cancel(OrderBean orderBean) throws Exception {
		OrderBean oldOrderBean = fhGoodsHandelDao.existUserByFh(orderBean.getUserid(), orderBean.getCpid(),
				orderBean.getSpid(),orderBean.getActiveType()+"", orderBean.getFhcpid(), orderBean.getFhspid());
		//活动表无数据
		if (oldOrderBean == null){
			orderBean.setFlowstatus("8");//流量赠送状态 ： 8、不处理 
			orderBean.setVipstatus("2");//腾讯会员赠送状态 ：2未赠送 
			setVipConflag(orderBean,oldOrderBean);
			setOrderTime(orderBean); //设置订购时间及订购渠道
			fhGoodsHandelDao.insertBeans(orderBean);
			//无数据，暂时不做处理
			//logger.info("退订处理：无数据，暂时不做处理");
		//已退订
		} else {
			 OrderBean vipOrderBean = getVipInfo(oldOrderBean);
			 orderBean.setVipstatus(vipOrderBean.getVipstatus());
			 orderBean.setViptime(vipOrderBean.getViptime());
			 orderBean.setId(oldOrderBean.getId());
			 setVipConflag(orderBean,oldOrderBean);
			 fhGoodsHandelDao.cancelOrder(orderBean);
		}
	}
	
	/**
	 * 获取VIP赠送信息
	 * @param activityOrderBean
	 * @return
	 * @throws BusinessException
	 */
	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";//赠送状态 : 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";//赠送状态 0 成功 1失败 2未赠送
					 viptime ="";
				 }
			 }else{
				 //throw new BusinessException("8010", "viptime数据异常");
				 logger.error("viptime数据异常");
			 }
		 }
		 orderBean.setVipstatus(vipstatus);
		 orderBean.setViptime(viptime);
		return orderBean;
	} 
	
	/**
	 * 设定VIPCONFLAG
	 */
	private void setVipConflag(OrderBean orderBean, OrderBean activityOrderBean){
		String cancelMonth = ""; //退订月份
		String orderMonth = "";  //订购月份
		if(activityOrderBean != null && activityOrderBean.getVipconflag() != null){
			orderBean.setVipconflag(activityOrderBean.getVipconflag());
			if(activityOrderBean.getCanceltime() != null && !"".equals(activityOrderBean.getCanceltime().trim()) && activityOrderBean.getCanceltime().trim().length() >= 6){
				cancelMonth = activityOrderBean.getCanceltime().trim().substring(0, 6);
			}
		}else{
			orderBean.setVipconflag("");
		}
		//复合产品要设置vipconflag
		if("0".equals(orderBean.getOrderstatus()) 
				&& orderBean.getFhcpid() != null && !"".equals(orderBean.getFhcpid())
				&& orderBean.getFhspid() != null && !"".equals(orderBean.getFhspid())){
			
			if(orderBean.getOrdertime() != null && !"".equals(orderBean.getOrdertime().trim()) && orderBean.getOrdertime().trim().length() >= 6){
				orderMonth = orderBean.getOrdertime().trim().substring(0, 6);
			}
			//当前是订购状态，且订购月份与退订月份不相同
			if(!"".equals(orderMonth) && !"".equals(cancelMonth) && !orderMonth.equals(cancelMonth)){ //订购
				orderBean.setVipconflag(""); //续送会员标识，0送，其他不送
			}
		}
	}
	
	private void setOrderTime(OrderBean orderBean)throws Exception{
		 //获取订购关系表中所有数据
		 List<NetOrderBean> list = fhGoodsHandelDao.findOrder(orderBean.getFhcpid(), orderBean.getUserid(),orderBean.getFhspid());
		 if(list != null && list.size()>0){
			 NetOrderBean noBean = list.get(0);
			 orderBean.setOrdertime(noBean.getOrdertimestr());
			 orderBean.setOrderchannel(noBean.getOrderchannel());
			 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); 
			 Calendar ca = Calendar.getInstance();    
			 //设置最后一天
			 ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
			 //最后一天格式化
			 String lastDay = format.format(ca.getTime());
			 orderBean.setEndtime(lastDay+"235959");
		 }
	}
}
