TopicControllerApi.java 5.82 KB
package com.cnlive.shenhe.api;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.entity.ShyTopic;
import com.cnlive.shenhe.service.TopicService;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.TransitServiceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Controller
@RequestMapping("/api/topic")
public class TopicControllerApi {

    private static Logger logger = LoggerFactory.getLogger(TopicControllerApi.class);
    @Autowired
    TopicService topicService;
    @Autowired
    TransitServiceUtils transitServiceUtils;

    /**
     * @author liwei
     * @Date: 2019/12/13 15:17
     * @Description: 话题送审接口
     */
    @PostMapping("/import")
    @ResponseBody
    public ResponseBean impotTopic(@RequestBody String param) {
        logger.info("话题入库,param=====" + param);
        JSONObject json = JSONObject.parseObject(param);
        boolean yorN;
        //获取所有参数
        //话题id
        String topicId = json.getString("topicId");
        Integer spId = null;
        try {
            spId = json.getInteger("spId");
        } catch (Exception e) {
        }
        //判断是否存在
        ShyTopic shyTopic = new ShyTopic();
        shyTopic.setTopic_id(topicId);
        List<ShyTopic> topicList = topicService.findShyTopic(shyTopic);
        //获取审核类型
        Integer shenheType = topicService.getShenheType(spId);
        if (topicList.size() > 0) {
            //存在则更新
            ShyTopic shyTopic1 = topicList.get(0);
            yorN = saveOrUpdateTopic(json, shyTopic1, shenheType, spId);
        } else {
            //送审时间
            shyTopic.setCreate_date(CommonUtils.getCurrentTime());
            //如果不存在直接插入
            yorN = saveOrUpdateTopic(json, shyTopic, shenheType, spId);
        }
        if (yorN) {
            return new ResponseBean(ErrorEnum.SUCCESS, new JSONObject().put("topic_id", topicId));
        } else {
            return new ResponseBean(ErrorEnum.ERROR, new JSONObject().put("topic_id", topicId));
        }
    }

    /**
     * 话题 保存和更新
     *
     * @param json
     * @param shyTopic
     * @param shenheType
     * @param spId
     * @return
     */
    private boolean saveOrUpdateTopic(JSONObject json, ShyTopic shyTopic, Integer shenheType, Integer spId) {
        //话题标题
        shyTopic.setTitle(json.getString("topicTitle"));
        //话题图片
        shyTopic.setTopic_image(json.getString("topicPic"));
        //审核类型
        shyTopic.setShenhe_type(shenheType);
        //话题内容
        shyTopic.setTopic_text(json.getString("topicMsg"));
        //话题标签
        shyTopic.setTopic_tag(json.getString("topicTag"));
        //话题url
        shyTopic.setTopic_url(json.getString("topicUrl"));
        //话题创建时间
        shyTopic.setTopic_time(json.getString("createTime"));
        //spid
        shyTopic.setSp_id(spId);
        //当前话题的审核状态
        shyTopic.setShenhe_status(CommonConst.AUDIT_INTT);
        //最后一次更新时间
        shyTopic.setUpdate_date(CommonUtils.getCurrentTime());
        if (shyTopic.getId() == null) {
            return topicService.impotShyTopic(shyTopic);
        } else {
            return topicService.updateShyTopic(shyTopic);
        }
    }

    @GetMapping("/send")
    @ResponseBody
    public String send() {
        logger.info("1111");
        JSONObject params = new JSONObject();
        params.put("video_id", "11111");
        params.put("state", "22222");
        params.put("attr_tags", "33333");
        params.put("msg", "4444");
        transitServiceUtils.send("cms", "/api/callback", false, params);
        return "1111";
    }

    @PostMapping("/callback")
    @ResponseBody
    public ResponseBean callback(@RequestBody String param) {
        logger.info("话题中转回调通知,param=====" + param);
        JSONObject json = JSONObject.parseObject(param);
        //获取中转服务器消息
        String errorCode = json.getString("errorCode");
        String errorMessage = json.getString("errorMessage");
        //获取接收方消息 
        JSONObject dataObject = json.getJSONObject("data");
        String topicId = dataObject.getString("topic_id");
        //获取当前话题实体
        ShyTopic topic = new ShyTopic();
        topic.setTopic_id(topicId);
        topic = topicService.getShyTopic(topic);

        String numcode = "0";
        if (numcode.equals(errorCode)) {
            logger.info("话题id为:" + topicId + ",cms系统已通知");
            if (topic != null) {
                topic.setShenhe_msg(topic.getShenhe_msg() + ",cms系统已通知");
            }
        } else {//中转服务器失败
            logger.info("话题id为:" + topicId + ",中转服务器调用cms失败,失败消息:" + errorMessage);
            topic.setShenhe_msg(topic.getShenhe_msg() + ",中转服务器调用cms失败,失败消息:" + errorMessage);
            topic.setShenhe_status(CommonConst.AUDIT_INTT);
            topic.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
            topic.setAuditor_id(null);
        }
        boolean rs = topicService.updateShyTopic(topic);
        Map<String, Object> ee = new HashMap<>(16);
        ee.put("topic_id", topicId);
        if (rs) {
            return new ResponseBean(ErrorEnum.SUCCESS, ee);
        }

        return new ResponseBean(300, "话题送审回调成功,入库失败", ee);
    }


}