package com.chinacreator.process.service;

import java.sql.SQLException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
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.AreaInfo;
import com.chinacreator.process.bean.NetOrderBean;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.bean.OrderLog;
import com.chinacreator.process.dao.CSactiveDao;
import com.chinacreator.process.dao.CallerAreacodeDao;
import com.chinacreator.process.dao.NetOrderDao;
import com.chinacreator.process.dao.SysDao;

@Component
public class CSorderService {

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

	@Autowired
	private NetOrderDao netOrderDao;

	@Autowired
	private CSactiveDao csActiveDao;
	
	@Autowired
	private CallerAreacodeDao callerAreacodeDao;
	
	@Autowired
	private EcbService ecbService;
	
	@Autowired
	private SysDao sysDao;
	
	public void handle(OrderBean bean){
		NetOrderBean netOrderBean = trunBean(bean);
		if(bean.getOrderType().equals("0")){
			compareOrderForNet(netOrderBean);
		}else{
			compareCancelForNet(netOrderBean);
		}
	}
	
	public NetOrderBean trunBean(OrderBean bean){
		NetOrderBean netOrderBean = new NetOrderBean();
		netOrderBean.setId(bean.getId());
		netOrderBean.setUserid(bean.getUserid());
		netOrderBean.setCpid(bean.getCpid());
		netOrderBean.setSpid(bean.getSpid());
		AreaInfo areaInfo = callerAreacodeDao.findByCallerhead(bean.getUserid().substring(0, 7));
		netOrderBean.setProvince(areaInfo.getProvince());
		netOrderBean.setArea(areaInfo.getArea());
		netOrderBean.setApptype("");
		netOrderBean.setIsexperience(0);
		if(bean.getOrderType().equals("0")){
			netOrderBean.setStatus("0");
			netOrderBean.setOrdertype("0");
			netOrderBean.setOrderstatus(2);
			netOrderBean.setOrderchannel(bean.getChannel());
		}else{
			netOrderBean.setStatus("1");
			netOrderBean.setOrdertype("1");
			netOrderBean.setOrderstatus(4);
			netOrderBean.setCancelchannel(bean.getChannel());
		}
		return netOrderBean;
	}
	
	public void compareOrderForNet(NetOrderBean bean) {
		String errorcode = "0";
		String errorinfo = "";
		String state ="3";
		NetOrderBean oldOrderBean = null;
		try {
			oldOrderBean = this.netOrderDao.findOrder(bean.getCpid(), bean.getSpid(), bean.getUserid());
			if (oldOrderBean != null && oldOrderBean.getStatus().equals("0")) {
				throw new BusinessException("9010", "已订购，请不要重复订购", new String[0]);
			}
			errorcode = ecbService.order(bean.getUserid(), bean.getCpid(), bean.getSpid());
			bean.setOrdertime(sysDao.getSysdate());
			this.netOrderDao.order(bean);
		} catch (Exception e) {
			 state ="6";
			 log.error(bean.getUserid()+"订购失败:",e);
			if ((e instanceof BusinessException)) {
				errorcode = ((BusinessException) e).getCode();
				errorinfo = e.getMessage();
			} else {
				errorcode = "8000";
				errorinfo = "系统忙！";
			}
			if(StringUtils.isNotEmpty(errorinfo) && errorinfo.length() >200){
				errorinfo = errorinfo.substring(0, 200);
			}
		} finally {
			try {
				csActiveDao.update(bean.getId(), errorcode, errorinfo,state);
				saveNetLog(bean, errorcode, errorinfo, "0");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void compareCancelForNet(NetOrderBean bean)  {
		String errorcode = "0";
		String errorinfo = "";
		String state ="5";
		NetOrderBean oldOrderBean = null;
		try {
			oldOrderBean = this.netOrderDao.findOrder(bean.getCpid(), bean.getSpid(), bean.getUserid());
			if (oldOrderBean == null) {
				throw new BusinessException("9026", "本地无订购", new String[0]);
			}
			if (oldOrderBean != null && !oldOrderBean.getStatus().equals("0")) {
				throw new BusinessException("9006", "已退订，请不要重复退订", new String[0]);
			}
			if(oldOrderBean.getOrdertime().compareTo(DateUtils.parseDate("20170621235959",new String[]{"yyyyMMddHHmmss"}))  > 0){
				errorcode = ecbService.cancelOrder(bean.getUserid(), bean.getCpid(),bean.getSpid(),DateFormatUtils.format(oldOrderBean.getOrdertime(), "yyyyMMddHHmmss"));
			}
			bean.setCanceltime(sysDao.getSysdate());
			this.netOrderDao.cancelOrder(bean);
		} catch (Exception e) {
			state ="7";
			log.error(bean.getUserid()+"退订失败:",e);
			if ((e instanceof BusinessException)) {
				errorcode = ((BusinessException) e).getCode();
				errorinfo = e.getMessage();
			} else {
				errorcode = "8000";
				errorinfo = "系统忙！";
			}
			if(StringUtils.isNotEmpty(errorinfo) && errorinfo.length() >200){
				errorinfo = errorinfo.substring(0, 200);
			}
		} finally {
			try {
				saveNetLog(bean, errorcode, errorinfo, "1");
				csActiveDao.update(bean.getId(), errorcode, errorinfo,state);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

	public void saveNetLog(NetOrderBean orderInfo, String errorcode, String errorinfo, String ordertype)
			throws SQLException {
		OrderLog orderLog = new OrderLog();
		orderLog.setApptype(orderInfo.getApptype());
		orderLog.setChannel(ordertype.equals("0") ? orderInfo.getOrderchannel() : orderInfo.getCancelchannel());
		orderLog.setOrderstatus(orderInfo.getOrderstatus());
		orderLog.setStatus(Integer.valueOf(orderInfo.getStatus()).intValue());
		orderLog.setCpid(orderInfo.getCpid());
		orderLog.setIsexperience(orderInfo.getIsexperience());
		orderLog.setOrdertype(orderInfo.getOrdertype());
		orderLog.setProvince(orderInfo.getProvince());
		orderLog.setArea(orderInfo.getArea());
		orderLog.setSpid(orderInfo.getSpid());
		orderLog.setUserid(orderInfo.getUserid());
		orderLog.setErrorcode(errorcode);
		orderLog.setErrorinfo(errorinfo);
		this.netOrderDao.addOrderLog(orderLog);
	}
}
