package com.ruoyi.xq.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.xq.domain.DynamicImage;
import com.ruoyi.xq.domain.DynamicStar;
import com.ruoyi.xq.dto.app.dynamic.MyStarDynamicListVO;
import com.ruoyi.xq.dto.app.dynamic.MyStarDynamicQuery;
import com.ruoyi.xq.dto.common.user.MinUser;
import com.ruoyi.xq.mapper.DynamicStarMapper;
import com.ruoyi.xq.service.DynamicImageService;
import com.ruoyi.xq.service.DynamicStarService;
import com.ruoyi.xq.service.UserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 动态点赞Service业务层处理
 *
 * @author 77
 * @date 2024-03-25
 */
@Service
public class DynamicStarServiceImpl extends ServiceImpl<DynamicStarMapper,DynamicStar> implements DynamicStarService {
    @Autowired
    private UserService userService;
    @Autowired
    private DynamicImageService dynamicImageService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void star(Long dynamicId, Long starUserId) {
        MinUser user = userService.getMinUserById(starUserId);
        if(user == null){
            return;
        }
        this.remove(Wrappers.lambdaQuery(DynamicStar.class).eq(DynamicStar::getDynamicId, dynamicId)
                .eq(DynamicStar::getStarUserId, starUserId));
        DynamicStar dynamicStar = new DynamicStar();
        dynamicStar.setStarUserId(user.getId());
        dynamicStar.setStarUsercode(user.getUsercode());
        dynamicStar.setDynamicId(dynamicId);
        this.save(dynamicStar);
    }

    @Override
    public void unStar(Long dynamicId, Long starUserId) {
        this.remove(Wrappers.lambdaQuery(DynamicStar.class).eq(DynamicStar::getDynamicId, dynamicId)
                .eq(DynamicStar::getStarUserId, starUserId));
    }

    @Override
    public List<DynamicStar> isStarList(List<Long> dynamicId, Long userId){
        if(CollectionUtils.isEmpty(dynamicId)){
            return Collections.emptyList();
        }
        List<DynamicStar> list = this.list(Wrappers.lambdaUpdate(DynamicStar.class).eq(DynamicStar::getStarUserId, userId)
                .in(DynamicStar::getDynamicId, dynamicId));
        return list;
    }

    @Override
    public Page<MyStarDynamicListVO> myStarDynamic(PageQuery pageQuery, MyStarDynamicQuery dynamicQuery) {
        Page<MyStarDynamicListVO> result = baseMapper.myStarDynamic(pageQuery.build(), dynamicQuery);
        List<MyStarDynamicListVO> records = result.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return result;
        }
        List<Long> dynamicIds = records.stream().map(MyStarDynamicListVO::getDynamicId).collect(Collectors.toList());
        List<DynamicImage> list = dynamicImageService.list(Wrappers.lambdaQuery(DynamicImage.class).in(DynamicImage::getDynamicId, dynamicIds));
        Map<Long, List<DynamicImage>> map = list.stream().collect(Collectors.groupingBy(DynamicImage::getDynamicId));
        for (MyStarDynamicListVO record : records) {
            List<DynamicImage> images = map.get(record.getDynamicId());
            if(images == null){
                images = new ArrayList<>();
            }
            record.setImageList(images.stream().map(DynamicImage::getUrl).collect(Collectors.toList()));
        }
        return result;
    }

    @Override
    public void removeByDynamicId(Long dynamicId) {
        this.remove(Wrappers.lambdaQuery(DynamicStar.class).eq(DynamicStar::getDynamicId,dynamicId));
    }
}
