package com.qqchen.deploy.backend.system.service.impl;

import com.qqchen.deploy.backend.system.entity.RoleTag;
import com.qqchen.deploy.backend.framework.enums.ResponseCode;
import com.qqchen.deploy.backend.framework.exception.BusinessException;
import com.qqchen.deploy.backend.framework.exception.UniqueConstraintException;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import com.qqchen.deploy.backend.system.model.RoleTagDTO;
import com.qqchen.deploy.backend.system.model.query.RoleTagQuery;
import com.qqchen.deploy.backend.system.repository.IRoleTagRepository;
import com.qqchen.deploy.backend.system.service.IRoleTagService;
import com.qqchen.deploy.backend.framework.annotation.ServiceType;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import static com.qqchen.deploy.backend.framework.annotation.ServiceType.Type.DATABASE;

@Slf4j
@Service
@ServiceType(DATABASE)
public class RoleTagServiceImpl extends BaseServiceImpl<RoleTag, RoleTagDTO, RoleTagQuery, Long> implements IRoleTagService {

    @Resource
    private IRoleTagRepository roleTagRepository;




    @Override
    protected void validateUniqueConstraints(RoleTagDTO dto) {
        // 检查标签名称唯一性
        if (roleTagRepository.existsByNameAndDeletedFalse(dto.getName())) {
            throw new UniqueConstraintException(ResponseCode.ROLE_TAG_NAME_EXISTS, "name", dto.getName());
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        RoleTag tag = findEntityById(id);
        // 检查标签是否正在使用
        if (!tag.getRoles().isEmpty()) {
            throw new BusinessException(ResponseCode.ROLE_TAG_IN_USE);
        }
        super.delete(id);
    }
} 