ContentServiceImpl.java 15.8 KB
package com.cnlive.shenhe.serviceImpl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyComments;
import com.cnlive.shenhe.entity.ShyContent;
import com.cnlive.shenhe.entity.ShyLive;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.entity.ShyVideofilter;
import com.cnlive.shenhe.entity.ShyVideos;
import com.cnlive.shenhe.mapper.ShyCommentsMapper;
import com.cnlive.shenhe.mapper.ShyContentMapper;
import com.cnlive.shenhe.mapper.ShySitesMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.utils.AntiFraudUtil;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther: liwei
 * @Date: 2019/5/20 17:30
 * @Description:
 */
@Service
public class ContentServiceImpl {

    private static Logger logger = LoggerFactory.getLogger(ContentServiceImpl.class);
    @Autowired
    ShyCommentsMapper shyCommentsMapper;
    @Autowired
    ShyContentMapper shyContentMapper;
    @Autowired
    ShySitesMapper shySitesMapper;
    @Autowired
    ShyUsersMapper shyUsersMapper;
    @Autowired
    AuditPass Pass;
    @Autowired
    SitesServiceImpl sitesService;
    @Autowired
    VideosServiceImpl videosService;
    @Autowired
    VideofilterServiceImpl videofilterServiceImpl;

    public ShyContent getShyContent(ShyContent shyContent) {
        List<ShyContent> shyContentList = shyContentMapper.select(shyContent);
        if(CommonUtils.isNotNull(shyContentList)&&shyContentList.size()>0){
        	return shyContentList.get(0);
        }
        return new ShyContent();
    }

    //**************************************
    public List<ShyContent> findshyContent(ShyContent shyContent) {
      
        return shyContentMapper.selectByRowBounds(shyContent, new RowBounds(0, 100));
    }

    //***************************
    public boolean updateshyContent(ShyContent shyContent) {
        int result = shyContentMapper.updateByPrimaryKey(shyContent);
        if (result == 0) {
            return false;
        }
        return true;
    }

    //***************************
    public boolean impotContent(ShyContent shyContent) {
        int result = shyContentMapper.insertSelective(shyContent);
        if (result == 0) {
            return false;
        }
        return true;
    }

    public int updateComment(Integer id, Integer state, String msg) {
        ShyComments shyComments = shyCommentsMapper.selectByPrimaryKey(id);
        shyComments.setState(state);
        shyComments.setMsg(msg);
        return shyCommentsMapper.updateByPrimaryKeySelective(shyComments);
    }

    public PageInfo<ShyContent> getPage( String title,String content_tag,String contentId,  Date start_date, Date end_date,  Integer receive_status, String updater, Integer page, Integer pageSize, String orderByClause, Integer spid, String userId) {
        Example example = new Example(ShyContent.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(orderByClause)) {
            example.setOrderByClause(orderByClause);
        } else {
            example.setOrderByClause("create_date desc");
        }
        if (CommonUtils.isNotNull(start_date) ) {
            criteria.andGreaterThanOrEqualTo("create_date", start_date);
        }
        if ( CommonUtils.isNotNull(end_date)) {
      	  criteria.andLessThanOrEqualTo("create_date", end_date);
        }
        if (CommonUtils.isNotEmpty(title)) {
            criteria.andLike("title", "%" + title + "%");
        }
        if (CommonUtils.isNotEmpty(updater)) {
            criteria.andEqualTo("updater", updater);
        }
        if (CommonUtils.isNotEmpty(content_tag)) {
        	criteria.andEqualTo("content_tag", content_tag);
        }
        if (CommonUtils.isNotNull(spid)) {
            criteria.andEqualTo("sp_id", spid);
        }
        if (CommonUtils.isNotEmpty(contentId)) {
        	criteria.andEqualTo("content_id", contentId);
        }
        if (CommonUtils.isNotNull(receive_status)) {
            criteria.andEqualTo("receive_status", receive_status);
            if (receive_status!=CommonConst.RECEIVE_BEFORE) {
                criteria.andEqualTo("auditor_id", userId);
            }
        }

        PageHelper.startPage(page, pageSize, true);
        List<ShyContent> contentList = shyContentMapper.selectByExample(example);
        PageInfo<ShyContent> pageInfo = new PageInfo<>(contentList);
        return pageInfo;
    }
    
