UcGatewayIptv2IptvConsumer.java 14.8 KB
package com.topdraw.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.topdraw.exception.BadRequestException;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.resttemplate.RestTemplateClient;
import com.topdraw.util.FileUtil;
import com.topdraw.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.util.Map;
import java.util.Objects;

@Component
@Slf4j
public class UcGatewayIptv2IptvConsumer {

    @Autowired
    RestTemplateClient restTemplateClient;

    @Value("#{rabbitMqErrorLogConfig.getUcgError()}")
    private Map<String, String> error;

    /**
     * 事件
     * @param content
     * @description 普通权益事件
     * @author Hongyan Wang
     * @date 2021/9/7 11:26 上午
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgEventQueue()}",
            containerFactory = "#{rabbitMqSourceConfig.getUcgEventSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getUcgEventStartUp()}",
            ackMode = "AUTO")
    public void eventConsumer(Channel channel, Message message, String content) throws IOException {
        log.info(" eventConsumer receive dataSync msg , content is : {} ", content);
        try {
            DataSyncMsg dataSyncMsg = JSONUtil.parseMsg2Object(content, DataSyncMsg.class);

            if (Objects.nonNull(dataSyncMsg)) {
                JSONObject jsonObject = this.restTemplateClient.dealTask(dataSyncMsg);
                if (Objects.isNull(jsonObject)) {
                    throw new BadRequestException("uce处理任务响应超时");
                }
            }

        } catch (Exception e) {

            log.error("普通权益事件处理异常, ==>> {}", e.getMessage());

            if (MapUtils.isNotEmpty(error)) {
                String errorStart = this.error.get("start");

                if (StringUtils.isEmpty(errorStart) || errorStart.equalsIgnoreCase("true")) {
                    String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
                    String filePath = this.error.get("filePath");
                    String filePath1 = filePath+fileName;
                    FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
                }

            }


        }
        log.info("ucEventConsumer ====>>>> end");
    }



    /**
     * @description 删除全部收藏记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getGrowthReportQueue()}",
            containerFactory = "#{rabbitMqSourceConfig.getGrowthReportSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getGrowthReportStartUp()}",
            ackMode = "AUTO")
    public void dealGrowthReport(Channel channel, Message message, String content) throws IOException {
        log.info("receive dealGrowthReport add message, content {}", content);

        try {

            JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
            if (Objects.nonNull(content)) {
                Object msgData = jsonObject.get("msgData");
                JSONObject response = this.restTemplateClient.saveGrowthReport(JSON.toJSONString(msgData));
                if (Objects.isNull(response)) {
                    log.error("同步大屏成长报告失败,uce接口响应超时");
                }
            }

        } catch (Exception e) {
            log.error("同步大屏成长报告失败,cause ==>> {}", e.getMessage());

            if (MapUtils.isNotEmpty(error)) {
                String errorStart = this.error.get("start");

                if (errorStart.equalsIgnoreCase("true")) {
                    String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
                    String filePath = this.error.get("filePath");
                    String filePath1 = filePath+fileName;
                    FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
                }

            }
        }
    }

    /**
     * @description 添加收藏记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgCollectionQueue()}",
            containerFactory = "#{rabbitMqSourceConfig.getUcgCollectionSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getUcgCollectionStartUp()}",
            ackMode = "AUTO")
    public void collectionConsumer(Channel channel, Message message, String content) throws IOException {
        log.info("receive UserCollection add message, content {}", content);

        try {

            JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
            if (Objects.nonNull(content)) {
                String evt = jsonObject.get("evt").toString();
                String msgData = jsonObject.get("msgData").toString();
                switch (evt.toUpperCase()) {
                    // 添加收藏
                    case "ADDCOLLECTION":
                        this.restTemplateClient.addCollection(msgData);
                        break;
                    // 删除收藏
                    case "DELETECOLLECTION":
                        this.restTemplateClient.deleteCollection(msgData);
                        break;
                    // 删除全部收藏
                    case "DELETEALLCOLLECTION":
                        this.restTemplateClient.deleteAllCollection(msgData);
                        break;
                    default:
                        break;

                }
            }

        } catch (Exception e) {

            log.error("收藏事件处理异常,cause ==>> {}", e.getMessage());

            if (MapUtils.isNotEmpty(error)) {
                String errorStart = this.error.get("start");

                if (errorStart.equalsIgnoreCase("true")) {
                    String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
                    String filePath = this.error.get("filePath");
                    String filePath1 = filePath+fileName;
                    FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
                }

            }

        }
    }

    /**
     * @description 处理观影记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getViewRecordQueue()}",
            containerFactory = "#{rabbitMqSourceConfig.getViewRecordSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getViewRecordStartUp()}",
            ackMode = "AUTO")
    public void viewRecordConsumer(Channel channel, Message message, String content) throws IOException {
        log.info("viewRecordConsumer receive ViewRecord add message, content {}", content);

        try {

            JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
            if (Objects.nonNull(content)) {
                String evt = jsonObject.get("evt").toString();
                String msgData = jsonObject.get("msgData").toString();
                switch (evt.toUpperCase()) {
                    // 添加收藏
                    case "VIEWRECORD":
                        this.restTemplateClient.dealViewRecord(msgData);
                        break;
                    default:
                        break;
                }
            }

        } catch (Exception e) {
            log.error("观影事件处理异常,cause ==>> {}", e.getMessage());

            if (MapUtils.isNotEmpty(error)) {
                String errorStart = this.error.get("start");

                if (errorStart.equalsIgnoreCase("true")) {
                    String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
                    String filePath = this.error.get("filePath");
                    String filePath1 = filePath+fileName;
                    FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
                }

            }


        }
    }


    /**
     * @description 添加收藏记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgCollectionQueueAdd()}",
            containerFactory = "#{rabbitMqSourceConfig.getUcgCollectionSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getUcgCollectionStartUp()}",
            ackMode = "AUTO")
    public void collectionConsumerAdd(Channel channel, Message message, String content) throws IOException {
        log.info("receive collectionConsumerAdd add message, content {}", content);

        try {

            JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
            if (Objects.nonNull(content)) {
                String evt = jsonObject.get("evt").toString();
                String msgData = jsonObject.get("msgData").toString();
                switch (evt.toUpperCase()) {
                    // 添加收藏
                    case "ADDCOLLECTION":
                        this.restTemplateClient.addCollection(msgData);
                        break;
                    // 删除收藏
                    case "DELETECOLLECTION":
                        this.restTemplateClient.deleteCollection(msgData);
                        break;
                    // 删除全部收藏
                    case "DELETEALLCOLLECTION":
                        this.restTemplateClient.deleteAllCollection(msgData);
                        break;
                    default:
                        break;

                }
            }


        } catch (Exception e) {

            log.error("添加收藏记录事件处理异常,cause ==>> {}", e.getMessage());

            if (MapUtils.isNotEmpty(error)) {
                String errorStart = this.error.get("start");

                if (errorStart.equalsIgnoreCase("true")) {
                    String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
                    String filePath = this.error.get("filePath");
                    String filePath1 = filePath+fileName;
                    FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
                }

            }

        }
    }

    /**
     * @description 删除收藏记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgCollectionQueueDelete()}",
            containerFactory = "#{rabbitMqSourceConfig.getUcgCollectionSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getUcgCollectionStartUp()}",
            ackMode = "AUTO")
    public void collectionConsumerDelete(Channel channel, Message message, String content) throws IOException {
        log.info("receive collectionConsumerDelete add message, content {}", content);

        try {
            JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
            if (Objects.nonNull(content)) {
                String evt = jsonObject.get("evt").toString();
                String msgData = jsonObject.get("msgData").toString();
                switch (evt.toUpperCase()) {
                    // 添加收藏
                    case "ADDCOLLECTION":
                        this.restTemplateClient.addCollection(msgData);
                        break;
                    // 删除收藏
                    case "DELETECOLLECTION":
                        this.restTemplateClient.deleteCollection(msgData);
                        break;
                    // 删除全部收藏
                    case "DELETEALLCOLLECTION":
                        this.restTemplateClient.deleteAllCollection(msgData);
                        break;
                    default:
                        break;

                }
            }

        } catch (Exception e) {

            log.error("删除收藏记录事件处理异常,cause ==>> {}", e.getMessage());

            if (MapUtils.isNotEmpty(error)) {
                String errorStart = this.error.get("start");

                if (errorStart.equalsIgnoreCase("true")) {
                    String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
                    String filePath = this.error.get("filePath");
                    String filePath1 = filePath+fileName;
                    FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
                }

            }


        }
    }

    /**
     * @description 删除全部收藏记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgCollectionQueueDeleteAll()}",
            containerFactory = "#{rabbitMqSourceConfig.getUcgCollectionSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getUcgCollectionStartUp()}",
            ackMode = "MANUAL")
    public void collectionConsumerDeleteAll(Channel channel, Message message, String content) throws IOException {
        log.info("receive collectionConsumerDeleteAll add message, content {}", content);

        try {

            JSONObject jsonObject = JSON.parseObject(content, JSONObject.class);
            if (Objects.nonNull(content)) {
                String evt = jsonObject.get("evt").toString();
                String msgData = jsonObject.get("msgData").toString();
                switch (evt.toUpperCase()) {
                    // 添加收藏
                    case "ADDCOLLECTION":
                        this.restTemplateClient.addCollection(msgData);
                        break;
                    // 删除收藏
                    case "DELETECOLLECTION":
                        this.restTemplateClient.deleteCollection(msgData);
                        break;
                    // 删除全部收藏
                    case "DELETEALLCOLLECTION":
                        this.restTemplateClient.deleteAllCollection(msgData);
                        break;
                    default:
                        break;

                }
            }

        } catch (Exception e) {
            log.error("删除全部收藏记录事件处理异常,cause ==>> {}", e.getMessage());

            if (MapUtils.isNotEmpty(error)) {
                String errorStart = this.error.get("start");

                if (errorStart.equalsIgnoreCase("true")) {
                    String fileName = this.error.get("fileName")+"_"+ LocalDate.now() +".log";
                    String filePath = this.error.get("filePath");
                    String filePath1 = filePath+fileName;
                    FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
                }

            }
        }
    }






}