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

102 lines
3.6 KiB
Java

package com.bigdata.wxappserver.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.wxappserver.entity.Goods;
import com.bigdata.wxappserver.entity.Order;
import com.bigdata.wxappserver.entity.User;
import com.bigdata.wxappserver.enums.OrderStatusEnum;
import com.bigdata.wxappserver.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Collections;
import java.util.List;
import static com.fasterxml.jackson.databind.type.LogicalType.Collection;
/**
* Created with IntelliJ IDEA.
*
* @Author: Cool
* @Date: 2024/08/28/20:28
* @Description:
*/
@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> {
@Autowired
UserService userService;
@Transactional(rollbackFor = Exception.class)
public JSONObject addOrUpdate(JSONObject jsonObject) {
Order order = jsonObject.toJavaObject(Order.class);
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());
}
// TODO 判空
boolean success = saveOrUpdate(order);
return new JSONObject().fluentPut("message", "success").fluentPut("success", success);
}
public JSONObject list(JSONObject jsonObject) {
String userId = jsonObject.getString("userId");
// 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);
dealList(list);
JSONObject returnData = new JSONObject();
returnData.fluentPut("data", list);
return returnData;
}
private void dealList(List<Order> records) {
if (CollectionUtils.isEmpty(records)) {
return;
}
for (Order record : records) {
record.setStatusDescription(OrderStatusEnum.describe(record.getStatus()));
}
}
public JSONObject queryById(Integer id) {
if (id == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少ID");
}
Order order = getById(id);
dealList(Collections.singletonList(order));
return new JSONObject().fluentPut("data", order);
}
@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", "出现未知错误,请联系管理员!");
}
}
}