    /**
     * 根据条件导出数据excel
     * @return
     */
    public List<ShyContent> findByCondition(String title,String content_tag,String contentId,  Date start_date, 
    		Date end_date,Integer receive_status,  Integer page, 
    		Integer pageSize, String orderByClause, Integer spid) {
    	
    	  Example example = new Example(ShyContent.class);
          Example.Criteria criteria = example.createCriteria();
          if (CommonUtils.isNotEmpty(orderByClause)) {
              example.setOrderByClause(orderByClause);
          } else {
              example.setOrderByClause("create_date desc");
          }
          if (CommonUtils.isNotNull(start_date) ) {
              criteria.andGreaterThanOrEqualTo("create_date", start_date);
          }
          if ( CommonUtils.isNotNull(end_date)) {
        	  criteria.andLessThanOrEqualTo("create_date", end_date);
          }
          if (CommonUtils.isNotEmpty(title)) {
              criteria.andLike("title", "%" + title + "%");
          }
          if (CommonUtils.isNotNull(spid)) {
              criteria.andEqualTo("sp_id", spid);
          }
          if (CommonUtils.isNotEmpty(contentId)) {
          	criteria.andEqualTo("content_id", contentId);
          }
          if (CommonUtils.isNotEmpty(content_tag)) {
        	  criteria.andEqualTo("content_tag", content_tag);
          }
          if (CommonUtils.isNotNull(receive_status)) {
              criteria.andEqualTo("receive_status", receive_status);
          }
        List<ShyContent> shyContentList = shyContentMapper.selectByExample(example);
        return shyContentList;
    }
    

    //******************************
    public ShyContent getDetailsContent(Integer id) {
    	ShyContent content = shyContentMapper.selectByPrimaryKey(id);
        if (content.getReceive_status() == CommonConst.RECEIVE_AUDIT) {
        	String updater = CommonUtils.isEmpty(content.getUpdater()) || "null".equals(content.getUpdater())? "白名单":content.getUpdater();
            String auditor_id = content.getAuditor_id();
            if (CommonUtils.isNotEmpty(auditor_id)) {
                ShyUsers user = new ShyUsers();
                user.setUser_id(auditor_id);
                user = shyUsersMapper.selectOne(user);
                String email = CommonUtils.isEmpty(user.getEmail())|| "null".equals(user.getEmail()) ? "" : user.getEmail();
                String userName = CommonUtils.isEmpty(user.getUsername()) ? email : user.getUsername();
                updater = userName ;
            }
            content.setUpdater(updater);
        }
        ShySites shySite=sitesService.findspidDescByspid(content.getSp_id());
    	if(CommonUtils.isNotNull(shySite)){
    		 content.setSp_desc(shySite.getName());
    	}
        return content;
    }

    //**********************
	public boolean callback(String contentId, String msg, Integer state, String userId) {
			boolean success = true;
			ShyContent content = shyContentMapper.selectByPrimaryKey(Integer.parseInt(contentId));
			JSONObject result = new JSONObject();
			try {
				if(CommonUtils.isNotEmpty(content.getContent_tag())&&content.getContent_tag().equals("content")){
					result = Pass.contentPass(content.getContent_id(), state+"", msg, content.getCallback());
				}else if(CommonUtils.isNotEmpty(content.getContent_tag())&&content.getContent_tag().equals("moment")){
					result = Pass.momentPass(content.getContent_id(), state, msg, content.getCallback(),content.getAuthor());
				}
			} catch (Exception e) {
				logger.error("图文审核回调失败,图文id:{}", content.getId());
				success = false;
				return success;
			}
			if (result == null) {
				logger.error("图文回调错误,且无返回信息。图文id:{}", content.getId());
				success = false;
				return success;
			} else {
				Integer code = result.getInteger("errorCode");
				if (code != 0) {
					logger.error("图文回调错误,图文id:{},errorCode:{},errorMessage:{}", content.getId(), code,
							result.getString("errorMessage"));
					success = false;
					return success;
				}
				
				//如果当前图文是朋友圈的视频,通知点播云
				if(CommonUtils.isNotEmpty(content.getContent_tag())
						&&content.getContent_tag().equals("moment")
						&&CommonUtils.isNotEmpty(content.getVideoId())
						&&state==CommonConst.AUDIT_REFUSE){
					ShyVideos shyVideos1 = new ShyVideos();
			        shyVideos1.setVideo_id(content.getVideoId());
			        List<ShyVideos> listshyVideos = videosService.findshyVideo(shyVideos1);
			        if (listshyVideos.size() > 0) {
			            ShyVideos shyVideos = listshyVideos.get(0);
					String showMsg = videosService.repulse(shyVideos.getId().toString(), msg, userId);
					if (CommonUtils.isNotEmpty(showMsg)) {
						success = false;
						return success;
					}
			      }
				}
				
				content.setContent_status(state);// 设置图文状态为审核后的状态
				content.setReceive_status(CommonConst.RECEIVE_AUDIT);// 领取状态为已审核
				content.setShenhe_msg(msg);// 设置审核消息
				if (CommonUtils.isNotEmpty(userId)) {
					content.setAuditor_id(userId);
					content.setShenhe_type(CommonConst.AUDIT_TYPE_USER);// 设置审核类型为人工
				}
				content.setLast_update(CommonUtils.getCurrentTime());
				int i = shyContentMapper.updateByPrimaryKeySelective(content);
				if (i != 1) {
					success = false;
				}
			}

		return success;
	}

