AudioServiceImpl.java 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
package com.cnlive.shenhe.service.serviceImpl;

import com.alibaba.fastjson.JSON;
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.ShyAudio;
import com.cnlive.shenhe.entity.ShyUsers;
import com.cnlive.shenhe.entity.ShyYidun;
import com.cnlive.shenhe.mapper.ShyAudioMapper;
import com.cnlive.shenhe.mapper.ShyUsersMapper;
import com.cnlive.shenhe.service.AudioService;
import com.cnlive.shenhe.service.SitesService;
import com.cnlive.shenhe.service.YiDunService;
import com.cnlive.shenhe.utils.*;
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 implements AudioService {

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

    @Autowired
    ShyAudioMapper shyAudioMapper;

    @Autowired
    AudioService audioService;

    @Autowired
    AuditPass pass;

    @Autowired
    ShyUsersMapper shyUsersMapper;

    @Autowired
    SitesService sitesService;
    @Autowired
    YiDunService yiDunService;
    @Value("${yiDun_audio_callback}")
    String yiDun_audio_callback;

    /**
     * 查询音频记录是否存在
     *
     * @param audioId
     * @return
     */
    @Override
    public ShyAudio getAudioMsg(String audioId) {
        return shyAudioMapper.getAudioMsg(audioId);
    }

    /**
     * 导入音频记录
     *
     * @param shyAudio
     * @return
     */
    @Override
    public int importAudioMsg(ShyAudio shyAudio) {
        return shyAudioMapper.insertSelective(shyAudio);
    }

    /**
     * 修改音频记录
     *
     * @param shyAudio
     * @return
     */
    @Override
    public int updateAudioMsg(ShyAudio shyAudio) {
        return shyAudioMapper.updateByPrimaryKeySelective(shyAudio);
    }

    /**
     * 根据id查询音频对象
     *
     * @param id
     * @return
     */
    @Override
    public ShyAudio selectByPrimaryKey(Integer id) {
        return shyAudioMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据音频条件查询符合条件的音频集合
     *
     * @param shyAudio
     * @return
     */
    @Override
    public List<ShyAudio> findshyAudio(ShyAudio shyAudio) {
        return shyAudioMapper.selectByRowBounds(shyAudio, new RowBounds(0, 100));
    }

    /**
     * 回调 更新音频和审核状态
     *
     * @param shyAudio
     * @param audioState 视频状态,1通过,2不通过
     */
    @Override
    public void 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 == CommonConst.AUDIT_SUCCESS) {
                shenheState = CommonConst.AUDIT_SUCCESS;
            }
            //修改审核云的音频状态
            int i = audioService.updateAudio(shyAudio.getId(), null, shyAudio.getUploader(), "", shenheState, shyAudio.getMsg());
            if (i != 1) {
                logger.error("点播音频更改失败,AudioId:{}", shyAudio.getAudioId());
            }
        } 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"));
        }
    }

    /**
     * 认领
     *
     * @param ids
     * @param num
     * @param userId
     * @param spid
     * @return
     */
    @Override
    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;
    }

    /**
     * 退领
     *
     * @param ids
     * @param userId
     * @return
     */
    @Override
    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(), "已退领");
    }

    @Override
    public String repulse(String shyAudioId, String msg, String userId) {
        StringBuilder showMsg = new StringBuilder();
        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.append(",").append(shyAudio.getAudioTitle()).append(" 拒绝请求失败");
            return showMsg.toString();
        }
        if (result == null) {
            logger.error("音频审核失败且接口错误没有返回信息,id==" + shyAudioId);
            showMsg.append(",").append(shyAudio.getAudioTitle()).append(" 拒绝请求失败");
            return showMsg.toString();
        }
        Integer code = result.getInteger("code");
        if (code != 0) {
            logger.error("音频审核失败,id:{},code:{}", shyAudioId, code);
            showMsg.append(",").append(shyAudio.getAudioTitle()).append(" ").append(result.getString("msg"));
            return showMsg.toString();
        }
        //0.未审核,1:已审核,2.拒绝
        int i = audioService.updateAudio(Integer.parseInt(shyAudioId), userId, null, "", 2, msg);
        if (i == 0) {
            showMsg.append(",").append(shyAudio.getAudioTitle()).append(" 更新失败");
            return showMsg.toString();
        }
        return showMsg.toString();
    }

    /**
     * 根据id查询到音频对象进行修改
     *
     * @param id
     * @param auditorId
     * @param updater
     * @param attrTags
     * @param state
     * @param msg
     * @return
     */
    @Override
    public int updateAudio(Integer id, String auditorId, String updater, String attrTags, Integer state, String msg) {
        ShyAudio shyAudio = shyAudioMapper.selectByPrimaryKey(id);
        shyAudio.setAuditor_id(auditorId);
        shyAudio.setState(state);
        if (CommonUtils.isNotEmpty(attrTags)) {
            shyAudio.setAttr_tags(attrTags);
        }
        if (CommonUtils.isNotEmpty(msg)) {
            shyAudio.setMsg(msg);
        }
        shyAudio.setUploader(updater);
        if (CommonUtils.isNotEmpty(auditorId)) {
            shyAudio.setReceive(CommonConst.RECEIVE_AFTER);
            shyAudio.setReceive_user_id(auditorId);
            shyAudio.setShenhe_type(CommonConst.AUDIT_TYPE_USER);
        }
        return shyAudioMapper.updateByPrimaryKey(shyAudio);
    }

    /**
     * 分页查询音频数据
     *
     * @param audioDTO
     * @return
     */
    @Override
    public PageInfo<ShyAudio> getListAudios(AudioDTO audioDTO) {
        Example example = new Example(ShyAudio.class);
        Example.Criteria criteria = example.createCriteria();
        if (CommonUtils.isNotEmpty(audioDTO.getOrderByClause())) {
            example.setOrderByClause(audioDTO.getOrderByClause());
        }
        if (CommonUtils.isNotEmpty(audioDTO.getStart_date()) && CommonUtils.isNotEmpty(audioDTO.getEnd_date())) {
            criteria.andGreaterThanOrEqualTo("createTime", audioDTO.getStart_date());
            criteria.andLessThanOrEqualTo("createTime", audioDTO.getEnd_date());
        }
        if (CommonUtils.isNotEmpty(audioDTO.getVideo_title())) {
            criteria.andLike("audioTitle", "%" + audioDTO.getVideo_title() + "%");
        }
        if (CommonUtils.isNotNull(audioDTO.getSpid())) {
            criteria.andEqualTo("spId", audioDTO.getSpid());
        } else if (CommonUtils.isNotEmpty(audioDTO.getSp_Id())) {
            criteria.andEqualTo("spId", audioDTO.getSp_Id());
        }
        //领取状态 0-未领取 1-已领取
        if (CommonUtils.isNotNull(audioDTO.getReceive())) {
            criteria.andEqualTo("receive", audioDTO.getReceive());
            if (audioDTO.getReceive().equals(CommonConst.RECEIVE_AFTER)) {
                criteria.andEqualTo("receive_user_id", audioDTO.getUserId());
            }
        }
        //审核状态 0-未审核 1-已审核 2-拒绝 3-我的已审
        //不是   我的已审 进来的,只查一种状态
        if (CommonUtils.isNotNull(audioDTO.getState()) && audioDTO.getState() != 3) {
            criteria.andEqualTo("state", audioDTO.getState());
            //是 我的已审,查通过和拒绝的两种状态
        } else if (CommonUtils.isNotNull(audioDTO.getState()) && audioDTO.getState() == 3) {
            criteria.andBetween("state", CommonConst.AUDIT_SUCCESS, CommonConst.AUDIT_REFUSE);
        }

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

    @Override
    public ShyAudio getDetailsAudio(Integer id, Integer spid) {
        ShyAudio shyAudio = shyAudioMapper.getAudioBySpId(id, spid);
        if (CommonUtils.isNotNull(shyAudio)) {
            if (!shyAudio.getState().equals(CommonConst.AUDIT_INTT)) {
                String updater = CommonUtils.isEmpty(shyAudio.getUploader()) || "null".equals(shyAudio.getUploader()) ? "白名单" : shyAudio.getUploader();
                String auditorId = shyAudio.getAuditor_id();
                if (CommonUtils.isNotEmpty(auditorId)) {
                    ShyUsers user = new ShyUsers();
                    user.setUser_id(auditorId);
                    user = shyUsersMapper.selectOne(user);
                    String email = CommonUtils.isEmpty(user.getEmail()) || "null".equals(user.getEmail()) ? "" : user.getEmail();
                    updater = CommonUtils.isEmpty(user.getUsername()) ? email : user.getUsername();
                }
                shyAudio.setUploader(updater);
            }
        }
        return shyAudio;
    }

    /**
     * 根据条件导出数据excel
     *
     * @param audioDTO
     * @return
     */
    @Override
    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.getStart_date()) && CommonUtils.isNotEmpty(audioDTO.getEnd_date())) {
            criteria.andGreaterThanOrEqualTo("createTime", audioDTO.getStart_date());
            criteria.andLessThanOrEqualTo("createTime", audioDTO.getEnd_date());
        }
        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.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("createTime desc");
        return shyAudioMapper.selectByExample(example);
    }


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

    /**
     * 易盾点播音频过滤
     * 音频送审时 给了回调地址
     * 送审类型:1:文本 2:图片 3.网页 4:点播音频
     *
     * @param shyAudio
     * @return
     */
    @Override
    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");
            String dataId = result.getString("dataId");
            ShyYidun shyYidun = new ShyYidun();
            shyYidun.setId(taskId);
            shyYidun.setUrl(shyAudio.getAudioUrl());
            shyYidun.setContent_id(shyAudio.getId().toString());
            shyYidun.setCreate_time(new Date());
            shyYidun.setTask_id(taskId);
            shyYidun.setData_id(dataId);
            shyYidun.setCheck_type(CommonConstStates.DIANBOYINPIN);
            yiDunService.addYiDunMsg(shyYidun);
        }
    }
}