package com.sonic.shark.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sonic.shark.dao.FileUploadLogDao;
import com.sonic.shark.domain.bo.S3StsToken;
import com.sonic.shark.domain.entity.FileUploadLog;
import com.sonic.shark.service.FileUploadLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author code
 */
@Slf4j
@Service
public class FileUploadLogServiceImpl extends ServiceImpl<FileUploadLogDao, FileUploadLog> implements FileUploadLogService {

    @Override
    public void save(Long userId, S3StsToken s3StsToken, String ip, String endpoint) {
        FileUploadLog log = FileUploadLog.builder()
                .bucketName(s3StsToken.getBucket())
                .filePath(s3StsToken.getPath())
                .fullPath(s3StsToken.getUrlPath())
                .ipAddr(ip)
                .endpoint(endpoint)
                .bizConfirmStatus(false)
                .isDelete(false)
                .creatorId(userId)
                .createTime(LocalDateTime.now())
                .editorId(userId)
                .editTime(LocalDateTime.now())
                .build();
        save(log);
    }

    @Override
    public void confirm(Long userId, List<String> imageList) {
        if(CollectionUtils.isEmpty(imageList)) {
            return;
        }
        update(Wrappers.<FileUploadLog>lambdaUpdate()
                .set(FileUploadLog::getBizConfirmStatus, true)
                .set(userId != null, FileUploadLog::getEditorId, userId)
                .eq(FileUploadLog::getBizConfirmStatus, false)
                .in(FileUploadLog::getFullPath, imageList));
    }
}
