package com.ltgk.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltgk.common.constant.CacheConstants;
import com.ltgk.common.core.redis.RedisCache;
import com.ltgk.system.domain.SysAreaInfo;
import com.ltgk.system.domain.dto.SysAreaDto;
import com.ltgk.system.mapper.SysAreaInfoMapper;
import com.ltgk.system.service.SysAreaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @Author: wan kun
 * @Description: 行政区域实现类
 * @Date: 2024/12/11 16:25
 * @Version: 1.0
 */
@Service
public class SysAreaInfoServiceImpl extends ServiceImpl<SysAreaInfoMapper, SysAreaInfo> implements SysAreaInfoService {
    @Autowired
    private RedisCache redisCache;

    @Override
    public List<SysAreaDto> getAreaTree(String areaCode) {
        if (ObjectUtils.isNull(areaCode)) areaCode = "0";
        // 获取缓存
        if (redisCache.hasKey(CacheConstants.SYS_AREA_KEY + areaCode)) {
            return redisCache.getCacheObject(CacheConstants.SYS_AREA_KEY + areaCode);
        }
        LambdaQueryWrapper<SysAreaInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysAreaInfo::getPid, areaCode);
        List<SysAreaInfo> list = this.list(lqw);
        if (ObjectUtils.isNull(list)) {
            return null;
        }
        ArrayList<SysAreaDto> result = new ArrayList<>();
        list.forEach(item -> {
            SysAreaDto sysAreaDto = new SysAreaDto();
            sysAreaDto.setAreaCode(item.getAreaCode());
            sysAreaDto.setAreaName(item.getAreaName());
            sysAreaDto.setChildArea(getAreaTree(item.getAreaCode()));
            result.add(sysAreaDto);
        });
        redisCache.setCacheObject(CacheConstants.SYS_AREA_KEY + areaCode, result);
        return result;
    }

    @Override
    public List<SysAreaInfo> findAreaInfoByAreaCodeSet(Set<String> areaCodeSet) {
        if (CollectionUtils.isEmpty(areaCodeSet)) {
            return Collections.emptyList();
        }

        List<SysAreaInfo> sysAreaInfoList = new ArrayList<>();

        List<String> areaCodeList = new ArrayList<>();

        areaCodeSet.forEach(areaCode -> {
            if (redisCache.hasKey(CacheConstants.SYS_AREA_KEY + areaCode)) {
                sysAreaInfoList.add(redisCache.getCacheObject(CacheConstants.SYS_AREA_KEY + areaCode));
            } else {
                areaCodeList.add(areaCode);
            }
        });
        List<SysAreaInfo> dbSysAreaInfoList = this.lambdaQuery()
                .in(SysAreaInfo::getAreaCode, areaCodeList)
                .list();
        sysAreaInfoList.addAll(dbSysAreaInfoList);
        return sysAreaInfoList;
    }

    /**
     * 根据行政区域名称查询相关的行政区域
     *
     * @param areaNameSet：区域名称集合
     * @return java.util.List<com.ltgk.system.domain.SysAreaInfo>
     * @author Qi ChengBin
     * @date 2025/7/16
     */
    @Override
    public List<SysAreaInfo> findAreaInfoByAreaNameSet(Set<String> areaNameSet) {
        return this.lambdaQuery()
                .in(SysAreaInfo::getAreaName, areaNameSet)
                .list();
    }
}
