ContentControllerApi.java 9.19 KB
package com.cnlive.shenhe.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.entity.ShyContent;
import com.cnlive.shenhe.service.ContentService;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
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.Date;
import java.util.List;

/**
 * 图文审核
 *
 * @author
 */
@Controller
@RequestMapping("/api/content")
public class ContentControllerApi {
    private static Logger logger = LoggerFactory.getLogger(ContentControllerApi.class);

    @Autowired
    ContentService contentService;

    /**
     * @author liwei
     * @Date: 2019/05/15 11:17
     * @Description: 图文送审接口
     */
    @PostMapping("/import")
    @ResponseBody
    public ResponseBean importContent(String param) {
        logger.info("图文入库,param=====" + param);
        JSONObject json = JSONObject.parseObject(param);
        boolean yorN;
        //判断是否存在
        String contentId = json.getString("contentId");
        //图文标签
        String contentTag = json.getString("content_tag");
        String newContentTag = StringUtils.isEmpty(contentTag) ? "content" : contentTag;

        ShyContent shyContent = new ShyContent();
        shyContent.setContent_id(contentId);
        shyContent.setContent_tag(newContentTag);
        //获取审核类型
        Integer spId = json.getInteger("spId");
        Integer shenheType;
        if (spId != null && (spId == 1423 || spId == 1955 || spId == 1946 || spId == 1714 || spId == 1807 || spId == 1715
                || spId == 1137 || spId == 1199 || spId == 577 || spId == 1877 || spId == 1815 || spId == 1935)) {
            shenheType = CommonConst.AUDIT_TYPE_USER;//暂时人工审核
        } else {
            shenheType = contentService.getShenheType(json.getInteger("spId"), newContentTag);
        }
        List<ShyContent> contentList = contentService.findShyContent(shyContent);
        if (contentList.size() > 0) {
            ShyContent shyContent1 = contentList.get(0);
            // 图文存在则更新
            yorN = saveOrUpdateContent(json, shyContent1, shenheType);
            logger.info("图文同步接口更新操作:{}", contentId);
        } else {
            // 图文不存在就保存
            yorN = saveOrUpdateContent(json, shyContent, shenheType);
            logger.info("图文同步接口保存操作:{}", contentId);
        }
        if (yorN) {
            return new ResponseBean(ErrorEnum.SUCCESS);
        } else {
            return new ResponseBean(ErrorEnum.ERROR);
        }
    }

    /**
     * 保存和修改 图文 家庭相册 作业作品 操作
     *
     * @param json
     * @param shyContent
     * @param shenheType
     * @param
     * @return
     */
    private boolean saveOrUpdateContent(JSONObject json, ShyContent shyContent, Integer shenheType) {
        //如果不存在直接插入
        //标题
        shyContent.setTitle(json.getString("title"));
        //标题图片
        shyContent.setTitle_image(json.getString("titleImage"));
        //审核类型
        shyContent.setShenhe_type(shenheType);
        //当前图文的审核状态
        shyContent.setContent_status(json.getInteger("contentStatus"));
        //来源
        shyContent.setSource(json.getString("source"));
        //作者
        shyContent.setAuthor(json.getString("author"));
        //图文内容
        shyContent.setContent_text(json.getString("content_text"));
        //创建时间
        shyContent.setContent_time(new Date());
        shyContent.setCreate_date(CommonUtils.getCurrentTime());
        //回调地址
        shyContent.setCallback(json.getString("callback"));
        //图文发布h5的url
        shyContent.setContent_url(json.getString("contentUrl"));
        //图文发布pc的url
        shyContent.setContent_pc_url(json.getString("contentPCUrl"));
        //spid
        shyContent.setSp_id(json.getInteger("spId"));
        //领取状态
        shyContent.setReceive_status(CommonConst.RECEIVE_BEFORE);
        //最后一次更新时间
        shyContent.setLast_update(CommonUtils.getCurrentTime());
        if (shyContent.getId() == null) {
            //id 存在就插入
            return contentService.impotContent(shyContent);
        } else {
            // id不存在就修改
            return contentService.updateShyContent(shyContent);
        }
    }


