ReportRpcService.java 7.62 KB
package cn.fw.freya.service.rpc;

import cn.fw.freya.common.PathConstant;
import cn.fw.freya.enums.AccountTypeEnum;
import cn.fw.freya.model.data.pool.LivePool;
import cn.fw.freya.model.data.pool.VideoPool;
import cn.fw.freya.model.dto.rpc.LivePoolDto;
import cn.fw.freya.model.dto.rpc.ReportAccountDto;
import cn.fw.freya.model.dto.rpc.VideoPoolDto;
import cn.fw.freya.utils.RequestUtil;
import cn.fw.freya.utils.http.HttpConfig;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author kurisu
 * @date 2021-11-11 18:11
 * @description 数据上报 接口
 */
@Slf4j
@Service
public class ReportRpcService {
    @Getter
    @Value("${freya.url}")
    private String baseUrl;

    /**
     * 账户信息上报
     *
     * @param accountNo  账户号
     * @param type       账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
     * @param accountMsg 账户信息
     * @return
     */
    public boolean reportAccountMsg(String accountNo, Integer type, ReportAccountDto accountMsg) {
        Map<String, Object> params = new HashMap<>();
        params.put("account", accountNo);
        params.put("type", type);
        params.put("fansCnt", accountMsg.getFansCnt());
        params.put("accountName", accountMsg.getAccountName());
        HttpConfig config = HttpConfig.custom()
                .encoding(java.nio.charset.StandardCharsets.UTF_8.displayName())
                .url(getBaseUrl() + PathConstant.REPORT_ACCOUNT_MSG)
                .map(params);
        String res = RequestUtil.put(config);
        if (!StringUtils.hasText(res)) {
            return false;
        }
        JSONObject resObj = JSONObject.parseObject(res);
        Boolean result = resObj.getBoolean("success");
        if (Boolean.FALSE.equals(result)) {
            Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
            String message = Optional.ofNullable(resObj.getString("message")).orElse("账号信息上报失败");
            log.error(LocalDate.now() + "上报账户 " + accountNo + AccountTypeEnum.getNameByValue(type) + "平台粉丝数据异常", message);
            return false;
        }
        return true;
    }

    /**
     * 视频数据上报
     *
     * @param accountNo 账户号
     * @param type      账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
     * @param videoList 视频数据集合
     * @return
     */
    public boolean reportVideo(String accountNo, Integer type, List<VideoPool> videoList) {
        VideoPool videoPool;
        if (videoList.size() > 0) {
            videoPool = videoList.get(0);
        } else {
            return true;
        }
        if (Objects.isNull(videoPool.getVideoId()) || Objects.isNull(videoPool.getVideoUrl())) {
            log.info(LocalDate.now() + AccountTypeEnum.getNameByValue(type) + "平台, 账户号为: " + accountNo + "的账号, 暂无视频数据");
            return true;
        }
        VideoPoolDto dto = new VideoPoolDto();
        dto.setAccount(accountNo);
        dto.setType(type);
        dto.setVideoList(videoList.stream().map(item -> VideoPoolDto.VideoDTO
                .builder()
                .videoId(item.getVideoId())
                .title(item.getTitle())
                .preview(item.getPreview())
                .uv(item.getPlayCount())
                .fullUv(item.getFullPlayCount())
                .likeNum(item.getLikeCount())
                .shareNum(item.getShareCount())
                .commentNum(item.getCommentCount())
                .newFanNum(item.getNewFansUserCnt())
                .duration(item.getDuration())
                .publishTime(item.getPublishTime())
                .playUrl(item.getVideoUrl())
                .build()
        ).collect(Collectors.toList()));
        HttpConfig config = HttpConfig.custom()
                .encoding(java.nio.charset.StandardCharsets.UTF_8.displayName())
                .url(getBaseUrl() + PathConstant.REPORT_VIDEO)
                .json(JSON.toJSONString(dto));
        String res = RequestUtil.post(config);
        if (!StringUtils.hasText(res)) {
            return false;
        }
        JSONObject resObj = JSONObject.parseObject(res);
        Boolean result = resObj.getBoolean("success");
        if (Boolean.FALSE.equals(result)) {
            Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
            String message = Optional.ofNullable(resObj.getString("message")).orElse("账号视频数据上报失败");
            log.error(LocalDate.now() + " 上报账户 " + AccountTypeEnum.getNameByValue(type) + "平台视频数据异常", message);
            return false;
        }
        return true;
    }

    /**
     * 直播数据上报
     *
     * @param accountNo 账户号
     * @param type      账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
     * @param liveList  直播数据集合
     * @return
     */
    public boolean reportLive(String accountNo, Integer type, List<LivePool> liveList) {
        LivePool livePool;
        if (liveList.size() > 0) {
            livePool = liveList.get(0);
        } else {
            return true;
        }
        if (Objects.isNull(livePool.getOpenTime()) || Objects.isNull(livePool.getRoomId())) {
            log.info(LocalDate.now() + AccountTypeEnum.getNameByValue(type) + "平台, 账户号为: " + accountNo + "的账号, 暂无直播数据");
            return true;
        }
        LivePoolDto dto = new LivePoolDto();
        dto.setAccount(accountNo);
        dto.setType(type);
        dto.setLiveList(liveList.stream().map(item -> LivePoolDto.LiveDTO
                .builder()
                .roomNo(item.getRoomId())
                .userNick(item.getUserNick())
                .title(item.getRoomName())
                .cover(item.getRoomCoverImage())
                .playbackUrl(item.getPlaybackUrl())
                .uv(item.getWatchUserCnt())
                .uvPeak(item.getWatchPeakUserCnt())
                .likeNum(item.getLikeCnt())
                .shareNum(item.getShareCnt())
                .commentNum(item.getCommentUserCnt())
                .newFanNum(item.getNewFansUserCnt())
                .receiveNum(item.getScore())
                .receiveAmount(item.getIncome())
                .liveDuration(item.getDuration())
                .liveStartTime(item.getOpenTime())
                .liveEndTime(item.getLiveEndTime())
                .build()
        ).collect(Collectors.toList()));
        //System.out.println(JSON.toJSONString(dto));
        HttpConfig config = HttpConfig.custom()
                .encoding(java.nio.charset.StandardCharsets.UTF_8.displayName())
                .url(getBaseUrl() + PathConstant.REPORT_LIVE)
                .json(JSON.toJSONString(dto));
        String res = RequestUtil.post(config);
        if (!StringUtils.hasText(res)) {
            return false;
        }
        JSONObject resObj = JSONObject.parseObject(res);
        Boolean result = resObj.getBoolean("success");
        if (Boolean.FALSE.equals(result)) {
            Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
            String message = Optional.ofNullable(resObj.getString("message")).orElse("账号直播数据上报失败");
            log.error(LocalDate.now() + " 上报账户 " + accountNo + AccountTypeEnum.getNameByValue(type) + "平台直播数据异常", message);
            return false;
        }
        return true;
    }
}