/*
|
* Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
|
*
|
* Redistribution and use in source and binary forms, with or without
|
* modification, are permitted provided that the following conditions are met:
|
*
|
* Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* Redistributions in binary form must reproduce the above copyright
|
* notice, this list of conditions and the following disclaimer in the
|
* documentation and/or other materials provided with the distribution.
|
* Neither the name of the dreamlu.net developer nor the names of its
|
* contributors may be used to endorse or promote products derived from
|
* this software without specific prior written permission.
|
* Author: Chill 庄骞 (smallchill@163.com)
|
*/
|
package org.springblade.modules.pay.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springblade.common.enums.OrderStatus;
|
import org.springblade.modules.pay.entity.OrderInfoEntity;
|
import org.springblade.modules.pay.vo.OrderInfoVO;
|
import org.springblade.modules.pay.mapper.OrderInfoMapper;
|
import org.springblade.modules.pay.service.IOrderInfoService;
|
import org.springblade.modules.property.entity.PropertyChargeRecord;
|
import org.springblade.modules.property.service.IPropertyChargeRecordService;
|
import org.springframework.stereotype.Service;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
import javax.annotation.Resource;
|
import java.text.SimpleDateFormat;
|
import java.time.Duration;
|
import java.time.Instant;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Random;
|
|
/**
|
* 订单信息 服务实现类
|
*
|
* @author BladeX
|
* @since 2024-04-07
|
*/
|
@Service
|
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfoEntity> implements IOrderInfoService {
|
|
private static Logger logger = LoggerFactory.getLogger(OrderInfoServiceImpl.class);
|
|
|
@Resource
|
private IPropertyChargeRecordService iPropertyChargeRecordService;
|
|
@Override
|
public IPage<OrderInfoVO> selectOrderInfoPage(IPage<OrderInfoVO> page, OrderInfoVO orderInfo) {
|
return page.setRecords(baseMapper.selectOrderInfoPage(page, orderInfo));
|
}
|
|
|
@Override
|
public OrderInfoEntity createOrderByPropertyChargeRecordId(Long propertyChargeRecordId) {
|
|
//查找已存在但未支付的订单
|
OrderInfoEntity orderInfo = this.getNoPayOrderByPropertyChargeRecordId(propertyChargeRecordId);
|
if( orderInfo != null){
|
return orderInfo;
|
}
|
//获取商品信息
|
PropertyChargeRecord propertyChargeRecord = iPropertyChargeRecordService.getById(propertyChargeRecordId);
|
//生成订单
|
orderInfo = new OrderInfoEntity();
|
orderInfo.setTitle(propertyChargeRecord.getPayContent());
|
orderInfo.setOrderNo(getOrderNoStr()); //订单号
|
orderInfo.setProperChargeRecordId(propertyChargeRecordId);
|
orderInfo.setTotalFee(propertyChargeRecord.getPayPrice().intValue()); //分
|
orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());
|
baseMapper.insert(orderInfo);
|
return orderInfo;
|
}
|
|
/**
|
* 存储订单二维码
|
* @param orderNo
|
* @param codeUrl
|
*/
|
@Override
|
public void saveCodeUrl(String orderNo, String codeUrl) {
|
|
QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("order_no", orderNo);
|
|
OrderInfoEntity orderInfo = new OrderInfoEntity();
|
orderInfo.setCodeUrl(codeUrl);
|
|
baseMapper.update(orderInfo, queryWrapper);
|
}
|
|
/**
|
* 查询订单列表,并倒序查询
|
* @return
|
*/
|
@Override
|
public List<OrderInfoEntity> listOrderByCreateTimeDesc() {
|
|
QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<OrderInfoEntity>().orderByDesc("create_time");
|
return baseMapper.selectList(queryWrapper);
|
}
|
|
/**
|
* 根据订单号更新订单状态
|
* @param orderNo
|
* @param orderStatus
|
*/
|
@Override
|
public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {
|
|
logger.info("更新订单状态 ===> {}", orderStatus.getType());
|
|
QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("order_no", orderNo);
|
|
OrderInfoEntity orderInfo = new OrderInfoEntity();
|
orderInfo.setOrderStatus(orderStatus.getType());
|
|
baseMapper.update(orderInfo, queryWrapper);
|
}
|
|
/**
|
* 根据订单号获取订单状态
|
* @param orderNo
|
* @return
|
*/
|
@Override
|
public String getOrderStatus(String orderNo) {
|
|
QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("order_no", orderNo);
|
OrderInfoEntity orderInfo = baseMapper.selectOne(queryWrapper);
|
if(orderInfo == null){
|
return null;
|
}
|
return orderInfo.getOrderStatus();
|
}
|
|
/**
|
* 查询创建超过minutes分钟并且未支付的订单
|
* @param minutes
|
* @return
|
*/
|
@Override
|
public List<OrderInfoEntity> getNoPayOrderByDuration(int minutes) {
|
|
Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
|
|
QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());
|
queryWrapper.le("create_time", instant);
|
|
List<OrderInfoEntity> orderInfoList = baseMapper.selectList(queryWrapper);
|
|
return orderInfoList;
|
}
|
|
/**
|
* 根据订单号获取订单
|
* @param orderNo
|
* @return
|
*/
|
@Override
|
public OrderInfoEntity getOrderByOrderNo(String orderNo) {
|
|
QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("order_no", orderNo);
|
OrderInfoEntity orderInfo = baseMapper.selectOne(queryWrapper);
|
|
return orderInfo;
|
}
|
|
|
/**
|
* 根据商品id查询未支付订单
|
* 防止重复创建订单对象
|
* @param propertyChargeRecordId
|
* @return
|
*/
|
private OrderInfoEntity getNoPayOrderByPropertyChargeRecordId(Long propertyChargeRecordId) {
|
|
QueryWrapper<OrderInfoEntity> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("product_id", propertyChargeRecordId);
|
queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());
|
// queryWrapper.eq("user_id", userId);
|
OrderInfoEntity orderInfo = baseMapper.selectOne(queryWrapper);
|
return orderInfo;
|
}
|
|
//生成订单号
|
public static String getOrderNoStr() {
|
String order = "pay" + new SimpleDateFormat("yyyyMMdd").format(new Date());
|
Random r = new Random();
|
for (int i = 0; i < 10; i++) {
|
order += r.nextInt(9);
|
}
|
return order;
|
}
|
}
|