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

import cn.hutool.core.util.StrUtil;
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.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.zcloud.zcGbsServicer.persistence.dataobject.RescueTeamDO;
import com.zcloud.zcGbsServicer.persistence.mapper.RescueTeamMapper;
import com.zcloud.zcGbsServicer.persistence.repository.RescueTeamRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* web-infrastructure
* @Author makejava
* @Date 2026-01-21 17:23:00
*/
@Service
@RequiredArgsConstructor
public class RescueTeamRepositoryImpl extends BaseRepositoryImpl<RescueTeamMapper, RescueTeamDO> implements RescueTeamRepository {
    private final RescueTeamMapper rescueTeamMapper;

    @Override
    public PageResponse<RescueTeamDO> listPage(Map<String, Object> params) {
        IPage<RescueTeamDO> iPage = new Query<RescueTeamDO>().getPage(params);
        QueryWrapper<RescueTeamDO> queryWrapper = new QueryWrapper<>();
        Object corpId = params == null ? null : params.remove("eqCorpId");
        String menuPerms = params == null ? null : (String) params.remove("menuPerms");
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        if (corpId != null) {
            queryWrapper.eq("tenant_id", corpId);
        }
        IPage<RescueTeamDO> result = rescueTeamMapper.selectPageSql(iPage, queryWrapper, menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public PageResponse<RescueTeamDO> listScreen(IPage<RescueTeamDO> page, Map<String, Object> params) {
        IPage<RescueTeamDO> result = rescueTeamMapper.selectScreenList(page, params);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public boolean existsByTeamName(String teamName, Long excludeId) {
        if (StrUtil.isBlank(teamName)) {
            return false;
        }
        QueryWrapper<RescueTeamDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_enum", "FALSE");
        queryWrapper.eq("team_name", teamName.trim());
        if (excludeId != null) {
            queryWrapper.ne("id", excludeId);
        }
        return this.count(queryWrapper) > 0;
    }
}
