LivePoolServiceImpl.java 4.25 KB
package cn.fw.dalaran.service.data.impl;

import cn.fw.dalaran.dao.LivePoolDao;
import cn.fw.dalaran.domain.db.Account;
import cn.fw.dalaran.domain.db.ActivityTheme;
import cn.fw.dalaran.domain.db.LivePool;
import cn.fw.dalaran.domain.dto.PoolQueryDTO;
import cn.fw.dalaran.domain.vo.LivePoolVO;
import cn.fw.dalaran.service.data.AccountService;
import cn.fw.dalaran.service.data.ActivityThemeService;
import cn.fw.dalaran.service.data.LivePoolService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author kurisu
 * @date 2021-11-17 14:54
 * @description LivePoolService
 */
@Service
@RequiredArgsConstructor
@SuppressWarnings("Duplicates")
public class LivePoolServiceImpl extends ServiceImpl<LivePoolDao, LivePool> implements LivePoolService {

    private final ActivityThemeService activityThemeService;
    private final AccountService accountService;

    @Override
    public void removeAccountDataByDate(Long accountId, LocalDate date) {
        this.getBaseMapper().deleteByAccountDate(accountId, date);
    }

    @Override
    public List<LivePoolVO> liveList(PoolQueryDTO queryDTO) {
        int current = queryDTO.getCurrent() <= 0 ? 1 : queryDTO.getCurrent();
        Integer pageSize = queryDTO.getPageSize();
        Integer startIndex = (current - 1) * pageSize;
        return Optional.ofNullable(getBaseMapper().liveList(startIndex, pageSize, queryDTO)).orElse(new ArrayList<>());
    }

    @Override
    public long liveCount(PoolQueryDTO queryDTO) {
        return Optional.ofNullable(getBaseMapper().liveCount(queryDTO)).orElse(0L);
    }

    /**
     * 获取主题活动员工有效直播数
     *
     * @param userId  用户id
     * @param themeId 活动主题id
     * @return 有效直播数
     */
    @Override
    public Integer getValidLiveCnt(Long userId, Long themeId) {
        final ActivityTheme activityTheme = activityThemeService.getById(themeId);
        if (Objects.isNull(activityTheme))
            return 0;
        final Set<Long> accountIds = accountService.lambdaQuery()
                .eq(Account::getUserId, userId)
                .list()
                .stream()
                .map(Account::getId)
                .collect(Collectors.toSet());
        if (Objects.equals(accountIds.size(), 0))
            return 0;
        final List<LivePool> list = this.lambdaQuery()
                .in(LivePool::getAccountId, accountIds)
                .eq(LivePool::getValidLive, 1)
                .ge(LivePool::getLiveStartTime, activityTheme.getStartTime())
                .le(LivePool::getLiveStartTime, activityTheme.getEndTime())
                .list();
        if (Objects.equals(list.size(), 0))
            return 0;
        return list.stream().collect(Collectors.groupingBy(LivePool::getRoomNo)).size();
    }

    /**
     * 设置最佳直播
     *
     * @param liveId 直播数据id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean markBestLive(Long liveId) {
        final LivePool live = this.getById(liveId);
        final Long themeId = live.getThemeId();// 获取主题id
        final Account currentAccount = accountService.getById(live.getAccountId());
        final Long userId = currentAccount.getUserId();// 获取用户id
        final List<Account> accounts = accountService.getByUserId(userId);// 根据用户id获取拥有的所有账户
        this.lambdaUpdate()
                .set(LivePool::getValidLive, 1)
                .eq(LivePool::getValidLive, 11)
                .eq(LivePool::getThemeId, themeId)
                .in(LivePool::getAccountId, accounts
                        .stream()
                        .map(Account::getId)
                        .collect(Collectors.toList())
                )
                .update();// 还原以前标记过的最佳直播
        this.lambdaUpdate()
                .set(LivePool::getValidLive, 11)
                .eq(LivePool::getThemeId, themeId)
                .eq(LivePool::getId, liveId)
                .update();// 设置最新的最佳直播
        return true;
    }
}