AudioServiceImpl.java 15.6 KB
package com.cnlive.shenhe.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.dto.AudioDTO;
import com.cnlive.shenhe.entity.*;
import com.cnlive.shenhe.mapper.ShyAudioMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.YiDunAntiFraudUtil;
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.util.Date;
import java.util.List;

/**
 * @Author: xujincheng
 * @Date: 2020/6/8 10:19
 */
@Service
public class AudioServiceImpl {

    private static Logger logger = LoggerFactory.getLogger(AudioServiceImpl.class);

    @Autowired
    ShyAudioMapper shyAudioMapper;

    @Autowired
    AudioServiceImpl audioService;

    @Autowired
    AuditPass Pass;

    @Autowired
    ShyUsersMapper shyUsersMapper;

    @Autowired
    SitesServiceImpl sitesService;
    @Autowired
    YiDunServiceImpl yiDunServiceImpl;
    @Value("${yiDun_audio_callback}")
    String yiDun_audio_callback;

    public ShyAudio getAudioMsg(String audioId) {
        return shyAudioMapper.getAudioMsg(audioId);
    }

    public int importAudioMsg(ShyAudio ShyAudio) {
        return shyAudioMapper.insertSelective(ShyAudio);
    }

    public int updateAudioMsg(ShyAudio ShyAudio) {
        return shyAudioMapper.updateByPrimaryKeySelective(ShyAudio);
    }

    public ShyAudio selectByPrimaryKey(Integer id) {
        ShyAudio shyAudio = shyAudioMapper.selectByPrimaryKey(id);
        return shyAudio;
    }

    public List<ShyAudio> findshyAudio(ShyAudio shyAudio) {
        List<ShyAudio> shyAudiolist = shyAudioMapper.selectByRowBounds(shyAudio, new RowBounds(0, 100));
        return shyAudiolist;
    }

    public int updateAudioAndShenhe(ShyAudio shyAudio, int audioState) {
        //修改点播云音频状态
        JSONObject result = Pass.audioPass(shyAudio.getAudioId(), audioState, shyAudio.getAttr_tags(), shyAudio.getMsg(), shyAudio.getCallback());
        Integer code = result.getInteger("code");
        Integer shenheState = CommonConst.AUDIT_REFUSE;
        if (0 == code) {
            if (audioState == 3) {
                shenheState = CommonConst.AUDIT_SUCCESS;
            }
            //修改审核云的音频状态
            int i = audioService.updateAudio(shyAudio.getId(), null, shyAudio.getUploader(), "", shenheState, shyAudio.getMsg());
            if (i != 1) {
                logger.error("点播音频更改失败,AudioId:{}", shyAudio.getAudioId());
            }
            return i;
        } else {
            shyAudio.setMsg("点播音频回调失败," + shyAudio.getMsg());
            audioService.updateAudio(shyAudio.getId(), null, shyAudio.getUploader(), "", CommonConst.AUDIT_CALLBACK_FAIL, shyAudio.getMsg());
            logger.error("点播音频回调失败,activity_id:{},code:{},msg:{}", shyAudio.getAudioId(), code, result.getString("msg"));
        }

        return 0;
    }

