VideosServiceImpl.java 14.6 KB
package com.cnlive.shenhe.serviceImpl;

import com.alibaba.fastjson.JSON;
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.ShySites;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.entity.ShyVideofilter;
import com.cnlive.shenhe.entity.ShyVideos;
import com.cnlive.shenhe.job.VideosJob;
import com.cnlive.shenhe.mapper.ShySitesMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.mapper.ShyVideosMapper;
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.beans.factory.annotation.Value;
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.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chenhao
 * @Date: 2018/11/30 14:23
 * @Description:
 */
@Service
public class VideosServiceImpl {
	private static Logger logger = LoggerFactory.getLogger(VideosServiceImpl.class);
    @Autowired
    ShyVideosMapper shyVideosMapper;
    @Autowired
    ShySitesMapper shySitesMapper;
    @Autowired
    ShyUsersMapper shyUsersMapper;
    
    @Autowired
    VideosServiceImpl videosService;
    
    @Autowired
    VideofilterServiceImpl videofilterServiceImpl;
    
    @Value("${qn_domain}")
    String qn_domain;

    @Value("${ks_domain}")
    String ks_domain;
    
    @Autowired
    AuditPass Pass;

    public boolean impotVideo(ShyVideos shyVideos) {
        int result = shyVideosMapper.insertSelective(shyVideos);
        if (result == 0) {
            return false;
        }
        return true;
    }

    public ShyVideos selectByPrimaryKey(Integer id) {
        ShyVideos shyVideos = shyVideosMapper.selectByPrimaryKey(id);
        return shyVideos;
    }

    public List<ShyVideos> findshyVideo(ShyVideos shyVideos) {
        List<ShyVideos> shyVideoslist = shyVideosMapper.selectByRowBounds(shyVideos, new RowBounds(0, 100));
        return shyVideoslist;
    }

    public boolean updateshyVideos(ShyVideos shyVideos) {
        int i = shyVideosMapper.updateByPrimaryKeySelective(shyVideos);
        if (i == 0) {
            return false;
        }
        return true;
    }

    public int updateVideo(Integer id, String auditor_id,String updater, Integer state, String msg) {
        ShyVideos shyVideos = shyVideosMapper.selectByPrimaryKey(id);
        shyVideos.setAuditor_id(auditor_id);
        shyVideos.setState(state);
        shyVideos.setMsg(msg);
        shyVideos.setUpdater(updater);
        return shyVideosMapper.updateByPrimaryKeySelective(shyVideos);
    }

