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

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.FireBrigadeScreenMemberRow;
import com.zcloud.zcGbsServicer.persistence.dataobject.FireBrigadeUserDO;
import com.zcloud.zcGbsServicer.persistence.mapper.FireBrigadeUserMapper;
import com.zcloud.zcGbsServicer.persistence.repository.FireBrigadeUserRepository;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class FireBrigadeUserRepositoryImpl extends BaseRepositoryImpl<FireBrigadeUserMapper, FireBrigadeUserDO> implements FireBrigadeUserRepository {

    @Override
    public List<FireBrigadeUserDO> listAll() {
        return this.list(new LambdaQueryWrapper<FireBrigadeUserDO>()
                .eq(FireBrigadeUserDO::getTenantId, AuthContext.getTenantId())
                .eq(FireBrigadeUserDO::getDeleteEnum, "FALSE"));
    }

    @Override
    public List<FireBrigadeUserDO> listByFireBrigadeIds(Collection<String> fireBrigadeIds) {
        return listByFireBrigadeIds(fireBrigadeIds, AuthContext.getTenantId());
    }

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

    @Override
    public Map<String, Integer> countByFireBrigadeIds(Collection<String> fireBrigadeIds) {
        if (fireBrigadeIds == null || fireBrigadeIds.isEmpty()) {
            return Collections.emptyMap();
        }
        return listByFireBrigadeIds(fireBrigadeIds).stream()
                .filter(item -> item != null && StrUtil.isNotBlank(item.getFireBrigadeId()))
                .collect(Collectors.groupingBy(FireBrigadeUserDO::getFireBrigadeId,
                        Collectors.collectingAndThen(Collectors.counting(), Long::intValue)));
    }

    @Override
    public List<FireBrigadeUserDO> listByFireBrigadeId(String fireBrigadeId) {
        return this.list(new LambdaQueryWrapper<FireBrigadeUserDO>()
                .eq(FireBrigadeUserDO::getFireBrigadeId, fireBrigadeId)
                .eq(FireBrigadeUserDO::getTenantId, AuthContext.getTenantId())
                .eq(FireBrigadeUserDO::getDeleteEnum, "FALSE"));
    }

    @Override
    public List<FireBrigadeUserDO> listByUserId(Long userId) {
        return this.list(new LambdaQueryWrapper<FireBrigadeUserDO>()
                .eq(FireBrigadeUserDO::getUserId, userId)
                .eq(FireBrigadeUserDO::getTenantId, AuthContext.getTenantId())
                .eq(FireBrigadeUserDO::getDeleteEnum, "FALSE"));
    }

    @Override
    public boolean existsRelation(String fireBrigadeId, Long userId, Long excludeId) {
        LambdaQueryWrapper<FireBrigadeUserDO> wrapper = new LambdaQueryWrapper<FireBrigadeUserDO>()
                .eq(FireBrigadeUserDO::getFireBrigadeId, fireBrigadeId)
                .eq(FireBrigadeUserDO::getUserId, userId)
                .eq(FireBrigadeUserDO::getTenantId, AuthContext.getTenantId())
                .eq(FireBrigadeUserDO::getDeleteEnum, "FALSE");
        if (excludeId != null) {
            wrapper.ne(FireBrigadeUserDO::getId, excludeId);
        }
        return this.count(wrapper) > 0;
    }

    @Override
    public PageResponse<FireBrigadeScreenMemberRow> listScreenMembers(Long corpinfoId, Integer portArea, int pageIndex, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", pageIndex <= 0 ? 1 : pageIndex);
        params.put("pageSize", pageSize <= 0 ? 10 : pageSize);
        IPage<FireBrigadeScreenMemberRow> page = new Query<FireBrigadeScreenMemberRow>().getPage(params);
        IPage<FireBrigadeScreenMemberRow> result = this.baseMapper.listScreenMembers(page, corpinfoId, portArea);
        return PageHelper.pageToResponse(result, result.getRecords());
    }
}