    //**********************************
    public Integer receive(String ids, Integer num, String userId, Integer spid) {
        Integer n = 0;
        if (CommonUtils.isNotEmpty(ids)) {
            String[] cids = ids.split(",");
            for (String cid : cids) {
                ShyContent shyContent = shyContentMapper.selectByPrimaryKey(Integer.parseInt(cid));
                if (CommonUtils.isNotNull(shyContent) && shyContent.getReceive_status() == CommonConst.RECEIVE_BEFORE ) {
                	shyContent.setAuditor_id(userId);
                	shyContent.setReceive_status(CommonConst.RECEIVE_AFTER);
                	shyContent.setLast_update(CommonUtils.getCurrentTime());
                    shyContentMapper.updateByPrimaryKeySelective(shyContent);
                    n++;
                }
            }
        } else if (CommonUtils.isNotNull(num) && num > 0) {
            Example example = new Example(ShyContent.class);
            example.setOrderByClause("create_date desc");
            example.and().andEqualTo("receive_status", CommonConst.RECEIVE_BEFORE);
//            example.and().andEqualTo("receive", CommonConst.RECEIVE_AFTER);
            if (CommonUtils.isNotNull(spid) && spid != 0) example.and().andEqualTo("spid", spid);
            List<ShyContent> shyContentList = shyContentMapper.selectByExampleAndRowBounds(example, new RowBounds(0, num));
            if (!shyContentList.isEmpty()) {
                for (ShyContent shyContent : shyContentList) {
                	shyContent.setAuditor_id(userId);
                	shyContent.setReceive_status(CommonConst.RECEIVE_AFTER);
                	shyContent.setLast_update(CommonUtils.getCurrentTime());
                    shyContentMapper.updateByPrimaryKeySelective(shyContent);
                    n++;
                }
            }
        }
        return n;
    }

    //***************************************
    public Integer retReceive(String ids, String userId) {
        Integer n = 0;
        String[] cids = ids.split(",");
        for (String cid : cids) {
            ShyContent shyContent = shyContentMapper.selectByPrimaryKey(Integer.parseInt(cid));
            if (shyContent.getReceive_status() == CommonConst.RECEIVE_AFTER && userId.equals(shyContent.getAuditor_id())) {
                shyContent.setReceive_status(CommonConst.RECEIVE_BEFORE);
                shyContent.setAuditor_id(null);
                shyContent.setLast_update(CommonUtils.getCurrentTime());
                int i = shyContentMapper.updateByPrimaryKeySelective(shyContent);
                if (i == 1) n++;
            }
        }
        return n;
    }
    //**************************************
    public ShyContent selectByPrimaryKey(Integer id) {
        ShyContent shyContent = shyContentMapper.selectByPrimaryKey(id);
        return shyContent;
    }
    public int updateByPrimaryKeySelective(ShyContent content) {
        return shyContentMapper.updateByPrimaryKeySelective(content);
    }
    
    /**
     * 获得审核类型
     * @param spId
     */
    public Integer getShenheType(Integer spId){
		
		//获取图文免审sp
        List<Integer> sites = sitesService.getBusinessList(CommonConst.BUSINESS_CONTENT);
        //初始化一个审核状态
        Integer shenheType=3;//审核类型,1:免审,2:机审,3:人工审
        //如果当前sp是免审sp
        if(sites.contains(spId)){
        	shenheType=1;//免审
        }else{
        	shenheType=2;//机审
        }
        return shenheType;
	}
    
    /**
     * 数美视频(图片)过滤
     * @param cid 审核云中图文id
     * @param channel 尺度
     * @return
     */
    public int contentFilter(ShyContent shyContent,String channel){
             
              JSONObject result = null;//数美返回结果
              String type="";//数美返回结果的类型
              String desc="";//数美失败结果的详情
              String Token_id=UUID.randomUUID().toString();
              try{
            	  //调用数美接口
            	   result =  AntiFraudUtil.filterArticle(Token_id,shyContent.getContent_url(),channel);
            	   type=result.getString("type");
            	   desc=result.getString("desc");//数美失败结果的详情
              }catch(Exception e){
            	  type=CommonConst.REJECT;//报错就给一个拒绝状态
            	  desc="数美审核报错了";
              }
              if(type.toLowerCase().equals(CommonConst.PASS)){//数美通过
            	  desc="数美审核通过了";
            	  shyContent.setShenhe_msg(desc);
            	  //修改图文状态
            	  callback(shyContent.getId().toString(),desc,CommonConst.AUDIT_SUCCESS,"");
              }else if(type.toLowerCase().equals(CommonConst.REJECT)){//数美拒绝
            	  shyContent.setShenhe_type(CommonConst.AUDIT_TYPE_USER);//审核类型改为人工审核
            	  shyContent.setShenhe_msg(desc);//拒绝原因
            	  updateshyContent(shyContent);
//            	  videosService.updateDianboAndShenhe(shyVideos,4);
              }
              ShyVideofilter videofilter=new ShyVideofilter();
    		  videofilter.setChannel(channel);
    		  videofilter.setDesc_msg(desc);
    		  videofilter.setResult_type(type);
    		  videofilter.setToken_id(Token_id);
    		  videofilter.setVideos_id(shyContent.getId());
    		  videofilter.setDuration(-1);
    		  videofilter.setCreated_at(CommonUtils.getCurrentTime());
    		  videofilterServiceImpl.impotVideofilter(videofilter);
    	
    	return 0;
    }

}