package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BusinessException;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.DictionaryDao;
import com.chinacreator.video.restful.service.BackBusiOrderAsynService;
import com.chinacreator.video.restful.service.ChannelSyncService;
import com.chinacreator.video.restful.service.MuorderService;
import com.chinacreator.video.restful.service.SbSyncService;
import com.chinacreator.video.restful.util.JsonUtil;
import com.chinacreator.video.restful.util.RequestUtil;
import com.chinacreator.video.util.DesUtil;
import com.chinacreator.video.util.URLUtil;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 深报订购关系同步
 * 接收深报支撑的订购/退订数据，在本地生成订购关系
 * @author xu.zhou
 * @date 20220214
 */
@RestController
public class SbSyncController {
	
	private static Logger logger = Logger.getLogger(String.valueOf(SbSyncController.class));
	
	private static Logger log = Logger.getLogger("muorder");
	
	@Autowired
	private SbSyncService service;

	@Autowired
	private DictionaryDao dictionaryDao;
		
	/**
	 * POST方式办理业务
	 * @param inputObj
	 * @param request
	 * @return
	 * @throws Exception 
	 */
    @RequestMapping(value = "/syncorder", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> order(@RequestBody (required=false) OrderInfo orderInfo, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("forward", "syncorder");
    	logmap.put("reqData", orderInfo);
    	orderInfo.setInvokeface("syncorder");
    	long startime = System.currentTimeMillis();
    	
    	String resultcode = "";
    	String errorinfo = "";
    	try {
    		orderInfo.setTimes(startime+"");
   	        service.busiCheck(orderInfo);
   	        service.busiOper(orderInfo);
            resultcode ="0";
        	errorinfo = "";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else {
				e.printStackTrace();
				log.error("出现异常，"+e.getMessage(), e);
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
		} finally {
            logmap.put("resultcode",resultcode);
            logmap.put("errorinfo",errorinfo);
            logmap.put("rspData", outpipe.getData());
            logmap.put("times", System.currentTimeMillis()-startime);
            
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
			//发送短信
			inserSmstMq(orderInfo, resultcode);
			
			logmap.put("alltimes", System.currentTimeMillis()-startime);
			log.info(JSON.toJSONString(logmap));
		}
    	
    	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();
		}
	}
}
