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.FireAlarmMeetingRoomDO;
import com.zcloud.zcGbsServicer.persistence.mapper.FireAlarmMeetingRoomMapper;
import com.zcloud.zcGbsServicer.persistence.repository.FireAlarmMeetingRoomRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@RequiredArgsConstructor
public class FireAlarmMeetingRoomRepositoryImpl extends BaseRepositoryImpl<FireAlarmMeetingRoomMapper, FireAlarmMeetingRoomDO> implements FireAlarmMeetingRoomRepository {

    @Override
    public PageResponse<FireAlarmMeetingRoomDO> listPage(Map<String, Object> params) {
        IPage<FireAlarmMeetingRoomDO> page = new Query<FireAlarmMeetingRoomDO>().getPage(params);
        QueryWrapper<FireAlarmMeetingRoomDO> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_enum", "FALSE");
        if (AuthContext.getTenantId() != null) {
            wrapper.eq("tenant_id", AuthContext.getTenantId());
        }
        if (params != null) {
            String roomCode = (String) params.get("roomCode");
            String roomName = (String) params.get("roomName");
            Integer roomStatus = (Integer) params.get("roomStatus");
            if (StrUtil.isNotBlank(roomCode)) {
                wrapper.like("room_code", roomCode.trim());
            }
            if (StrUtil.isNotBlank(roomName)) {
                wrapper.like("room_name", roomName.trim());
            }
            if (roomStatus != null) {
                wrapper.eq("room_status", roomStatus);
            }
        }
        wrapper.orderByDesc("create_time");
        IPage<FireAlarmMeetingRoomDO> result = this.page(page, wrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public boolean existsByRoomCode(String roomCode, Long excludeId) {
        if (StrUtil.isBlank(roomCode)) {
            return false;
        }
        QueryWrapper<FireAlarmMeetingRoomDO> wrapper = new QueryWrapper<>();
        wrapper.eq("room_code", roomCode.trim()).eq("delete_enum", "FALSE");
        if (AuthContext.getTenantId() != null) {
            wrapper.eq("tenant_id", AuthContext.getTenantId());
        }
        if (excludeId != null) {
            wrapper.ne("id", excludeId);
        }
        return this.count(wrapper) > 0;
    }

    @Override
    public FireAlarmMeetingRoomDO getByRoomCode(String roomCode) {
        if (StrUtil.isBlank(roomCode)) {
            return null;
        }
        QueryWrapper<FireAlarmMeetingRoomDO> wrapper = new QueryWrapper<>();
        wrapper.eq("room_code", roomCode.trim()).eq("delete_enum", "FALSE").last("limit 1");
        if (AuthContext.getTenantId() != null) {
            wrapper.eq("tenant_id", AuthContext.getTenantId());
        }
        return this.getOne(wrapper);
    }

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