|
|
package com.yohoufo.order.service.impl;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.core.cache.LocalCache;
|
|
|
import com.yoho.core.cache.LocalCacheCallback;
|
|
|
import com.yoho.core.common.helpers.ImagesHelper;
|
|
|
import com.yohoufo.dal.order.AppraiseAddressMapper;
|
|
|
import com.yohoufo.dal.order.model.AppraiseAddress;
|
|
|
import com.yohoufo.order.model.response.AppraiseAddressResp;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.security.SecureRandom;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* Created by li.ma on 2018/9/28.
|
|
|
* 鉴定中心匹配查询接口
|
|
|
*/
|
|
|
@Service
|
|
|
public class AppraiseAddressService {
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(AppraiseAddressService.class);
|
|
|
|
|
|
private final static String CACHE_KEY="appraiseAddress_cache";
|
|
|
|
|
|
@Autowired
|
|
|
private AppraiseAddressMapper appraiseAddressMapper;
|
|
|
|
|
|
private LocalCache localCache = new LocalCache();
|
|
|
|
|
|
// 防止缓存在同一时刻全部失效
|
|
|
private final static SecureRandom RANDOM = new SecureRandom();
|
|
|
|
|
|
// 根据四级地址code查询匹配的鉴定中心地址
|
|
|
public AppraiseAddressResp queryAddressByAreaCode(String areaCode) {
|
|
|
if (StringUtils.isEmpty(areaCode) || areaCode.length() < 2) {
|
|
|
return queryInitAddress();
|
|
|
}
|
|
|
|
|
|
String provinceCode = areaCode.substring(0, 2);
|
|
|
List<AppraiseAddressResp> appraiseAddressResps = queryAddressInfoList();
|
|
|
|
|
|
AppraiseAddressResp result = null;
|
|
|
|
|
|
for (AppraiseAddressResp resp : appraiseAddressResps) {
|
|
|
if (provinceCode.equals(String.valueOf(resp.getAreaCode()))) {
|
|
|
return resp;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return queryInitAddress();
|
|
|
}
|
|
|
|
|
|
// 根据默认鉴定中心地址
|
|
|
public AppraiseAddressResp queryInitAddress() {
|
|
|
List<AppraiseAddressResp> appraiseAddressResps = queryAddressInfoList();
|
|
|
|
|
|
if (CollectionUtils.isEmpty(appraiseAddressResps)) {
|
|
|
return new AppraiseAddressResp.builder().setMobile("15010019250").setAddressName("YOHO!北京鉴定中心").setAddress("北京市朝阳区建国路81号华贸中心1号写字楼3层").build();
|
|
|
}
|
|
|
return appraiseAddressResps.get(0);
|
|
|
}
|
|
|
|
|
|
public List<AppraiseAddressResp> queryAddressInfoList() {
|
|
|
Object value=localCache.get(CACHE_KEY);
|
|
|
if(null == value) {
|
|
|
return Lists.newArrayList();
|
|
|
}
|
|
|
return (List<AppraiseAddressResp>)value;
|
|
|
}
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
private void init() {
|
|
|
localCache.init("AddressInfoCache", 300000 + RANDOM.nextInt(100), TimeUnit.SECONDS, new LocalCacheCallback() {
|
|
|
@Override
|
|
|
public Object load(String key, Object oldvalue) {
|
|
|
List<AppraiseAddressResp> appraiseAddressResps = Lists.newArrayList();
|
|
|
try {
|
|
|
List<AppraiseAddress> appraiseAddresses = appraiseAddressMapper.selectAll();
|
|
|
|
|
|
if (CollectionUtils.isEmpty(appraiseAddresses)) {
|
|
|
LOG.warn("No data in database table (ufo_order.appraise_address).");
|
|
|
return Lists.newArrayList();
|
|
|
}
|
|
|
|
|
|
appraiseAddresses.stream().forEach(item ->
|
|
|
appraiseAddressResps.add(new AppraiseAddressResp.builder().setMobile(item.getMobile())
|
|
|
.setAreaCode(item.getAreaCode()).setAddressName(item.getAddressName()).setAddress(item.getAddress()).build())
|
|
|
);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
LOG.warn("appraiseAddressMapper failed!!!!!!!!!!", e);
|
|
|
}
|
|
|
return appraiseAddressResps;
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|