Commit d772bb98 by Ren Ping

feat:路网更新为redis缓存

1 parent 810f3686
......@@ -19,6 +19,7 @@ package com.dituhui.pea.dispatch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
......@@ -27,6 +28,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@EnableFeignClients(basePackages = {"com.dituhui.pea.user"})
@Import(cn.hutool.extra.spring.SpringUtil.class)
public class DispatchServiceApplication {
public static void main(String[] args) {
......
package com.dituhui.pea.dispatch.common;
package com.dituhui.pea.dispatch.common.redis;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
......@@ -16,10 +21,27 @@ public class RedisConfig {
// objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
// ObjectMapper.DefaultTyping.EVERYTHING, JsonTypeInfo.As.PROPERTY);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer jacksonSerial = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
// 指定要序列化的域,field, get和set,以及修饰符范围,ANY是都有包括private和public
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// 指定序列化输入的类型,类必须是非final修饰的,final修饰的类,比如String,Integer等会抛出异常
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
// jsonToBean时,json中有的字段,bean中没有 无法匹配时,忽略此字段,不抛出异常(默认是抛出异常的)
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
jacksonSerial.setObjectMapper(objectMapper);
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jacksonSerial);
// 设置hash key 和 value 序列化模式
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jacksonSerial);
return redisTemplate;
}
......
package com.dituhui.pea.dispatch.common;
package com.dituhui.pea.dispatch.common.redis;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
......
package com.dituhui.pea.dispatch.common;
package com.dituhui.pea.dispatch.common.redis;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;
......
package com.dituhui.pea.dispatch.constraint;
public class RedisKeyConstant {
public static final String LOCATION_DISTANCE_KEY = "location:%s:distance:%s";
public static final String LOCATION_DISTANCE_TIME_KEY = "location:%s:distanceTime:%s";
}
package com.dituhui.pea.dispatch.quartz.dispatch;
import com.dituhui.pea.dispatch.common.RedissonUtil;
import com.dituhui.pea.dispatch.service.SchedulerService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
......
......@@ -88,7 +88,7 @@ public class BatchScheduler {
log.info("dispatchRun done ------ teamId:{}, day:{}", teamId, currDay);
JacksonSolutionFileIO<DispatchSolution> exporter = new JacksonSolutionFileIO<DispatchSolution>(DispatchSolution.class);
exporter.write(solution, new File(String.format("dispatchSolution_%s_%s.json",teamId, currDay)));
exporter.write(solution, new File(String.format("dispatchSolution_%s_%s.json", teamId, currDay)));
}
}
......@@ -155,7 +155,7 @@ public class BatchScheduler {
// Set the output file.
exporter.write(solution, new File(String.format("dispatchSolution_%s_%s.json",groupId, currDay)));
exporter.write(solution, new File(String.format("dispatchSolution_%s_%s.json", groupId, currDay)));
Thread.sleep(1000 * 5);
......
......@@ -3,9 +3,12 @@ package com.dituhui.pea.dispatch.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.dituhui.pea.dispatch.dao.*;
import com.dituhui.pea.dispatch.entity.*;
import com.dituhui.pea.dispatch.pojo.Customer;
import com.dituhui.pea.dispatch.pojo.DispatchSolution;
import com.dituhui.pea.dispatch.service.ExtractService;
import com.google.common.collect.Maps;
......@@ -295,7 +298,13 @@ public class ExtractServiceImpl implements ExtractService {
orderInfo.setArriveElapsed(0);
orderInfo.setArriveDistance(0);
orderInfo.setAppointmentStatus("INIT");
if (cutOff) {
orderInfo.setAppointmentMethod("MANUAL");
orderInfo.setDispatcher("MANUAL");
log.info("算法结果更新到工单为人工, teamId:{}, batchNo:{}, orderId:{}", teamId, batchNo, orderId);
} else {
orderInfo.setDispatcher("AUTO_BATCH");
}
orderInfo.setUpdateTime(LocalDateTime.now());
orderInfoRepo.save(orderInfo);
......
......@@ -3,7 +3,6 @@ package com.dituhui.pea.dispatch.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.dituhui.pea.dispatch.common.RedissonUtil;
import com.dituhui.pea.dispatch.dao.DispatchBatchRepository;
import com.dituhui.pea.dispatch.dao.OrgGroupRepository;
import com.dituhui.pea.dispatch.dao.OrgTeamDao;
......@@ -11,7 +10,6 @@ import com.dituhui.pea.dispatch.entity.DispatchBatch;
import com.dituhui.pea.dispatch.entity.OrgGroup;
import com.dituhui.pea.dispatch.entity.OrgTeamEntity;
import com.dituhui.pea.dispatch.pojo.DispatchSolution;
import com.dituhui.pea.dispatch.quartz.dispatch.AutoDispatchJob;
import com.dituhui.pea.dispatch.service.BatchService;
import com.dituhui.pea.dispatch.service.ExtractService;
import com.dituhui.pea.dispatch.service.SchedulerService;
......
......@@ -2,6 +2,7 @@ package com.dituhui.pea.dispatch.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
......@@ -10,6 +11,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.hutool.core.util.StrUtil;
import com.dituhui.pea.dispatch.common.redis.RedisUtil;
import org.springframework.stereotype.Component;
import com.dituhui.pea.dispatch.pojo.Location;
......@@ -25,7 +28,6 @@ import lombok.extern.slf4j.Slf4j;
* TODO 调用方式需要改成批量调用方式
*
* @author gpzhang
*
*/
@Component
@Slf4j
......@@ -37,7 +39,7 @@ public class RoadDistanceUtils {
/**
* 格式 x1,y1;x2,y2
*/
private static Map<String, Distance> distanceCache = Maps.newHashMap();
//private static Map<String, Distance> distanceCache = Maps.newHashMap();
private static Gson gson = new Gson();
/**
......@@ -50,9 +52,9 @@ public class RoadDistanceUtils {
*/
public Distance getDistance2(Location from, Location to, int vehicleType) {
try {
String key = from.getLongitude() + "," + from.getLatitude() + ";" + to.getLongitude() + ","
+ to.getLatitude() + "|" + vehicleType;
Distance distance = distanceCache.get(key);
String key = getLocationKey(from, to, vehicleType);
//Distance distance = distanceCache.get(key);
Distance distance = (Distance) RedisUtil.get(key);
if (null == distance) {
distance = getDistance(from.getLatitude() + "," + from.getLongitude(),
to.getLatitude() + "," + to.getLongitude(), vehicleType);
......@@ -61,7 +63,8 @@ public class RoadDistanceUtils {
Distance dis = new Distance();
return dis;
} else {
distanceCache.put(key, distance);
//distanceCache.put(key, distance);
RedisUtil.set(key, distance);
}
return distance;
} else {
......@@ -86,9 +89,9 @@ public class RoadDistanceUtils {
*/
public static Distance getDistance(Location from, Location to, int vehicleType) {
try {
String key = from.getLongitude() + "," + from.getLatitude() + ";" + to.getLongitude() + ","
+ to.getLatitude() + "|" + vehicleType;
Distance distance = distanceCache.get(key);
String key = getLocationKey(from, to, vehicleType);
//Distance distance = distanceCache.get(key);
Distance distance = (Distance) RedisUtil.get(key);
if (null == distance) {
distance = getDistance(from.getLatitude() + "," + from.getLongitude(),
to.getLatitude() + "," + to.getLongitude(), vehicleType);
......@@ -96,7 +99,8 @@ public class RoadDistanceUtils {
Distance dis = new Distance();
return dis;
} else {
distanceCache.put(key, distance);
//distanceCache.put(key, distance);
RedisUtil.set(key, distance);
}
return distance;
} else {
......@@ -108,6 +112,27 @@ public class RoadDistanceUtils {
}
}
public static String getLocationKey(Location from, Location to, int vehicleType) {
return convertDoubleToStr(from.getLongitude()) + ","
+ convertDoubleToStr(from.getLatitude()) + ";"
+ convertDoubleToStr(to.getLongitude()) + ","
+ convertDoubleToStr(to.getLatitude()) + "|" + vehicleType;
}
public static String convertDoubleToStr(double value) {
String[] ss = BigDecimal.valueOf(value).toString().split("\\.");
if (ss.length == 1) {
return ss[0] + ".00000";
}
if (ss[1].length() >= 5) {
return ss[0] + "." + ss[1].substring(0, 5);
}
return ss[0] + "." + StrUtil.padAfter(ss[1], 5, "0");
}
private static Distance getDistance(String yx1, String yx2, int vehicleType) throws Exception {
......
......@@ -3,7 +3,7 @@ server:
dispatch:
cron:
expr: 0 9 8-23 * * ?
expr: 0 37 8-23 * * ?
next-day-limit: 2
scheduler:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!