敏捷工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.1 KiB

2 years ago
  1. package com.whn.hellospring.service;
  2. import com.whn.hellospring.model.*;
  3. import com.whn.hellospring.repository.CoffeeOrderRepository;
  4. import com.whn.hellospring.repository.CoffeeRepository;
  5. import com.whn.hellospring.utils.DO2DTOUtil;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.joda.money.CurrencyUnit;
  8. import org.joda.money.Money;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Service;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. @Slf4j
  14. @Service
  15. public class CoffeeOrderService {
  16. @Autowired
  17. private CoffeeOrderRepository orderRepository;
  18. @Autowired
  19. private CoffeeRepository coffeeRepository;
  20. /**
  21. * 删除一个订单
  22. */
  23. public void deleteOrderById(Long id) {
  24. //删除订单
  25. orderRepository.deleteOrderWithId(id);
  26. //删除订单下的咖啡
  27. coffeeRepository.deleteCoffeeByOrderId(id);
  28. }
  29. /**
  30. * 创建一个订单
  31. */
  32. public void createOrder(CoffeeOrderDTO coffeeOrderDTO) {
  33. //保存订单
  34. CoffeeOrderDO order = (CoffeeOrderDO) DO2DTOUtil.getInstance().dtoToDo(coffeeOrderDTO, CoffeeOrderDO.class);
  35. order.setState(OrderState.INIT);
  36. orderRepository.save(order);
  37. //保存咖啡
  38. if (coffeeOrderDTO.getItems() != null && coffeeOrderDTO.getItems().size() != 0) {
  39. List<CoffeeDTO> list = coffeeOrderDTO.getItems();
  40. for (CoffeeDTO coffeeDTO : list) {
  41. CoffeeDO coffeeDO = (CoffeeDO) DO2DTOUtil.getInstance().dtoToDo(coffeeDTO, CoffeeDO.class);
  42. coffeeDO.setOrder_id_fk(order.getId());
  43. coffeeDO.setPrice(Money.of(CurrencyUnit.of("CNY"), coffeeDTO.getPrice()));
  44. coffeeRepository.save(coffeeDO);
  45. }
  46. }
  47. }
  48. /**
  49. * 获取一个订单信息
  50. */
  51. public CoffeeOrderDTO getOrderById(Long id) {
  52. //获取订单主体
  53. CoffeeOrderDO orderDO = orderRepository.queryOrderWithId(id);
  54. CoffeeOrderDTO orderDTO = (CoffeeOrderDTO) DO2DTOUtil.getInstance().doToDto(orderDO, CoffeeOrderDTO.class);
  55. //获取订单下的咖啡列表
  56. List<CoffeeDO> coffeeList = coffeeRepository.selectCoffeeListWithOrder(id);
  57. if (coffeeList != null && coffeeList.size() != 0) {
  58. List<CoffeeDTO> coffeeDTOList = new ArrayList<>();
  59. for (CoffeeDO coffeeDo : coffeeList) {
  60. CoffeeDTO dto = new CoffeeDTO(coffeeDo);
  61. coffeeDTOList.add(dto);
  62. }
  63. orderDTO.setItems(coffeeDTOList);
  64. }
  65. return orderDTO;
  66. }
  67. /**
  68. * 修改订单状态
  69. */
  70. public boolean updateState(CoffeeOrderDO order, OrderState state) {
  71. if (state.compareTo(order.getState()) <= 0) {
  72. log.warn("Wrong state order{},{}", state, order.getState());
  73. return false;
  74. }
  75. order.setState(state);
  76. orderRepository.save(order);
  77. log.info("update Order{}", order);
  78. return false;
  79. }
  80. /**
  81. * 根据客户id,获取订单列表
  82. */
  83. public List<CoffeeOrderDTO> getOrderListByCustomerId(String customerId) {
  84. if (customerId.isEmpty()) return null;
  85. List<CoffeeOrderDTO> orderDTOList = new ArrayList<>();
  86. List<CoffeeOrderDO> orderDOList = orderRepository.queryOrderListWithCustomerId(customerId);
  87. for (CoffeeOrderDO orderDO : orderDOList) {
  88. CoffeeOrderDTO orderDTO = (CoffeeOrderDTO) DO2DTOUtil.getInstance().doToDto(orderDO, CoffeeOrderDTO.class);
  89. //获取订单下的咖啡列表
  90. List<CoffeeDO> coffeeList = coffeeRepository.selectCoffeeListWithOrder(orderDO.getId());
  91. if (coffeeList != null && coffeeList.size() != 0) {
  92. List<CoffeeDTO> coffeeDTOList = new ArrayList<>();
  93. for (CoffeeDO coffeeDo : coffeeList) {
  94. CoffeeDTO dto = new CoffeeDTO(coffeeDo);
  95. coffeeDTOList.add(dto);
  96. }
  97. orderDTO.setItems(coffeeDTOList);
  98. }
  99. orderDTOList.add(orderDTO);
  100. }
  101. return orderDTOList;
  102. }
  103. }