package com.zcloud.fire.check.persistence.repository.impl;

import com.alibaba.cola.dto.PageResponse;
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.fire.check.domain.enums.CorpTypeEnum;
import com.zcloud.fire.check.domain.enums.MenuEnum;
import com.zcloud.fire.check.domain.enums.UserTypeEnum;
import com.zcloud.fire.check.persistence.dataobject.*;
import com.zcloud.fire.check.persistence.mapper.FireDeviceMapper;
import com.zcloud.fire.check.persistence.repository.CorpInfoRepository;
import com.zcloud.fire.check.persistence.repository.FireDeviceRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:12:34
 */
@Service
@RequiredArgsConstructor
public class FireDeviceRepositoryImpl extends BaseRepositoryImpl<FireDeviceMapper, FireDeviceDO> implements FireDeviceRepository {
    private final FireDeviceMapper fireDeviceMapper;
    private final CorpInfoRepository corpInfoRepository;

    @Override
    public PageResponse<FireDeviceDO> listPage(Map<String, Object> params) {
        IPage<FireDeviceDO> iPage = new Query<FireDeviceDO>().getPage(params);
        if (params.get("corpinfoId") != null && StringUtils.isNotEmpty(params.get("corpinfoId").toString())) {
            params.put("corpinfoId", params.get("corpinfoId"));
        } else {
            params.put("corpinfoId", AuthContext.getTenantId());
        }
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<FireDeviceDO> result = fireDeviceMapper.getFireDevicePage(iPage, params,menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public long countByFirePointId(String firePointId) {
        QueryWrapper<FireDeviceDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_point_id", firePointId);
        return fireDeviceMapper.selectCount(queryWrapper);
    }

    @Override
    public Map<String, Long> countByFirePointIdIn(List<String> firePointIds) {
        List<FireDeviceStatisticDO> fireDeviceStatisticDOList = fireDeviceMapper.countByFireRegionIdIn(firePointIds);
        return fireDeviceStatisticDOList.stream().collect(Collectors.toMap(FireDeviceStatisticDO::getFirePointId, FireDeviceStatisticDO::getFireDeviceCount));

    }

    @Override
    public boolean existsByDeviceCode(String fireDeviceCode) {
        QueryWrapper<FireDeviceDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_device_code", fireDeviceCode);
        return fireDeviceMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public List<FireDeviceDO> getDeviceList() {
        QueryWrapper<FireDeviceDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", AuthContext.getTenantId());
        return fireDeviceMapper.selectList(queryWrapper);
    }

    @Override
    public boolean addBatch(List<FireDeviceDO> fireDeviceDOS) {
        saveBatch(fireDeviceDOS);
        return true;
    }

    @Override
    public List<FireDeviceDO> getAllList(Map<String, Object> params) {
        if (params.get("corpinfoId") != null && StringUtils.isNotEmpty(params.get("corpinfoId").toString())) {
            params.put("corpinfoId", params.get("corpinfoId"));
        } else {
            if (!corpInfoRepository.isSupper()) {
                params.put("corpinfoId", AuthContext.getTenantId());
            }
        }
        List<FireDeviceDO> result = fireDeviceMapper.getAllList(params);
        return result;
    }

    @Override
    public FireDeviceDO getInfoById(Long id) {
        return fireDeviceMapper.getInfoById(id);
    }

    @Override
    public Long getFireDeviceCount() {
        Map<String, Object> params = new HashMap<>();
        params.put("corpTypeList", CorpTypeEnum.getCodesByEnterpriseType(UserTypeEnum.QY.getCode()));
        Long count = fireDeviceMapper.getFireDeviceCount(params);

        return count;
    }

    @Override
    public List<FireDeviceDO> getDeviceListByFirePointId(String firePointId) {
        return fireDeviceMapper.getDeviceListByFirePointId(firePointId);
    }

    @Override
    public List<DataStatisticDO> countByCorpinfoIds(List<Long> corpinfoIds) {
        return fireDeviceMapper.countByCorpinfoIds(corpinfoIds);
    }

    @Override
    public PageResponse<FireDeviceDO> pageByPointId(Map<String, Object> params) {
        IPage<FireDeviceDO> iPage = new Query<FireDeviceDO>().getPage(params);

        IPage<FireDeviceDO> result = fireDeviceMapper.pageByPointId(iPage, params);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public FireDeviceDO getInfoByCode(String fireDeviceCode) {
        QueryWrapper<FireDeviceDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_device_code", fireDeviceCode);
        return fireDeviceMapper.selectOne(queryWrapper);
    }

    @Override
    public List<FireDeviceDO> getListByTime(String tomorrowTime) {
        QueryWrapper<FireDeviceDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("validity_end_time", tomorrowTime);
        return fireDeviceMapper.selectList(queryWrapper);
    }
}

