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.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.axonframework.modelling.command.TargetAggregateIdentifier; import org.axonframework.modelling.command.TargetAggregateIdentifier;
import java.io.Serializable;
import java.math.BigDecimal; import java.math.BigDecimal;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public class CreateSalesCommand { public class CancleSalesCommand implements Serializable{
private static final long serialVersionUID = 1L;
@TargetAggregateIdentifier @TargetAggregateIdentifier
private final String id; private final String salesOrderId;
private final String name; private final Integer orderQuantity;
private final String comment;
private final BigDecimal price;
// private final Integer quantity;
} }
package com.showcase.synapse.sales.command; package com.showcase.synapse.common.command;
import java.io.Serializable;
import org.axonframework.modelling.command.TargetAggregateIdentifier; import org.axonframework.modelling.command.TargetAggregateIdentifier;
...@@ -7,9 +9,13 @@ import lombok.Getter; ...@@ -7,9 +9,13 @@ import lombok.Getter;
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public class ChangeSalesQuantityCommand { public class ChangeInventoryQuantityCommand implements Serializable{
private static final long serialVersionUID = 1L;
@TargetAggregateIdentifier @TargetAggregateIdentifier
private String productId; private String salesOrderId;
private int quantity; 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; ...@@ -2,8 +2,10 @@ package com.showcase.synapse.sales;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
@SpringBootApplication @SpringBootApplication
@Import({ AxonConfig.class })
public class MsaSalesApplication { public class MsaSalesApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package com.showcase.synapse.sales.aggregate; package com.showcase.synapse.sales.aggregate;
import lombok.NoArgsConstructor; import static org.axonframework.modelling.command.AggregateLifecycle.apply;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler; import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler; import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateIdentifier; import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.spring.stereotype.Aggregate; import org.axonframework.spring.stereotype.Aggregate;
import com.showcase.synapse.sales.command.ChangeSalesQuantityCommand; import com.showcase.synapse.common.command.CancleSalesCommand;
import com.showcase.synapse.sales.command.CreateSalesCommand; 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.SalesCreatedEvent;
import com.showcase.synapse.sales.event.SalesQuantityChangedEvent; import com.showcase.synapse.sales.event.SalesQuantityChangedEvent;
import com.showcase.synapse.sales.saga.SalesSaga;
import java.math.BigDecimal; import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static org.axonframework.modelling.command.AggregateLifecycle.apply;
@Aggregate @Aggregate
@NoArgsConstructor @NoArgsConstructor
...@@ -25,30 +24,39 @@ public class SalesAggregate { ...@@ -25,30 +24,39 @@ public class SalesAggregate {
@AggregateIdentifier @AggregateIdentifier
private String id; private String id;
// private int quantiy; private int orderQuantity;
private SalesStatus salesStatus;
@CommandHandler @CommandHandler
public SalesAggregate(CreateSalesCommand command) { public SalesAggregate(CreateSalesCommand command) {
log.info("[SalesAggregate(CreateSalesCommand) > apply new SalesCreatedEvent]"); 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 @EventSourcingHandler
public void createProduct(SalesCreatedEvent event) { public void createSalesOrder(SalesCreatedEvent event) {
this.id = event.getProductId(); this.id = event.getSalesOrderId();
// this.quantiy = event.getQuantity(); this.orderQuantity = event.getOrderQuantity();
this.salesStatus = SalesStatus.CREATED;
}
@EventSourcingHandler
public void cancelSalesOrder(SalesCanceledEvent event) {
this.salesStatus = SalesStatus.CANCELED;
} }
@CommandHandler @CommandHandler
public void changeQuantity(ChangeSalesQuantityCommand command) { public void cancelSalesOrder(CancleSalesCommand command) {
log.info("[@CommandHandler ProductchangeQuantity]"); log.info("[@CommandHandler cancelSalesOrder CancleSalesCommand]");
// if(this.quantiy < command.getQuantity()) throw new IllegalArgumentException("tfy "); // if(this.quantiy < command.getQuantity()) throw new IllegalArgumentException("tfy ");
// apply(new SalesQuantityChangedEvent(command.getProductId(), this.quantiy - command.getQuantity())); // apply(new SalesQuantityChangedEvent(command.getProductId(), this.quantiy - command.getQuantity()));
} }
@EventSourcingHandler @EventSourcingHandler
public void changeQuantity(SalesQuantityChangedEvent event) { public void changeSalesQuantity(SalesQuantityChangedEvent event) {
log.info("[@CommandHandler changeSalesQuantity SalesQuantityChangedEvent]");
// this.quantiy = event.getQuantity(); // this.quantiy = event.getQuantity();
} }
......
package com.showcase.synapse.sales.controller; package com.showcase.synapse.sales.controller;
import java.math.BigDecimal;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -16,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -16,7 +15,6 @@ import org.springframework.web.bind.annotation.RestController;
import com.showcase.synapse.sales.dto.ProductSalesDto; import com.showcase.synapse.sales.dto.ProductSalesDto;
import com.showcase.synapse.sales.dto.SalesCreateDto; 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.entity.SalesEntity;
import com.showcase.synapse.sales.service.SalesService; import com.showcase.synapse.sales.service.SalesService;
...@@ -44,13 +42,14 @@ public class SalesController { ...@@ -44,13 +42,14 @@ public class SalesController {
} }
@PostMapping("/") @PostMapping("/")
public ResponseEntity<Map<String, String>> productCreate(@RequestBody SalesCreateDto productCreateDto) { public ResponseEntity<Map<String, String>> salesCreate(@RequestBody SalesCreateDto salesCreateDto) {
String productId = salesService.createProduct(productCreateDto.getName(), String salesOrderId = salesService.createSales(salesCreateDto.getWhId(),
productCreateDto.getComment(), salesCreateDto.getOrderName(),
BigDecimal.valueOf(productCreateDto.getPrice())); salesCreateDto.getOrderComment(),
salesCreateDto.getOrderQuantity());
HashMap<String, String> m = new HashMap<>(); HashMap<String, String> m = new HashMap<>();
m.put("productId", productId); m.put("salesOrderId", salesOrderId);
m.put("productName", productCreateDto.getName()); m.put("orderQuantity", String.valueOf(salesCreateDto.getOrderQuantity()));
return ResponseEntity.ok(m); return ResponseEntity.ok(m);
} }
......
...@@ -6,8 +6,9 @@ import lombok.Setter; ...@@ -6,8 +6,9 @@ import lombok.Setter;
@Getter @Getter
@Setter @Setter
public class SalesCreateDto { public class SalesCreateDto {
private String name; private String whId;
private String comment; private String orderName;
private int price; private String orderComment;
private Integer orderQuantity;
} }
package com.showcase.synapse.sales.entity; 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.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.math.BigDecimal;
@Entity @Entity
@Getter @Getter
@Setter @Setter
...@@ -16,8 +19,19 @@ import java.math.BigDecimal; ...@@ -16,8 +19,19 @@ import java.math.BigDecimal;
@NoArgsConstructor @NoArgsConstructor
public class SalesEntity { public class SalesEntity {
@Id @Id
private String id; private String salesOrderId;
private String name; private String whId;
private String comment; private String orderName;
private BigDecimal price; 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; ...@@ -8,8 +8,9 @@ import java.math.BigDecimal;
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public class SalesCreatedEvent { public class SalesCreatedEvent {
private String productId; private final String salesOrderId;
private String name; private final String whId;
private String comment; private final String orderName;
private BigDecimal price; private final String orderComment;
private final Integer orderQuantity;
} }
...@@ -6,6 +6,7 @@ import lombok.Getter; ...@@ -6,6 +6,7 @@ import lombok.Getter;
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
public class SalesQuantityChangedEvent { public class SalesQuantityChangedEvent {
private String productId; private String salesOrderId;
private int quantity; private String whId;
private int orderQuantity;
} }
...@@ -6,6 +6,7 @@ import org.axonframework.eventhandling.EventHandler; ...@@ -6,6 +6,7 @@ import org.axonframework.eventhandling.EventHandler;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.showcase.synapse.sales.entity.SalesEntity; 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.SalesCreatedEvent;
import com.showcase.synapse.sales.event.SalesQuantityChangedEvent; import com.showcase.synapse.sales.event.SalesQuantityChangedEvent;
import com.showcase.synapse.sales.repository.SalesRepository; import com.showcase.synapse.sales.repository.SalesRepository;
...@@ -15,31 +16,33 @@ import com.showcase.synapse.sales.repository.SalesRepository; ...@@ -15,31 +16,33 @@ import com.showcase.synapse.sales.repository.SalesRepository;
@Slf4j @Slf4j
public class SalesEventHandler { public class SalesEventHandler {
private final SalesRepository productRepository; private final SalesRepository salesRepository;
@EventHandler @EventHandler
protected void saveProduct(SalesCreatedEvent productCreatedEvent) { protected void saveSalesOrder(SalesCreatedEvent productCreatedEvent) {
log.info("SalesCreatedEvent 이벤트 받음?"); log.info("SalesCreatedEvent 이벤트 받음?");
log.info("SalesEventHandler > [SalesCreatedEvent] productCreatedEvent"); log.info("SalesEventHandler > [SalesCreatedEvent] SalesCreatedEvent");
SalesEntity productEntity = new SalesEntity(); SalesEntity salesEntity = new SalesEntity();
productEntity.setId(productCreatedEvent.getProductId()); salesEntity.setSalesOrderId(productCreatedEvent.getSalesOrderId());
productEntity.setName(productCreatedEvent.getName()); salesEntity.setWhId(productCreatedEvent.getWhId());
productEntity.setComment(productCreatedEvent.getComment()); salesEntity.setOrderName(productCreatedEvent.getOrderName());
productEntity.setPrice(productCreatedEvent.getPrice()); salesEntity.setOrderComment(productCreatedEvent.getOrderComment());
salesEntity.setOrderQuantity(productCreatedEvent.getOrderQuantity());
productRepository.save(productEntity); salesEntity.setOrderStatus(SalesStatus.CREATED);
salesRepository.save(salesEntity);
} }
@EventHandler @EventHandler
protected void changeQuantity(SalesQuantityChangedEvent productQuantityChangedEvent) { protected void changeOrderQuantity(SalesQuantityChangedEvent salesQuantityChangedEvent) {
log.info("SalesQuantityChangedEvent 이벤트 받음?"); log.info("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()); // log.info("[{}] quantity:{}", productEntity.getName(), productEntity.getQuentity());
// productEntity.setQuentity(productQuantityChangedEvent.getQuantity()); // productEntity.setQuentity(productQuantityChangedEvent.getQuantity());
productRepository.save(productEntity); salesRepository.save(salesEntity);
} }
} }
package com.showcase.synapse.sales.saga; 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.commandhandling.gateway.CommandGateway;
import org.axonframework.modelling.saga.SagaEventHandler;
import org.axonframework.modelling.saga.StartSaga;
import org.axonframework.spring.stereotype.Saga; import org.axonframework.spring.stereotype.Saga;
import org.springframework.beans.factory.annotation.Autowired; 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; import lombok.extern.slf4j.Slf4j;
//@Saga @Saga
@Slf4j @Slf4j
public class SalesSaga { public class SalesSaga {
@Autowired @Autowired
private transient CommandGateway commandGateway; 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; package com.showcase.synapse.sales.service;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
...@@ -11,7 +12,7 @@ import org.axonframework.messaging.responsetypes.ResponseTypes; ...@@ -11,7 +12,7 @@ import org.axonframework.messaging.responsetypes.ResponseTypes;
import org.axonframework.queryhandling.QueryGateway; import org.axonframework.queryhandling.QueryGateway;
import org.springframework.stereotype.Service; 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.dto.ProductSalesDto;
import com.showcase.synapse.sales.entity.ProductEntity; import com.showcase.synapse.sales.entity.ProductEntity;
import com.showcase.synapse.sales.entity.SalesEntity; import com.showcase.synapse.sales.entity.SalesEntity;
...@@ -32,16 +33,16 @@ public class SalesService { ...@@ -32,16 +33,16 @@ public class SalesService {
this.queryGateway = queryGateway; this.queryGateway = queryGateway;
} }
public String createProduct(String name, String comment, BigDecimal price) { public String createSales(String whId, String orderName, String orderComment, Integer orderQuantity) {
log.info("[@Service createProduct] new CreateSalesCommand"); log.info("[@Service createSales] new CreateSalesCommand");
// command생성 // command생성
CreateSalesCommand createProductCommand = new CreateSalesCommand( CreateSalesCommand createSalesCommand = new CreateSalesCommand(
UUID.randomUUID().toString(), name, comment, price UUID.randomUUID().toString(), whId, orderName, orderComment, orderQuantity
); );
System.out.println("test");
// 여기 // 여기
// 생성한 command전송(비동기) // 생성한 command전송(비동기)
String returnValue = commandGateway.sendAndWait(createProductCommand); log.info("[@createSalesCommand] 전송");
String returnValue = commandGateway.sendAndWait(createSalesCommand);
System.out.printf("returnValue: %s \n", returnValue); System.out.printf("returnValue: %s \n", returnValue);
return returnValue; return returnValue;
} }
...@@ -68,7 +69,7 @@ public class SalesService { ...@@ -68,7 +69,7 @@ public class SalesService {
dto.setProductComment(tempPE.getComment()); dto.setProductComment(tempPE.getComment());
dto.setOrgPrice(tempPE.getPrice()); dto.setOrgPrice(tempPE.getPrice());
dto.setDiscountRate(tempPE.getChannel().getDiscountRate()); 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.setSalesChannel(tempPE.getChannel().getName());
dto.setWhId(tempPE.getInventory().getWhid()); dto.setWhId(tempPE.getInventory().getWhid());
dto.setWhName(tempPE.getInventory().getWhName()); dto.setWhName(tempPE.getInventory().getWhName());
......
#axon:
# serializer:
# general: jackson
# events: jackson
# messages: jackson
axon: axon:
serializer: serializer:
general: xstream 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