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.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jeesharp.business.modules.contentsecurity.entity.ContentSecurityTask;
import com.jeesharp.business.modules.contentsecurity.enums.ContentSecurityTaskStatusEnum;
import com.jeesharp.business.modules.contentsecurity.mapper.ContentSecurityTaskMapper;
import com.jeesharp.business.modules.contentsecurity.service.ContentSecurityTaskService;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.pojo.query.PaginationVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 内容安全异步任务领域服务实现。
 *
 * @author jeesharp
 * @since 2026-06-27
 */
@Service
@Transactional(readOnly = true)
public class ContentSecurityTaskServiceImpl
        extends CrudService<ContentSecurityTaskMapper, ContentSecurityTask, PaginationVO>
        implements ContentSecurityTaskService {

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = false)
    public void createPendingTask(ContentSecurityTask task) {
        if (task.getTaskStatus() == null) {
            task.setTaskStatus(ContentSecurityTaskStatusEnum.PENDING.getValue());
        }
        getBaseMapper().insert(task);
    }

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

    @Override
    public ContentSecurityTask getByTraceId(String traceId) {
        if (StrUtil.isBlank(traceId)) {
            return null;
        }
        LambdaQueryWrapper<ContentSecurityTask> w = new LambdaQueryWrapper<>();
        w.eq(ContentSecurityTask::getTraceId, traceId);
        return getBaseMapper().selectOne(w);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateTaskStatus(String traceId, Integer taskStatus, String resultLabel) {
        if (StrUtil.isBlank(traceId) || taskStatus == null) {
            return;
        }
        LambdaUpdateWrapper<ContentSecurityTask> uw = new LambdaUpdateWrapper<>();
        uw.eq(ContentSecurityTask::getTraceId, traceId)
                .set(ContentSecurityTask::getTaskStatus, taskStatus)
                .set(ContentSecurityTask::getCallbackDate, LocalDateTime.now());
        if (StrUtil.isNotBlank(resultLabel)) {
            uw.set(ContentSecurityTask::getRemarks, resultLabel);
        }
        getBaseMapper().update(null, uw);
    }
}