    public PageInfo<ShyVideos> getListContent(String video_title, String start_date, String end_date, Integer state, Boolean receive, Integer page, Integer pageSize, String video_priority, String orderByClause, Integer spid, String userId) {
        Example example = new Example(ShyVideos.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria2 = example.createCriteria();
        if (CommonUtils.isNotEmpty(orderByClause)) {
            example.setOrderByClause(orderByClause);
        }
        if (CommonUtils.isNotEmpty(start_date) && CommonUtils.isNotEmpty(end_date)) {
            Timestamp startdate = null;
            Timestamp enddate = null;
            try {
                startdate = CommonUtils.parseDate(start_date, "yyyy-MM-dd HH:mm:ss");
                enddate = CommonUtils.parseDate(end_date, "yyyy-MM-dd HH:mm:ss");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andGreaterThanOrEqualTo("video_upload_time", startdate);
            criteria.andLessThanOrEqualTo("video_upload_time", enddate);
        }
        if (CommonUtils.isNotEmpty(video_title)) {
            criteria.andLike("video_title", "%" + video_title + "%");
        }
        if (CommonUtils.isNotNull(state) && state == 0) {
            criteria.andEqualTo("state", state);
        } else if (CommonUtils.isNotNull(state) && state == 1) {
            criteria.andEqualTo("state", state);
            criteria2.andEqualTo("state", 2);
            example.or(criteria2);
        }
        if (CommonUtils.isNotEmpty(video_priority)) {
            criteria.andEqualTo("video_priority", video_priority);
        }
        if (CommonUtils.isNotNull(spid)) {
            criteria.andEqualTo("spid", spid);
        }
        if (CommonUtils.isNotNull(receive)) {
            criteria.andEqualTo("receive", receive);
            if (receive == true) {
                criteria.andEqualTo("receive_user_id", userId);
            }
        }
        //默认按上传时间倒序排序
        example.setOrderByClause("video_upload_time desc");
        PageHelper.startPage(page, pageSize, true);
        List<ShyVideos> shyVideosList = shyVideosMapper.selectByExample(example);
        PageInfo<ShyVideos> pageInfo = new PageInfo<>(shyVideosList);
        return pageInfo;
    }

    public ShyVideos getDetailsContent(Integer id) {
        ShyVideos shyVideos = shyVideosMapper.selectByPrimaryKey(id);
        if (shyVideos.getState() != CommonConst.AUDIT_INTT) {
            String updater = CommonUtils.isEmpty(shyVideos.getUpdater()) || "null".equals(shyVideos.getUpdater())? "白名单":shyVideos.getUpdater();
            String auditor_id = shyVideos.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;
            }
            shyVideos.setUpdater(updater);
        }
        return shyVideos;
    }

    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) {
                ShyVideos shyVideos = shyVideosMapper.selectByPrimaryKey(Integer.parseInt(cid));
                if (CommonUtils.isNotNull(shyVideos) && shyVideos.getState() == CommonConst.AUDIT_INTT && !shyVideos.getReceive()) {
                    shyVideos.setReceive_user_id(userId);
                    shyVideos.setReceive(true);
                    shyVideosMapper.updateByPrimaryKeySelective(shyVideos);
                    n++;
                }
            }
        } else if (CommonUtils.isNotNull(num) && num > 0) {
            Example example = new Example(ShyVideos.class);
            example.setOrderByClause("created_at desc");
            example.and().andEqualTo("state", CommonConst.AUDIT_INTT);
            example.and().andEqualTo("receive", CommonConst.RECEIVE_BEFORE);
            if (CommonUtils.isNotNull(spid) && spid != 0) example.and().andEqualTo("spid", spid);
            List<ShyVideos> shyCommentsList = shyVideosMapper.selectByExampleAndRowBounds(example, new RowBounds(0, num));
            if (!shyCommentsList.isEmpty()) {
                for (ShyVideos shyComments : shyCommentsList) {
                    shyComments.setReceive_user_id(userId);
                    shyComments.setReceive(true);
                    shyVideosMapper.updateByPrimaryKeySelective(shyComments);
                    n++;
                }
            }
        }
        return n;
    }

    public ResponseBean retReceive(String ids, String userId) {
        boolean result = true;
        String[] cids = ids.split(",");
        for (String cid : cids) {
            ShyVideos shyVideos = shyVideosMapper.selectByPrimaryKey(Integer.parseInt(cid));
            if (shyVideos.getState() == CommonConst.AUDIT_INTT && shyVideos.getReceive() && shyVideos.getReceive_user_id().equals(userId)) {
                shyVideos.setReceive(CommonConst.RECEIVE_BEFORE);
                shyVideos.setReceive_user_id(null);
                int i = shyVideosMapper.updateByPrimaryKeySelective(shyVideos);
                if (i != 1) {
                    return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "退领失败,请重试");
                }
            } else {
                return new ResponseBean(ErrorEnum.ERROR.getErrorCode(), "退领失败,您选中的内容包含已退领的或已经审核的");
            }
        }
        return new ResponseBean(ErrorEnum.SUCCESS.getErrorCode(), "已退领");
    }

    /**
     * 根据条件导出数据excel
     *
     * @param video_title
     * @param start_date
     * @param end_date
     * @param state
     * @param receive
     * @param video_priority
     * @param orderByClause
     * @param spid
     * @param userId
     * @return
     */
    public List<ShyVideos> findByCondition(String video_title, String start_date, String end_date, Integer state, Boolean receive, String video_priority, String orderByClause, Integer spid, String userId) {
        Example example = new Example(ShyVideos.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria2 = example.createCriteria();
        if (CommonUtils.isNotEmpty(orderByClause)) {
            example.setOrderByClause(orderByClause);
        }
        if (CommonUtils.isNotEmpty(start_date) && CommonUtils.isNotEmpty(end_date)) {
            Timestamp startdate = null;
            Timestamp enddate = null;
            try {
                startdate = CommonUtils.parseDate(start_date, "yyyy-MM-dd HH:mm:ss");
                enddate = CommonUtils.parseDate(end_date, "yyyy-MM-dd HH:mm:ss");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andGreaterThanOrEqualTo("video_upload_time", startdate);
            criteria.andLessThanOrEqualTo("video_upload_time", enddate);
        }
        if (CommonUtils.isNotEmpty(video_title)) {
            criteria.andLike("video_title", "%" + video_title + "%");
        }
        if (CommonUtils.isNotNull(state) && state == 0) {
            criteria.andEqualTo("state", state);
        } else if (CommonUtils.isNotNull(state) && state == 1) {
            criteria.andEqualTo("state", state);
            criteria2.andEqualTo("state", 2);
            example.or(criteria2);
        }
        if (CommonUtils.isNotEmpty(video_priority)) {
            criteria.andEqualTo("video_priority", video_priority);
        }
        if (CommonUtils.isNotNull(spid)) {
            criteria.andEqualTo("spid", spid);
        }
        if (CommonUtils.isNotNull(receive)) {
            criteria.andEqualTo("receive", receive);
            if (receive == true) {
                criteria.andEqualTo("receive_user_id", userId);
            }
        }
        //默认按上传时间倒序排序
        //example.setOrderByClause("video_upload_time desc");
        List<ShyVideos> shyVideosList = shyVideosMapper.selectByExample(example);
        return shyVideosList;
    }
    
    /**
     * 数美视频(图片)过滤
     * @param cid 审核云中视频的id
     * @param channel 尺度
     * @return
     */
    public int videoFilter(Integer cid,String channel){
              ShyVideos shyVideos = shyVideosMapper.selectByPrimaryKey(cid);
              shyVideos.setUpdater("自动审核");
              Integer plat = shyVideos.getPlat();
              String yuming = "";
              if (plat == CommonConst.QINIU_PLAT) {
                  yuming = qn_domain + "/";
              } else if (plat == CommonConst.KS_PLAT) {
            	  yuming = ks_domain + "/";
              }
              List<String> imgList =JSONArray.parseArray(shyVideos.getAvsample_imgs(),String.class);
              List<Map<String,Object>> list=new ArrayList<>();
              for (String imgStr : imgList) {
            	  String imgUrl=yuming+imgStr;
            	  Map<String,Object> map=new HashMap<>();
            	  map.put("imageUrl", imgUrl);
            	  list.add(map);
              }
              String jsonList = JSONArray.toJSONString(list);
              //调用数美接口
              JSONObject result =  AntiFraudUtil.filterImg(shyVideos.getTask_id(),jsonList,CommonConst.DYNAMIC_USER);
              String type=result.getString("type");
              if(type.toLowerCase().equals(CommonConst.PASS)){//数美通过
            	  //修改点播云视频状态
            	  videosService.updateDianboAndShenhe(shyVideos,3);
              }else if(type.toLowerCase().equals(CommonConst.REJECT)){//数美拒绝
            	  videosService.updateDianboAndShenhe(shyVideos,4);
              }
              ShyVideofilter videofilter=new ShyVideofilter();
    		  videofilter.setChannel(channel);
    		  videofilter.setDesc_msg(result.getString("desc"));
    		  videofilter.setResult_type(type);
    		  videofilter.setToken_id(shyVideos.getTask_id());
    		  videofilter.setVideos_id(cid);
    		  videofilter.setCreated_at(CommonUtils.getCurrentTime());
    		  videofilterServiceImpl.impotVideofilter(videofilter);
    	
    	return 0;
    }
    
    /**
     * 更新点播和审核的视频状态
     * @param video
     * @param dianboState 视频状态,3通过,4不通过
     * @return
     */
	public int updateDianboAndShenhe(ShyVideos video,int dianboState) {
		//修改点播云的视频状态
		JSONObject result = Pass.auditPass(video.getVideo_id(), dianboState, "", "", video.getCallback());
		Integer code = result.getInteger("code");
		Integer shenheState=CommonConst.AUDIT_REFUSE;
		if (0 == code) {
			if(dianboState==3){
				shenheState=CommonConst.AUDIT_SUCCESS;
			}
			//修改审核云的视频状态
			int i = videosService.updateVideo(video.getId(), null,video.getUpdater(), shenheState, result.getString("msg"));
			if (i != 1) {
                logger.error("点播视频更改失败,activity_id:{}", video.getVideo_id());
            }
			return i;
		} else {
			videosService.updateVideo(video.getId(), null,video.getUpdater(), CommonConst.AUDIT_CALLBACK_FAIL, result.getString("msg"));
			logger.error("点播视频回调失败,activity_id:{},code:{},msg:{}", video.getVideo_id(), code, result.getString("msg"));
		}
		
		return 0;
	}


}