package com.chinacreator.video.restful.service;

import java.util.HashMap;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.CommConfDao;
import com.chinacreator.video.restful.bean.OrderLog;
import com.chinacreator.video.restful.util.RequestUtil;
import com.chinacreator.video.service.CommonService;
import com.chinacreator.video.validate.ChannelValidate;
import com.chinacreator.video.validate.IpValidate;
import com.chinacreator.video.validate.MobValidate;
import com.chinacreator.video.validate.ParamValidate;
import com.chinacreator.video.validate.SignValidate;
import com.chinacreator.video.validate.TimetampValidate;

/**
 * 积分商城服务类
 * @author xu.zhou
 * @date 20190618
 */
@Component
public class CshuyuService {
	
	@Autowired
	private ChannelValidate channelValidate;
	
	@Autowired
	private IpValidate ipValidate;
	
	@Autowired
	private MobValidate mobValidate;
	
	@Autowired
	private TimetampValidate timetampValidate;
	
	@Autowired
	private SignValidate signValidate;
	
	@Autowired
	private ParamValidate paramValidate;
	
	@Autowired
	private CommConfDao commConfDao;
	
	@Autowired
	private CommonService commonService;
	
	private static Logger logger = Logger.getLogger(CshuyuService.class);
	
	private static Logger log = Logger.getLogger("cshuyu");
	
	public void busiCheck(OrderInfo orderInfo) throws Exception{
		HashMap<String, String> params =new HashMap<String, String>();
		params.put("type", "0");
		if("cshyapp".equals(orderInfo.getChannel())){
			paramValidate.validateAdd(orderInfo, params);
		}else{
			paramValidate.validate(orderInfo,null);
		}

		//timetamp验证
		timetampValidate.validate(orderInfo.getTimestamp());
		//渠道验证
		channelValidate.validate(orderInfo.getChannel());
		//ip验证
		ipValidate.volidate(orderInfo);
		//签名验证
		signValidate.validate(orderInfo);
		//手机号码验证
		mobValidate.validate(orderInfo);
	}
	
	/**
	 * 业务处理
	 * @param orderInfo
	 */
	public void busiOper(OrderInfo orderInfo)throws Exception{
		String errorcode = "0";
		String errorinfo = "ok";
		try {
			commonService.setOperTime(orderInfo);
			commonService.setAreaInfo(orderInfo);
			HashMap<String, Object> remap = commonService.getOrderStatus(orderInfo.getUserid(),orderInfo.getSpid());
			String relid = (String)remap.get("RELID"); //订购关系表ID
			if(relid == null || "".equals(relid)){
				relid = commConfDao.generateId();
			}
			orderInfo.setId(relid);
			if("0".equals(orderInfo.getType())){
				commConfDao.addOrderRal(orderInfo);
			}else if("1".equals(orderInfo.getType()) && "0".equals(remap.get("ORDERSTATUS"))){ //已订购
				commConfDao.addCancelRal(orderInfo);
			}else if("1".equals(orderInfo.getType()) && "1".equals(remap.get("ORDERSTATUS"))){ //已退订
				throw new BussinessException("9017", "已退订该产品");
			}else if("1".equals(orderInfo.getType())){
				throw new BussinessException("9118", "未订购或者已失效");
			}
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof BussinessException) {
				errorcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else {
				errorcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
			throw e;
		} finally{
			//记录订购日志
		    commonService.saveLog(orderInfo, errorcode, errorinfo);
		    if("0".equals(errorcode)){
			    //添加订单ID与订购关系的关联记录
			    commonService.addRelidRec(orderInfo);
		    }
		}
	}
}
