MrShi
6 小时以前 eb7a808aaf7dd0a6dd2ff70f9ef3f8ce0b1e31d1
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();
@@ -130,6 +146,11 @@
        }
        //刷新缓存数据
        cacheData();
    }
    @Override
    public Areas getById(Integer id) {
       return areasMapper.selectById(id);
    }
    @Override
@@ -548,6 +569,7 @@
        return list;
    }
    @Override
    public Areas getOpenedCityByName(String cityName) {
        if (StringUtils.isBlank(cityName)) {
@@ -563,4 +585,80 @@
        return areasMapper.selectOne(qw);
    }
    @Override
    public Areas getOpenedCityByCode(String code) {
        if (StringUtils.isBlank(code)) {
            return null;
        }
        Areas areas = areasMapper.selectById(code);
        if(Objects.isNull(areas)||Objects.isNull(areas.getParentId())){
            return null;
        }
        Areas city = areasMapper.selectById(areas.getParentId());
        if(Objects.isNull(city)||Constants.equalsInteger(city.getIsdeleted(),Constants.ONE)||
            Constants.equalsInteger(city.getStatus(),Constants.ZERO)){
            return null;
        }
        return city;
    }
    @Override
    public void updateStatus(Areas areas) {
        if (areas == null || areas.getId() == null) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        Areas update = new Areas();
        update.setId(areas.getId());
        update.setStatus(areas.getStatus());
        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);
    }
}