1.将api中的WXPay依赖移至service的pom.xml中。
2.
OrderService:
OrderService:
public interface OrderService {
public Map<String,String> addOrder(String cids, Orders order) throws SQLException;
public int updateOrderStatus(String orderId,String status);
public ResultVO GetOrderById(String orderId);
public void closeOrder(String orderId);
}
OrderServiceImpl:
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private ShoppingCartMapper shoppingCartMapper;
@Autowired
private OrdersMapper ordersMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private ProductSkuMapper productSkuMapper;
//保存订单业务
@Transactional//事务
public Map<String,String> addOrder(String cids,Orders order) throws SQLException{
Map<String,String> map = new HashMap<>();
//1.根据cids查询当前订单中关联的购物车记录详情(包括库存)
String[] arr = cids.split(",");
List<Integer> cidsList = new ArrayList<>();
for (int i=0; i<arr.length;i++){
cidsList.add(Integer.parseInt(arr[i]));
}
List<ShoppingCartVO> list = shoppingCartMapper.selectShopcartByCids(cidsList);
//2.校验库存
boolean f = true;
String untitled = "";
for (ShoppingCartVO sc:list){
if (Integer.parseInt( sc.getCartNum() )>sc.getSkuStock()){
f=false;
//break;
}
//获取所有商品的名称,以“,”分割拼接成字符串
untitled = untitled + sc.getProductName() + " ";
}
if (f){
//3.表示用户充足----------保存订单
order.setUntitled(untitled);
order.setCreateTime(new Date());
order.setStatus("1");
//生成订单编号
String orderId = UUID.randomUUID().toString().replace("-", "");
order.setOrderId(orderId);
//4.保存订单
ordersMapper.insert(order);
//if (i>0){
//生成商品快照
//List<OrderItem> orderItems = new ArrayList<>();
for (ShoppingCartVO sc:list){
int cnum = Integer.parseInt(sc.getCartNum());
String itemId = System.currentTimeMillis()+""+ (new Random().nextInt(89999)+10000 );
OrderItem orderItem = new OrderItem(itemId, orderId, sc.getProductId(), sc.getProductName(), sc.getProductImg(),
sc.getSkuId(), sc.getSkuName(),new BigDecimal(sc.getSellPrice()) , cnum,
new BigDecimal(sc.getSellPrice() * cnum) , new Date(), new Date(), 0);
//orderItems.add(orderItem);
orderItemMapper.insert(orderItem);
}
//int j = orderItemMapper.insertList(orderItems);
//5.扣减库存
for (ShoppingCartVO sc:list){
String skuId = sc.getSkuId();
int newStock = sc.getSkuStock() - Integer.parseInt(sc.getCartNum());
// Example example = new Example(ProductSku.class);
// Example.Criteria criteria = example.createCriteria();
// criteria.andEqualTo("skuId",skuId);
// ProductSku productSku = productSkuMapper.selectByPrimaryKey(skuId);
// //ProductSku productSku = new ProductSku();
// productSku.setStock(newStock);
// int k = productSkuMapper.updateByPrimaryKey(productSku);
ProductSku productSku = new ProductSku();
productSku.setSkuId(skuId);
productSku.setStock(newStock);
productSkuMapper.updateByPrimaryKeySelective(productSku);
}
//6.删除购物车:当购物车中的记录购买成功后,购物车对应做删除操作
for (int cid:cidsList){
shoppingCartMapper.deleteByPrimaryKey(cid);
}
map.put("orderId",orderId);
map.put("productNames",untitled);
return map;
}else {
//表示库存不足
return null;
}
}
@Override
public int updateOrderStatus(String orderId, String status) {
Orders orders = new Orders();
orders.setOrderId(orderId);
orders.setStatus(status);
int i = ordersMapper.updateByPrimaryKeySelective(orders);
return i;
}
@Override
public ResultVO GetOrderById(String orderId) {
Orders orders = ordersMapper.selectByPrimaryKey(orderId);
return new ResultVO(ResStatus.OK,"success", orders.getStatus());
}
@Override
@Transactional(isolation public void closeOrder(String orderId) {
// b.修改当前订单status=6(订单关闭),close_type=1(超时未支付),
Orders cancleOrder = new Orders();
cancleOrder.setOrderId(orderId);
cancleOrder.setStatus("6");
cancleOrder.setCloseType(1);
ordersMapper.updateByPrimaryKeySelective(cancleOrder);
// c.还原库存:先根据当前订单编号查询商品快照(skuid buy_count)--->修改product_sku
Example example1 = new Example(OrderItem.class);
Example.Criteria criteria1 = example1.createCriteria();
criteria1.andEqualTo("orderId", orderId);
List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
//还原库存
for (int j = 0; j < orderItems.size(); j++) {
OrderItem orderItem = orderItems.get(j);
//修改
ProductSku productSku = productSkuMapper.selectByPrimaryKey(orderItem.getSkuId());
//productSku.setSkuId(orderItem.getSkuId());
productSku.setStock(productSku.getStock() + orderItem.getBuyCounts());
productSkuMapper.updateByPrimaryKey(productSku);
}
}
}
}
OrderTimeoutCheckJob:
@Component
public class OrderTimeoutCheckJob {
@Autowired
private OrdersMapper ordersMapper;
@Autowired
private OrderService orderService;
private WXPay wxPay = new WXPay(new MyPayConfig());
@Scheduled(cron = "0/5 * * * * ?")
public void checkAndCloseOrder(){
//1.查询超过30分钟,订单状态依然为待支付状态的订单
try {
Example example = new Example(Orders.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("status","1");
Date time = new Date(System.currentTimeMillis() - 30*60*1000);
criteria.andLessThan("createTime",time);
List<Orders> orders = ordersMapper.selectByExample(example);
//2.访问微信平台接口,确认当前订单最终的支付状态
for (int i = 0; i < orders.size(); i++) {
Orders order = orders.get(i);
HashMap<String, String> params = new HashMap<>();
params.put("out_trade_no", order.getOrderId());
Map<String, String> resp = wxPay.orderQuery(params);
if ("SUCCESS".equalsIgnoreCase(resp.get("trade_state"))){
//2.1如果订单已经支付则修改订单状态为已支付/待发货,status=2
Orders updateOrder = new Orders();
updateOrder.setOrderId(order.getOrderId());
updateOrder.setStatus("2");
ordersMapper.updateByPrimaryKeySelective(updateOrder);
}else if ("NOTPAY".equalsIgnoreCase(resp.get("trade_state"))){
//2.2如果确实未支付则取消订单:
// a.向微信支付平台发送请求关闭当前订单支付链接;
Map<String, String> map = wxPay.closeOrder(params);
System.out.println(map);
//关闭订单
orderService.closeOrder(order.getOrderId());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128082.html