YzgFileConsumer.java 7.45 KB
package com.yanzuoguang.cloud.file;

import com.rabbitmq.client.Channel;
import com.yanzuoguang.cloud.vo.req.YzgFileCreateReqVo;
import com.yanzuoguang.cloud.vo.req.YzgFileStatusReqVo;
import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.vo.file.YzgFileMoveReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageItemReqVo;
import com.yanzuoguang.util.vo.file.YzgFileVideoImageReqVo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件处理
 */
@Component
public class YzgFileConsumer implements InitializingBean {

    @Autowired
    private MqService mqService;

    @Autowired
    private YzgFileService fileService;

    @Autowired
    private YzgFileProcedure fileProcedure;

    @Autowired
    private YzgExcelService excelService;

    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied
     * (and satisfied BeanFactoryAware and ApplicationContextAware).
     * <p>This method allows the bean instance to perform initialization only
     * possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     *
     * @throws Exception in the event of misconfiguration (such
     *                   as failure to set an essential property) or if initialization fails.
     */
    @Override
    public void afterPropertiesSet() throws Exception {

    }


    /**
     * 删除临时木经
     *
     * @param tempFolder
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {YzgFileProcedure.YZG_REMOVE_TEMP_FOLDER})
    public void notesCheckBack(String tempFolder, Message message, Channel channel) {
        try {
            fileService.removeTempFolder(tempFolder);
        } catch (CodeException ex) {
            Log.error(YzgFileConsumer.class, ex);
        } catch (Exception ex) {
            Log.error(YzgFileConsumer.class, ex);
            fileProcedure.removeTempFolder(tempFolder);
        } finally {
            mqService.basicAck(message, channel);
        }
    }

    /**
     * 移动临时目录
     *
     * @param json
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {YzgFileProcedure.YZG_MOVE_FILE})
    public void moveFile(String json, Message message, Channel channel) {
        try {
            YzgFileMoveReqVo req = JsonHelper.deserialize(json, YzgFileMoveReqVo.class);
            fileService.moveFile(req);
        } catch (CodeException ex) {
            Log.error(YzgFileConsumer.class, ex);
        } catch (Exception ex) {
            Log.error(YzgFileConsumer.class, ex);
            fileProcedure.moveFile(json, 60 * 1000);
        } finally {
            mqService.basicAck(message, channel);
        }
    }

    /**
     * 处理文件转换
     *
     * @param json
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {YzgFileProcedure.YZG_CONVERT_IMAGE})
    public void convertImage(String json, Message message, Channel channel) {
        try {
            YzgFileVideoImageReqVo req = JsonHelper.deserialize(json, YzgFileVideoImageReqVo.class);
            // 等待下一次MQ执行
            List<YzgFileVideoImageItemReqVo> cacheList = req.getList();
            if (cacheList == null || cacheList.isEmpty()) {
                return;
            }
            // 本次执行的转换操作
            List<YzgFileVideoImageItemReqVo> runList = new ArrayList<>();
            runList.add(cacheList.remove(0));
            req.setList(runList);
            // 执行本次转换
            fileService.convertImage(req);
            // 继续执行下一次转换
            if (!cacheList.isEmpty()) {
                req.setList(cacheList);
                fileProcedure.convertImage(req);
            }
        } catch (CodeException ex) {
            Log.error(YzgFileConsumer.class, ex);
        } catch (Exception ex) {
            Log.error(YzgFileConsumer.class, ex);
            fileProcedure.convertImage(json, 60 * 1000);
        } finally {
            mqService.basicAck(message, channel);
        }
    }

    /**
     * 处理视频转换
     *
     * @param json
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {YzgFileProcedure.YZG_CONVERT_VIDEO})
    public void convertVideo(String json, Message message, Channel channel) {
        try {
            YzgFileVideoImageReqVo req = JsonHelper.deserialize(json, YzgFileVideoImageReqVo.class);
            // 等待下一次MQ执行
            List<YzgFileVideoImageItemReqVo> cacheList = req.getList();
            if (cacheList == null || cacheList.isEmpty()) {
                return;
            }
            // 本次执行的转换操作
            List<YzgFileVideoImageItemReqVo> runList = new ArrayList<>();
            runList.add(cacheList.remove(0));
            req.setList(runList);
            // 执行本次转换
            fileService.convertVideo(req);
            // 继续执行下一次转换
            if (!cacheList.isEmpty()) {
                req.setList(cacheList);
                fileProcedure.convertVideo(req);
            }
        } catch (CodeException ex) {
            Log.error(YzgFileConsumer.class, ex);
        } catch (Exception ex) {
            Log.error(YzgFileConsumer.class, ex);
            fileProcedure.convertVideo(json, 60 * 1000);
        } finally {
            mqService.basicAck(message, channel);
        }
    }

    /**
     * 处理视频转换
     *
     * @param json
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {YzgFileProcedure.YZG_FILE_CREATE})
    public void fileCreate(String json, Message message, Channel channel) {
        YzgFileCreateReqVo fileReq = null;
        try {
            fileReq = JsonHelper.deserialize(json, YzgFileCreateReqVo.class);
            if (StringHelper.isEmpty(fileReq.getFileId(), fileReq.getCallbackMQ())) {
                return;
            }
            excelService.fileCreate(json, fileReq);
        } catch (CodeException ex) {
            Log.error(YzgFileConsumer.class, ex);
        } catch (Exception ex) {
            Log.error(YzgFileConsumer.class, ex);
            fileProcedure.fileCreateDelay(json);
        } finally {
            mqService.basicAck(message, channel);
        }
    }

    /**
     * 处理视频转换
     *
     * @param json
     * @param message
     * @param channel
     */
    @RabbitListener(queues = {YzgFileProcedure.YZG_FILE_STATUS})
    public void fileStatus(String json, Message message, Channel channel) {
        try {
            YzgFileStatusReqVo fileReq = JsonHelper.deserialize(json, YzgFileStatusReqVo.class);
            if (StringHelper.isEmpty(fileReq.getFileId(), fileReq.getFileStatus())) {
                return;
            }
            excelService.updateStatus(fileReq);
        } catch (CodeException ex) {
            Log.error(YzgFileConsumer.class, ex);
        } catch (Exception ex) {
            Log.error(YzgFileConsumer.class, ex);
            fileProcedure.fileStatus(json);
        } finally {
            mqService.basicAck(message, channel);
        }
    }
}