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

import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.auth.utils.AuthContext;
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.DomainGroupDO;
import com.zcloud.domain.persistence.mapper.DomainGroupMapper;
import com.zcloud.domain.persistence.repository.DomainGroupRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2026-01-27 11:29:27
 */
@Service
@RequiredArgsConstructor
public class DomainGroupRepositoryImpl extends BaseRepositoryImpl<DomainGroupMapper, DomainGroupDO> implements DomainGroupRepository {
    private final DomainGroupMapper domainGroupMapper;
    private final CorpInfoRepositoryImpl corpInfoRepository;

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

    @Override
    public DomainGroupDO getInfoById(Long id) {
        return domainGroupMapper.getInfoById(id);
    }

    @Override
    public List<DomainGroupDO> listAll(Map<String, Object> params) {
        if(!corpInfoRepository.isSupper()){
            params.put("corpinfoId", AuthContext.getTenantId());
        }
        return domainGroupMapper.listAll( params);
    }

    @Override
    public List<DomainGroupDO> getListByCorpinfoId(Long corpinfoId) {
        QueryWrapper<DomainGroupDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", corpinfoId);
        return domainGroupMapper.selectList(queryWrapper);
    }

    @Override
    public DomainGroupDO getInfoByGroupId(String domainGroupId) {
        return domainGroupMapper.getInfoByGroupId(domainGroupId);
    }
}

