Skip to content

Commit

Permalink
候选人?
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCao committed Dec 4, 2017
1 parent ec7aff6 commit 3b006bd
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package club.newtech.qbike.order.domain.core.entity;

import club.newtech.qbike.order.domain.core.root.Order;
import lombok.Data;

import javax.persistence.*;
import java.util.Date;

@Data
@Entity
@Table(name = "t_candidate")
public class Candidate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int cid;
@ManyToOne
@JoinColumn(name = "order_id")
private Order order;
private String candidateDriverId;
@Temporal(TemporalType.TIMESTAMP)
private Date updated;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ public class Order {
@Column(length = 32, nullable = false)
private Status orderStatus;
private String intentionId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package club.newtech.qbike.order.domain.repository;

import club.newtech.qbike.order.domain.core.entity.Candidate;
import org.springframework.data.repository.CrudRepository;

public interface CanidateRepository extends CrudRepository<Candidate, Integer> {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package club.newtech.qbike.order.domain.service;

import club.newtech.qbike.order.domain.core.entity.Candidate;
import club.newtech.qbike.order.domain.core.root.Order;
import club.newtech.qbike.order.domain.core.vo.CustomerVo;
import club.newtech.qbike.order.domain.core.vo.DriverVo;
import club.newtech.qbike.order.domain.core.vo.IntentionVo;
import club.newtech.qbike.order.domain.core.vo.Status;
import club.newtech.qbike.order.domain.repository.CanidateRepository;
import club.newtech.qbike.order.domain.repository.OrderRepository;
import club.newtech.qbike.order.infrastructure.UserRibbonHystrixApi;
import org.slf4j.Logger;
Expand All @@ -21,6 +22,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.concurrent.DelayQueue;
import java.util.stream.Collectors;

Expand All @@ -36,6 +38,8 @@ public class OrderService {
private OrderRepository orderRepository;
@Autowired
private SequenceFactory sequenceFactory;
@Autowired
private CanidateRepository canidateRepository;

private String generateOrderId() {
return "T" + String.format("%010d", sequenceFactory.generate("order"));
Expand Down Expand Up @@ -68,30 +72,44 @@ public void handleIntention() {
2000L);
intentions.put(newVo);
} else {
LOGGER.info("获取附近司机为{} 目前的策略为取首位",
result.getContent().stream()
.map(x -> x.getContent().getName())
.collect(Collectors.joining(",")));
String driverId = result.getContent().get(0).getContent().getName();
LOGGER.info("{} 匹配到的司机ID为 {}", intentionVo.getCustomerId(), driverId);
createOrder(intentionVo, driverId);
List<String> drivers = result.getContent()
.stream()
.map(x -> x.getContent().getName())
.collect(Collectors.toList());
LOGGER.info("获取附近司机为{} 将发送给司机确认",
drivers);
Order order = createOrder(intentionVo);

LOGGER.info("{} 创建的OrderId为 {}", intentionVo.getCustomerId(), order.getOid());
drivers.forEach(driverId -> createCandidate(order, driverId));
}
}
} catch (InterruptedException e) {
} catch (Exception e) {
LOGGER.error("Error happended", e);
}
}
}

@Transactional
protected void createOrder(IntentionVo intention, String driverId) {
protected Candidate createCandidate(Order order, String driverId) {
Candidate candidate = new Candidate();
candidate.setCandidateDriverId(driverId);
candidate.setUpdated(new Date());
canidateRepository.save(candidate);
return candidate;

}


@Transactional
protected Order createOrder(IntentionVo intention) {
//在调用远程user服务获取用户信息的时候,必须有熔断,否则在事务中很危险
Order order = new Order();
order.setOid(generateOrderId());
CustomerVo customerVo = userService.findCustomerById(Integer.parseInt(intention.getCustomerId()));
DriverVo driverVo = userService.findDriverById(Integer.parseInt(driverId));
// DriverVo driverVo = userService.findDriverById(Integer.parseInt(driverId));
order.setCustomer(customerVo);
order.setDriver(driverVo);
// order.setDriver(driverVo);
order.setOrderStatus(Status.OPENED);
order.setOpened(new Date());
order.setStartLong(intention.getStartLong());
Expand All @@ -100,6 +118,7 @@ protected void createOrder(IntentionVo intention, String driverId) {
order.setDestLat(intention.getDestLat());
order.setIntentionId(intention.getMid());
orderRepository.save(order);
return order;
}

@Transactional
Expand All @@ -108,4 +127,9 @@ public void handlePosition(int driverId, double longitude, double latitude) {
redisTemplate.opsForGeo().geoAdd("Drivers", new Point(longitude, latitude), String.valueOf(driverId));
}

@Transactional
public void handleCandidate(int driverId, String orderId) {

}

}

0 comments on commit 3b006bd

Please sign in to comment.