Commit b2ab17e0 authored by lemin's avatar lemin

ccc

parent dab6eada
package com.showcase.synapse.sales.command;
package com.showcase.synapse.common.command;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.axonframework.modelling.command.TargetAggregateIdentifier;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
@AllArgsConstructor
@Getter
public class CreateSalesCommand {
public class CancleSalesCommand implements Serializable{
private static final long serialVersionUID = 1L;
@TargetAggregateIdentifier
private final String id;
private final String name;
private final String comment;
private final BigDecimal price;
// private final Integer quantity;
private final String salesOrderId;
private final Integer orderQuantity;
}
package com.showcase.synapse.sales.command;
package com.showcase.synapse.common.command;
import java.io.Serializable;
import org.axonframework.modelling.command.TargetAggregateIdentifier;
......@@ -7,9 +9,13 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public class ChangeSalesQuantityCommand {
public class ChangeInventoryQuantityCommand implements Serializable{
private static final long serialVersionUID = 1L;
@TargetAggregateIdentifier
private String productId;
private int quantity;
private String salesOrderId;
private String whId;
private String orderComment;
private Integer orderQuantity;
}
package com.showcase.synapse.common.command;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.axonframework.modelling.command.TargetAggregateIdentifier;
import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
@AllArgsConstructor
@Getter
public class CreateSalesCommand implements Serializable{
private static final long serialVersionUID = 1L;
@TargetAggregateIdentifier
private final String salesOrderId;
private final String whId;
private final String orderName;
private final String orderComment;
private final Integer orderQuantity;
}
package com.showcase.synapse.sales;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.thoughtworks.xstream.XStream;
@Configuration
public class AxonConfig {
@Bean
public XStream xStream() {
XStream xStream = new XStream();
xStream.allowTypesByWildcard(new String[] {
"com.showcase.synapse.**"
});
return xStream;
}
}
\ No newline at end of file
......@@ -2,8 +2,10 @@ package com.showcase.synapse.sales;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@Import({ AxonConfig.class })
public class MsaSalesApplication {
public static void main(String[] args) {
......
package com.showcase.synapse.sales.aggregate;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.spring.stereotype.Aggregate;
import com.showcase.synapse.sales.command.ChangeSalesQuantityCommand;
import com.showcase.synapse.sales.command.CreateSalesCommand;
import com.showcase.synapse.common.command.CancleSalesCommand;
import com.showcase.synapse.common.command.CreateSalesCommand;
import com.showcase.synapse.sales.enums.SalesStatus;
import com.showcase.synapse.sales.event.SalesCanceledEvent;
import com.showcase.synapse.sales.event.SalesCreatedEvent;
import com.showcase.synapse.sales.event.SalesQuantityChangedEvent;
import com.showcase.synapse.sales.saga.SalesSaga;
import java.math.BigDecimal;
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Aggregate
@NoArgsConstructor
......@@ -25,30 +24,39 @@ public class SalesAggregate {
@AggregateIdentifier
private String id;
// private int quantiy;
private int orderQuantity;
private SalesStatus salesStatus;
@CommandHandler
public SalesAggregate(CreateSalesCommand command) {
log.info("[SalesAggregate(CreateSalesCommand) > apply new SalesCreatedEvent]");
apply(new SalesCreatedEvent(command.getId(), command.getName(), command.getComment(), command.getPrice()));
apply(new SalesCreatedEvent(command.getSalesOrderId(), command.getWhId(), command.getOrderName(),
command.getOrderComment(), command.getOrderQuantity()));
}
@EventSourcingHandler
public void createProduct(SalesCreatedEvent event) {
this.id = event.getProductId();
// this.quantiy = event.getQuantity();
public void createSalesOrder(SalesCreatedEvent event) {
this.id = event.getSalesOrderId();
this.orderQuantity = event.getOrderQuantity();
this.salesStatus = SalesStatus.CREATED;
}
@EventSourcingHandler
public void cancelSalesOrder(SalesCanceledEvent event) {
this.salesStatus = SalesStatus.CANCELED;
}
@CommandHandler
public void changeQuantity(ChangeSalesQuantityCommand command) {
log.info("[@CommandHandler ProductchangeQuantity]");
public void cancelSalesOrder(CancleSalesCommand command) {
log.info("[@CommandHandler cancelSalesOrder CancleSalesCommand]");
// if(this.quantiy < command.getQuantity()) throw new IllegalArgumentException("tfy ");
// apply(new SalesQuantityChangedEvent(command.getProductId(), this.quantiy - command.getQuantity()));
}
@EventSourcingHandler
public void changeQuantity(SalesQuantityChangedEvent event) {
public void changeSalesQuantity(SalesQuantityChangedEvent event) {
log.info("[@CommandHandler changeSalesQuantity SalesQuantityChangedEvent]");
// this.quantiy = event.getQuantity();
}
......
package com.showcase.synapse.sales.controller;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -16,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.showcase.synapse.sales.dto.ProductSalesDto;
import com.showcase.synapse.sales.dto.SalesCreateDto;
import com.showcase.synapse.sales.entity.ProductEntity;
import com.showcase.synapse.sales.entity.SalesEntity;
import com.showcase.synapse.sales.service.SalesService;
......@@ -44,13 +42,14 @@ public class SalesController {
}
@PostMapping("/")
public ResponseEntity<Map<String, String>> productCreate(@RequestBody SalesCreateDto productCreateDto) {
String productId = salesService.createProduct(productCreateDto.getName(),
productCreateDto.getComment(),
BigDecimal.valueOf(productCreateDto.getPrice()));
public ResponseEntity<Map<String, String>> salesCreate(@RequestBody SalesCreateDto salesCreateDto) {
String salesOrderId = salesService.createSales(salesCreateDto.getWhId(),
salesCreateDto.getOrderName(),
salesCreateDto.getOrderComment(),
salesCreateDto.getOrderQuantity());
HashMap<String, String> m = new HashMap<>();
m.put("productId", productId);
m.put("productName", productCreateDto.getName());
m.put("salesOrderId", salesOrderId);
m.put("orderQuantity", String.valueOf(salesCreateDto.getOrderQuantity()));
return ResponseEntity.ok(m);
}
......
......@@ -6,8 +6,9 @@ import lombok.Setter;
@Getter
@Setter
public class SalesCreateDto {
private String name;
private String comment;
private int price;
private String whId;
private String orderName;
private String orderComment;
private Integer orderQuantity;
}
package com.showcase.synapse.sales.entity;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import com.showcase.synapse.sales.enums.SalesStatus;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;
@Entity
@Getter
@Setter
......@@ -16,8 +19,19 @@ import java.math.BigDecimal;
@NoArgsConstructor
public class SalesEntity {
@Id
private String id;
private String name;
private String comment;
private BigDecimal price;
private String salesOrderId;
private String whId;
private String orderName;
private String orderComment;
private Integer orderQuantity;
@Enumerated(EnumType.STRING)
private SalesStatus orderStatus;
public void cancelOrder() {
this.orderStatus = SalesStatus.CANCELED;
if(this.orderComment != null && !this.orderComment.isEmpty()) {
this.orderComment.concat(" : order["+salesOrderId+"], ["+orderName+"] cancled by over qty.");
}
}
}
package com.showcase.synapse.sales.event;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class SalesCanceledEvent {
private String salesOrderId;
}
......@@ -8,8 +8,9 @@ import java.math.BigDecimal;
@AllArgsConstructor
@Getter
public class SalesCreatedEvent {
private String productId;
private String name;
private String comment;
private BigDecimal price;
private final String salesOrderId;
private final String whId;
private final String orderName;
private final String orderComment;
private final Integer orderQuantity;
}
......@@ -6,6 +6,7 @@ import lombok.Getter;
@AllArgsConstructor
@Getter
public class SalesQuantityChangedEvent {
private String productId;
private int quantity;
private String salesOrderId;
private String whId;
private int orderQuantity;
}
......@@ -6,6 +6,7 @@ import org.axonframework.eventhandling.EventHandler;
import org.springframework.stereotype.Component;
import com.showcase.synapse.sales.entity.SalesEntity;
import com.showcase.synapse.sales.enums.SalesStatus;
import com.showcase.synapse.sales.event.SalesCreatedEvent;
import com.showcase.synapse.sales.event.SalesQuantityChangedEvent;
import com.showcase.synapse.sales.repository.SalesRepository;
......@@ -15,31 +16,33 @@ import com.showcase.synapse.sales.repository.SalesRepository;
@Slf4j
public class SalesEventHandler {
private final SalesRepository productRepository;
private final SalesRepository salesRepository;
@EventHandler
protected void saveProduct(SalesCreatedEvent productCreatedEvent) {
protected void saveSalesOrder(SalesCreatedEvent productCreatedEvent) {
log.info("SalesCreatedEvent 이벤트 받음?");
log.info("SalesEventHandler > [SalesCreatedEvent] productCreatedEvent");
SalesEntity productEntity = new SalesEntity();
productEntity.setId(productCreatedEvent.getProductId());
productEntity.setName(productCreatedEvent.getName());
productEntity.setComment(productCreatedEvent.getComment());
productEntity.setPrice(productCreatedEvent.getPrice());
log.info("SalesEventHandler > [SalesCreatedEvent] SalesCreatedEvent");
SalesEntity salesEntity = new SalesEntity();
salesEntity.setSalesOrderId(productCreatedEvent.getSalesOrderId());
salesEntity.setWhId(productCreatedEvent.getWhId());
salesEntity.setOrderName(productCreatedEvent.getOrderName());
salesEntity.setOrderComment(productCreatedEvent.getOrderComment());
salesEntity.setOrderQuantity(productCreatedEvent.getOrderQuantity());
salesEntity.setOrderStatus(SalesStatus.CREATED);
productRepository.save(productEntity);
salesRepository.save(salesEntity);
}
@EventHandler
protected void changeQuantity(SalesQuantityChangedEvent productQuantityChangedEvent) {
protected void changeOrderQuantity(SalesQuantityChangedEvent salesQuantityChangedEvent) {
log.info("SalesQuantityChangedEvent 이벤트 받음?");
log.info("[SalesQuantityChangedEvent]");
SalesEntity productEntity = productRepository.findById(productQuantityChangedEvent.getProductId()).get();
SalesEntity salesEntity = salesRepository.findById(salesQuantityChangedEvent.getSalesOrderId()).get();
// log.info("[{}] quantity:{}", productEntity.getName(), productEntity.getQuentity());
// productEntity.setQuentity(productQuantityChangedEvent.getQuantity());
productRepository.save(productEntity);
salesRepository.save(salesEntity);
}
}
package com.showcase.synapse.sales.saga;
import org.axonframework.commandhandling.CommandCallback;
import org.axonframework.commandhandling.CommandMessage;
import org.axonframework.commandhandling.CommandResultMessage;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.modelling.saga.SagaEventHandler;
import org.axonframework.modelling.saga.StartSaga;
import org.axonframework.spring.stereotype.Saga;
import org.springframework.beans.factory.annotation.Autowired;
import com.showcase.synapse.common.command.CancleSalesCommand;
import com.showcase.synapse.common.command.ChangeInventoryQuantityCommand;
import com.showcase.synapse.sales.event.SalesCreatedEvent;
import lombok.extern.slf4j.Slf4j;
//@Saga
@Saga
@Slf4j
public class SalesSaga {
@Autowired
private transient CommandGateway commandGateway;
@StartSaga
@SagaEventHandler(associationProperty = "salesOrderId")
public void createOrder(SalesCreatedEvent event) {
log.info("[SalesSaga] Sales saga");
log.info("event.getSalesOrderId() === "+event.getSalesOrderId());
log.info("event.getOrderComment() === "+event.getOrderComment());
log.info("event.getOrderQuantity() === "+event.getOrderQuantity());
ChangeInventoryQuantityCommand ciqc = new ChangeInventoryQuantityCommand(event.getSalesOrderId(), event.getWhId(), event.getOrderComment(), event.getOrderQuantity());
commandGateway.send(ciqc, new CommandCallback<ChangeInventoryQuantityCommand, Object>() {
@Override
public void onResult(CommandMessage<? extends ChangeInventoryQuantityCommand> commandMessage, CommandResultMessage<?> commandResultMessage) {
log.info("ChangeInventoryQuantityCommand onResult commandResultMessage.isExceptional() = "+commandResultMessage.isExceptional());
if(commandResultMessage.isExceptional()){
// 보상 transaction
log.info("[주문 qty over에 대한 보상Transaction -> 주문은 취소] cancel order");
commandGateway.send(new CancleSalesCommand(event.getSalesOrderId(), event.getOrderQuantity()));
}
}
});
}
}
package com.showcase.synapse.sales.service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
......@@ -11,7 +12,7 @@ import org.axonframework.messaging.responsetypes.ResponseTypes;
import org.axonframework.queryhandling.QueryGateway;
import org.springframework.stereotype.Service;
import com.showcase.synapse.sales.command.CreateSalesCommand;
import com.showcase.synapse.common.command.CreateSalesCommand;
import com.showcase.synapse.sales.dto.ProductSalesDto;
import com.showcase.synapse.sales.entity.ProductEntity;
import com.showcase.synapse.sales.entity.SalesEntity;
......@@ -32,16 +33,16 @@ public class SalesService {
this.queryGateway = queryGateway;
}
public String createProduct(String name, String comment, BigDecimal price) {
log.info("[@Service createProduct] new CreateSalesCommand");
public String createSales(String whId, String orderName, String orderComment, Integer orderQuantity) {
log.info("[@Service createSales] new CreateSalesCommand");
// command생성
CreateSalesCommand createProductCommand = new CreateSalesCommand(
UUID.randomUUID().toString(), name, comment, price
CreateSalesCommand createSalesCommand = new CreateSalesCommand(
UUID.randomUUID().toString(), whId, orderName, orderComment, orderQuantity
);
System.out.println("test");
// 여기
// 생성한 command전송(비동기)
String returnValue = commandGateway.sendAndWait(createProductCommand);
log.info("[@createSalesCommand] 전송");
String returnValue = commandGateway.sendAndWait(createSalesCommand);
System.out.printf("returnValue: %s \n", returnValue);
return returnValue;
}
......@@ -68,7 +69,7 @@ public class SalesService {
dto.setProductComment(tempPE.getComment());
dto.setOrgPrice(tempPE.getPrice());
dto.setDiscountRate(tempPE.getChannel().getDiscountRate());
dto.setRatePrice(tempPE.getPrice().multiply(tempPE.getChannel().getDiscountRate().add(new BigDecimal(1))));
dto.setRatePrice(tempPE.getPrice().multiply((new BigDecimal(1)).subtract(tempPE.getChannel().getDiscountRate())).setScale(2, RoundingMode.DOWN));
dto.setSalesChannel(tempPE.getChannel().getName());
dto.setWhId(tempPE.getInventory().getWhid());
dto.setWhName(tempPE.getInventory().getWhName());
......
#axon:
# serializer:
# general: jackson
# events: jackson
# messages: jackson
axon:
serializer:
general: xstream
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment