CacheServiceImpl.java 1.58 KB
package com.huaheng.control.management.service.impl;

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

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.TypeReference;
import com.huaheng.control.management.dto.Zone;
import com.huaheng.control.management.service.CacheService;
import com.huaheng.control.management.utils.OkHttpUtils;
import com.huaheng.control.management.utils.config.ApplicationConfig;
import com.huaheng.control.management.vo.Result;

@Service
public class CacheServiceImpl implements CacheService {

    @Resource
    private ApplicationConfig applicationConfig;

    @Override
    @Cacheable(cacheNames = "getZoneMap#60", key = "#root.methodName", unless = "#result == null")
    public Map<String, Zone> getZoneMap() {
        Map<String, Zone> zoneMap = new LinkedHashMap<String, Zone>();
        String url = applicationConfig.getWms_url() + "/api/cmc/getZones";
        Map<String, String> headers = new LinkedHashMap<>();
        headers.put("token", applicationConfig.getWms_token());
        Result<List<Zone>> result = OkHttpUtils.sendPostByJsonStr(url, headers, "{}", new TypeReference<Result<List<Zone>>>() {});
        if (result == null || !result.getCode().equals(200) || CollectionUtils.isEmpty(result.getResult())) {
            return null;
        }
        for (Zone zone : result.getResult()) {
            zoneMap.put(zone.getCode(), zone);
        }
        return zoneMap;
    }

}