package com.zcloud.domain.persistence.repository.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.domain.domain.enums.MenuEnum;
import com.zcloud.domain.persistence.dataobject.DomainDepartmentDO;
import com.zcloud.domain.persistence.mapper.DomainDepartmentMapper;
import com.zcloud.domain.persistence.repository.DomainDepartmentRepository;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2026-01-27 11:28:52
 */
@Service
@RequiredArgsConstructor
public class DomainDepartmentRepositoryImpl extends BaseRepositoryImpl<DomainDepartmentMapper, DomainDepartmentDO> implements DomainDepartmentRepository {
    private final DomainDepartmentMapper domainDepartmentMapper;

    @Override
    public PageResponse<DomainDepartmentDO> listPage(Map<String, Object> params) {
        IPage<DomainDepartmentDO> iPage = new Query<DomainDepartmentDO>().getPage(params);
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<DomainDepartmentDO> result = domainDepartmentMapper.listPage(iPage, params,menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public void checkDomainType(String domainType, Long id) {
        QueryWrapper<DomainDepartmentDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("domain_type", domainType);
        if(id != null){
            queryWrapper.ne("id", id);
        }
        List<DomainDepartmentDO> list = list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            throw new BizException("该领域类型已存在");
        }
    }

    @Override
    public DomainDepartmentDO getInfoById(Long id) {
        return domainDepartmentMapper.getInfoById(id);
    }

    @Override
    public List<DomainDepartmentDO> listAll(Map<String, Object> params) {
        return domainDepartmentMapper.listAll(params);
    }

    @Override
    public DomainDepartmentDO getInfoByUUID(String domainDepartmentId) {
        DomainDepartmentDO domainDepartmentDO = domainDepartmentMapper.getInfoByUUID(domainDepartmentId);
        return domainDepartmentDO;
    }
}

