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.LambdaQueryWrapper;
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.gbscommon.utils.Query;
import com.zcloud.zcGbsServicer.persistence.dataobject.FireBrigadeDO;
import com.zcloud.zcGbsServicer.persistence.mapper.FireBrigadeMapper;
import com.zcloud.zcGbsServicer.persistence.repository.FireBrigadeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class FireBrigadeRepositoryImpl extends BaseRepositoryImpl<FireBrigadeMapper, FireBrigadeDO> implements FireBrigadeRepository {

    private final FireBrigadeMapper fireBrigadeMapper;

    @Override
    public PageResponse<FireBrigadeDO> listPage(Map<String, Object> params) {
        IPage<FireBrigadeDO> page = new Query<FireBrigadeDO>().getPage(params);
        String brigadeName = params == null ? null : (String) params.get("brigadeName");
        IPage<FireBrigadeDO> result = fireBrigadeMapper.selectPageSql(page, brigadeName, AuthContext.getTenantId());
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public List<FireBrigadeDO> listByFireBrigadeIds(Collection<String> fireBrigadeIds) {
        if (fireBrigadeIds == null || fireBrigadeIds.isEmpty()) {
            return Collections.emptyList();
        }
        return this.list(new LambdaQueryWrapper<FireBrigadeDO>()
                .in(FireBrigadeDO::getFireBrigadeId, fireBrigadeIds)
                .eq(FireBrigadeDO::getTenantId, AuthContext.getTenantId())
                .eq(FireBrigadeDO::getDeleteEnum, "FALSE"));
    }

    @Override
    public boolean existsByBrigadeName(String brigadeName, Long excludeId) {
        if (StrUtil.isBlank(brigadeName)) {
            return false;
        }
        LambdaQueryWrapper<FireBrigadeDO> wrapper = new LambdaQueryWrapper<FireBrigadeDO>()
                .eq(FireBrigadeDO::getBrigadeName, brigadeName.trim())
                .eq(FireBrigadeDO::getTenantId, AuthContext.getTenantId())
                .eq(FireBrigadeDO::getDeleteEnum, "FALSE");
        if (excludeId != null) {
            wrapper.ne(FireBrigadeDO::getId, excludeId);
        }
        return this.count(wrapper) > 0;
    }

    @Override
    public FireBrigadeDO getByFireBrigadeId(String fireBrigadeId) {
        return this.getOne(new LambdaQueryWrapper<FireBrigadeDO>()
                .eq(FireBrigadeDO::getFireBrigadeId, fireBrigadeId)
                .eq(FireBrigadeDO::getTenantId, AuthContext.getTenantId())
                .eq(FireBrigadeDO::getDeleteEnum, "FALSE")
                .last("limit 1"));
    }
}
