package com.ycwl.basic.service.pc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.exception.BaseException;
import com.ycwl.basic.exception.BizException;
import com.ycwl.basic.mapper.VideoMapper;
import com.ycwl.basic.model.pc.video.entity.VideoEntity;
import com.ycwl.basic.model.pc.video.req.VideoReqQuery;
import com.ycwl.basic.model.pc.video.resp.VideoRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.pc.VideoService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author：longbinbin
 * @Date：2024/12/3 16:31
 */
@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private ScenicRepository scenicRepository;

    @Override
    public ApiResponse<PageInfo<VideoRespVO>> pageQuery(VideoReqQuery videoReqQuery) {
        PageHelper.startPage(videoReqQuery.getPageNum(), videoReqQuery.getPageSize());
        List<VideoRespVO> list = videoMapper.list(videoReqQuery);
        
        // 批量获取景区名称
        List<Long> scenicIds = list.stream()
                .map(VideoRespVO::getScenicId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, String> scenicNames = scenicRepository.batchGetScenicNames(scenicIds);
        
        // 设置景区名称
        list.forEach(item -> {
            if (item.getScenicId() != null) {
                item.setScenicName(scenicNames.get(item.getScenicId()));
            }
        });
        
        PageInfo<VideoRespVO> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse<List<VideoRespVO>> list(VideoReqQuery videoReqQuery) {
        List<VideoRespVO> list = videoMapper.list(videoReqQuery);
        
        // 批量获取景区名称
        List<Long> scenicIds = list.stream()
                .map(VideoRespVO::getScenicId)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        Map<Long, String> scenicNames = scenicRepository.batchGetScenicNames(scenicIds);
        
        // 设置景区名称
        list.forEach(item -> {
            if (item.getScenicId() != null) {
                item.setScenicName(scenicNames.get(item.getScenicId()));
            }
        });
        
        return ApiResponse.success(list);
    }

    @Override
    public ApiResponse<VideoRespVO> getById(Long id) {
        return ApiResponse.success(videoMapper.getById(id));
    }

    @Override
    public Boolean checkVideoBuyStatus(Long videoId) {
        // 查询 member_video 表中是否存在该视频的已购买记录
        int count = videoMapper.countBuyRecordByVideoId(videoId);
        // count > 0 表示存在已购买记录
        return count > 0;
    }

}
