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

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.member.UserObjectRoleDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collections;
import java.util.List;

@Mapper
public interface UserObjectRoleMapper extends BaseMapperX<UserObjectRoleDO> {

    default List<UserObjectRoleDO> selectListByObject(String objectType, Long objectId) {
        return selectList(new LambdaQueryWrapperX<UserObjectRoleDO>()
                .eq(UserObjectRoleDO::getObjectType, objectType)
                .eq(UserObjectRoleDO::getObjectId, objectId)
                .orderByAsc(UserObjectRoleDO::getStatus)
                .orderByAsc(UserObjectRoleDO::getJoinedTime)
                .orderByAsc(UserObjectRoleDO::getId));
    }

    default UserObjectRoleDO selectByObjectAndUserId(String objectType, Long objectId, Long userId) {
        return selectOne(new LambdaQueryWrapperX<UserObjectRoleDO>()
                .eq(UserObjectRoleDO::getObjectType, objectType)
                .eq(UserObjectRoleDO::getObjectId, objectId)
                .eq(UserObjectRoleDO::getUserId, userId));
    }

    default UserObjectRoleDO selectByIdAndObject(Long id, String objectType, Long objectId) {
        return selectOne(new LambdaQueryWrapperX<UserObjectRoleDO>()
                .eq(UserObjectRoleDO::getId, id)
                .eq(UserObjectRoleDO::getObjectType, objectType)
                .eq(UserObjectRoleDO::getObjectId, objectId));
    }

    default UserObjectRoleDO selectActiveByObjectAndUserId(String objectType, Long objectId, Long userId) {
        return selectOne(new LambdaQueryWrapperX<UserObjectRoleDO>()
                .eq(UserObjectRoleDO::getObjectType, objectType)
                .eq(UserObjectRoleDO::getObjectId, objectId)
                .eq(UserObjectRoleDO::getUserId, userId)
                .eq(UserObjectRoleDO::getStatus, 0));
    }

    default List<UserObjectRoleDO> selectListByIdsAndObject(List<Long> ids, String objectType, Long objectId) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
        return selectList(new LambdaQueryWrapperX<UserObjectRoleDO>()
                .in(UserObjectRoleDO::getId, ids)
                .eq(UserObjectRoleDO::getObjectType, objectType)
                .eq(UserObjectRoleDO::getObjectId, objectId));
    }

}
