VodDictServiceImpl.java 5.88 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.common.configure.VODConfig;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.content.GetContendVodsDTO;
import com.subsidy.dto.vod.AddVodDictDTO;
import com.subsidy.dto.vod.ChangeOrdersDTO;
import com.subsidy.dto.vod.VodDictPageDTO;
import com.subsidy.mapper.ContentVodMappingMapper;
import com.subsidy.mapper.CourseDictMapper;
import com.subsidy.mapper.VodDictMapper;
import com.subsidy.model.ContentVodMappingDO;
import com.subsidy.model.CourseDictDO;
import com.subsidy.model.VodDictDO;
import com.subsidy.service.VodDictService;
import com.subsidy.util.Signature;
import com.subsidy.util.VodUtil;
import com.subsidy.vo.vod.GetContendVodsVO;
import com.subsidy.vo.vod.QueryVodPageVO;
import com.subsidy.vo.vod.SignatureVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>
 * 视频表 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-10-11
 */
@Service
public class VodDictServiceImpl extends ServiceImpl<VodDictMapper, VodDictDO> implements VodDictService {

    @Autowired
    private VODConfig vodConfig;

    @Value("${spring.profiles.active}")
    private String env;

    @Autowired
    private ContentVodMappingMapper contentVodMappingMapper;

    @Autowired
    private CourseDictMapper courseDictMapper;

    @Override
    public SignatureVO signature() {
        SignatureVO signatureVO = new SignatureVO();

        Signature signature = new Signature();
        signature.setSecretId(vodConfig.getSecretId());
        signature.setSecretKey(vodConfig.getSecretKey());
        signature.setCurrentTime(System.currentTimeMillis());
        signature.setRandom(new Random().nextInt(Integer.MAX_VALUE));
        signature.setSignValidDuration(3600 * 24 * 2);
        signature.setClassId(vodConfig.getClassId());
        String sign = "";
        try {
            sign = signature.getUploadSignature();
            signatureVO.setSign(sign);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return signatureVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVod(VodDictDO vodDictDO) {
        VodDictDO vod = this.baseMapper.selectById(vodDictDO.getId());
        if(null == vod) {
            throw new HttpException(18000);
        }

        if(vodDictDO.getVodCode() != null) {
            VodUtil.deleteMedia(vodConfig, vodDictDO.getVodCode());
        }

        this.baseMapper.deleteById(vodDictDO.getId());
    }

    @Override
    public void addVod(VodDictDO vodDictDO) {
        int orderNo = 1;//排序
        List<VodDictDO> vodDictDOS = this.baseMapper.selectList(new QueryWrapper<VodDictDO>()
                .lambda()
                .eq(VodDictDO::getLabelId, vodDictDO.getLabelId())
                .orderByDesc(VodDictDO::getOrderNo));

        if (CollectionUtils.isNotEmpty(vodDictDOS)){
            VodDictDO vod = vodDictDOS.get(0);
            if (null != vod){
                orderNo = vod.getOrderNo() + 1;
            }
        }

        vodDictDO.setOrderNo(orderNo);
        this.baseMapper.insert(vodDictDO);

        //测试环境就不转码了
        if (env.equals("prod")){
            VodUtil.processMedia(vodConfig, vodDictDO.getVodCode());
        }
    }

    @Override
    public void addCompanyVod(AddVodDictDTO addVodDictDTO) {
        int orderNo = 1;//排序
        List<VodDictDO> vodDictDOS = this.baseMapper.getCompanyVod(addVodDictDTO.getContentId());
        if (CollectionUtils.isNotEmpty(vodDictDOS)){
            VodDictDO vod = vodDictDOS.get(0);
            if (null != vod){
                orderNo = vod.getOrderNo() + 1;
            }
        }

        addVodDictDTO.setOrderNo(orderNo);
        this.baseMapper.insert(addVodDictDTO);

        if(addVodDictDTO.getContentId() != null) {
            ContentVodMappingDO contentVodMappingDO = new ContentVodMappingDO();
            contentVodMappingDO.setVodId(addVodDictDTO.getId());
            contentVodMappingDO.setContentId(addVodDictDTO.getContentId());
            contentVodMappingMapper.insert(contentVodMappingDO);
        }

        //测试环境就不转码了
        if (env.equals("prod")){
            VodUtil.processMedia(vodConfig, addVodDictDTO.getVodCode());
        }
    }

    @Override
    public void updateVod(VodDictDO vodDictDO) {
        this.baseMapper.updateById(vodDictDO);
    }

    @Override
    public void changeOrders(ChangeOrdersDTO changeOrdersDTO){
        int i = 1 ;
        List<Long> longs = changeOrdersDTO.getVodIds();
        for (Long lg : longs){
            this.baseMapper.updateVodOrderNo(lg,i++);
        }
    }

    @Override
    public IPage queryVodPage(VodDictPageDTO vodDictPageDTO) {
        Page page = new Page(vodDictPageDTO.getPageNum(), vodDictPageDTO.getPageSize());
        IPage<QueryVodPageVO> iPage = this.baseMapper.queryVodPage(page, vodDictPageDTO.getLabelId(), vodDictPageDTO.getVodName());
        for (QueryVodPageVO queryVodPageVO : iPage.getRecords()) {
            List<CourseDictDO> list = courseDictMapper.getListByVodId(queryVodPageVO.getId());
            List<String> courseNames = list.stream().map(CourseDictDO::getCourseName).collect(Collectors.toList());
            queryVodPageVO.setCourseNames(courseNames);
        }
        return iPage;
    }

}