package com.chinacreator.video.restful.controller;

import com.alibaba.fastjson.JSONObject;
import com.chinacreator.video.bean.OrderInfo;
import com.chinacreator.video.exception.BussinessException;
import com.chinacreator.video.restful.service.CheckSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.ws.soap.Addressing;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CheckSyncContoller {

    @Autowired
    private CheckSyncService  checkSyncService;
    private Logger logger = LoggerFactory.getLogger(CheckSyncContoller.class);
    @PostMapping("/checkCapSync")
    public Map<String,String> insertSync(@RequestBody Map<String,String> params){

        String resultcode="0";
        String errorinfo="";
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setInvokeface("checkCapSync");
        orderInfo.setChannel(params.get("CHANNEL"));
        orderInfo.setCpid("temp");
        orderInfo.setSpid("0");
        try {
            checkSyncService.check(orderInfo);
            checkSyncService.insertSync(params);
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof BussinessException){
                resultcode = ((BussinessException) e).getCode();
                errorinfo = e.getMessage();
            }else {
                resultcode ="8000";
                errorinfo ="系统异常请稍后再试!";
            }

        }
        Map<String,String> resultMap=new HashMap<>();
        resultMap.put("resultcode",resultcode);
        resultMap.put("errorinfo",errorinfo);
        logger.info("请求参数---"+JSONObject.toJSONString(params)+"--返回报文--"+JSONObject.toJSONString(resultMap));

        return resultMap;
    }
}
