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.FireRegionStateEnum;
import com.zcloud.fire.check.domain.enums.MenuEnum;
import com.zcloud.fire.check.domain.enums.UserTypeEnum;
import com.zcloud.fire.check.persistence.dataobject.DataStatisticDO;
import com.zcloud.fire.check.persistence.dataobject.FirePointDeviceCountDO;
import com.zcloud.fire.check.persistence.dataobject.FirePointDO;
import com.zcloud.fire.check.persistence.dataobject.FirePointStatisticDO;
import com.zcloud.fire.check.persistence.dataobject.FireRegionDO;
import com.zcloud.fire.check.persistence.mapper.FirePointMapper;
import com.zcloud.fire.check.persistence.repository.CorpInfoRepository;
import com.zcloud.fire.check.persistence.repository.FirePointRepository;
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:55
 */
@Service
@RequiredArgsConstructor
public class FirePointRepositoryImpl extends BaseRepositoryImpl<FirePointMapper, FirePointDO> implements FirePointRepository {
    private final FirePointMapper firePointMapper;
    private final CorpInfoRepository corpInfoRepository;

    @Override
    public PageResponse<FirePointDO> listPage(Map<String, Object> params) {
        IPage<FirePointDO> iPage = new Query<FirePointDO>().getPage(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());
//            }
        }
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<FirePointDO> result = firePointMapper.getFirePointPage(iPage, params,menuPerms);


        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public Map<String, Long> countByFireRegionIdIn(List<String> fireRegionIds) {
        List<FirePointStatisticDO> firePointStatisticDOList = firePointMapper.countByFireRegionIdIn(fireRegionIds);
        return firePointStatisticDOList.stream().collect(Collectors.toMap(FirePointStatisticDO::getFireRegionId, FirePointStatisticDO::getFirePointCount));
    }

    @Override
    public long countByFireRegionId(String fireRegionId) {
        QueryWrapper<FirePointDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_region_id", fireRegionId);
        queryWrapper.eq("delete_enum", "FALSE");
        return firePointMapper.selectCount(queryWrapper);
    }

    @Override
    public void editAllDepartment(List<Long> ids, Long departmentId) {
        QueryWrapper<FirePointDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        queryWrapper.eq("delete_enum", "FALSE");
        FirePointDO firePointDO = new FirePointDO();
        firePointDO.setDepartmentId(departmentId);
        firePointMapper.update(firePointDO, queryWrapper);
    }

    @Override
    public List<FirePointDO> listByCorpinfoId(Long corpinfoId) {
        QueryWrapper<FirePointDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("corpinfo_id", corpinfoId);
        return firePointMapper.selectList(queryWrapper);
    }

    @Override
    public List<FirePointDO> listByFireRegionId(String fireRegionId) {
        QueryWrapper<FirePointDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_region_id", fireRegionId);
        queryWrapper.eq("corpinfo_id", AuthContext.getTenantId());
        return firePointMapper.selectList(queryWrapper);
    }

    @Override
    public boolean existsByFirePointCode(String firePointCode) {
        QueryWrapper<FirePointDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_point_code", firePointCode);
        return firePointMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public FirePointDO getInfoByCode(String firePointCode) {
        QueryWrapper<FirePointDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_point_code", firePointCode);
        return firePointMapper.selectOne(queryWrapper);
    }

    @Override
    public FirePointDO getInfoById(Long id) {
        return firePointMapper.getInfoById(id);
    }

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

        return count;
    }

    @Override
    public FirePointDO getInfoByUUId(String firePointId) {
        return firePointMapper.getInfoByUUId(firePointId);
    }

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

    @Override
    public PageResponse<FirePointDeviceCountDO> listDevice(Map<String, Object> params) {
        IPage<FirePointDeviceCountDO> iPage = new Query<FirePointDeviceCountDO>().getPage(params);
        IPage<FirePointDeviceCountDO> result = firePointMapper.listDevice(iPage, params);
        return PageHelper.pageToResponse(result, result.getRecords());
    }
}

