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.*;
import com.zcloud.fire.check.domain.model.CorpInfoE;
import com.zcloud.fire.check.persistence.dataobject.DataStatisticDO;
import com.zcloud.fire.check.persistence.dataobject.FireRegionDO;
import com.zcloud.fire.check.persistence.mapper.FireRegionMapper;
import com.zcloud.fire.check.persistence.repository.CorpInfoRepository;
import com.zcloud.fire.check.persistence.repository.FireRegionRepository;
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;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:13:10
 */
@Service
@RequiredArgsConstructor
public class FireRegionRepositoryImpl extends BaseRepositoryImpl<FireRegionMapper, FireRegionDO> implements FireRegionRepository {
    private final FireRegionMapper fireRegionMapper;
    private final CorpInfoRepository corpInfoRepository;

    @Override
    public PageResponse<FireRegionDO> listPage(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());
//            }
        }
        IPage<FireRegionDO> iPage = new Query<FireRegionDO>().getPage(params);
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<FireRegionDO> result =fireRegionMapper.getFireRegionPage(iPage,params,menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }
    @Override
    public void editAllDepartment(List<Long> ids, Long departmentId) {
        FireRegionDO fireRegionDO = new FireRegionDO();
        fireRegionDO.setDepartmentId(departmentId);
        fireRegionMapper.update(fireRegionDO, new QueryWrapper<FireRegionDO>().lambda().in(FireRegionDO::getId, ids).eq(FireRegionDO::getDeleteEnum, "FALSE"));
    }

    @Override
    public List<FireRegionDO> listByCorpinfoId(Long corpinfoId) {
        return fireRegionMapper.listByCorpinfoId(corpinfoId);
    }
    @Override
    public boolean existsByFireRegionCode(String fireRegionCode) {
        QueryWrapper<FireRegionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_region_code", fireRegionCode);
        return fireRegionMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public FireRegionDO getInfoByfireRefionCode(String fireRegionCode) {
        QueryWrapper<FireRegionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_region_code", fireRegionCode);
        return fireRegionMapper.selectOne(queryWrapper);
    }

    @Override
    public Long getFireRegionCount() {
        Map<String, Object> params = new HashMap<>();
        params.put("state", FireRegionStateEnum.ENABLE.getCode());
        params.put("corpTypeList", CorpTypeEnum.getCodesByEnterpriseType(UserTypeEnum.QY.getCode()));
        Long count = fireRegionMapper.getFireRegionCount(params);
        return count;
    }

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

    @Override
    public void checkRegion(String fireRegionId) {
        QueryWrapper<FireRegionDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_region_id", fireRegionId);
        FireRegionDO one = getOne(queryWrapper);
        if(CommonFlagEnum.YES.getCode().equals(one.getState())){
            throw new RuntimeException("该区域已禁用");
        }
    }
}

