OrdersController.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package com.example.controller;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.example.common.Result;
  7. import com.example.common.enums.OrderStatus;
  8. import com.example.entity.Orders;
  9. import com.example.entity.Product;
  10. import com.example.entity.SaleStatement;
  11. import com.example.service.OrdersService;
  12. import com.example.service.SaleStatementService;
  13. import com.github.pagehelper.PageInfo;
  14. import org.springframework.web.bind.annotation.*;
  15. import javax.annotation.Resource;
  16. import java.util.ArrayList;
  17. import java.util.LinkedHashMap;
  18. import java.util.List;
  19. /**
  20. * Orders订单表接口
  21. **/
  22. @RestController
  23. @RequestMapping("/orders")
  24. public class OrdersController {
  25. @Resource
  26. private OrdersService ordersService;
  27. @Resource
  28. private SaleStatementService saleStatementService;
  29. /**
  30. * 获取店铺未完成的订单个数
  31. * @param shopId
  32. * @return
  33. */
  34. @GetMapping("getnums/{shopId}")
  35. public Result getNums(@PathVariable Integer shopId){
  36. return Result.success(ordersService.getNumsWithShopId(shopId));
  37. }
  38. /**
  39. * 创建订单返回生成的订单信息
  40. * 传参:
  41. * @param orders
  42. * @return 订单信息
  43. */
  44. @PostMapping("createOrder")
  45. public Result createOrder(@RequestBody Orders orders){
  46. return Result.success(ordersService.add(orders));
  47. }
  48. /**
  49. * 商家修改状态为订单已送达
  50. * 传参 : id
  51. * @param orders {id 只需订单id}
  52. * @return null
  53. */
  54. @PostMapping("setOrdersStateArrived")
  55. public Result setOrdersArrived(@RequestBody Orders orders){
  56. orders.setOrderState(String.valueOf(OrderStatus.ARRIVED));
  57. ordersService.updateById(orders);
  58. Orders dbOrders = ordersService.selectById(orders.getId());
  59. // 添加到报表信息中去
  60. // 构建报表信息
  61. // List<Product> orderProductsLists = dbOrders.getOrderProductsLists(); // 新加入的产品
  62. // String jsonString1 = JSON.toJSONString(orderProductsLists); // 将对象转换成json格式数据
  63. // JSONArray jsonObject = JSON.parseArray(jsonString1); // 在转回去
  64. // List<Product> orderProductsListsTrue = JSON.parseObject(jsonObject.getString("list"), Product.class); // 这样就可以了
  65. List<Product> orderProductsLists = dbOrders.getOrderProductsLists(); // 获取产品列表
  66. // 将产品列表转换为 JSON 字符串
  67. String jsonString = JSON.toJSONString(orderProductsLists);
  68. // 将 JSON 字符串解析为 JSONArray
  69. JSONArray jsonArray = JSON.parseArray(jsonString);
  70. // 创建一个用于存储 Product 对象的列表
  71. List<Product> orderProductsListsTrue = new ArrayList<>();
  72. // 遍历 JSONArray,将每个元素转换为 Product 对象并添加到新的列表中
  73. for (int i = 0; i < jsonArray.size(); i++) {
  74. JSONObject jsonObject = jsonArray.getJSONObject(i);
  75. Product product = jsonObject.toJavaObject(Product.class);
  76. orderProductsListsTrue.add(product);
  77. }
  78. // 现在 orderProductsListsTrue 就是包含正确类型的 Product 对象的列表
  79. orderProductsListsTrue.forEach(productMap -> {
  80. Product product = (Product) productMap; // 进行类型转换
  81. LinkedHashMap<String ,Object> t = new LinkedHashMap<>();
  82. SaleStatement saleStatement = new SaleStatement();
  83. saleStatement.setProductId(product.getId());
  84. saleStatement.setShopId(dbOrders.getStoreId());
  85. saleStatement.setAmount(product.getAmount());
  86. saleStatement.setCount(product.getCount());
  87. saleStatement.setTimeOver(DateUtil.now());
  88. saleStatementService.add(saleStatement);
  89. });
  90. // 一种产品一个报表
  91. // orderProductsLists.forEach(productMap -> {
  92. // Product product = (Product) productMap; // 进行类型转换
  93. // SaleStatement saleStatement = new SaleStatement();
  94. // saleStatement.setProductId(product.getId());
  95. // saleStatement.setShopId(orders.getStoreId());
  96. // saleStatement.setAmount(product.getAmount());
  97. // saleStatement.setCount(product.getCount());
  98. // saleStatement.setTimeOver(DateUtil.now());
  99. // saleStatementService.add(saleStatement);
  100. // });
  101. return Result.success(ordersService.selectById(orders.getId()));
  102. }
  103. /**
  104. * 商家修改状态为订单已完成
  105. * @param orders {id 只需订单id}
  106. * @return null
  107. */
  108. @PostMapping("ordersFinished")
  109. public Result setOrdersState(@RequestBody Orders orders){
  110. orders.setOrderState(String.valueOf(OrderStatus.DONE));
  111. ordersService.updateById(orders);
  112. ordersService.updateById(orders);
  113. return Result.success(ordersService.selectById(orders.getId()));
  114. }
  115. /**
  116. * 删除
  117. */
  118. @DeleteMapping("/delete/{id}")
  119. public Result deleteById(@PathVariable Integer id) {
  120. ordersService.deleteById(id);
  121. return Result.success();
  122. }
  123. /**
  124. * 批量删除
  125. */
  126. @DeleteMapping("/delete/batch")
  127. public Result deleteBatch(@RequestBody List<Integer> ids) {
  128. ordersService.deleteBatch(ids);
  129. return Result.success();
  130. }
  131. /**
  132. * 修改
  133. */
  134. @PutMapping("/update")
  135. public Result updateById(@RequestBody Orders orders) {
  136. ordersService.updateById(orders);
  137. return Result.success();
  138. }
  139. /**
  140. * 根据ID查询
  141. */
  142. @GetMapping("/selectById/{id}")
  143. public Result selectById(@PathVariable Integer id) {
  144. Orders orders = ordersService.selectById(id);
  145. return Result.success(orders);
  146. }
  147. /**
  148. * 查询所有
  149. */
  150. @GetMapping("/selectAll")
  151. public Result selectAll(Orders orders) {
  152. List<Orders> list = ordersService.selectAll(orders);
  153. return Result.success(list);
  154. }
  155. /**
  156. * 分页查询
  157. */
  158. @GetMapping("/selectPage")
  159. public Result selectPage(Orders orders,
  160. @RequestParam(defaultValue = "1") Integer pageNum,
  161. @RequestParam(defaultValue = "10") Integer pageSize) {
  162. PageInfo<Orders> page = ordersService.selectPage(orders, pageNum, pageSize);
  163. return Result.success(page);
  164. }
  165. }