WxAppServer/src/main/java/com/bigdata/wxappserver/service/OrderService.java

115 lines
4.4 KiB
Java
Raw Normal View History

2024-08-28 20:47:20 +08:00
package com.bigdata.wxappserver.service;
import com.alibaba.fastjson.JSONObject;
2024-08-29 16:46:49 +08:00
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
2024-08-28 20:47:20 +08:00
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.wxappserver.dto.OrderDto;
import com.bigdata.wxappserver.entity.Goods;
2024-08-28 20:47:20 +08:00
import com.bigdata.wxappserver.entity.Order;
2024-08-29 23:50:44 +08:00
import com.bigdata.wxappserver.entity.User;
2024-08-29 16:46:49 +08:00
import com.bigdata.wxappserver.enums.OrderStatusEnum;
2024-08-28 20:47:20 +08:00
import com.bigdata.wxappserver.mapper.OrderMapper;
import org.springframework.beans.BeanUtils;
2024-08-29 23:50:44 +08:00
import org.springframework.beans.factory.annotation.Autowired;
2024-08-28 20:47:20 +08:00
import org.springframework.stereotype.Service;
2024-08-29 16:46:49 +08:00
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
2024-08-29 23:50:44 +08:00
import org.springframework.util.StringUtils;
import java.util.*;
import java.util.stream.Collectors;
2024-08-28 20:47:20 +08:00
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/20:28
* @Description:
*/
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {
2024-08-29 23:50:44 +08:00
@Autowired
UserService userService;
@Autowired
GoodsService goodsService;
2024-08-29 16:46:49 +08:00
@Transactional(rollbackFor = Exception.class)
2024-08-28 20:47:20 +08:00
public JSONObject addOrUpdate(JSONObject jsonObject) {
Order order = jsonObject.toJavaObject(Order.class);
2024-08-29 23:50:44 +08:00
if (order == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误");
}
if (!StringUtils.hasLength(order.getAddress())) {
User user = userService.getById(order.getUserId());
order.setAddress(user.getAddress());
2024-08-28 20:47:20 +08:00
}
// TODO 判空
boolean success = saveOrUpdate(order);
2024-08-29 23:50:44 +08:00
return new JSONObject().fluentPut("message", "success").fluentPut("success", success);
2024-08-28 20:47:20 +08:00
}
2024-08-29 16:46:49 +08:00
2024-08-29 23:50:44 +08:00
public JSONObject list(JSONObject jsonObject) {
2024-08-30 15:24:39 +08:00
Integer userId = jsonObject.getInteger("userId");
2024-08-29 23:50:44 +08:00
// Integer currentPage = jsonObject.getInteger("currentPage");
// Integer pageSize = jsonObject.getInteger("pageSize");
// IPage<Order> page = new Page<>(currentPage, pageSize);
LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(userId != null, Order::getUserId, userId);
// IPage<Order> orderIPage = page(page, wrapper);
List<Order> list = list(wrapper);
List<OrderDto> orderDtoList = dealList(list);
2024-08-29 23:50:44 +08:00
JSONObject returnData = new JSONObject();
returnData.fluentPut("data", orderDtoList);
2024-08-29 16:46:49 +08:00
return returnData;
}
private List<OrderDto> dealList(List<Order> records) {
2024-08-29 23:50:44 +08:00
if (CollectionUtils.isEmpty(records)) {
return new ArrayList<>();
2024-08-29 16:46:49 +08:00
}
List<Goods> goodsList = goodsService.listByIds(records.stream().map(Order::getGoodsId).collect(Collectors.toSet()));
Set<Goods> goodsSet =new HashSet<>(goodsList);
List<OrderDto> list =new ArrayList<>();
2024-08-29 16:46:49 +08:00
for (Order record : records) {
OrderDto orderDto=new OrderDto();
2024-08-29 16:46:49 +08:00
record.setStatusDescription(OrderStatusEnum.describe(record.getStatus()));
BeanUtils.copyProperties(record,orderDto);
Goods goods = goodsSet.stream().filter(item -> Objects.equals(item.getId(), record.getGoodsId()))
.findFirst().orElse(null);
if(goods!=null){
orderDto.setGoodDetail(goods.getGoodDetail());
orderDto.setGoodPrice(goods.getGoodPrice());
orderDto.setGoodImage(goods.getGoodImage());
orderDto.setGoodName(goods.getGoodName());
}
2024-08-29 16:46:49 +08:00
}
return list;
2024-08-29 16:46:49 +08:00
}
2024-08-29 23:50:44 +08:00
public JSONObject queryById(Integer id) {
if (id == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少ID");
}
Order order = getById(id);
List<OrderDto> orderDtoList = dealList(Collections.singletonList(order));
return new JSONObject().fluentPut("data", orderDtoList.get(0));
2024-08-29 23:50:44 +08:00
}
@Transactional(rollbackFor = Exception.class)
public JSONObject delete(Integer id) {
if (id == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少Id");
}
boolean success = removeById(id);
if (success) {
return new JSONObject().fluentPut("success", true);
} else {
return new JSONObject().fluentPut("success", false).fluentPut("message", "出现未知错误,请联系管理员!");
}
}
2024-08-28 20:47:20 +08:00
}