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.FireAlarmInfoDO;
import com.zcloud.zcGbsServicer.persistence.mapper.FireAlarmInfoMapper;
import com.zcloud.zcGbsServicer.persistence.repository.FireAlarmInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class FireAlarmInfoRepositoryImpl extends BaseRepositoryImpl<FireAlarmInfoMapper, FireAlarmInfoDO> implements FireAlarmInfoRepository {

    private final FireAlarmInfoMapper fireAlarmInfoMapper;

    @Override
    public PageResponse<FireAlarmInfoDO> listPage(Map<String, Object> params) {
        IPage<FireAlarmInfoDO> page = new Query<FireAlarmInfoDO>().getPage(params);
        String fireBrigadeName = params == null ? null : (String) params.get("fireBrigadeName");
        Integer alarmStatus = params == null ? null : (Integer) params.get("alarmStatus");
        List<String> fireBrigadeIds = params == null ? null : (List<String>) params.get("fireBrigadeIds");
        Boolean feedbackOnly = params == null ? null : (Boolean) params.get("feedbackOnly");
        IPage<FireAlarmInfoDO> result = fireAlarmInfoMapper.selectPageSql(
                page,
                AuthContext.getTenantId(),
                fireBrigadeName,
                alarmStatus,
                fireBrigadeIds,
                feedbackOnly
        );
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public FireAlarmInfoDO getByFireAlarmInfoId(String fireAlarmInfoId) {
        if (fireAlarmInfoId == null) {
            return null;
        }
        return this.getOne(new LambdaQueryWrapper<FireAlarmInfoDO>()
                .eq(FireAlarmInfoDO::getFireAlarmInfoId, fireAlarmInfoId)
                .eq(FireAlarmInfoDO::getTenantId, AuthContext.getTenantId())
                .eq(FireAlarmInfoDO::getDeleteEnum, "FALSE")
                .last("limit 1"));
    }
}
