提交 37735437 authored 作者: huyufan's avatar huyufan

开发:增加订单状态MQ

上级 543e7cd8
package com.clx.performance.component;
import com.clx.performance.constant.RedisConstants;
import com.clx.performance.dao.OrderGoodsDao;
import com.msl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -18,25 +19,27 @@ public class OrderGoodsIdGenerate {
private OrderGoodsDao orderGoodsDao;
public int getOrderGoodsId(String type, Integer size) {
int baseStart = Integer.parseInt(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd") + "00001");
Object o = redisTemplate.opsForHash().get("performance:orderGoodsId:", type);
public long getOrderGoodsId(String type, Integer size) {
long baseStart = Long.parseLong(DateUtils.formatDate(LocalDate.now(), "yyyyMMdd").get() + "00001");
Object o = redisTemplate.opsForHash().get(RedisConstants.ORDER_GOODS_ID, type);
if (o == null) {
int maxOrderGoodsId = orderGoodsDao.getMaxOrderGoodsId(type);
Long maxOrderGoodsId = orderGoodsDao.getMaxOrderGoodsId(type);
if (maxOrderGoodsId == null) {
maxOrderGoodsId = 0L;
}
if (baseStart > maxOrderGoodsId) {
redisTemplate.opsForHash().put("performance:orderGoodsId:", type, baseStart + size);
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(baseStart + size));
return baseStart;
} else {
int orderGoodsId = maxOrderGoodsId + 1;
redisTemplate.opsForHash().put("performance:orderGoodsId:", type, orderGoodsId + size);
return orderGoodsId;
long orderGoodsId = maxOrderGoodsId + size;
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId));
return maxOrderGoodsId + 1;
}
} else {
int redisOrderGoodsId = Integer.parseInt(o.toString());
int orderGoodsId = redisOrderGoodsId + 1;
redisTemplate.opsForHash().put("performance:orderGoodsId:", type, orderGoodsId + size);
return orderGoodsId;
long redisOrderGoodsId = Long.parseLong(o.toString());
long orderGoodsId = redisOrderGoodsId + size;
redisTemplate.opsForHash().put(RedisConstants.ORDER_GOODS_ID, type, String.valueOf(orderGoodsId));
return redisOrderGoodsId;
}
}
}
package com.clx.performance.config;
import com.clx.performance.constant.RabbitKeyConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author xujianke
* @date 2017年9月14日
* @description rabbit配置文件
*/
@Configuration
public class RabbitConfig {
/**
* 订单已挂单队列
*/
@Bean
public Queue orderPostedQueue() {
return new Queue(RabbitKeyConstants.ORDER_POSTED_QUEUE, true);
}
/**
* 订单已挂单交换机
**/
@Bean
public DirectExchange orderPostedExchange() {
return new DirectExchange(RabbitKeyConstants.ORDER_POSTED_EXCHANGE);
}
/**
* 订单已挂单绑定
*/
@Bean
public Binding orderPostedExchangeBind() {
return BindingBuilder.bind(orderPostedQueue()).to(orderPostedExchange()).with(RabbitKeyConstants.ORDER_POSTED_ROUTE_KEY);
}
@Bean
public Queue orderOnQueue() {
Map<String, Object> params = new HashMap<>(6);
params.put("x-dead-letter-exchange", RabbitKeyConstants.ORDER_ON_DEAD_EXCHANGE);
params.put("x-dead-letter-routing-key", RabbitKeyConstants.ORDER_ON_ROUTE_KEY);
return new Queue(RabbitKeyConstants.ORDER_ON_QUEUE, true, false, false, params);
}
/**
* 订单挂单中交换机
**/
@Bean
public DirectExchange orderOnExchange() {
return new DirectExchange(RabbitKeyConstants.ORDER_ON_EXCHANGE);
}
/**
* 订单挂单中绑定
*/
@Bean
public Binding orderOnExchangeBind() {
return BindingBuilder.bind(orderOnQueue()).to(orderOnExchange()).with(RabbitKeyConstants.ORDER_ON_ROUTE_KEY);
}
/**
* 死信队列:死信队列处理延迟消息
* @return
*/
@Bean
public Queue orderOnDeadQueue() {
return new Queue(RabbitKeyConstants.ORDER_ON_DEAD_QUEUE, true, false, false);
}
/**
* 订单挂单中交换机:死信队列处理延迟消息
**/
@Bean
public DirectExchange orderOnDeadExchange() {
return new DirectExchange(RabbitKeyConstants.ORDER_ON_DEAD_EXCHANGE);
}
/**
* 订单挂单中绑定:死信队列处理延迟消息
*/
@Bean
public Binding orderDeadExchangeBind() {
return BindingBuilder.bind(orderOnDeadQueue()).to(orderOnDeadExchange()).with(RabbitKeyConstants.ORDER_ON_ROUTE_KEY);
}
}
package com.clx.performance.constant;
public class RabbitKeyConstants {
public static final String ORDER_POSTED_QUEUE = "order.posted.queue";
public static final String ORDER_POSTED_EXCHANGE = "order.posted.exchange";
public static final String ORDER_POSTED_ROUTE_KEY ="order.posted.route.key";
public static final String ORDER_ON_ROUTE_KEY ="order.on.route.key";
public static final String ORDER_ON_QUEUE ="order.on.queue";
public static final String ORDER_ON_EXCHANGE ="order.on.exchange";
public static final String ORDER_ON_DEAD_EXCHANGE ="order.on.dead.exchange";
public static final String ORDER_ON_DEAD_QUEUE ="order.on.dead.queue";
}
......@@ -4,4 +4,6 @@ public class RedisConstants {
public static final String ORDER_NO_BLOCK = "performance:order_no_block:";
public static final String ORDER_GOODS_ID ="performance:orderGoodsId:";
}
......@@ -9,6 +9,7 @@ import com.clx.order.params.PageCarrierOrderListParam;
import com.clx.order.vo.feign.FeignOrderVO;
import com.clx.order.vo.feign.FeignPageOrderVO;
import com.clx.performance.component.OrderGoodsIdGenerate;
import com.clx.performance.constant.RabbitKeyConstants;
import com.clx.performance.constant.RedisConstants;
import com.clx.performance.mapper.OrderGoodsMapper;
import com.clx.performance.model.OrderGoods;
......@@ -17,12 +18,18 @@ import com.msl.common.base.PageData;
import com.msl.common.convertor.aspect.UnitCovert;
import com.msl.common.result.Result;
import com.msl.common.utils.DateUtils;
import com.msl.user.data.UserSessionData;
import com.msl.user.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
......@@ -33,6 +40,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.concurrent.TimeUnit;
......@@ -58,7 +66,7 @@ public class GoodsOrderController {
OrderGoodsMapper orderGoodsMapper;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
private RabbitTemplate rabbitTemplate;
@Autowired
private RedissonClient redissonClient;
......@@ -73,6 +81,8 @@ public class GoodsOrderController {
@PostMapping("/saveGoodsOrder")
public Result<Object> saveGoodName(@RequestBody @Validated OrderGoodsParams orderGoodsParams) {
String orderNo = orderGoodsParams.getOrderNo();
LocalDateTime sendLazyTime = null;
//UserSessionData loginUserInfo = TokenUtil.getLoginUserInfo();
RLock rLock = null;
try {
//1. 加分布式锁通过订单ID
......@@ -92,7 +102,7 @@ public class GoodsOrderController {
throw new RuntimeException("当前货单总吨数已超订单总吨数");
}
LocalDateTime now = LocalDateTime.now();
int beginOrderGoodsId = orderGoodsIdGenerate.getOrderGoodsId("pt", childParamsList.size());
long beginOrderGoodsId = orderGoodsIdGenerate.getOrderGoodsId("pt", childParamsList.size());
for (OrderGoodsChildParams child : childParamsList) {
if (child.getPendingOrderWay().equals(2) && child.getNeedTruckNum() == null) {
throw new RuntimeException("定向派单必须选择车辆");
......@@ -109,7 +119,15 @@ public class GoodsOrderController {
//挂单方式
orderGoods.setPendingOrderWay(child.getPendingOrderWay());
//挂单时间
orderGoods.setPendingOrderTime(DateUtils.parseDateTime(child.getPendingOrderTime()).get());
LocalDateTime postedTime = DateUtils.parseDateTime(child.getPendingOrderTime()).get();
if (sendLazyTime == null) {
sendLazyTime = postedTime;
} else {
if (sendLazyTime.isAfter(postedTime)) {
sendLazyTime = postedTime;
}
}
orderGoods.setPendingOrderTime(postedTime);
orderGoods.setPendingOrderFreight(child.getPendingOrderFreight());
orderGoods.setLastArriveSendTime(DateUtils.parseDateTime(child.getLastArriveSendTime()).get());
......@@ -129,9 +147,12 @@ public class GoodsOrderController {
orderGoods.setSendLatitude(orderInfo.getSendLatitude());
orderGoods.setReceiveLatitude(orderInfo.getReveiveLatitude());
orderGoods.setReceiveLongitude(orderInfo.getReveiveLongitude());
orderGoods.setReceiveAddressId(orderInfo.getReveiveAddressId());
orderGoods.setGoodsName(orderInfo.getGoodsName());
orderGoods.setCreateTime(now);
orderGoods.setModifiedTime(now);
orderGoods.setUserName("loginUserInfo.getUserName()");
orderGoods.setUserNo(123L);
beginOrderGoodsId = beginOrderGoodsId + 1;
orderGoodsMapper.insert(orderGoods);
}
......@@ -146,7 +167,7 @@ public class GoodsOrderController {
throw new RuntimeException("全部自有车辆只能全部提取");
}
LocalDateTime now = LocalDateTime.now();
int beginOrderGoodsId = orderGoodsIdGenerate.getOrderGoodsId("pt", childParamsList.size());
long beginOrderGoodsId = orderGoodsIdGenerate.getOrderGoodsId("pt", childParamsList.size());
for (OrderGoodsChildParams child : childParamsList) {
OrderGoods orderGoods = new OrderGoods();
......@@ -164,7 +185,15 @@ public class GoodsOrderController {
//挂单方式
orderGoods.setPendingOrderWay(child.getPendingOrderWay());
//挂单时间
orderGoods.setPendingOrderTime(DateUtils.parseDateTime(child.getPendingOrderTime()).get());
LocalDateTime postedTime = DateUtils.parseDateTime(child.getPendingOrderTime()).get();
if (sendLazyTime == null) {
sendLazyTime = postedTime;
} else {
if (sendLazyTime.isAfter(postedTime)) {
sendLazyTime = postedTime;
}
}
orderGoods.setPendingOrderTime(postedTime);
orderGoods.setPendingOrderFreight(child.getPendingOrderFreight());
orderGoods.setLastArriveSendTime(DateUtils.parseDateTime(child.getLastArriveSendTime()).get());
......@@ -184,9 +213,12 @@ public class GoodsOrderController {
orderGoods.setSendLatitude(orderInfo.getSendLatitude());
orderGoods.setReceiveLatitude(orderInfo.getReveiveLatitude());
orderGoods.setReceiveLongitude(orderInfo.getReveiveLongitude());
orderGoods.setReceiveAddressId(orderInfo.getReveiveAddressId());
orderGoods.setGoodsName(orderInfo.getGoodsName());
orderGoods.setCreateTime(now);
orderGoods.setModifiedTime(now);
orderGoods.setUserName("loginUserInfo.getUserName()");
orderGoods.setUserNo(123L);
beginOrderGoodsId = beginOrderGoodsId + 1;
orderGoodsMapper.insert(orderGoods);
}
......@@ -203,6 +235,13 @@ public class GoodsOrderController {
log.error("redis 分布式锁释放异常!", e);
}
}
rabbitTemplate.send(RabbitKeyConstants.ORDER_POSTED_EXCHANGE, RabbitKeyConstants.ORDER_POSTED_ROUTE_KEY, MessageBuilder.withBody(orderNo.getBytes()).build());
Message message = MessageBuilder.withBody(orderNo.getBytes()).build();
long epochMilli = sendLazyTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
message.getMessageProperties().setExpiration(String.valueOf(epochMilli));
rabbitTemplate.send(
RabbitKeyConstants.ORDER_ON_EXCHANGE, RabbitKeyConstants.ORDER_ON_ROUTE_KEY, message
);
return Result.ok();
}
......
......@@ -11,7 +11,7 @@ import com.clx.performance.model.OrderGoods;
* Time 16:45
*/
public interface OrderGoodsDao extends BaseDao<OrderGoodsMapper, OrderGoods, Integer> {
Integer getMaxOrderGoodsId(String type);
Long getMaxOrderGoodsId(String type);
Optional<OrderGoods> getByOrderGoodsNo(String orderGoodsNo);
......
......@@ -18,7 +18,7 @@ import org.springframework.stereotype.Repository;
public class OrderGoodsDaoImpl extends BaseDaoImpl<OrderGoodsMapper, OrderGoods, Integer> implements OrderGoodsDao {
@Override
public Integer getMaxOrderGoodsId(String type) {
public Long getMaxOrderGoodsId(String type) {
return baseMapper.getMaxOrderGoodsId(type);
}
......
......@@ -13,5 +13,5 @@ import org.apache.ibatis.annotations.SelectProvider;
public interface OrderGoodsMapper extends BaseMapper<OrderGoods> {
@SelectProvider(type = OrderGoodsSqlProvider.class, method = "getMaxOrderGoodsId")
Integer getMaxOrderGoodsId(String type);
Long getMaxOrderGoodsId(String type);
}
......@@ -3,7 +3,7 @@ package com.clx.performance.sqlProvider;
public class OrderGoodsSqlProvider {
public String getMaxOrderGoodsId(String type) {
return "SELECT s.order_goods_no FROM `order_goods` s WHERE s. id = ( SELECT max(id) FROM order_goods where order_goods_type = " + type + ")";
return "SELECT s.order_goods_no FROM `order_goods` s WHERE s. id = ( SELECT max(id) FROM order_goods where order_goods_type = '" + type + "')";
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论