package com.chinacreator.video.web;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.chinacreator.video.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.chinacreator.video.annotation.Log;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.dto.IncrDataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.restful.util.JsonUtil;
import com.chinacreator.video.service.CsmakeupOrderService;
import com.chinacreator.video.util.URLUtil;

/**
 * 畅视C+会员组合产品订购接口
 * @author xu.zhou
 * @date 20201323
 */
@RestController
public class CsmakeupOrderControl {
	
	private static final  Logger logger = LoggerFactory.getLogger(CsmakeupOrderControl.class);
	
	private static final  Logger log = LoggerFactory.getLogger("csmakeup");
	
	@Autowired
	private CsmakeupOrderService service;
	
	@Autowired
	private DictionaryDao dictionaryDao;
	
	@Log
	@RequestMapping("/csmakeup")
	public DataOutPipe<?> hand(OrderInfo orderInfo) throws Exception {
		Map<String,Object> logmap = new HashMap<String,Object>();
		DataOutPipe outpipe = new DataOutPipe();
		String resultcode = "";
		String errorinfo = "";
		logmap.put("forward", "csmakeup");
		logmap.put("userInfo", orderInfo);
		try {
		    orderInfo.setInvokeface("csmakeup");
			service.busiCheck(orderInfo);
			String numberlimit = dictionaryDao.getValue("Numberlimit");
			if (numberlimit != null && numberlimit.indexOf(orderInfo.getUserid().substring(0, 7)) != -1) {
				throw new BussinessException("9010", "用户为黑名单用户");
			}
			logmap.put("userInfo", orderInfo);
			service.busiProc(orderInfo);
			resultcode = "0";
			errorinfo = "ok";
		}catch(Exception e) {
			e.printStackTrace();
			resultcode = "80003";
			errorinfo = "出现异常，"+e.getMessage();
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
				throw e;
			}else {
				throw new BussinessException(resultcode, errorinfo);
			}
		}finally {
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
			logmap.put("resultcode", resultcode);
			logmap.put("errorinfo", errorinfo);
			log.info(JSON.toJSONString(logmap));
			//发送短信
			inserSmstMq(orderInfo, resultcode);
		}
		return outpipe;
	}
	
	/**
	 * 推送办理成功的短信到队列
	 * @param orderInfo
	 */
	public void inserSmstMq(OrderInfo orderInfo, String result){
		try{
			if("0".equals(result)){
				Map<String, String> map = new HashMap<String, String>();
				map.put("userid", orderInfo.getUserid());
				map.put("cpid", orderInfo.getCpid());
				map.put("spid", orderInfo.getSpid());
				map.put("result", "0");
				map.put("channel", "");
				map.put("style","0000");
				map.put("times", "");
				map.put("orderType", "");
				map.put("type", "cssms");	
				if("0".equals(orderInfo.getType())){//订购
					map.put("busiType", "tran_succ");	
				}else{//退订
					map.put("busiType", "cancel_succ");	
				}
				String  mqReciveUrl = dictionaryDao.getValue("mqReciveUrl");			
				URLUtil.post(mqReciveUrl, JsonUtil.convertObject2Json(map));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
