package com.jeesharp.business.modules.contentsecurity.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jeesharp.business.modules.contentsecurity.entity.ContentSecurityLog;
import com.jeesharp.business.modules.contentsecurity.mapper.ContentSecurityLogMapper;
import com.jeesharp.business.modules.contentsecurity.service.ContentSecurityLogService;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.business.modules.contentsecurity.vo.ContentSecurityLogQueryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 内容安全检测流水领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-27
 */
@Service
@Transactional(readOnly = true)
public class ContentSecurityLogServiceImpl
        extends CrudService<ContentSecurityLogMapper, ContentSecurityLog, ContentSecurityLogQueryVO>
        implements ContentSecurityLogService {

    /**
     * 独立事务写入流水：评价/反馈提交主事务因 REJECT 回滚时，检测记录仍保留便于审核举证。
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
    public void appendLog(ContentSecurityLog log) {
        getBaseMapper().insert(log);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateBizIdByIds(String bizModule, String bizId, List<String> logIds) {
        if (StrUtil.isBlank(bizModule) || StrUtil.isBlank(bizId) || CollUtil.isEmpty(logIds)) {
            return;
        }
        LambdaUpdateWrapper<ContentSecurityLog> uw = new LambdaUpdateWrapper<>();
        uw.eq(ContentSecurityLog::getBizModule, bizModule)
                .in(ContentSecurityLog::getId, logIds)
                .set(ContentSecurityLog::getBizId, bizId);
        getBaseMapper().update(null, uw);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateBizIdByTraceIds(String bizModule, String bizId, List<String> traceIds) {
        if (StrUtil.isBlank(bizModule) || StrUtil.isBlank(bizId) || CollUtil.isEmpty(traceIds)) {
            return;
        }
        LambdaUpdateWrapper<ContentSecurityLog> uw = new LambdaUpdateWrapper<>();
        uw.eq(ContentSecurityLog::getBizModule, bizModule)
                .in(ContentSecurityLog::getTraceId, traceIds)
                .set(ContentSecurityLog::getBizId, bizId);
        getBaseMapper().update(null, uw);
    }
}
