package com.sonic.shark.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sonic.common.utils.LogUtils;
import com.sonic.shark.dao.NsfwLogDao;
import com.sonic.shark.domain.bo.NsfwRecognitionResult;
import com.sonic.shark.domain.entity.NsfwLog;
import com.sonic.shark.enums.S3BucketNameEnum;
import com.sonic.shark.service.NsfwLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 图鉴鉴黄日志表
 */
@Slf4j
@Service
public class NsfwLogServiceImpl extends ServiceImpl<NsfwLogDao, NsfwLog> implements NsfwLogService {


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Long userId, S3BucketNameEnum s3BucketNameEnum, String fileFullPath, NsfwRecognitionResult nsfwRecognitionResult) {
        NsfwLog log = new NsfwLog();
        log.setUserId(userId);
        log.setBucketName(s3BucketNameEnum.getBucketName());
        log.setNsfwLabel(CollectionUtils.isEmpty(nsfwRecognitionResult.getLabelList()) ? null : JSONObject.toJSONString(nsfwRecognitionResult.getLabelList()));
        log.setUrl(s3BucketNameEnum.getRootUrl() + fileFullPath);
        log.setTraceId(LogUtils.getTraceId());
        log.setIsDelete(false);
        log.setCreateTime(LocalDateTime.now());
        save(log);
    }

}
