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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.user.iptv.service.UserTvService;
import com.topdraw.business.module.user.iptv.service.dto.UserTvDTO;
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 org.springframework.util.Assert;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Map;
import java.util.Objects;

@Component
@Slf4j
public class UcGatewayIptv2IptvConsumer {

    @Autowired
    RestTemplateClient restTemplateClient;

    @Autowired
    AutoRoute autoUser;

    @Autowired
    private MemberService memberService;
    @Autowired
    private UserTvService userTvService;

    @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 = "MANUAL")
    public void eventConsumer(Channel channel, Message message, String content) throws IOException {
        log.info(" receive dataSync msg , content is : {} ", content);
        try {
            DataSyncMsg dataSyncMsg = this.parseContent(content);

            this.taskDeal(dataSyncMsg);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        } catch (Exception e) {

            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);

            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());
                }

            }

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

    /**
     * 数据解析
     * @param content
     * @return
     */
    private DataSyncMsg parseContent(String content) {
        DataSyncMsg dataSyncMsg = JSONUtil.parseMsg2Object(content,DataSyncMsg.class);
        Assert.notNull(dataSyncMsg,"ERROR -->> operationConsumer -->> parseContent -->> 【dataSyncMsg】 not be null !!");
        DataSyncMsg.MsgData msgData = dataSyncMsg.getMsg();
        Long memberId = msgData.getMemberId();
        String memberCode = msgData.getMemberCode();
        if (Objects.nonNull(memberId) && StringUtils.isBlank(memberCode)) {
            MemberDTO memberDTO = this.memberService.findById(memberId);
            String code = memberDTO.getCode();
            msgData.setMemberCode(code);
        }

        String platformAccount = msgData.getPlatformAccount();
        if (StringUtils.isNotBlank(platformAccount)) {
            UserTvDTO userTvDTO = userTvService.findByPlatformAccount(platformAccount);
            if (Objects.nonNull(userTvDTO)) {
                String priorityMemberCode = userTvDTO.getPriorityMemberCode();
                if (StringUtils.isNotBlank(priorityMemberCode)) {
                    msgData.setMemberCode(priorityMemberCode);
                } else {
                    MemberDTO memberDTO = this.memberService.findById(userTvDTO.getMemberId());
                    msgData.setMemberCode(memberDTO.getCode());
                }
            }
        }
        Assert.notNull(msgData,"ERROR -->> operationConsumer -->> parseContent -->> 【msgData】 not be null !!");
        return dataSyncMsg;
    }

    /**
     * 任务处理
     * @param dataSyncMsg
     */
    private void taskDeal(DataSyncMsg dataSyncMsg) {
        this.restTemplateClient.dealTask(dataSyncMsg);
    }


    /**
     * @description 添加收藏记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getUcgCollectionQueue()}",
            containerFactory = "#{rabbitMqSourceConfig.getUcgCollectionSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getUcgCollectionStartUp()}",
            ackMode = "MANUAL")
    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;

                }
            }

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        } catch (Exception e) {

            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);

            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());
                }

            }

            e.printStackTrace();
        }
    }

    /**
     * @description 处理观影记录
     * @param content 消息内容
     */
    @RabbitHandler
    @RabbitListener(queues = "#{rabbitMqSourceConfig.getViewRecordQueue()}",
            containerFactory = "#{rabbitMqSourceConfig.getViewRecordSource()}",
            autoStartup = "#{rabbitMqSourceConfig.getViewRecordStartUp()}",
            ackMode = "MANUAL")
    public void viewRecordConsumer(Channel channel, Message message, String content) throws IOException {
        log.info("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;

                }
            }

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        } catch (Exception e) {

            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);

            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());
                }

            }

            e.printStackTrace();
        }
    }
}