Blame view

VodDictServiceImpl.java 6.19 KB
涂亚平 committed
1 2
package com.subsidy.service.impl;

涂亚平 committed
3
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
涂亚平 committed
4
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
涂亚平 committed
5
import com.subsidy.common.configure.VODConfig;
涂亚平 committed
6
import com.subsidy.common.exception.HttpException;
涂亚平 committed
7
import com.subsidy.dto.content.GetContendVodsDTO;
涂亚平 committed
8
import com.subsidy.dto.vod.ChangeOrdersDTO;
涂亚平 committed
9
import com.subsidy.mapper.VodDictMapper;
涂亚平 committed
10
import com.subsidy.model.VodDictDO;
涂亚平 committed
11 12 13 14 15
import com.subsidy.service.VodDictService;
import com.subsidy.util.ConstantUtils;
import com.subsidy.util.Signature;
import com.subsidy.vo.vod.GetContendVodsVO;
import com.subsidy.vo.vod.SignatureVO;
涂亚平 committed
16 17 18 19 20
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
涂亚平 committed
21 22 23 24
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.MediaProcessTaskInput;
import com.tencentcloudapi.vod.v20180717.models.ProcessMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.TranscodeTaskInput;
涂亚平 committed
25
import org.springframework.beans.factory.annotation.Autowired;
涂亚平 committed
26
import org.springframework.beans.factory.annotation.Value;
涂亚平 committed
27 28
import org.springframework.stereotype.Service;

涂亚平 committed
29
import java.util.List;
涂亚平 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import java.util.Random;

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

    @Autowired
    private VODConfig vodConfig;

涂亚平 committed
46 47
    @Value("${spring.profiles.active}")
    private String env;
涂亚平 committed
48

涂亚平 committed
49
    public SignatureVO signature() {
涂亚平 committed
50 51 52 53 54 55 56 57
        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);
涂亚平 committed
58
        signature.setClassId(vodConfig.getClassId());
涂亚平 committed
59 60 61 62 63 64 65 66 67 68
        String sign = "";
        try {
            sign = signature.getUploadSignature();
            signatureVO.setSign(sign);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return signatureVO;
    }

涂亚平 committed
69 70 71
    public List<GetContendVodsVO> getContendVods(GetContendVodsDTO getContendVodsDTO) {
        //Page pager = new Page(getContendVodsDTO.getPageNum(), getContendVodsDTO.getPageSize());
        return this.baseMapper.getContendVods( getContendVodsDTO.getVodName(), getContendVodsDTO.getContentId());
涂亚平 committed
72 73 74
    }

    public String deleteVod(VodDictDO vodDictDO) {
涂亚平 committed
75
        VodDictDO vodDictDO1 = this.baseMapper.selectById(vodDictDO.getId());
涂亚平 committed
76
        this.baseMapper.deleteById(vodDictDO.getId());
涂亚平 committed
77
        try {
涂亚平 committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

            //删除原视频
            Credential cred = new Credential(vodConfig.getSecretId(), vodConfig.getSecretKey());

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("vod.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            VodClient client = new VodClient(cred, "", clientProfile);

            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(vodDictDO1.getVodCode());
            // 返回的resp是一个DeleteMediaResponse的实例,与请求对象对应
            client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
        }

涂亚平 committed
98 99 100 101
        return ConstantUtils.DELETE_SUCCESS;
    }

    public String addVod(VodDictDO vodDictDO) {
涂亚平 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

        //排序
        int orderNo = 1;

        List<VodDictDO> vodDictDOS = this.baseMapper.selectList(new QueryWrapper<VodDictDO>()
        .lambda()
        .eq(VodDictDO::getContentId,vodDictDO.getContentId())
        .orderByDesc(VodDictDO::getOrderNo));

        if (vodDictDOS.size()>0){
            VodDictDO vodDictDO1 = vodDictDOS.get(0);
            if (null!=vodDictDO1){
                orderNo = vodDictDOS.get(0).getOrderNo()+1;
            }
        }
        vodDictDO.setOrderNo(orderNo);
涂亚平 committed
118
        this.baseMapper.insert(vodDictDO);
涂亚平 committed
119

涂亚平 committed
120 121 122 123
        //测试环境就不转码了
        if (env.equals("prod")){
            //上传后直接转码
            Credential cred = new Credential(vodConfig.getSecretId(), vodConfig.getSecretKey());
涂亚平 committed
124

涂亚平 committed
125 126
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("vod.tencentcloudapi.com");
涂亚平 committed
127

涂亚平 committed
128 129
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
涂亚平 committed
130

涂亚平 committed
131
            VodClient client = new VodClient(cred, "", clientProfile);
涂亚平 committed
132

涂亚平 committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            ProcessMediaRequest processMediaRequest = new ProcessMediaRequest();
            MediaProcessTaskInput mediaProcessTaskInput1 = new MediaProcessTaskInput();
            TranscodeTaskInput[] transcodeTaskInputs1 = new TranscodeTaskInput[1];
            TranscodeTaskInput transcodeTaskInput1 = new TranscodeTaskInput();
            transcodeTaskInput1.setDefinition(ConstantUtils.TEMPLATE_VOD);
            transcodeTaskInputs1[0] = transcodeTaskInput1;
            mediaProcessTaskInput1.setTranscodeTaskSet(transcodeTaskInputs1);
            processMediaRequest.setMediaProcessTask(mediaProcessTaskInput1);
            processMediaRequest.setFileId(vodDictDO.getVodCode());

            try {
                client.ProcessMedia(processMediaRequest);
            } catch (Exception ex) {
                throw new HttpException(50001);
            }
涂亚平 committed
148
        }
涂亚平 committed
149 150 151 152 153 154 155 156
        return ConstantUtils.ADD_SUCCESS;
    }

    public String updateVod(VodDictDO vodDictDO) {
        this.baseMapper.updateById(vodDictDO);
        return ConstantUtils.SET_SUCCESS;
    }

涂亚平 committed
157 158 159 160 161 162 163 164 165
    public String changeOrders(ChangeOrdersDTO changeOrdersDTO){

        int i = 1 ;
        List<Long> longs = changeOrdersDTO.getVodIds();
        for (Long lg : longs){
            this.baseMapper.updateVodOrderNo(lg,i++);
        }
        return ConstantUtils.SET_SUCCESS;
    }
涂亚平 committed
166 167

}