package com.yeshi.buwan.service.imp.user; import com.yeshi.buwan.dao.user.UserDPContentWatchStatisticDao; import com.yeshi.buwan.domain.user.UserDPContentWatchStatistic; import com.yeshi.buwan.service.inter.user.UserDPContentWatchStatisticService; import com.yeshi.buwan.util.RedisManager; import com.yeshi.buwan.util.StringUtil; import com.yeshi.buwan.util.TimeUtil; import com.yeshi.buwan.vo.AcceptData; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Date; /** * @author hxh * @title: UserDPContentWatchStatisticServiceImpl * @description: TODO * @date 2021/12/29 18:04 */ @Service public class UserDPContentWatchStatisticServiceImpl implements UserDPContentWatchStatisticService { @Resource private UserDPContentWatchStatisticDao userDPContentWatchStatisticDao; @Resource private RedisManager redisManager; private String getRedisKeyPrefix(AcceptData acceptData) { return String.format("dpevent-%s-", getIdentityId(acceptData)); } private UserDPContentWatchStatistic getBaseBean(AcceptData acceptData, String loginUid) { UserDPContentWatchStatistic statistic = new UserDPContentWatchStatistic(); statistic.setId(getIdentityId(acceptData)); statistic.setDetailSystemId(acceptData.getDetailSystem().getId()); String device = acceptData.getUtdId(); if (StringUtil.isNullOrEmpty(device)) { device = acceptData.getDevice(); } statistic.setDevice(device); statistic.setLoginUid(loginUid); return statistic; } //增加小说阅读时长 private void addNovelDuration(AcceptData acceptData, String loginUid, long ds) { UserDPContentWatchStatistic statistic = getBaseBean(acceptData, loginUid); UserDPContentWatchStatistic old = userDPContentWatchStatisticDao.get(statistic.getId()); if (old == null) { statistic.setNovelDuration(ds); statistic.setCreateTime(new Date()); userDPContentWatchStatisticDao.save(statistic); } else { if (old.getNovelDuration() == null) { statistic.setNovelDuration(ds); } else { statistic.setNovelDuration(ds + old.getNovelDuration()); } userDPContentWatchStatisticDao.updateSelective(statistic); } } @Override public void readNovel(AcceptData acceptData, long duration, String loginUid) { long ds = duration / 1000; //获取今日是否已经上传了 String key = getRedisKeyPrefix(acceptData) + "novel-" + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMdd"); String old = redisManager.getCommonString(key); if (StringUtil.isNullOrEmpty(old)) { //增加阅读时长 addNovelDuration(acceptData, loginUid, ds); //今日首次添加 redisManager.cacheCommonString(key, ds + "", 60 * 60 * 24); } else { //不是今日首次添加 if (ds - Long.parseLong(old) > 0) { addNovelDuration(acceptData, loginUid, ds - Long.parseLong(old)); //增加阅读时长 redisManager.cacheCommonString(key, ds + "", 60 * 60 * 24); } } } @Override public void playDrawVideo(AcceptData acceptData, String loginUid) { UserDPContentWatchStatistic statistic = getBaseBean(acceptData, loginUid); UserDPContentWatchStatistic old = userDPContentWatchStatisticDao.get(statistic.getId()); if (old == null) { statistic.setCreateTime(new Date()); statistic.setDrawVideoNum(1L); userDPContentWatchStatisticDao.save(statistic); } else { if (old.getDrawVideoNum() == null) { statistic.setDrawVideoNum(1L); } else { statistic.setDrawVideoNum(1L + old.getDrawVideoNum()); } userDPContentWatchStatisticDao.updateSelective(statistic); } } @Override public void readNews(AcceptData acceptData, String loginUid) { UserDPContentWatchStatistic statistic = getBaseBean(acceptData, loginUid); UserDPContentWatchStatistic old = userDPContentWatchStatisticDao.get(statistic.getId()); if (old == null) { statistic.setCreateTime(new Date()); statistic.setNewsNum(1L); userDPContentWatchStatisticDao.save(statistic); } else { if (old.getNewsNum() == null) { statistic.setNewsNum(1L); } else { statistic.setNewsNum(1L + old.getNewsNum()); } userDPContentWatchStatisticDao.updateSelective(statistic); } } @Override public String getIdentityId(AcceptData acceptData) { String device = acceptData.getUtdId(); if (StringUtil.isNullOrEmpty(device)) { device = acceptData.getDevice(); } if (StringUtil.isNullOrEmpty(device)) { return null; } return UserDPContentWatchStatistic.createId(acceptData.getDetailSystem().getId(), device); } @Override public boolean isDPUser(AcceptData acceptData) { //暂时设置 if(true){ return false; } String id = getIdentityId(acceptData); if (StringUtil.isNullOrEmpty(id)) { return false; } UserDPContentWatchStatistic bean = userDPContentWatchStatisticDao.get(id); if (bean == null) { return false; } //新闻阅读数量 if (bean.getNewsNum() != null && bean.getNewsNum() >=1) { return true; } //短视频阅读数量 if (bean.getDrawVideoNum() != null && bean.getDrawVideoNum() >= 5) { return true; } //小说阅读数量 if (bean.getNovelDuration() != null && bean.getNovelDuration() >= 60) { return true; } return false; } }