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.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.gbscommon.utils.Query;
import com.zcloud.zcGbsServicer.persistence.dataobject.FireAlarmMeetingUserDO;
import com.zcloud.zcGbsServicer.persistence.mapper.FireAlarmMeetingUserMapper;
import com.zcloud.zcGbsServicer.persistence.repository.FireAlarmMeetingUserRepository;
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 FireAlarmMeetingUserRepositoryImpl extends BaseRepositoryImpl<FireAlarmMeetingUserMapper, FireAlarmMeetingUserDO> implements FireAlarmMeetingUserRepository {

    @Override
    public PageResponse<FireAlarmMeetingUserDO> listPage(Map<String, Object> params) {
        IPage<FireAlarmMeetingUserDO> page = new Query<FireAlarmMeetingUserDO>().getPage(params);
        QueryWrapper<FireAlarmMeetingUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_enum", "FALSE");
        if (AuthContext.getTenantId() != null) {
            wrapper.eq("tenant_id", AuthContext.getTenantId());
        }
        if (params != null) {
            String localUserName = (String) params.get("localUserName");
            String fireBrigadeName = (String) params.get("fireBrigadeName");
            Integer userType = (Integer) params.get("userType");
            Integer authStatus = (Integer) params.get("authStatus");
            Integer syncStatus = (Integer) params.get("syncStatus");
            if (StrUtil.isNotBlank(localUserName)) {
                wrapper.like("local_user_name", localUserName.trim());
            }
            if (StrUtil.isNotBlank(fireBrigadeName)) {
                wrapper.like("fire_brigade_name", fireBrigadeName.trim());
            }
            if (userType != null) {
                wrapper.eq("user_type", userType);
            }
            if (authStatus != null) {
                wrapper.eq("auth_status", authStatus);
            }
            if (syncStatus != null) {
                wrapper.eq("sync_status", syncStatus);
            }
        }
        wrapper.orderByDesc("create_time");
        IPage<FireAlarmMeetingUserDO> result = this.page(page, wrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public boolean existsByLocalUserId(Long localUserId, Long tenantId, Long excludeId) {
        if (localUserId == null) {
            return false;
        }
        QueryWrapper<FireAlarmMeetingUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("local_user_id", localUserId).eq("delete_enum", "FALSE");
        if (tenantId != null) {
            wrapper.eq("tenant_id", tenantId);
        }
        else if (AuthContext.getTenantId() != null) {
            wrapper.eq("tenant_id", AuthContext.getTenantId());
        }
        if (excludeId != null) {
            wrapper.ne("id", excludeId);
        }
        return this.count(wrapper) > 0;
    }

    @Override
    public FireAlarmMeetingUserDO getByLocalUserId(Long localUserId, Long tenantId) {
        if (localUserId == null) {
            return null;
        }
        QueryWrapper<FireAlarmMeetingUserDO> wrapper = new QueryWrapper<>();
        wrapper.eq("local_user_id", localUserId).eq("delete_enum", "FALSE").last("limit 1");
        if (tenantId != null) {
            wrapper.eq("tenant_id", tenantId);
        }
        else if (AuthContext.getTenantId() != null) {
            wrapper.eq("tenant_id", AuthContext.getTenantId());
        }
        return this.getOne(wrapper);
    }

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

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