package com.njcn.rdms.module.project.dal.mysql.project.execution;

import com.njcn.rdms.framework.mybatis.core.mapper.BaseMapperX;
import com.njcn.rdms.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.njcn.rdms.module.project.dal.dataobject.project.execution.ExecutionAssigneeDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ExecutionAssigneeMapper extends BaseMapperX<ExecutionAssigneeDO> {

    default List<ExecutionAssigneeDO> selectListByExecutionId(Long executionId) {
        return selectList(new LambdaQueryWrapperX<ExecutionAssigneeDO>()
                .eq(ExecutionAssigneeDO::getExecutionId, executionId)
                .orderByAsc(ExecutionAssigneeDO::getRemovedAt)
                .orderByAsc(ExecutionAssigneeDO::getJoinedAt)
                .orderByAsc(ExecutionAssigneeDO::getId));
    }

    /**
     * 仅返当前活跃协办人（removed_at IS NULL）。B 模型下同 userId 至多一段未失效。
     */
    default List<ExecutionAssigneeDO> selectActiveListByExecutionId(Long executionId) {
        return selectList(new LambdaQueryWrapperX<ExecutionAssigneeDO>()
                .eq(ExecutionAssigneeDO::getExecutionId, executionId)
                .isNull(ExecutionAssigneeDO::getRemovedAt)
                .orderByAsc(ExecutionAssigneeDO::getJoinedAt)
                .orderByAsc(ExecutionAssigneeDO::getId));
    }

    default ExecutionAssigneeDO selectByExecutionIdAndUserId(Long executionId, Long userId) {
        return selectOne(new LambdaQueryWrapperX<ExecutionAssigneeDO>()
                .eq(ExecutionAssigneeDO::getExecutionId, executionId)
                .eq(ExecutionAssigneeDO::getUserId, userId));
    }

    default ExecutionAssigneeDO selectByIdAndExecutionId(Long id, Long executionId) {
        return selectOne(new LambdaQueryWrapperX<ExecutionAssigneeDO>()
                .eq(ExecutionAssigneeDO::getId, id)
                .eq(ExecutionAssigneeDO::getExecutionId, executionId));
    }

    default ExecutionAssigneeDO selectActiveByExecutionIdAndUserId(Long executionId, Long userId) {
        return selectOne(new LambdaQueryWrapperX<ExecutionAssigneeDO>()
                .eq(ExecutionAssigneeDO::getExecutionId, executionId)
                .eq(ExecutionAssigneeDO::getUserId, userId)
                .isNull(ExecutionAssigneeDO::getRemovedAt));
    }

    /**
     * 查 userId 当前在指定项目下，活跃协办的所有执行 ID（removed_at IS NULL）。
     * 走 JOIN 是因为 execution_assignee 表没有 project_id 冗余字段。
     * 用于 VisibilityScopeResolver 收集"我是执行协办人"的 scope 来源。
     */
    @Select("""
            SELECT a.execution_id
              FROM rdms_execution_assignee a
              JOIN rdms_project_execution e ON e.id = a.execution_id AND e.deleted = b'0'
             WHERE a.deleted = b'0'
               AND a.removed_at IS NULL
               AND e.project_id = #{projectId}
               AND a.user_id    = #{userId}
            """)
    List<Long> selectActiveExecutionIdsByProjectIdAndUserId(@Param("projectId") Long projectId,
                                                            @Param("userId") Long userId);

}
