package cn.fw.valhalla.service.bus.follow; import cn.fw.valhalla.common.utils.DateUtil; import cn.fw.valhalla.common.utils.StringUtils; import cn.fw.valhalla.common.utils.ThreadPoolUtil; import cn.fw.valhalla.domain.db.follow.FollowClue; import cn.fw.valhalla.domain.db.pub.PubCluePool; import cn.fw.valhalla.domain.enums.ClueStatusEnum; import cn.fw.valhalla.domain.enums.FollowTypeEnum; import cn.fw.valhalla.domain.enums.PublicClueStateEnum; import cn.fw.valhalla.domain.enums.SettingTypeEnum; import cn.fw.valhalla.domain.vo.setting.SettingVO; import cn.fw.valhalla.sdk.result.CustomerClueDeadline; import cn.fw.valhalla.service.bus.setting.strategy.impl.GeneralSetting; import cn.fw.valhalla.service.data.FollowClueService; import cn.fw.valhalla.service.data.PubCluePoolService; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.time.Duration; import java.time.LocalDateTime; import java.util.*; import java.util.concurrent.CompletableFuture; /** * 线索api查询 * * @author : kurisu * @version : 2.0 * @className : ClueApiBizService * @description : 线索查询 * @date : 2023-04-27 17:07 */ @Slf4j @RequiredArgsConstructor @Service public class ClueApiBizService { private final FollowClueService followClueService; private final GeneralSetting generalSetting; private final PubCluePoolService pubCluePoolService; /** * 查询线索最快到期的时间 * * @param vin * @param groupId * @return */ public CustomerClueDeadline queryClueDeadline(final String vin, final Long groupId) { final CustomerClueDeadline customerClueDeadline = new CustomerClueDeadline(); customerClueDeadline.setVin(vin); SettingVO bySettingType = generalSetting.getBySettingType(SettingTypeEnum.EFFECTIVE_TIME, groupId, generalSetting.COMMON_BRAND_ID); int hours = Optional.ofNullable(bySettingType).map(SettingVO::getDetailValue).orElse(36); Duration duration = Duration.ofHours(hours); customerClueDeadline.setFollowDuration(duration); FollowClue followClue = followClueService.getOne(Wrappers.lambdaQuery() .eq(FollowClue::getVin, vin) .eq(FollowClue::getGroupId, groupId) .eq(FollowClue::getClueState, ClueStatusEnum.ONGOING) .ne(FollowClue::getClueType, FollowTypeEnum.AC) .orderByAsc(FollowClue::getEndTime) .last(" limit 1") ); LocalDateTime deadline = null; if (Objects.nonNull(followClue)) { deadline = followClue.getEndTime(); } PubCluePool pubCluePool = pubCluePoolService.getOne(Wrappers.lambdaQuery() .eq(PubCluePool::getVin, vin) .eq(PubCluePool::getGroupId, groupId) .eq(PubCluePool::getState, PublicClueStateEnum.ONGOING) .orderByAsc(PubCluePool::getDeadline) .last(" limit 1") ); if (Objects.nonNull(pubCluePool)) { LocalDateTime pubClueDeadline = pubCluePool.getDeadline().atStartOfDay(); if (Objects.isNull(deadline)) { deadline = pubClueDeadline; } else { if (pubClueDeadline.isBefore(deadline)) { deadline = pubClueDeadline; } } } if (Objects.nonNull(deadline)) { customerClueDeadline.setDeadline(DateUtil.localDateTime2Date(deadline)); } return customerClueDeadline; } /** * 批量查询档案最快到期线索的截止时间 * * @param vinList * @param groupId * @return */ public List queryClueDeadlineBatch(final List vinList, final Long groupId) { HashSet vinSet = new HashSet<>(); for (String vin : vinList) { if (StringUtils.isValid(vin)) { vinSet.add(vin); } } final List list = new ArrayList<>(); CompletableFuture[] futureArr = vinSet.stream() .map(vin -> CompletableFuture.runAsync(() -> list.add(queryClueDeadline(vin, groupId)), ThreadPoolUtil.getInstance().getExecutor())) .>toArray(CompletableFuture[]::new); try { CompletableFuture.allOf(futureArr).get(); } catch (Exception e) { log.error("Failed to query clue deadline", e); } return list; } }