package com.ylx.order.service.impl;

import cn.hutool.core.collection.CollectionUtil;
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.ylx.order.domain.TCommentPicture;
import com.ylx.order.mapper.TCommentPictureMapper;
import com.ylx.order.service.TCommentPictureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 类描述：
 *
 * @author Administrator
 * @version 1.0
 * @date 2026/6/9 16:56
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TCommentPictureServiceImpl extends ServiceImpl<TCommentPictureMapper, TCommentPicture> implements TCommentPictureService {

    private final TCommentPictureMapper pictureMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSavePictures(String commentId, List<String> pictureUrls) {
        if (commentId == null || CollectionUtil.isEmpty(pictureUrls)) {
            return;
        }
        List<TCommentPicture> pictureList = new ArrayList<>();
        for (int i = 0; i < pictureUrls.size(); i++) {
            TCommentPicture picture = new TCommentPicture();
            picture.setCommentId(commentId);
            picture.setPictureUrl(pictureUrls.get(i));
            picture.setSortOrder(i);
            picture.setCreateTime(new Date());
            pictureList.add(picture);
        }
        this.saveBatch(pictureList);
        log.info("批量保存评论图片成功，commentId: {}，图片数量: {}", commentId, pictureUrls.size());
    }

    @Override
    public List<TCommentPicture> getByCommentId(String commentId) {
        LambdaQueryWrapper<TCommentPicture> wrapper = Wrappers.lambdaQuery(TCommentPicture.class)
                .eq(TCommentPicture::getCommentId, commentId)
                .orderByAsc(TCommentPicture::getSortOrder);
        return this.list(wrapper);
    }

    @Override
    public List<TCommentPicture> selectByCommentIds(List<String> commentIds) {
        LambdaQueryWrapper<TCommentPicture> wrapper = Wrappers.lambdaQuery(TCommentPicture.class)
                .in(TCommentPicture::getCommentId, commentIds)
                .orderByAsc(TCommentPicture::getSortOrder);
        return this.list(wrapper);
    }
}