package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.User;
import com.ruoyi.cai.domain.UserAlbum;
import com.ruoyi.cai.dto.app.query.AlbumAddReq;
import com.ruoyi.cai.dto.app.query.AlbumResetReq;
import com.ruoyi.cai.mapper.UserAlbumMapper;
import com.ruoyi.cai.service.UserAlbumService;
import com.ruoyi.cai.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 主播集锦Service业务层处理
 *
 * @author 77
 * @date 2023-12-23
 */
@RequiredArgsConstructor
@Service
public class UserAlbumServiceImpl extends ServiceImpl<UserAlbumMapper, UserAlbum> implements UserAlbumService {
    @Autowired
    private UserService userService;
    @Override
    public List<String> getUserAlbum(Long userId,Integer anchor){
        LambdaQueryWrapper<UserAlbum> eq = Wrappers.lambdaQuery(UserAlbum.class)
                .select(UserAlbum::getUrl)
                .eq(UserAlbum::getUserId, userId);
        if(anchor != null && anchor == 1){
            eq.ne(UserAlbum::getAuditRemark,2);
        }else{
            eq.eq(UserAlbum::getAuditRemark,1);
        }
        List<UserAlbum> list = this.list(eq);
        return list.stream().map(UserAlbum::getUrl).collect(Collectors.toList());
    }

    @Override
    public List<UserAlbum> listByUserId(Long userId) {
        return this.list(Wrappers.lambdaQuery(UserAlbum.class).eq(UserAlbum::getUserId,userId));
    }

    @Override
    public boolean addAlbum(AlbumAddReq res) {
        User user = userService.getById(res.getUserId());
        UserAlbum album = new UserAlbum();
        album.setUserId(res.getUserId());
        album.setUrl(res.getUrl());
        album.setIsAnchor(user.getIsAnchor());
        album.setOrderBy(res.getOrderBy());
        this.save(album);
        return true;
    }

    @Override
    public boolean updateAlbum(AlbumAddReq res) {
        UserAlbum album = new UserAlbum();
        album.setId(res.getId());
        album.setUrl(res.getUrl());
        album.setOrderBy(res.getOrderBy());
        this.updateById(album);
        return true;
    }


    @Override
    public boolean removeAlbum(Long id){
        return this.removeById(id);
    }

    @Override
    public boolean resetAlbum(List<AlbumResetReq> res, Long userId) {
        List<UserAlbum> update = new ArrayList<>();
        for (AlbumResetReq re : res) {
            UserAlbum album = new UserAlbum();
            album.setId(re.getId());
            album.setOrderBy(re.getOrderBy());
            update.add(album);
        }
        this.updateBatchById(update);
        return true;
    }

}
