TopicControllerApi.java 6.94 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.serviceImpl.CommentsServiceImpl;
import com.cnlive.shenhe.serviceImpl.TopicServiceImpl;
import com.cnlive.shenhe.utils.AuditPass;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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
    AuditPass Pass;
    @Autowired
    CommentsServiceImpl commentsService;
    @Autowired
    TopicServiceImpl topicService;
    
    @Autowired
    TransitServiceUtils t;

    /**
     * @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);
        JSONObject result = new JSONObject();
        
        //获取所有参数
        String topic_id = json.getString("topicId");//话题id
//        Integer spId = Integer.parseInt(json.getString("spId"));//spId
//        Integer spId = json.getInteger("spId");//spId
        Integer spId=null;
        try {
        	spId=json.getInteger("spId");
		} catch (Exception e) {
			spId=null;
		}
        String topicTitle = json.getString("topicTitle");//话题标题
        String topicPic = json.getString("topicPic");//话题图片
        String topic_text = json.getString("topicMsg");//话题内容
        String topic_time = json.getString("createTime");//话题创建时间
        
        String topic_tag = json.getString("topicTag");//话题标签
        String topic_url = json.getString("topicUrl");//话题url
        
        //判断是否存在
        ShyTopic shyTopic=new ShyTopic();
        shyTopic.setTopic_id(topic_id);
        List<ShyTopic> topicList = topicService.findshyTopic(shyTopic);
        
        //获取审核类型
        Integer shenheType = topicService.getShenheType(spId);
        //存在则更新
        if (topicList.size() > 0) {
        	ShyTopic shyTopic1 = topicList.get(0);
        	shyTopic1.setTitle(topicTitle);//话题标题
        	shyTopic1.setTopic_image(topicPic);//话题图片
        	shyTopic1.setShenhe_type(shenheType);//审核类型
        	shyTopic1.setTopic_text(topic_text);//话题内容
        	shyTopic1.setTopic_tag(topic_tag);//话题标签
        	shyTopic1.setTopic_url(topic_url);//话题url
        	shyTopic1.setTopic_time(topic_time);//话题创建时间
        	shyTopic1.setSp_id(spId);//spid
        	shyTopic1.setShenhe_status(CommonConst.AUDIT_INTT);;//当前话题的审核状态
        	shyTopic1.setUpdate_date(CommonUtils.getCurrentTime());//最后一次更新时间
            
            
           
            boolean YorN = topicService.updateshyTopic(shyTopic1);
            if (YorN == true) {
            	return new ResponseBean(ErrorEnum.SUCCESS,new JSONObject().put("topic_id", topic_id));
            } else {
                return new ResponseBean(ErrorEnum.ERROR,new JSONObject().put("topic_id", topic_id));
            }
        }
        //如果不存在直接插入
        shyTopic.setTitle(topicTitle);//话题标题
    	shyTopic.setTopic_image(topicPic);//话题图片
    	shyTopic.setShenhe_type(shenheType);//审核类型
    	shyTopic.setTopic_text(topic_text);//话题内容
    	shyTopic.setTopic_tag(topic_tag);//话题标签
    	shyTopic.setTopic_url(topic_url);//话题url
    	shyTopic.setTopic_time(topic_time);//话题创建时间
    	shyTopic.setSp_id(spId);//spid
    	shyTopic.setShenhe_status(CommonConst.AUDIT_INTT);;//当前话题的审核状态
    	shyTopic.setUpdate_date(CommonUtils.getCurrentTime());//最后一次更新时间
    	shyTopic.setCreate_date(CommonUtils.getCurrentTime());//送审时间
        boolean YorN = topicService.impotshyTopic(shyTopic);
        if (YorN == true) {
        	return new ResponseBean(ErrorEnum.SUCCESS,new JSONObject().put("topic_id", topic_id));
        } else {
            return new ResponseBean(ErrorEnum.ERROR,new JSONObject().put("topic_id", topic_id));
        }
    }
    
    @GetMapping("/send")
    @ResponseBody
    public String send() {
    	System.out.println("1111");
    	JSONObject params = new JSONObject();
        params.put("video_id", "11111");
        params.put("state", "22222");
        params.put("attr_tags", "33333");
        params.put("msg", "4444");
    	t.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 topic_id=dataObject.getString("topic_id");
        //获取当前话题实体
        ShyTopic topic=new ShyTopic();
        topic.setTopic_id(topic_id);
        topic = topicService.getShyTopic(topic);
        
        if(errorCode.equals("0")){
        	logger.info("话题id为:"+topic_id+",cms系统已通知");
        	if(topic!=null){
        	  topic.setShenhe_msg(topic.getShenhe_msg()+",cms系统已通知");
        	}
        }else{//中转服务器失败
        	logger.info("话题id为:"+topic_id+",中转服务器调用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<>();
        ee.put("topic_id", topic_id);
        if(rs){
        	return new ResponseBean(ErrorEnum.SUCCESS,ee);
        }
        
        return new ResponseBean(300,"话题送审回调成功,入库失败",ee);
     }
    
    
}