    /**
     * @author liwei
     * @Date: 2019/05/15 11:17
     * @Description: 朋友圈送审接口
     */
    @PostMapping("/importMoment")
    @ResponseBody
    public ResponseBean impotMoment(@RequestBody String param) {
        logger.info("朋友圈入库,param=====" + param);
        JSONObject json = JSONObject.parseObject(param);
        boolean yorN;
        //判断是否存在
        String momentId = json.getString("momentId");
        ShyContent shyContent = new ShyContent();
        //朋友圈id
        shyContent.setContent_id(momentId);
        //朋友圈标签
        shyContent.setContent_tag("moment");
        List<ShyContent> contentList = contentService.findShyContent(shyContent);
        //获取审核类型
        Integer shenheType = contentService.getShenheType(json.getInteger("spId"), "moment");
        //获取生活圈内容的类型 1:图片 2:视频 3:分享
        Integer contentType = json.getInteger("contentType");
        //内容图片地址
        StringBuilder contentImageUrl = new StringBuilder();
        if (contentType != null && contentType == 1) {
            //朋友圈分享的内容的url
            JSONArray contentImageUrlList = JSONObject.parseArray(json.getString("contentImageUrl"));
            for (int i = 0; i < contentImageUrlList.size(); i++) {
                JSONObject contentImageUrlMap = contentImageUrlList.getJSONObject(i);
                contentImageUrl.append(contentImageUrlMap.getString("imageUrl")).append(",");
            }
            //去掉最后一个逗号
            contentImageUrl = new StringBuilder(contentImageUrl.substring(0, contentImageUrl.length() - 1));
        } else {
            contentImageUrl = new StringBuilder(json.getString("contentImageUrl"));
        }
        if (contentList.size() > 0) {
            //朋友圈存在则更新
            ShyContent shyContent1 = contentList.get(0);
            yorN = saveOrUpdateMoment(json, shyContent1, shenheType, contentImageUrl.toString());
            logger.info("朋友圈接口更新操作:{}", momentId);
        } else {
            //当前朋友圈内容的审核状态,先发后审
            shyContent.setContent_status(1);
            //朋友圈不存在直接保存
            yorN = saveOrUpdateMoment(json, shyContent, shenheType, contentImageUrl.toString());
            logger.info("朋友圈接口保存操作:{}", momentId);
        }
        if (yorN) {
            return new ResponseBean(ErrorEnum.SUCCESS);
        } else {
            return new ResponseBean(ErrorEnum.ERROR);
        }
    }

    /**
     * 朋友圈 保存和修改
     *
     * @param json
     * @param shyContent
     * @param shenheType
     * @param contentImageUrl
     * @return
     */
    private boolean saveOrUpdateMoment(JSONObject json, ShyContent shyContent, Integer shenheType, String contentImageUrl) {
        //标题
        shyContent.setTitle(json.getString("title"));
        //用户头像
        shyContent.setTitle_image(json.getString("image"));
        //审核类型
        shyContent.setShenhe_type(shenheType);
        //创建时间
        String contentTime = json.getString("createTime");
        shyContent.setContent_time(new Date(Long.parseLong(contentTime)));
        shyContent.setCreate_date(CommonUtils.getCurrentTime());
        //回调地址
        shyContent.setCallback(json.getString("callbackUrl"));
        //朋友圈分享的url
        shyContent.setContent_url(json.getString("pageUrl"));
        //朋友圈分享的内容的url,以逗号分隔
        shyContent.setContent_pc_url(contentImageUrl);
        //spid
        shyContent.setSp_id(json.getInteger("spId"));
        //用户id
        shyContent.setAuthor(json.getString("userId"));
        //用户昵称
        shyContent.setSource(json.getString("nickName"));
        //关联videoId
        shyContent.setVideo_id(json.getString("videoId"));
        //领取状态
        shyContent.setReceive_status(CommonConst.RECEIVE_BEFORE);
        //最后一次更新时间
        shyContent.setLast_update(CommonUtils.getCurrentTime());
        if (shyContent.getId() == null) {
            //id不存在直接插入
            return contentService.impotContent(shyContent);
        } else {
            // id存在就修改
            return contentService.updateShyContent(shyContent);
        }
    }
}