package com.chinacreator.video.restful.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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 com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.ChannelOrgService;

/**
 * 柏源二级渠道匹配接口类
 * @author xu.zhou
 * @date 20210809
 */
@RestController
public class ChannelOrgController {
	
	private static Logger logger = Logger.getLogger(ChannelOrgController.class);
	
	private static Logger log = Logger.getLogger("pointshop");

	@Autowired
	private ChannelOrgService service;
	
	/**
	 * 新增或更新渠道匹配信息
	 * @param inputObj
	 * @param request
	 * @return
	 * @throws Exception 
	 */
    @RequestMapping(value = "/scosetinfo", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?>  setinfo(@RequestBody String params, HttpServletRequest request) throws Exception {
    	DataOutPipe outpipe = new DataOutPipe();
    	HashMap<String, Object> logmap = new HashMap<String, Object>();
    	logmap.put("forward", "scosetinfo");
    	logmap.put("reqData", params);
    	String resultcode = "";
        String errorinfo = "";
    	try {
    		JSONObject inputObj = JSON.parseObject(params);
    		//logmap.put("inputObj", inputObj);
    		service.checkBusi(inputObj, "scosetinfo");
        	
    		JSONArray resdata = service.operBusi(inputObj, "scosetinfo");
    		logmap.put("rspDate", resdata);
    		outpipe.setData(resdata);   		
     		
        	resultcode = "0";
            errorinfo = "ok";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else {
				e.printStackTrace();
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
		} finally {
			logmap.put("resultcode",resultcode);
			logmap.put("errorinfo",errorinfo);	
			
			//发送短信
			log.info(logmap);
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
		}

		return outpipe;
    }
    
    /**
     * 根据子渠道编码获取渠道匹配信息
     * @param param
     * @param request
     * @return
     */
    @RequestMapping(value = "/scogetinfo", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> scogetinfo(@RequestBody String params, HttpServletRequest request) {
    	DataOutPipe outpipe = new DataOutPipe();
    	HashMap<String, Object> logmap = new HashMap<String, Object>();
    	logmap.put("forward", "scogetinfo");
    	logmap.put("reqData", params);
    	String resultcode = "";
        String errorinfo = "";
    	try {
    		JSONObject inputObj = JSON.parseObject(params);
    		service.checkBusi(inputObj, "scogetinfo");

    		JSONArray resdata = service.operBusi(inputObj, "scogetinfo");
    		logmap.put("rspDate", resdata);
    		outpipe.setData(resdata); 

        	resultcode = "0";
            errorinfo = "ok";
		} catch (Exception e) {
			if(e instanceof BussinessException) {
				resultcode = ((BussinessException) e).getCode();
				errorinfo = ((BussinessException) e).getMessage();
			}else {
				e.printStackTrace();
				resultcode = "8000";
				errorinfo = "出现异常，"+e.getMessage();
			}
			outpipe.setData(new JSONArray()); 
		} finally {
			logmap.put("resultcode",resultcode);
			logmap.put("errorinfo",errorinfo);	
			//记录日志
			log.info(logmap);
			outpipe.setResultcode(resultcode);
			outpipe.setErrorinfo(errorinfo);
		}

		return outpipe;
    }
}
