123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package com.winhc.repal.service.impl;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.map.MapUtil;
- import cn.hutool.core.util.ObjectUtil;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.winhc.repal.entity.RepalRemindHistory;
- import com.winhc.repal.entity.RepalRemindReadInfo;
- import com.winhc.repal.enums.Dict;
- import com.winhc.repal.enums.RemindEnum;
- import com.winhc.repal.model.bo.BillRemindBO;
- import com.winhc.repal.model.bo.BookDynamicCountBO;
- import com.winhc.repal.model.bo.UserBillUnreadInfoBO;
- import com.winhc.repal.repository.RepalRemindReadInfoMapper;
- import com.winhc.repal.service.RepalRemindHistoryService;
- import com.winhc.repal.service.RepalRemindReadInfoService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.winhc.repal.util.RedisUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.time.LocalDateTime;
- import java.time.temporal.ChronoUnit;
- import java.util.*;
- import java.util.concurrent.TimeUnit;
- /**
- * @description RepalRemindReadInfo 接口实现类
- * @author Generator
- * @date 2022-04-27
- */
- @Service
- @Slf4j
- public class RepalRemindReadInfoServiceImpl extends ServiceImpl<RepalRemindReadInfoMapper, RepalRemindReadInfo> implements RepalRemindReadInfoService {
- @Autowired
- private RepalRemindHistoryService repalRemindHistoryService;
- @Autowired
- private RedisUtil redisUtil;
- private static final String MESSAGE_PRIFIX = "winhc:repal:read_";
- /**
- * 查询账款已读情况,没数据则插入一条全部已读
- * @param userId
- * @param repalBillId
- * @param repalBookId
- * @return
- */
- @Override
- public RepalRemindReadInfo getReadInfoByUserId(Long userId,Long repalBillId,Long repalBookId) {
- RepalRemindReadInfo repalRemindReadInfo = getOne(Wrappers.lambdaQuery(RepalRemindReadInfo.class)
- .eq(RepalRemindReadInfo::getUserId,userId)
- .eq(RepalRemindReadInfo::getRepalBillId,repalBillId)
- .eq(RepalRemindReadInfo::getRepalBookId,repalBookId),false);
- if(ObjectUtil.isNotNull(repalRemindReadInfo)){
- return repalRemindReadInfo;
- }
- repalRemindReadInfo = new RepalRemindReadInfo().setUserId(userId).setRepalBillId(repalBillId).setRepalBookId(repalBookId)
- .setRankRemindId(0L).setRiskRemindId(0L).setFinanceRemindId(0L).setOverdueRemindId(0L);
- RepalRemindHistory overDue = repalRemindHistoryService.getOne(Wrappers.lambdaQuery(RepalRemindHistory.class)
- .eq(RepalRemindHistory::getRepalBillId,repalBillId)
- .eq(RepalRemindHistory::getTrentType, RemindEnum.逾期提醒.getCode())
- .orderByDesc(RepalRemindHistory::getId)
- .last(" LIMIT 1 "),false);
- if(ObjectUtil.isNotNull(overDue)){
- repalRemindReadInfo.setOverdueRemindId(overDue.getId());
- }
- RepalRemindHistory rank = repalRemindHistoryService.getOne(Wrappers.lambdaQuery(RepalRemindHistory.class)
- .eq(RepalRemindHistory::getRepalBillId,repalBillId)
- .eq(RepalRemindHistory::getTrentType, RemindEnum.账款评级.getCode())
- .orderByDesc(RepalRemindHistory::getId)
- .last(" LIMIT 1 "),false);
- if(ObjectUtil.isNotNull(rank)){
- repalRemindReadInfo.setRankRemindId(rank.getId());
- }
- RepalRemindHistory finance = repalRemindHistoryService.getOne(Wrappers.lambdaQuery(RepalRemindHistory.class)
- .eq(RepalRemindHistory::getRepalBillId,repalBillId)
- .eq(RepalRemindHistory::getTrentType, RemindEnum.财产线索.getCode())
- .orderByDesc(RepalRemindHistory::getId)
- .last(" LIMIT 1 "),false);
- if(ObjectUtil.isNotNull(finance)){
- repalRemindReadInfo.setFinanceRemindId(finance.getId());
- }
- RepalRemindHistory risk = repalRemindHistoryService.getOne(Wrappers.lambdaQuery(RepalRemindHistory.class)
- .eq(RepalRemindHistory::getRepalBillId,repalBillId)
- .eq(RepalRemindHistory::getTrentType, RemindEnum.风险预警.getCode())
- .orderByDesc(RepalRemindHistory::getId)
- .last(" LIMIT 1 "),false);
- if(ObjectUtil.isNotNull(risk)){
- repalRemindReadInfo.setRiskRemindId(risk.getId());
- }
- save(repalRemindReadInfo);
- return repalRemindReadInfo;
- }
- /**
- * 根据类型更新已读状态
- * 风险预警 逾期动态 财产提醒 由于是每天跑调度,一天更新一次即可
- * 评级变更 10s更新一次
- * @param userId
- * @param remindType
- */
- @Override
- public void updateReadInfoByUserId(Long userId, String remindType,Long repalBillId,Long repalBookId) {
- String redisKey = MESSAGE_PRIFIX+userId+"_"+remindType+"_"+repalBookId+"_"+repalBillId;
- if(redisUtil.get(redisKey) != null){
- return;
- }
- RepalRemindReadInfo repalRemindReadInfo = getReadInfoByUserId(userId,repalBillId,repalBookId);
- RepalRemindHistory repalRemindHistory = repalRemindHistoryService.getOne(Wrappers.lambdaQuery(RepalRemindHistory.class)
- .eq(RepalRemindHistory::getRepalBillId,repalBillId)
- .eq(RepalRemindHistory::getRemindType, remindType)
- .orderByDesc(RepalRemindHistory::getId)
- .last(" LIMIT 1 "),false);
- if(ObjectUtil.isNotNull(repalRemindHistory)){
- if(RemindEnum.账款评级.getCode().equals(remindType)){
- repalRemindReadInfo.setRankRemindId(repalRemindHistory.getId());
- updateById(repalRemindReadInfo);
- redisUtil.setEx(redisKey,"1",10, TimeUnit.SECONDS);
- }else if(RemindEnum.风险预警.getCode().equals(remindType)){
- repalRemindReadInfo.setRiskRemindId(repalRemindHistory.getId());
- updateById(repalRemindReadInfo);
- redisUtil.setEx(redisKey,"1",calTTL(), TimeUnit.MILLISECONDS);
- }else if(RemindEnum.财产线索.getCode().equals(remindType)){
- repalRemindReadInfo.setFinanceRemindId(repalRemindHistory.getId());
- updateById(repalRemindReadInfo);
- redisUtil.setEx(redisKey,"1",calTTL(), TimeUnit.MILLISECONDS);
- }else if(RemindEnum.逾期提醒.getCode().equals(remindType)){
- repalRemindReadInfo.setOverdueRemindId(repalRemindHistory.getId());
- updateById(repalRemindReadInfo);
- redisUtil.setEx(redisKey,"1",calTTL(), TimeUnit.MILLISECONDS);
- }
- }
- }
- /**
- * 计算当天剩余的毫秒时间
- * @return
- */
- private Long calTTL(){
- LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
- return ChronoUnit.MILLIS.between(LocalDateTime.now(),midnight);
- }
- @Override
- public List<UserBillUnreadInfoBO> getUserBillUnreadInfo(Long userId, List<Long> repalBillIdList) {
- if (CollUtil.isEmpty(repalBillIdList)) {
- return new ArrayList<>();
- }
- List<RepalRemindReadInfo> readInfoList = this.list(Wrappers.lambdaQuery(RepalRemindReadInfo.class).eq(RepalRemindReadInfo::getUserId, userId)
- .in(RepalRemindReadInfo::getRepalBillId, repalBillIdList));
- if (CollUtil.isEmpty(readInfoList)) {
- return new ArrayList<>();
- }
- Map<Long, List<BillRemindBO>> remindMap = repalRemindHistoryService.getRepalBillLastRemind(repalBillIdList);
- if (MapUtil.isEmpty(remindMap)) {
- return new ArrayList<>();
- }
- List<UserBillUnreadInfoBO> unreadInfoList = new ArrayList<>();
- readInfoList.forEach(readInfo -> {
- List<BillRemindBO> remindBOList = remindMap.get(readInfo.getRepalBillId());
- if (CollUtil.isNotEmpty(remindBOList)) {
- UserBillUnreadInfoBO unreadInfo = new UserBillUnreadInfoBO();
- unreadInfo.setRepalBillId(readInfo.getRepalBillId());
- unreadInfo.setUnreadFinanceCount(getRemindTypeUnread(readInfo.getId(), remindBOList, RemindEnum.财产线索.getCode()));
- unreadInfo.setUnreadRankCount(getRemindTypeUnread(readInfo.getId(), remindBOList, RemindEnum.账款评级.getCode()));
- unreadInfo.setUnreadOverdueCount(getRemindTypeUnread(readInfo.getId(), remindBOList, RemindEnum.逾期提醒.getCode()));
- unreadInfo.setUnreadRiskCount(getRemindTypeUnread(readInfo.getId(), remindBOList, RemindEnum.风险预警.getCode()));
- unreadInfoList.add(unreadInfo);
- }
- });
- return unreadInfoList;
- }
- /**
- * 计算未读数量
- * @param currentId currentId
- * @param billRemindBOList billRemindBOList
- * @param remindType remindType
- * @return java.lang.Integer
- * @author xda
- * @date 2022/4/30 14:31
- */
- public Integer getRemindTypeUnread(Long currentId, List<BillRemindBO> billRemindBOList, String remindType) {
- if (Objects.isNull(currentId)) {
- return 0;
- }
- Optional<BillRemindBO> first = billRemindBOList.stream().filter(t -> remindType.equals(t.getRemindType())).findFirst();
- return first.map(billRemindBO -> (int) (billRemindBO.getRepalRemindHistoryId() - currentId)).orElse(0);
- }
- }
|