    //认领
    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) {
                ShyAudio shyAudio = shyAudioMapper.selectByPrimaryKey(Integer.parseInt(cid));
                if (CommonUtils.isNotNull(shyAudio) && shyAudio.getState().equals(CommonConst.AUDIT_INTT) && shyAudio.getReceive().equals(CommonConst.RECEIVE_BEFORE)) {
                    shyAudio.setReceive_user_id(userId);
                    shyAudio.setReceive(CommonConst.RECEIVE_AFTER);
                    shyAudioMapper.updateByPrimaryKeySelective(shyAudio);
                    n++;
                }
            }
        } else if (CommonUtils.isNotNull(num) && num > 0) {
            Example example = new Example(ShyAudio.class);
            example.setOrderByClause("createTime 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<ShyAudio> shyCommentsList = shyAudioMapper.selectByExampleAndRowBounds(example, new RowBounds(0, num));
            if (!shyCommentsList.isEmpty()) {
                for (ShyAudio shyComments : shyCommentsList) {
                    shyComments.setReceive_user_id(userId);
                    shyComments.setReceive(CommonConst.RECEIVE_AFTER);
                    shyAudioMapper.updateByPrimaryKeySelective(shyComments);
                    n++;
                }
            }
        }
        return n;
    }

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

    public String repulse(String shyAudioId, String msg, String userId) {
        String showMsg = "";
        ShyAudio shyAudio = audioService.selectByPrimaryKey(Integer.parseInt(shyAudioId));
        JSONObject result;
        try {
            result = Pass.audioPass(shyAudio.getAudioId(), 4, msg, msg, shyAudio.getCallback());
        } catch (Exception e) {
            logger.error("音频审核失败,id==" + shyAudioId, e);
            showMsg = showMsg + "," + shyAudio.getAudioTitle() + " 拒绝请求失败";
            return showMsg;
        }
        if (result == null) {
            logger.error("音频审核失败且接口错误没有返回信息,id==" + shyAudioId);
            showMsg = showMsg + "," + shyAudio.getAudioTitle() + " 拒绝请求失败";
            return showMsg;
        }
        Integer code = result.getInteger("code");
        if (code != 0) {
            logger.error("音频审核失败,id:{},code:{}", shyAudioId, code);
            showMsg = showMsg + "," + shyAudio.getAudioTitle() + " " + result.getString("msg");
            return showMsg;
        }
        //0.未审核,1:已审核,2.拒绝
        int i = audioService.updateAudio(Integer.parseInt(shyAudioId), userId, null, "", 2, msg);
        if (i == 0) {
            showMsg = showMsg + "," + shyAudio.getAudioTitle() + " 更新失败";
            return showMsg;
        }

        return showMsg;
    }

    public int updateAudio(Integer id, String auditor_id, String updater, String attr_tags, Integer state, String msg) {
        ShyAudio shyAudio = shyAudioMapper.selectByPrimaryKey(id);
        shyAudio.setAuditor_id(auditor_id);
        shyAudio.setState(state);
        if (CommonUtils.isNotEmpty(attr_tags)) {
            shyAudio.setAttr_tags(attr_tags);
        }
        if (CommonUtils.isNotEmpty(msg)) {
            shyAudio.setMsg(msg);
        }
        shyAudio.setUploader(updater);
        if (CommonUtils.isNotEmpty(auditor_id)) {
            shyAudio.setReceive(CommonConst.RECEIVE_AFTER);
            shyAudio.setReceive_user_id(auditor_id);
            shyAudio.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
        }
        return shyAudioMapper.updateByPrimaryKey(shyAudio);
    }

    //分页查询音频数据
    public PageInfo<ShyAudio> getListContent(AudioDTO audioDTO) {
        Example example = new Example(ShyAudio.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria2 = example.createCriteria();
        if (CommonUtils.isNotEmpty(audioDTO.getOrderByClause())) {
            example.setOrderByClause(audioDTO.getOrderByClause());
        }
        if (CommonUtils.isNotEmpty(audioDTO.getsDate()) && CommonUtils.isNotEmpty(audioDTO.geteDate())) {
            criteria.andGreaterThanOrEqualTo("createTime", audioDTO.getsDate());
            criteria2.andGreaterThanOrEqualTo("createTime", audioDTO.getsDate());
            criteria.andLessThanOrEqualTo("createTime", audioDTO.geteDate());
            criteria2.andLessThanOrEqualTo("createTime", audioDTO.geteDate());
        }
        if (CommonUtils.isNotEmpty(audioDTO.getVideo_title())) {
            criteria.andLike("audioTitle", "%" + audioDTO.getVideo_title() + "%");
            criteria2.andLike("audioTitle", "%" + audioDTO.getVideo_title() + "%");
        }

        if (CommonUtils.isNotEmpty(audioDTO.getVideo_priority())) {
            criteria.andEqualTo("priority", audioDTO.getVideo_priority());
            criteria2.andEqualTo("priority", audioDTO.getVideo_priority());
        }

        if (CommonUtils.isNotNull(audioDTO.getSpid())) {
            criteria.andEqualTo("spId", audioDTO.getSpid());
            criteria2.andEqualTo("spId", audioDTO.getSpid());
        } else if (CommonUtils.isNotEmpty(audioDTO.getSp_Id())) {
            criteria.andEqualTo("spId", audioDTO.getSp_Id());
            criteria2.andEqualTo("spId", audioDTO.getSp_Id());
        }

        //领取状态 0-未领取 1-已领取
        if (CommonUtils.isNotNull(audioDTO.getReceive())) {
            criteria.andEqualTo("receive", audioDTO.getReceive());
            criteria2.andEqualTo("receive", audioDTO.getReceive());
            if (audioDTO.getReceive().equals(CommonConst.RECEIVE_AFTER)) {
                criteria.andEqualTo("receive_user_id", audioDTO.getUserId());
                criteria2.andEqualTo("receive_user_id", audioDTO.getUserId());
            }
        }

        //审核状态 0-未审核 1-已审核 2-拒绝
        if (CommonUtils.isNotNull(audioDTO.getState()) && audioDTO.getState() != 3) {
            criteria.andEqualTo("state", audioDTO.getState());
        } else if (CommonUtils.isNotNull(audioDTO.getState()) && audioDTO.getState() == 3) {
            criteria.andEqualTo("state", 1);
            criteria2.andEqualTo("state", 2);
            example.or(criteria2);
        }

        //默认按上传时间倒序排序
        example.setOrderByClause("createTime desc");
        PageHelper.startPage(audioDTO.getPage(), audioDTO.getPageSize(), true);
        List<ShyAudio> shyAudioList = shyAudioMapper.selectByExample(example);
        PageInfo<ShyAudio> pageInfo = new PageInfo<>(shyAudioList);
        return pageInfo;
    }


    public ShyAudio getDetailsContent(Integer id) {
        ShyAudio shyAudio = shyAudioMapper.selectByPrimaryKey(id);
        if (!shyAudio.getState().equals(CommonConst.AUDIT_INTT)) {
            String updater = CommonUtils.isEmpty(shyAudio.getUploader()) || "null".equals(shyAudio.getUploader()) ? "白名单" : shyAudio.getUploader();
            String auditor_id = shyAudio.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;
            }
            shyAudio.setUploader(updater);
        }
        return shyAudio;
    }

    /**
     * 根据条件导出数据excel
     *
     * @param audioDTO
     * @return
     */
    public List<ShyAudio> findByCondition(AudioDTO audioDTO) {
        Example example = new Example(ShyAudio.class);
        Example.Criteria criteria = example.createCriteria();
        Example.Criteria criteria2 = example.createCriteria();
        if (CommonUtils.isNotEmpty(audioDTO.getOrderByClause())) {
            example.setOrderByClause(audioDTO.getOrderByClause());
        }
        if (CommonUtils.isNotEmpty(audioDTO.getsDate()) && CommonUtils.isNotEmpty(audioDTO.geteDate())) {
            criteria.andGreaterThanOrEqualTo("createTime", audioDTO.getsDate());
            criteria.andLessThanOrEqualTo("createTime", audioDTO.geteDate());
        }
        if (CommonUtils.isNotEmpty(audioDTO.getVideo_title())) {
            criteria.andLike("audioTitle", "%" + audioDTO.getVideo_title() + "%");
        }
        if (CommonUtils.isNotNull(audioDTO.getState()) && audioDTO.getState() == 0) {
            criteria.andEqualTo("state", audioDTO.getState());
        } else if (CommonUtils.isNotNull(audioDTO.getState()) && audioDTO.getState() == 1) {
            criteria.andEqualTo("state", audioDTO.getState());
            criteria2.andEqualTo("state", 2);
            example.or(criteria2);
        }
        if (CommonUtils.isNotEmpty(audioDTO.getVideo_priority())) {
            criteria.andEqualTo("priority", audioDTO.getVideo_priority());
        }
        if (CommonUtils.isNotNull(audioDTO.getSpid())) {
            criteria.andEqualTo("spId", audioDTO.getSpid());
        }
        if (CommonUtils.isNotNull(audioDTO.getReceive())) {
            criteria.andEqualTo("receive", audioDTO.getReceive());
            if (audioDTO.getReceive().equals(CommonConst.RECEIVE_AFTER)) {
                criteria.andEqualTo("receive_user_id", audioDTO.getUserId());
            }
        }
        //默认按上传时间倒序排序
        //example.setOrderByClause("video_upload_time desc");
        List<ShyAudio> shyAudioList = shyAudioMapper.selectByExample(example);
        return shyAudioList;
    }


    /**
     * 根据sp、栏目、用户id来判断审核类型
     *
     * @param shyAudio
     */
    public void getShenheType(ShyAudio shyAudio) {
        //获取点播免审sp
        List<Integer> sites = sitesService.getBusinessList(CommonConst.BUSINESS_AUDIO);
        logger.info("sites" + sites.toString());
        //初始化一个审核状态
        //审核类型 1-免审 2-机审 3-人工审
        Integer shenheType = 3;
        //如果当前sp是免审sp
        if (sites.contains(shyAudio.getSpId())) {
            //免审
            shenheType = 1;
        }
        shyAudio.setShenhe_type(shenheType);
    }

    /**
     * 易盾点播音频过滤
     *
     * @param shyAudio
     * @return
     */
    public void audioFilter(ShyAudio shyAudio) {
        //调用易盾点播音频检测接口
        JSONObject jsonObject = YiDunAntiFraudUtil.submitAudioOnDemandUrl(shyAudio.getAudioUrl(), yiDun_audio_callback);
        if (jsonObject != null) {
            JSONObject result = jsonObject.getJSONObject("result");
            String taskId = result.getString("taskId");
            ShyYidun shyYidun = new ShyYidun();
            shyYidun.setId(taskId);
            shyYidun.setUrl(shyAudio.getAudioUrl());
            shyYidun.setContent_id(shyAudio.getId().toString());
            shyYidun.setCreate_time(new Date());
            yiDunServiceImpl.addYiDunMsg(shyYidun);
        }
    }
}