package com.chinacreator.process.job;


import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import net.sf.json.JSONObject;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.PersistJobDataAfterExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.common.util.DESUtil;
import com.chinacreator.common.util.MD5;
import com.chinacreator.common.util.URLUtil;
import com.chinacreator.process.bean.OrderLog;
import com.chinacreator.process.bean.PointShopMqBean;
import com.chinacreator.process.bean.PointShopOrderBean;
import com.chinacreator.process.dao.DictionaryDao;
import com.chinacreator.process.dao.PointShopDao;
import com.chinacreator.process.util.HttpInvoke;
import com.chinacreator.process.util.JsonUtil;
import com.chinacreator.video.queue.MessageService;
import com.chinacreator.video.queue.bean.MessagePipe;

/**
 * 积分商城业务处理
 * @author xu.zhou
 * @date 20190622
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class PointShopMQJob {

	private Logger logger = Logger.getLogger(PointShopMQJob.class);
	
	private Logger log = Logger.getLogger("pointshop");

	@Autowired
	private DictionaryDao dictionaryDao;
	
	@Autowired
	private PointShopDao pointShopDao;

	@Autowired
	private MessageService messageService;

	public void doProcess() throws Exception {
		logger.info("接收积分商城订购队列JOB启动");
		if (dictionaryDao.getValue("recivemq").equals("0")) {
			long beginTime = System.currentTimeMillis();
			List<MessagePipe> list = messageService.reciveBatchMessage("pointShop", 500);
			logger.info("接收积分商城订购队列花费时间：" + (System.currentTimeMillis() - beginTime));
			List<PointShopMqBean> dataList = new ArrayList<PointShopMqBean>();
			for (MessagePipe messagePipe : list) {
				PointShopMqBean mqBean = transBean(messagePipe.getBody());
				dataList.add(mqBean);
			}
			
			//{orderNo=bd84d022-5bd6-4e71-b8ef-52a347fe4994, requestId=20190718112010821332728, type=pointShop, orderId=201907181120199638342}
//			List<PointShopMqBean> dataList = new ArrayList<PointShopMqBean>();
//			PointShopMqBean bean = new PointShopMqBean();
//			bean.setOrderId("201907181120199638342");
//			bean.setOrderNo("bd84d022-5bd6-4e71-b8ef-52a347fe4994");
//			bean.setRequestId("20190718112010821332728");
//			dataList.add(bean);
			List<PointShopMqBean> notrepeatList = getNotRepeatData(dataList);
			if(notrepeatList != null && notrepeatList.size()>0){
				for (PointShopMqBean mqBean : notrepeatList) {
					 this.handleOrder(mqBean);
			     }
			}
		} else {
			logger.info("停止积分商城订购接收队列消息");
		}
	}
	
	/**
	 * 业务处理
	 * @param mqBean
	 */
	public void handleOrder(PointShopMqBean mqBean){
		Map logMap = new HashMap();
		logMap.put("data", mqBean);
		PointShopOrderBean psoBean = null;
		String resultCode = "-1";
		String resultInfo = "";
		String huchiSpid = ""; //互斥业务ID，如果不为空，则业务互斥
		try {
			psoBean = pointShopDao.getOrderRec(mqBean.getOrderId());
			if(psoBean == null || !psoBean.getOrderNo().equals(mqBean.getOrderNo())){
				throw new BusinessException("9071", "无对应订单信息，我侧订单流水号："+mqBean.getOrderId());
			}
			//判断业务互斥
			huchiSpid = checkMutual(psoBean); //判断业务互斥
			if(!"".equals(huchiSpid)){
				psoBean.setVipstatus("4");
				throw new BusinessException("9003","已办理互斥业务");
			}

			String busiType = psoBean.getBusiType();
			
			//赠送会员状态，0成功，1未赠送，2赠送中，3失败，4不赠送
			//办理结果编码，0成功，1待处理，2处理中，其他为异常
			if(!"1".equals(psoBean.getResultCode())){
				throw new BusinessException("9072", "业务处理状态异常，"+psoBean.getResultCode());
			}
			
			//获取业务结束时间
			String newEndtime = this.getEndTime(psoBean);
			
			//默认记录赠送会员进行中，赠送成功后小毛会改为成功
			psoBean.setVipstatus("2");
			psoBean.setResultCode("2");
			psoBean.setResultInfo("处理中");
			if("1".equals(busiType)){ //会员
				//更新订购状态
				pointShopDao.updBusiStatus(psoBean,"TD_BACKBUSI_ORDER_REC");
				pointShopDao.updBusiStatus(psoBean,"TD_POINTS_ORDER_REC");
				String sendVipRes = sendVip(psoBean);
				if("9070".equals(sendVipRes)){
					psoBean.setVipstatus("5"); //赠送超时
					throw new BusinessException("9070","赠送会员超时", new String[0]);
				}
				if(!"0".equals(sendVipRes)){ //赠送失败，再查记录表状态，（成功后小毛会改为0）
					//获取办理编码，如果赠送会员成功，小毛会更新vipstatus为0
					sendVipRes = pointShopDao.getBackVipstatus(psoBean.getId());
				}

				if(!"0".equals(sendVipRes)){//赠送未成功
					//psoBean.setVipstatus("3");
					//throw new BusinessException("9002","赠送会员未成功");
					//未成功，当作超时处理启动重试
					psoBean.setVipstatus("5"); //赠送超时
					throw new BusinessException("9070","赠送会员超时", new String[0]);
				}
				psoBean.setVipstatus("0");
			}else if("2".equals(busiType)){ //免流
				psoBean.setVipstatus("4"); //不赠送
				pointShopDao.updBusiStatus(psoBean,"TD_BACKBUSI_ORDER_REC");
				pointShopDao.updBusiStatus(psoBean,"TD_POINTS_ORDER_REC");
				//设置订购结束时间和订购渠道
				psoBean.setEndtime(newEndtime);
				psoBean.setOrderchannel(psoBean.getAppId());		
				pointShopDao.order(psoBean);
				//saveOrderLog(psoBean,"0","成功"); //写订购日志
			}else if("3".equals(busiType)){ //会员+免流			
				pointShopDao.updBusiStatus(psoBean,"TD_BACKBUSI_ORDER_REC");
				pointShopDao.updBusiStatus(psoBean,"TD_POINTS_ORDER_REC");
				String sendVipRes = sendVip(psoBean);
				if("9070".equals(sendVipRes)){
					psoBean.setVipstatus("5"); //赠送超时
					throw new BusinessException("9070","赠送会员超时", new String[0]);
				}
				if(!"0".equals(sendVipRes)){ //赠送失败，再查记录表状态，（成功后小毛会改为0）
					//办理结果编码，0成功，1待处理，2处理中，其他为异常
					sendVipRes = pointShopDao.getBackVipstatus(psoBean.getId());
				}
				if("0".equals(sendVipRes)){//赠送成功，更新或新增订购关系
					psoBean.setVipstatus("0");
					//设置订购结束时间和订购渠道
					psoBean.setEndtime(newEndtime);
					psoBean.setOrderchannel(psoBean.getAppId());
					pointShopDao.order(psoBean);
					//saveOrderLog(psoBean,"0","成功");
				}else{
					//psoBean.setVipstatus("3");
					//throw new BusinessException("9002","赠送会员未成功");
					//未成功，当作超时处理启动重试
					psoBean.setVipstatus("5"); //赠送超时
					throw new BusinessException("9070","赠送会员超时", new String[0]);
				}
			}
			
			resultCode = "0";
			resultInfo = "成功";
		} catch (Exception e) {
			e.printStackTrace();
			if (e instanceof BusinessException) {
				resultInfo =  ((BusinessException) e).getMessage();
				resultCode = ((BusinessException) e).getCode();
			}else{
				resultCode = "8000";
				resultInfo = "系统错误，"+e.getMessage();
			}
		} finally{
			//更新订购操作表信息， 9071无订单信息，9072订单状态非待处理
			if(psoBean != null && !"9071".equals(resultCode) && !"9072".equals(resultCode)){
				try {
					if(resultInfo != null && resultInfo.length()>250){
						resultInfo = resultInfo.substring(0,250);
					}
					psoBean.setResultCode(resultCode);
					psoBean.setResultInfo(resultInfo);
					pointShopDao.updBackOrderRec(psoBean);
					pointShopDao.updOrderRec(psoBean);
					saveOrderLog(psoBean); //写订购日志
				} catch (Exception e) {
					e.printStackTrace();
					log.error("更新数据出现异常，"+psoBean.getUserid()+", resultCode:"+resultCode + ", resultInfo:"+resultInfo);
				}
			}
			
			//充值失败发送短信， 9002赠送会员失败， 0成功
			//if("9002".equals(resultCode) || "0".equals(resultCode)){
			if("0".equals(resultCode)){
				inserSmstMq(psoBean); //发送短信
			}
			
			//发送业务互斥短信
/*			if("9003".equals(resultCode)){
				this.inserSmstMqHuChi(psoBean,huchiSpid); //发送业务互斥短信
			}*/
			
			String bcstatus = "";
			if("9070".equals(resultCode)){//赠送会员超时
				inserVipRetryMq(psoBean); //赠送会员重试
			}else{
				//回调通知
				bcstatus = this.callBack(mqBean.getOrderId(),mqBean.getRequestId());
			}

			logMap.put("data2", psoBean);
			logMap.put("reusltCode", resultCode);
			logMap.put("resultInfo", resultInfo);
			logMap.put("bcstatus", bcstatus);
			log.info(JsonUtil.objectToJson(logMap));
		}
	}
	
	/**
	 * 写订购日志
	 * @param noBean	PointShopOrderBean
	 */
	private void saveOrderLog(PointShopOrderBean noBean){
		OrderLog orderLog = new OrderLog();
		orderLog.setApptype("2");
		orderLog.setArea(noBean.getArea());
		orderLog.setChannel(noBean.getOrderchannel());
		orderLog.setOrderstatus(0);
		orderLog.setStatus(0);
		orderLog.setCpid(noBean.getCpid());
		orderLog.setIsexperience(0);
		orderLog.setOrdertype("0");
		orderLog.setProvince(noBean.getProvince());
		orderLog.setSpid(noBean.getSpid());
		orderLog.setErrorcode(noBean.getResultCode());
		orderLog.setErrorinfo(noBean.getResultInfo());		
		//手机号码不为空，且有办理免流业务的，记录订购日志，单独只送会员的不记订购日志
		if(noBean.getUserid() != null && !"null".equals(noBean.getUserid().trim()) && noBean.getUserid().trim().length() == 11 && noBean.getEndtime() != null){
			orderLog.setUserid(noBean.getUserid().trim());
			try {
				this.pointShopDao.addOrderLog(orderLog);
			} catch (Exception e) {
				log.error("orderId: "+noBean.getId()+", 添加日志出现异常，"+e.getMessage());
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 查询本地订购关系表当前是否有已生效的订购关系
	 * @param orderInfo
	 * @return
	 * @throws Exception
	 */
	private Map hasEffect(PointShopOrderBean psoBean) throws Exception{
		Map reMap = new HashMap();
		boolean hasEffect = false;
		Map currOrderInfo = null;
		String currentTime = pointShopDao.currTime();
	    //查询用户本地订购关系表未失效的订购数据
		currOrderInfo = pointShopDao.findByUserAndSpid(psoBean.getUserid(),psoBean.getCpid(),psoBean.getSpid());
		if(currOrderInfo != null){//本地有订购关系
			if(Long.parseLong(currOrderInfo.get("ENDTIME").toString()) >= Long.parseLong(currentTime)){ //结束时间大于或等于当前时间，订购关系有效
				hasEffect = true; 
			}
		}
		reMap.put("hasEffect", hasEffect);
		reMap.put("currOrderInfo", currOrderInfo);
		return reMap;
	}
	

	/**
	 * 判断业务是否互斥
	 * @param orderInfo
	 * @throws Exception
	 */
	private String checkMutual(PointShopOrderBean psoBean) throws Exception{
		String huchiSpid = "";
		boolean result = false;
		String spid = psoBean.getSpid();
		List<HashMap> spList = pointShopDao.findSpInfo(spid);
		if(spList != null && spList.size()>0){
			if (spList.get(0).get("MUTEX") != null && !StringUtils.isEmpty(spList.get(0).get("MUTEX").toString())) {
				String[] mutexSpids = spList.get(0).get("MUTEX").toString().trim().split(",");					
				//[{ORDERIDA=2019052, AREA=长沙, SPID=1167, PROVINCE=湖南, ID=201905241107578658666, ORDERCHANNEL=t, STATUS=1, ORDERTIME=20190524104129, USERID=18673197465, CPID=youtu}]
			    //查询用户本地订购关系表未失效的订购数据
			 	List<HashMap> list = pointShopDao.findOrderRelaAll(psoBean.getUserid());	
				if (list != null && list.size() > 0) {
					for (String mutexSpid : mutexSpids) {
						for (HashMap hm : list) {
							if (hm.get("SPID") != null && !hm.get("SPID").equals(spid) && hm.get("SPID").equals(mutexSpid)
									&& !"2".equals(hm.get("STATUS"))) {
								huchiSpid = hm.get("SPID")+"";
								result = true;
								break;
							}
						}
					}
				}
			}
		}
		return huchiSpid;
	}
	
	/**
	 * 赠送会员
	 * @param orderInfo
	 * @return
	 * @throws Exception
	 */
	private String sendVip(PointShopOrderBean orderBean)throws Exception{
		String resultcode = "3"; //失败
		try {
			//http://114.255.201.238:8090/video-activity/eshop/vip
			String vipurl = this.dictionaryDao.getValue("backBusiVipUrl");
			String timestamp = (System.currentTimeMillis() / 1000) + "";
			String id = orderBean.getId(); //TD_POINTS_ORDER_REC的ID，也是TD_BACKBUSI_ORDER_REC的ID
			String userid = orderBean.getUserid();
			String orderid = orderBean.getOrderNo();
			String cpid = orderBean.getCpid();
			String spid = orderBean.getSpid();
			//String goodscode = orderBean.getGoodsCode();
			String pwd = "";
			List<HashMap> confList = pointShopDao.getBackBusiConf(cpid, spid);
			pwd = confList.get(0).get("PWD").toString();
			userid = DESUtil.encode(userid, pwd);
			//MD5(orderid+userid+goodscode+pwd+timestamp)转换为十六进制ASCII 码字符串，共32 个字符，全小写 userid= Des(手机号码，pwd)
			//MD5(orderid+userid+timestamp+pwd)转换为十六进制ASCII 码字符串，共32 个字符，全小写
			String signature = MD5.MD5Encode(orderid + userid + timestamp + pwd);
			signature = signature.toLowerCase();
			vipurl = vipurl + "?userid=" + URLEncoder.encode(userid, "utf-8")+ "&id=" +id+ "&orderid="+ orderid + "&cpid=" + cpid + "&spid=" + spid + "&timestamp="
					+ timestamp + "&signature=" + signature+ "&apptype=2";
			logger.info("vipurl: "+vipurl);
			//http://114.255.201.228:86/activity/eshop/vip?userid=iafPbU9aRLghY%2FEVMXFeag%3D%3D&orderid=201906231206498662914&goodscode=pointshop130&timestamp=1561445765&signature=47fe0e3900b29ef88fd0889b7c0e4cc6&apptype=5
			String result = URLUtil.get(vipurl,30*1000); //调赠送会员接口，超时时间设置为10秒
			log.info("赠送会员结果=> userid: " +userid+", orderid: "+orderBean.getOrderNo()+" , result: "+result);
			Map<?,?> map = JsonUtil.jsonToMap(result);
			resultcode = (String)map.get("resultcode");
			if(resultcode.equals("0")){
				logger.info("赠送成功");
			}			
		} catch (Exception e) {
			e.printStackTrace();
			log.error("id=>"+orderBean.getId()+"=>userid: "+orderBean.getUserid()+"赠送会员失败，"+e);
		}
		
		return resultcode;
	}
	
    /**
     * 回调积分商城
     * @param outputObj
     * @param id 我方生成的订单流水号
     * @param requestId 我方生成的请求ID
     */
    private String callBack(String id,String requestId){
    	String bcstatus = "1"; //回调状态，1未回调，0回调完成，2回调异常
    	String cbReqParams = ""; //回调请求报文
    	String cbRspParams = ""; //回调响应报文
    	try {
    		PointShopOrderBean bean = pointShopDao.getOrderRec(id);
    		TreeMap<String, String > orderOutObj = null;
    		if(bean != null){
    			orderOutObj = new TreeMap<String, String>();
    			orderOutObj.put("account",bean.getUserid());
    			orderOutObj.put("orderId",bean.getId());
    			orderOutObj.put("finishTime",bean.getFinishTime());
    			orderOutObj.put("goodsCode",bean.getSpid());
    			orderOutObj.put("orderNo",bean.getOrderNo());
    			orderOutObj.put("orderTime",bean.getOrderTime());
    			//orderOutObj.put("failDesc",bean.getResultInfo());
    			String orderStatus = "3"; //充值状态：1充值中 2充值成功3充值失败
    			String reusltCode = bean.getResultCode(); //办理结果编码，0成功，1待处理，2处理中，其他为异常
    			if("1".equals(reusltCode) || "2".equals(reusltCode)){
    				orderStatus = "1";
    			}else if("0".equals(reusltCode)){
    				orderStatus = "2";
    			}else{ //充值失败，设置失败原因
        			orderOutObj.put("failDesc",bean.getResultInfo());
    			}
    			orderOutObj.put("orderStatus",orderStatus); //充值状态：1充值中 2充值成功3充值失败
    		}
    		
    		if(orderOutObj != null){
    			String callBackUrl = this.dictionaryDao.getValue("pointShopCallBackUrl");//"http://demo.mall.10010.com:8104/jf-service/zcvcard/notify";
    			cbReqParams = JsonUtil.objectToJson(orderOutObj);
    			log.info("===========回调地址："+callBackUrl);
    			logger.info("===========回调请求参数："+cbReqParams);
    			cbRspParams  = HttpInvoke.sendHttpByPost("POST", callBackUrl, cbReqParams, getProperty());
    			log.info("============回调响应参数:"+cbRspParams);
    			if("0000".equals(cbRspParams)){
    				bcstatus = "0";
    			}
/*    			Map json = JsonUtil.jsonToMap(cbRspParams);
    			if(json != null){
        			String result = json.get("result")+"";
        			if("0000".equals(result)){
        				bcstatus = "0";
        			}
    			}*/
    		}else{
    			cbRspParams = "回调接口时未找到订购信息";
    			log.error("订单流水号=>"+id+"=>requestId=>"+requestId+"，回调接口时未找到订购信息");
    		}
		} catch (Exception e) {
			bcstatus = "2";
			e.printStackTrace();
			log.error("订单流水号=>"+id+"=>requestId=>"+requestId+"，回调接口出现异常，"+e.getMessage());
			cbRspParams = "返回报文: "+cbRspParams+"，回调异常，"+e.getMessage();
		} finally{
			if(cbRspParams != null && cbRspParams.length()> 250){
				cbRspParams = cbRspParams.substring(0,250);
			}
			log.info("订单流水号=>"+id+"=>requestId=>"+requestId+"=>cbReqParams: "+cbReqParams+", cbRspParams=>"+cbRspParams);
			addParamsByCb(cbReqParams, cbRspParams, requestId);
		}
    	return bcstatus;
    }
    
    /**
     * 添加回调报文
     * @param cbReqParams
     * @param cbRspParams
     * @param requestId
     */
    private void addParamsByCb(String cbReqParams,String cbRspParams, String requestId){
		try {
			//一个中文占两个 字节
			if(cbRspParams != null && cbRspParams.length()> 100){
				cbRspParams = cbRspParams.substring(0,100);
			}
			pointShopDao.updParamsByCb(cbReqParams, cbRspParams, requestId);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("requestId: "+requestId+", 更新callBack状态出现异常，"+e.getMessage());
		}
    }
		
	/**
	 * 去重复数据
	 * @param oldBeans
	 * @return
	 */
	public List<PointShopMqBean> getNotRepeatData( List<PointShopMqBean> oldBeans){

		  List<PointShopMqBean> newBeans = new ArrayList<PointShopMqBean>();
		  Map<String,PointShopMqBean> map = new HashMap<String, PointShopMqBean>();
		  for (PointShopMqBean bean : oldBeans) {
			  if(map.containsKey(bean.getOrderId())){
				  //insertvacMq(bean);
			  }else{
				  map.put(bean.getOrderId(), bean);
			  }
		  }
		  Iterator<String> it = map.keySet().iterator();  
	      while (it.hasNext()) {  
	          String key = it.next().toString();  
	          newBeans.add(map.get(key));  
	      }
	      logger.info("积分商城:old="+oldBeans.size()+"  new:"+newBeans.size());
		  return newBeans;
	  }

	/**
	 * 解析数据
	 * @param body
	 * orderId TD_POINTS_ORDER_REC表ID字段，我方生成的订单流水号
     * orderNo 积分商城订单号
     * requestId 返回给客户的请求ID
	 * @return
	 */
	public PointShopMqBean transBean(Map<String, Object> body) {
		String jsonStr = JsonUtil.objectToJson(body);
		return (PointShopMqBean) JsonUtil.jsonToBean(jsonStr, PointShopMqBean.class);
	}
	
	/**
	 * 发送短信
	 * @param psoBean
	 * @param resultCode
	 */
	public void inserSmstMq(PointShopOrderBean psoBean){
		try{
			if(psoBean != null){
				Map<String, String> map = new HashMap<String, String>();
				map.put("userid", psoBean.getUserid());
				map.put("cpid", psoBean.getCpid());
				map.put("spid", psoBean.getSpid());
				map.put("result", "0");
				map.put("channel", "");
				map.put("style","0000");
				map.put("times", "");
				map.put("orderType", "");
				map.put("type", "cssms");
				map.put("busiType", "tran_succ");
				logger.info(JsonUtil.objectToJson(map));
				String  mqReciveUrl = dictionaryDao.getValue("mqReciveUrl");
				URLUtil.post(mqReciveUrl, JsonUtil.objectToJson(map));
			}

		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 发送业务互斥短信
	 * @param inputObj
	 * @param huchiSpid 
	 */
	public void inserSmstMqHuChi(PointShopOrderBean psoBean, String huchiSpid){
		try{
			String huchiSpName = "";
			List<HashMap> spList2 = pointShopDao.findSpInfo(huchiSpid);
			if(spList2 != null && spList2.size()>0 && spList2.get(0).get("SPNAME") != null){
				huchiSpName = spList2.get(0).get("SPNAME").toString();
			}
			String bt = psoBean.getBusiType();
			//免流，或者免流+会员，业务互斥要发短信
			if(("2".equals(bt) || "3".equals(bt)) && !"".equals(huchiSpName)){
				String succFlag = "fail";
				String busiType = "tran_cdn_fail"; //免流业务互斥短信
				if("3".equals(bt)){
					busiType = "tran_vipcdn_cdnfail"; //免流+会员业务互斥短信
				}

				Map<String, String> map = new HashMap<String, String>();
				map.put("userid", psoBean.getUserid());
				map.put("cpid", psoBean.getCpid());
				map.put("spid", psoBean.getSpid());
				map.put("result", "0");
				map.put("channel", "");
				map.put("param1", huchiSpName);
				map.put("style","0000");
				map.put("times", "");
				map.put("orderType", "");
				map.put("type", "cssms");
				map.put("busiType", busiType);
				logger.info(JsonUtil.objectToJson(map));
				String  mqReciveUrl = dictionaryDao.getValue("mqReciveUrl");
				URLUtil.post(mqReciveUrl,  JsonUtil.objectToJson(map));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	 /**
     * 获取请求属性性
     * @return
     */
    private static Map getProperty(){
    	 Map reqProperty = new HashMap();
    	 reqProperty.put("Content-type", "application/json;charset=UTF-8");
    	 return reqProperty;
    }
    
    /*
	 * 赠送会员超时异步重试
	 * @param orderInfo
	 */
	public void inserVipRetryMq2(PointShopOrderBean bean){
		try{
			Map<String, String> map = new HashMap<String, String>();
			map.put("userid", bean.getUserid());
			map.put("id", bean.getId());
			map.put("orderid", bean.getOrderNo());
			map.put("type", "vipretry");	
			logger.info("==============重试会员赠送队列参数，"+map);
			String  mqReciveUrl = dictionaryDao.getValue("mqReciveUrl");
			URLUtil.post(mqReciveUrl, JsonUtil.objectToJson(map));	
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
    /*
	 * 赠送会员超时异步重试队列推送，最多重试三次
	 * @param orderInfo
	 */
	public void inserVipRetryMq(PointShopOrderBean bean){
		try {
			MessagePipe mpipe = new MessagePipe();
			mpipe.setHeader("vipretry");
			mpipe.addBody("userid", bean.getUserid());
			mpipe.addBody("id",  bean.getId());
			mpipe.addBody("orderid",  bean.getOrderNo());
			mpipe.addBody("sendcont","1");
			this.messageService.sendMessage(mpipe);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("赠送会员超时异步重试队列推送出现异常，"+e.getMessage());
		}	
	}
	
	/**
	 * 获取endtime
	 * @param bean	PointShopOrderBean
	 * @return
	 */
	private String getEndTime(PointShopOrderBean bean)throws Exception{
		String endtime = pointShopDao.getBackEndtime(bean.getId());
		if(endtime == null || "null".equals(endtime) || endtime.length() != 14){
			endtime = "";
		}
		if("".equals(endtime)){
			String currEndtime = "";
			Map reMap = this.hasEffect(bean);
			if((Boolean)reMap.get("hasEffect")){ //存在有效订购关系
				Map currOrderInfo = (Map)reMap.get("currOrderInfo");
				currEndtime = (String)currOrderInfo.get("ENDTIME");
			}
			//[{PWD=kijkfds, NETDAYS=31, CHANNEL=test_01, NETDAYS=2, SPID=1168, BUSITYPE=3, CPID=youtu}]
			HashMap confHm = pointShopDao.getBackBusiConf(bean.getCpid(), bean.getSpid()).get(0);
			//String busiType = confHm.get("BUSITYPE")+"";
			if("2".equals(confHm.get("BUSITYPE")) || "3".equals(confHm.get("BUSITYPE"))){ 
				if(currEndtime != null && !"".equals(currEndtime)){			
					endtime = pointShopDao.endtimeParamDay(confHm.get("NETDAYS")+"", currEndtime);
				}else{
					endtime = pointShopDao.currParamDay(confHm.get("NETDAYS")+"");
				}
			}
		}

		return endtime;
	}
}
