123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- package com.example.controller;
- import cn.hutool.core.date.DateUtil;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.example.common.Result;
- import com.example.common.enums.OrderStatus;
- import com.example.entity.Orders;
- import com.example.entity.Product;
- import com.example.entity.SaleStatement;
- import com.example.service.OrdersService;
- import com.example.service.SaleStatementService;
- import com.github.pagehelper.PageInfo;
- import org.springframework.web.bind.annotation.*;
- import javax.annotation.Resource;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.List;
- /**
- * Orders订单表接口
- **/
- @RestController
- @RequestMapping("/orders")
- public class OrdersController {
- @Resource
- private OrdersService ordersService;
- @Resource
- private SaleStatementService saleStatementService;
- /**
- * 获取店铺未完成的订单个数
- * @param shopId
- * @return
- */
- @GetMapping("getnums/{shopId}")
- public Result getNums(@PathVariable Integer shopId){
- return Result.success(ordersService.getNumsWithShopId(shopId));
- }
- /**
- * 创建订单返回生成的订单信息
- * 传参:
- * @param orders
- * @return 订单信息
- */
- @PostMapping("createOrder")
- public Result createOrder(@RequestBody Orders orders){
- return Result.success(ordersService.add(orders));
- }
- /**
- * 商家修改状态为订单已送达
- * 传参 : id
- * @param orders {id 只需订单id}
- * @return null
- */
- @PostMapping("setOrdersStateArrived")
- public Result setOrdersArrived(@RequestBody Orders orders){
- orders.setOrderState(String.valueOf(OrderStatus.ARRIVED));
- ordersService.updateById(orders);
- Orders dbOrders = ordersService.selectById(orders.getId());
- // 添加到报表信息中去
- // 构建报表信息
- // List<Product> orderProductsLists = dbOrders.getOrderProductsLists(); // 新加入的产品
- // String jsonString1 = JSON.toJSONString(orderProductsLists); // 将对象转换成json格式数据
- // JSONArray jsonObject = JSON.parseArray(jsonString1); // 在转回去
- // List<Product> orderProductsListsTrue = JSON.parseObject(jsonObject.getString("list"), Product.class); // 这样就可以了
- List<Product> orderProductsLists = dbOrders.getOrderProductsLists(); // 获取产品列表
- // 将产品列表转换为 JSON 字符串
- String jsonString = JSON.toJSONString(orderProductsLists);
- // 将 JSON 字符串解析为 JSONArray
- JSONArray jsonArray = JSON.parseArray(jsonString);
- // 创建一个用于存储 Product 对象的列表
- List<Product> orderProductsListsTrue = new ArrayList<>();
- // 遍历 JSONArray,将每个元素转换为 Product 对象并添加到新的列表中
- for (int i = 0; i < jsonArray.size(); i++) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- Product product = jsonObject.toJavaObject(Product.class);
- orderProductsListsTrue.add(product);
- }
- // 现在 orderProductsListsTrue 就是包含正确类型的 Product 对象的列表
- orderProductsListsTrue.forEach(productMap -> {
- Product product = (Product) productMap; // 进行类型转换
- LinkedHashMap<String ,Object> t = new LinkedHashMap<>();
- SaleStatement saleStatement = new SaleStatement();
- saleStatement.setProductId(product.getId());
- saleStatement.setShopId(dbOrders.getStoreId());
- saleStatement.setAmount(product.getAmount());
- saleStatement.setCount(product.getCount());
- saleStatement.setTimeOver(DateUtil.now());
- saleStatementService.add(saleStatement);
- });
- // 一种产品一个报表
- // orderProductsLists.forEach(productMap -> {
- // Product product = (Product) productMap; // 进行类型转换
- // SaleStatement saleStatement = new SaleStatement();
- // saleStatement.setProductId(product.getId());
- // saleStatement.setShopId(orders.getStoreId());
- // saleStatement.setAmount(product.getAmount());
- // saleStatement.setCount(product.getCount());
- // saleStatement.setTimeOver(DateUtil.now());
- // saleStatementService.add(saleStatement);
- // });
- return Result.success(ordersService.selectById(orders.getId()));
- }
- /**
- * 商家修改状态为订单已完成
- * @param orders {id 只需订单id}
- * @return null
- */
- @PostMapping("ordersFinished")
- public Result setOrdersState(@RequestBody Orders orders){
- orders.setOrderState(String.valueOf(OrderStatus.DONE));
- ordersService.updateById(orders);
- ordersService.updateById(orders);
- return Result.success(ordersService.selectById(orders.getId()));
- }
- /**
- * 删除
- */
- @DeleteMapping("/delete/{id}")
- public Result deleteById(@PathVariable Integer id) {
- ordersService.deleteById(id);
- return Result.success();
- }
- /**
- * 批量删除
- */
- @DeleteMapping("/delete/batch")
- public Result deleteBatch(@RequestBody List<Integer> ids) {
- ordersService.deleteBatch(ids);
- return Result.success();
- }
- /**
- * 修改
- */
- @PutMapping("/update")
- public Result updateById(@RequestBody Orders orders) {
- ordersService.updateById(orders);
- return Result.success();
- }
- /**
- * 根据ID查询
- */
- @GetMapping("/selectById/{id}")
- public Result selectById(@PathVariable Integer id) {
- Orders orders = ordersService.selectById(id);
- return Result.success(orders);
- }
- /**
- * 查询所有
- */
- @GetMapping("/selectAll")
- public Result selectAll(Orders orders) {
- List<Orders> list = ordersService.selectAll(orders);
- return Result.success(list);
- }
- /**
- * 分页查询
- */
- @GetMapping("/selectPage")
- public Result selectPage(Orders orders,
- @RequestParam(defaultValue = "1") Integer pageNum,
- @RequestParam(defaultValue = "10") Integer pageSize) {
- PageInfo<Orders> page = ordersService.selectPage(orders, pageNum, pageSize);
- return Result.success(page);
- }
- }
|