通过表态投票获得精确读者反馈的技术路径

查看表态投票:让你的文章得到更真实的读者反馈思考和实现

为了简化我们暂时只聚焦在文章类型上,例如以下是以表格形式展示不同类型文章的表态选项:

通过这样的投票机制,不仅能让读者在阅读过程中更具参与感,还能通过投票结果帮助其他读者快速了解文章的受欢迎程度和优缺点。同时,作者也能通过这些反馈不断优化和改进自己的创作。

主架构

表态核心接口设计和实现

package org.zyf.javabasic.project.vote.attitude;
 
import org.zyf.javabasic.project.vote.model.attitud.AttitudeVo;
import org.zyf.javabasic.project.vote.model.attitud.AttitudeMakeReq;
import org.zyf.javabasic.project.vote.model.attitud.AttitudeQueryReq;
import org.zyf.javabasic.project.vote.model.attitud.AttitudeShowRes;
 
import java.util.List;
 
/**
 * @program: zyfboot-javabasic
 * @description: 表态服务
 * @author: zhangyanfeng
 * @create: 2022-10-01 17:19
 **/
public interface AttitudeService {
    /**
     * 查询态度
     *
     * @param attitudeQueryReq
     * @return
     */
    List<AttitudeVo> queryAttitude(AttitudeQueryReq attitudeQueryReq);
 
    /**
     * 表明态度
     *
     * @param showAttitudeRequest
     * @return
     */
    AttitudeShowRes makeAttitude(AttitudeMakeReq showAttitudeRequest);
}
 
package org.zyf.javabasic.project.vote.attitude;
 
import com.alibaba.fastjson.JSON;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zyf.javabasic.project.vote.core.VoteCounter;
import org.zyf.javabasic.project.vote.core.VoteManager;
import org.zyf.javabasic.project.vote.hbase.core.VoteRecordsHAO;
import org.zyf.javabasic.project.vote.hbase.model.VoteRecordHO;
import org.zyf.javabasic.project.vote.model.attitud.*;
import org.zyf.javabasic.project.vote.model.vote.*;
import org.zyf.javabasic.project.vote.utils.VoteUtils;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
/**
 * @program: zyfboot-javabasic
 * @description: 表态服务具体实现
 * @author: zhangyanfeng
 * @create: 2022-10-01 17:20
 **/
@Service
@Slf4j
public class AttitudeServiceImpl implements AttitudeService {
 
    @Autowired
    private VoteManager voteManager;
    @Autowired
    private VoteRecordsHAO voteRecordsHAO;
    @Autowired
    private VoteCounter voteCounter;
 
    private final String SHOW = "SHOW";
    private final String UNSHOW = "UNSHOW";
    private final LoadingCache<String, List<AttitudeVo>> localCache
            = CacheBuilder.newBuilder()
            .expireAfterWrite(1, TimeUnit.MINUTES)
            .maximumSize(5)
            .build(new CacheLoader<String, List<AttitudeVo>>() {
                @Override
                public List<AttitudeVo> load(String groupId) throws IOException {
                    return loadAttitudes(groupId);
                }
            });
 
    /**
     * 模拟配置后台处理:
     * 如果配置表态类型多的话,本处使用DB存储
     * 如果配置的表态类型基本固定的话,本次直接读取配置中心内容即可
     */
    private List<AttitudeVo> loadAttitudes(String groupId) throws IOException {
        //模拟配置后台结果
        InputStream inputStream = getClass().getResourceAsStream("/attitudes.json");
        Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
        String attitudesStr = scanner.hasNext() ? scanner.next() : "";
        List<AttitudeGroupVo> attitudeGroupVoList = JSON.parseArray(attitudesStr, AttitudeGroupVo.class);
        if (CollectionUtils.isEmpty(attitudeGroupVoList)) {
            return Lists.newArrayList();
        }
        List<AttitudeVo> attitudeVos = attitudeGroupVoList.stream().filter(
                        g -> StringUtils.equalsIgnoreCase(groupId, g.getGroupId())).findFirst().orElse(new AttitudeGroupVo())
                .getAttitudes();
        if (attitudeVos == null) {
            return Lists.newArrayList();
        }
        return attitudeVos;
    }
 
