package com.chinacreator.video.restful.controller;

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.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.util.RequestUtil;
import com.chinacreator.video.util.DesUtil;
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.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 音乐侧接口
 * zhengrong.yan
 * 20200904
 */
@RestController
public class MuorderController {
	
	private static Logger logger = Logger.getLogger(String.valueOf(MuorderController.class));
	
	private static Logger log = Logger.getLogger("muorder");
	
	@Autowired
	private MuorderService service;

    @Autowired
    private BackBusiOrderAsynService backBusiOrderAsynService;

    @Autowired
    private ChannelSyncService syncService;

	@Autowired
	private DictionaryDao dictionaryDao;
		
	/**
	 * POST方式办理业务
	 * @param inputObj
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@Log
    @RequestMapping(value = "/muorder", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> order(@RequestBody (required=false) OrderInfo orderInfo) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	Map<String,Object> logmap = new HashMap<String,Object>();
    	logmap.put("forward", "music");
    	logmap.put("reqData", orderInfo);
    	String resultcode = "";
    	String errorinfo = "";
//		System.out.println(orderInfo);
    	try {
    		orderInfo.setInvokeface("muorder");
    		orderInfo.setTimes(System.currentTimeMillis()+"");
   	        service.busiCheck(orderInfo);
			String numberlimit = dictionaryDao.getValue("Numberlimit");
			if (numberlimit != null && numberlimit.indexOf(orderInfo.getUserid().substring(0, 7)) != -1) {
				throw new BussinessException("9010", "用户为黑名单用户");
			}
            //orderInfo.setApptype(RequestUtil.getMobType(request));
            //获取spid
            String spid = orderInfo.getSpid();
            boolean bl=backBusiOrderAsynService.getBusiCount(spid);
            if(bl){
                orderInfo.setOrderstatus("3");
                logmap.put("forward", "woread");
                backBusiOrderAsynService.orderBusi(orderInfo);
                //service.busiOper(orderInfo);
            }else{
                orderInfo.setOrderstatus("2");
                service.busiOper(orderInfo);
            }
            resultcode ="0";
        	errorinfo = "";
		} catch (Exception e) {
			e.printStackTrace();
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else {
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
		} finally {
            logmap.put("resultcode",resultcode);
            logmap.put("errorinfo",errorinfo);
            logmap.put("rspData", outpipe.getData());
            log.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            if ("0".equals(resultcode)){
                syncService.insertChannelInfo(orderInfo);
            }
			return outpipe;
		}
    }
}
