package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.dto.DataOutPipe;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.mapper.ChannelDao;
import com.chinacreator.video.mapper.ColorSealDao;
import com.chinacreator.video.restful.service.ColorSealService;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * 彩铃侧同步接口
 * zhengrong.yan
 * 20201112
 */
@RestController
public class ColorSealController {

    private static Logger logger = Logger.getLogger(String.valueOf(ColorSealController.class));

    private static Logger log = Logger.getLogger("colorsealnotify");

    private static Logger log2 = Logger.getLogger("colorsealset");

    private static Logger log3 = Logger.getLogger("colorsealverify");

    private static Logger log4 = Logger.getLogger("colorsealquery");


    @Autowired
    private ColorSealService service;

    @Autowired
    ChannelDao channelDao;

    @Autowired
    private ColorSealDao colorSealDao;


    /**
     * POST方式办理业务
     *
     * @param
     * @param request
     * @return
     * @throws Exception
     */
    /*彩铃验证接口*/
    @RequestMapping(value = "/colorsealverify", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> verify(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "colorsealverify");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        String id = "";
        try {
            com.alibaba.fastjson.JSONObject inputObj = new JSONObject().parseObject(params);
            String orderchannel = "";
            String cancelchannel = "";

            service.busiCheck(inputObj, 0);//type 0 notify 1 setting
            OrderInfo orderInfo = service.setBean(inputObj);
            String channel = inputObj.getString("channel");
            String pwd = channelDao.queryChannel(channel).getPassword();
            orderInfo.setUserid(DesUtil.decode(orderInfo.getUserid(), pwd));
            service.verify(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.toString();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log3.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            return outpipe;
        }
    }


    /**
     * POST方式办理业务
     *
     * @param
     * @param request
     * @return
     * @throws Exception
     */
    /*彩铃通知接口*/
    @RequestMapping(value = "/colorsealnotify", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> notify(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "colorsealnotify");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        String id = "";
        try {
            com.alibaba.fastjson.JSONObject inputObj = new JSONObject().parseObject(params);
            String orderchannel = "";
            String cancelchannel = "";
            if (orderchannel.equals("") || cancelchannel.equals("")) {
                service.busiCheck(inputObj, 0);//type 0 notify 1 setting
                OrderInfo orderInfo = service.setBean(inputObj);
                String channel = inputObj.getString("channel");
                String pwd = channelDao.queryChannel(channel).getPassword();
                orderInfo.setUserid(DesUtil.decode(orderInfo.getUserid(), pwd));
                id = service.clnotify(orderInfo);
                resultcode = "0";
                errorinfo = "";
            } else {
                resultcode = "7000";
                errorinfo = "不接收该队列";
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException) {
                resultcode = ((BussinessException) e).getCode();
                errorinfo = ((BussinessException) e).getMessage();
            } else {
                resultcode = "8000";
                errorinfo = "出现异常，" + e.toString();
            }
        } 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 (!"".equals(id)) {
                HashMap map = new HashMap();
                map.put("id", id);
                map.put("resultcode", resultcode);
                map.put("resultinfo", errorinfo);
                colorSealDao.upColorSealRecResult(map);
            }
            return outpipe;
        }
    }

    /*彩铃一键设置接口*/
    @RequestMapping(value = "/colorsealset", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> setting(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "colorsealset");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        try {
            com.alibaba.fastjson.JSONObject inputObj = new JSONObject().parseObject(params);
            service.busiCheck(inputObj, 1);
            OrderInfo orderInfo = service.setBean(inputObj);
            String channel = inputObj.getString("channel");
            String pwd = channelDao.queryChannel(channel).getPassword();
            orderInfo.setUserid(DesUtil.decode(orderInfo.getUserid(), pwd));
            service.clset(orderInfo, inputObj.getString("ringid"));
            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.toString();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            logmap.put("rspData", outpipe.getData());
            log2.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            return outpipe;
        }
    }

    //音乐侧查询回调接口
    @RequestMapping(value = "/colorsealquery", method = RequestMethod.POST, produces = "application/json")
    public DataOutPipe<?> query(@RequestBody(required = false) String params, HttpServletRequest request) throws Exception {
        DataOutPipe outpipe = new DataOutPipe();
        Map<String, Object> logmap = new HashMap<String, Object>();
        logmap.put("forward", "colorsealquery");
        logmap.put("reqData", params);
        String resultcode = "";
        String errorinfo = "";
        String id = "";
        try {
            com.alibaba.fastjson.JSONObject inputObj = new JSONObject().parseObject(params);
            service.busiCheck(inputObj, 0);//type 0 notify 1 setting
            OrderInfo orderInfo = service.setBean(inputObj);
            String channel = inputObj.getString("channel");
            String pwd = channelDao.queryChannel(channel).getPassword();
            orderInfo.setUserid(DesUtil.decode(orderInfo.getUserid(), pwd));
            service.query(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.toString();
            }
        } finally {
            logmap.put("resultcode", resultcode);
            logmap.put("errorinfo", errorinfo);
            log4.info(JSON.toJSONString(logmap));
            outpipe.setResultcode(resultcode);
            outpipe.setErrorinfo(errorinfo);
            return outpipe;
        }
    }
}