    @Override
    public List<AttitudeVo> queryAttitude(AttitudeQueryReq attitudeQueryReq) {
        //1、获取态度组配置
        List<AttitudeVo> attitudeConfigConfigVos = null;
        try {
            attitudeConfigConfigVos = localCache.get(attitudeQueryReq.getContentType());
        } catch (ExecutionException e) {
            log.error("buildOptions {} 本地获取态度组失败", attitudeQueryReq, e);
        }
        log.info("attitudeConfigConfigVos:{}", attitudeConfigConfigVos);
        //拷贝list防止高并发时数据被篡改
        List<AttitudeVo> attitudeVos = deepCopy(attitudeConfigConfigVos);
        String votingId = VoteUtils.buildVotingId(attitudeQueryReq.getContentType(), attitudeQueryReq.getContentId());
        List<VoteOption> counterColumns = attitudeVos.stream().map(v -> {
            VoteOption voteOption = new VoteOption();
            voteOption.setKey(v.getId());
            return voteOption;
        }).collect(Collectors.toList());
        VotingInfo votingInfo = voteManager.queryVotingInfoWithCounter(votingId, attitudeQueryReq.getUserId(), counterColumns);
        if (Objects.isNull(votingInfo)) {
            attitudeVos.forEach(
                    attitudeVo -> {
                        attitudeVo.setShowed(false);
                        attitudeVo.setCount(0);
                    }
            );
            return attitudeVos;
        }
 
        Map<String, Long> countMap = votingInfo.getCounters().stream().collect(
                Collectors.toMap(VoteOptionVO::getKey, VoteOptionVO::getCount, (a, b) -> a));
        if (MapUtils.isEmpty(countMap)) {
            return attitudeVos;
        }
        attitudeVos.forEach(vo -> {
                    Long count = countMap.get(vo.getId());
                    if (count == null || count < 0) {
                        count = 0L;
                        if (count != null && count < 0) {
                            log.error("出现count<0场景", attitudeQueryReq);
                        }
                    }
                    vo.setCount(count);
 
                    if (StringUtils.equalsIgnoreCase(vo.getId(), votingInfo.getVotingResult())) {
                        vo.setShowed(true);
                    } else {
                        vo.setShowed(false);
                    }
                }
        );
        List<AttitudeVo> resultList = Lists.newArrayList();
        List<AttitudeVo> attitudeVosHasCount = attitudeVos.stream().filter(a -> a.getCount() > 0L).collect(
                Collectors.toList());
        List<AttitudeVo> attitudeVosNoCount = attitudeVos.stream().filter(a -> a.getCount() == 0L).collect(
                Collectors.toList());
        attitudeVosHasCount.sort(Comparator.comparingLong(AttitudeVo::getCount).reversed());
        resultList.addAll(attitudeVosHasCount);
        resultList.addAll(attitudeVosNoCount);
        return resultList;
    }
 
    /**
     * 深拷贝list,防止并发数值异常
     *
     * @param attitudeConfigConfigVos
     * @return
     */
    private List<AttitudeVo> deepCopy(List<AttitudeVo> attitudeConfigConfigVos) {
        List<AttitudeVo> attitudeVos = Lists.newArrayList();
        attitudeConfigConfigVos.forEach(
                attitudeVo -> {
                    AttitudeVo a = new AttitudeVo();
                    BeanUtils.copyProperties(attitudeVo, a);
                    a.setShowed(false);
                    a.setCount(0);
                    attitudeVos.add(a);
                }
        );
        return attitudeVos;
    }
 
    @Override
    public AttitudeShowRes makeAttitude(AttitudeMakeReq attitudeMakeReq) {
        AttitudeShowRes attitudeShowRes = new AttitudeShowRes();
        AttitudeShowRes errorResult = AttitudeShowRes.checkAttitudeRequest(attitudeMakeReq);
        if (errorResult != null) {
            return errorResult;
        }
        //1、发表态度(投票)
        VotingInfoReq request = VotingInfoReq.convert2VotingRequest(attitudeMakeReq);
        //获取用户当前的是否有态度
        VotingInfo current = queryVotingInfo(request.getVotingId(), request.getBizId(),
                attitudeMakeReq.getUserId(), request.getVotingResult());
        VoteRes voteResult = new VoteRes();
        if (StringUtils.equalsIgnoreCase(SHOW, attitudeMakeReq.getInteractActionType())) {
            voteResult = showAttitude(current, request, attitudeMakeReq.getDataType());
        } else if (StringUtils.equalsIgnoreCase(UNSHOW, attitudeMakeReq.getInteractActionType())) {
            voteResult = unShowAttitude(current, request, attitudeMakeReq.getDataType());
        }
 
        attitudeShowRes.setSuccess(voteResult.isSuccess());
        attitudeShowRes.setDesc(voteResult.getResultDesc());
        return attitudeShowRes;
    }
 
    //其他实现
}
 

投票核心接口设计和实现

在投票系统中,VoteManager 和 VoteCounter 接口定义了投票管理和计数操作的核心功能。

VoteManager 接口设计和处理

VoteManager 接口负责管理投票的创建、查询、存储和删除操作。

VoteCounter 接口设计和处理

VoteCounter 接口负责管理投票计数的增加和减少操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值