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 java.util.Collection;
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));
    }

    /**
     * 按 execution_id 批量软删执行协办（含已 removed 的历史段）。
     */
    default int deleteByExecutionId(Long executionId) {
        if (executionId == null) {
            return 0;
        }
        return delete(new LambdaQueryWrapperX<ExecutionAssigneeDO>()
                .eq(ExecutionAssigneeDO::getExecutionId, executionId));
    }

    /** 批量查多个执行的生效中协办人（removed_at 为空），告警接收人组装用；调用方保证 executionIds 非空 */
    default List<ExecutionAssigneeDO> selectActiveListByExecutionIds(Collection<Long> executionIds) {
        return selectList(new LambdaQueryWrapperX<ExecutionAssigneeDO>()
                .in(ExecutionAssigneeDO::getExecutionId, executionIds)
                .isNull(ExecutionAssigneeDO::getRemovedAt));
    }

}
