rk
7 小时以前 552238172036acf08ccf36134282a06b5e21b936
server/services/src/main/java/com/doumee/service/business/impl/AreasServiceImpl.java
@@ -12,6 +12,7 @@
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.PinYinUtil;
import com.doumee.core.utils.geocode.MapUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.AreasMapper;
import com.doumee.dao.business.PricingRuleMapper;
@@ -19,7 +20,9 @@
import com.doumee.dao.business.model.PricingRule;
import com.doumee.service.business.AreasService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -35,6 +38,7 @@
 * @author 江蹄蹄
 * @date 2023/02/15 08:55
 */
@Slf4j
@Service
public class AreasServiceImpl implements AreasService {
    public static   List<Areas> ALL_AREA_LIST;
@@ -71,6 +75,10 @@
        areas.setCreateDate(new Date());
        areasMapper.insert(areas);
        areas.setCode(areas.getId().toString());
        // 城市级区划自动填充经纬度
        if (Constants.equalsInteger(areas.getType(), Constants.ONE)) {
            areas.setInfo(geocodeCity(areas));
        }
        areasMapper.updateById(areas);
        //刷新缓存数据
        cacheData();
@@ -115,6 +123,14 @@
        Areas update = new Areas();
        update.setName(areas.getName());
        update.setSortnum(areas.getSortnum());
        // 城市级区划自动更新经纬度
        Areas existing = areasMapper.selectById(areas.getId());
        if (existing != null && Constants.equalsInteger(existing.getType(), Constants.ONE)) {
            String location = MapUtil.geocode(getParentName(existing.getParentId()) + areas.getName());
            if (location != null) {
                update.setInfo(location);
            }
        }
        areasMapper.update(update,wrapper);
        //刷新缓存数据
        cacheData();
@@ -598,4 +614,51 @@
        areasMapper.updateById(update);
    }
    @Override
    public String fillCityLocation() {
        Areas query = new Areas();
        query.setType(1);
        List<Areas> cities = areasMapper.selectList(new QueryWrapper<Areas>().lambda()
                .eq(Areas::getType, 1));
        int success = 0, fail = 0;
        for (Areas city : cities) {
            if (StringUtils.isNotBlank(city.getInfo())) {
                continue;
            }
            String parentName = "";
            if (city.getParentId() != null) {
                Areas parent = getById(city.getParentId());
                if (parent != null) {
                    parentName = parent.getName();
                }
            }
            String address = parentName + city.getName();
            String location = MapUtil.geocode(address);
            if (location != null) {
                city.setInfo(location);
                areasMapper.updateById(city);
                success++;
                log.info("城市经纬度填充成功: {} => {}", address, location);
            } else {
                fail++;
                log.warn("城市经纬度填充失败: {}", address);
            }
            try { Thread.sleep(200); } catch (InterruptedException ignored) {}
        }
        return "处理完成,成功: " + success + ",失败: " + fail + ",总计: " + cities.size();
    }
    private String getParentName(Integer parentId) {
        if (parentId == null) {
            return "";
        }
        Areas parent = getById(parentId);
        return parent != null ? parent.getName() : "";
    }
    private String geocodeCity(Areas city) {
        String address = getParentName(city.getParentId()) + city.getName();
        return MapUtil.geocode(address);
    }
}