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

170 lines
6.5 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;
2024-09-03 00:34:51 +08:00
import com.bigdata.wxappserver.result.Result;
import com.bigdata.wxappserver.utils.WeChatPayUtil;
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;
2024-09-03 00:34:51 +08:00
import java.math.BigDecimal;
import java.time.LocalDateTime;
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-09-03 00:34:51 +08:00
@Autowired
private WeChatPayUtil weChatPayUtil;
2024-08-29 23:50:44 +08:00
@Autowired
UserService userService;
@Autowired
GoodsService goodsService;
2024-09-03 00:34:51 +08:00
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-09-03 00:34:51 +08:00
String openId = jsonObject.getString("openId");
List<User> userList = userService.list();
User user = userList.stream().filter(e -> Objects.equals(e.getOpenId(), openId)).findFirst().orElse(null);
2024-09-01 02:51:50 +08:00
Integer status = jsonObject.getInteger("status");
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<>();
2024-09-03 00:34:51 +08:00
wrapper.eq(user != null, Order::getUserId, user.getId())
.eq(status != null, Order::getStatus, status);
2024-08-29 23:50:44 +08:00
// 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()));
2024-09-03 00:34:51 +08:00
Set<Goods> goodsSet = new HashSet<>(goodsList);
List<OrderDto> list = new ArrayList<>();
2024-08-29 16:46:49 +08:00
for (Order record : records) {
2024-09-03 00:34:51 +08:00
OrderDto orderDto = new OrderDto();
2024-08-29 16:46:49 +08:00
record.setStatusDescription(OrderStatusEnum.describe(record.getStatus()));
2024-09-03 00:34:51 +08:00
BeanUtils.copyProperties(record, orderDto);
Goods goods = goodsSet.stream().filter(item -> Objects.equals(item.getId(), record.getGoodsId()))
.findFirst().orElse(null);
2024-09-03 00:34:51 +08:00
if (goods != null) {
orderDto.setGoodDetail(goods.getGoodDetail());
orderDto.setGoodPrice(goods.getGoodPrice());
orderDto.setGoodImage(goods.getGoodImage());
orderDto.setGoodName(goods.getGoodName());
}
2024-09-01 02:51:50 +08:00
list.add(orderDto);
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-09-03 00:34:51 +08:00
public void paySuccess(String outTradeNo) {
List<Order> list = list();
Order order = list.stream().filter(e -> Objects.equals(String.valueOf(e.getId()), outTradeNo)).findFirst().orElse(null);
if (order != null) {
order.setStatus(2);
}
saveOrUpdate(order);
}
/**
* 订单支付
*
* @param ordersPaymentDTO
* @return
*/
public Result payment(JSONObject ordersPaymentDTO) throws Exception {
// 当前登录用户id
String openId = ordersPaymentDTO.getString("openId");
String orderCode = ordersPaymentDTO.getString("orderCode");
List<User> userList = userService.list();
User user = userList.stream().filter(e -> Objects.equals(e.getOpenId(), openId)).findFirst().orElse(null);
if (user == null) {
return Result.error("查询不到该用户");
}
//调用微信支付接口,生成预支付交易单
JSONObject jsonObject = weChatPayUtil.pay(
orderCode, //商户订单号
new BigDecimal(0.01), //支付金额,单位 元
"Test123", //商品描述
user.getOpenId() //微信用户的openid
);
if (jsonObject.getString("code") != null && jsonObject.getString("code").equals("ORDERPAID")) {
return Result.error("该订单已支付");
}
// OrderPaymentVO vo = jsonObject.toJavaObject(OrderPaymentVO.class);
// vo.setPackageStr(jsonObject.getString("package"));
//
// return vo;
return Result.success();
}
2024-08-28 20:47:20 +08:00
}