Przeglądaj źródła

[订单][修改]:订单order,couponOrder修改,订单表增加couponId字段,记录购买产品

release_toaliyun_real
Stormeye.Wu 7 lat temu
rodzic
commit
b46ba841bb
9 zmienionych plików z 327 dodań i 228 usunięć
  1. +10
    -1
      mallinkBApi/src/main/java/com/simple/controller/WxOrderController.java
  2. +38
    -6
      mallinkCApi/src/main/java/com/simple/controller/WxOrderController.java
  3. +1
    -2
      mallinkCApi/src/main/java/com/simple/controller/WxPayOrderController.java
  4. +12
    -0
      mallinkService/src/main/java/com/simple/domain/po/WxOrder.java
  5. +1
    -5
      mallinkService/src/main/java/com/simple/enums/EnumCouponStatus.java
  6. +10
    -3
      mallinkService/src/main/java/com/simple/service/WxOrderService.java
  7. +210
    -178
      mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java
  8. +37
    -25
      mallinkService/src/main/java/com/simple/service/impl/WxPayOrderServiceImpl.java
  9. +8
    -8
      mallinkService/src/main/java/com/simple/service/impl/WxRefundOrderServiceImpl.java

+ 10
- 1
mallinkBApi/src/main/java/com/simple/controller/WxOrderController.java Wyświetl plik

@@ -101,8 +101,17 @@ public class WxOrderController extends BaseController {
orderId = Long.valueOf(orderIdStr); orderId = Long.valueOf(orderIdStr);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
logger.error("orderId " + orderIdStr + ", e:" + e.getMessage()); logger.error("orderId " + orderIdStr + ", e:" + e.getMessage());
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "订单ID转换异常");
} }
wxOrderService.updateOrderStatus(orderId, EnumOrderStatus.ORDER_STATUS_PENDING_REFUND);
try {
WxOrder order = wxOrderService.getById(orderId);
if (order != null) {
wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_PENDING_REFUND);
}
} catch (Exception e) {
logger.error("orderId " + orderIdStr + ", e:" + e.getMessage());
}

return new ResultData(); return new ResultData();
} }
} }

+ 38
- 6
mallinkCApi/src/main/java/com/simple/controller/WxOrderController.java Wyświetl plik

@@ -46,10 +46,12 @@ public class WxOrderController extends BaseController {
return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "couponId: " + couponIdStr + ", e:" + e.getMessage()); return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "couponId: " + couponIdStr + ", e:" + e.getMessage());
} }


Long cUserId = getUserId();
WxCUser user = getUser();

WxOrder order = null;


try { try {
WxOrder order = wxOrderService.sendUserFreeCoupon(cUserId, couponId);
order = wxOrderService.sendUserFreeCoupon(user.getId(), couponId);
return new ResultData(order); return new ResultData(order);
} catch (MallinkException e) { } catch (MallinkException e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
@@ -107,7 +109,16 @@ public class WxOrderController extends BaseController {
logger.error(e.getMessage()); logger.error(e.getMessage());
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL, "orderId: " + orderIdStr + ", e: " + e.getMessage()); return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL, "orderId: " + orderIdStr + ", e: " + e.getMessage());
} }
wxOrderService.updateOrderStatus(orderId, EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL);
try {
WxOrder order = wxOrderService.getById(orderId);
if (order != null) {
wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL);
}
} catch (Exception e) {
logger.error("取消订单失败: " + e.getMessage());
return new ResultData(ErrorCode.ORDER_IS_FAIL.getCode(), "取消订单失败: " + e.getMessage());
}

return new ResultData(); return new ResultData();
} }


@@ -118,7 +129,7 @@ public class WxOrderController extends BaseController {
//Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名"); //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
String orderIdStr = paramMap.get("orderId"); String orderIdStr = paramMap.get("orderId");
if (StringUtils.isBlank(orderIdStr)) { if (StringUtils.isBlank(orderIdStr)) {
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空");
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "退款订单ID不能为空");
} }
Long orderId = 0L; Long orderId = 0L;
WxCUser user = getUser(); WxCUser user = getUser();
@@ -126,8 +137,17 @@ public class WxOrderController extends BaseController {
orderId = Long.valueOf(orderIdStr); orderId = Long.valueOf(orderIdStr);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "退款订单ID转换失败");
}
try {
WxOrder order = wxOrderService.getById(orderId);
if (order != null) {
wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_REFUND_SUCCESS);
}
} catch (Exception e) {
logger.error("更新退款订单状态失败:" + e.getMessage());
return new ResultData(ErrorCode.REFUND_ORDER_ERROR.getCode(), "更新退款订单状态失败:" + e.getMessage());
} }
wxOrderService.updateOrderStatus(orderId, EnumOrderStatus.ORDER_STATUS_REFUND_SUCCESS);
return new ResultData(); return new ResultData();
} }


@@ -160,8 +180,20 @@ public class WxOrderController extends BaseController {
orderId = Long.valueOf(orderIdStr); orderId = Long.valueOf(orderIdStr);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
logger.error("parse orderId failed"); logger.error("parse orderId failed");
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "订单ID转换异常");
}
WxOrder order = null;
try {
order = wxOrderService.getById(orderId);
if (order != null) {
return new ResultData(Result.SUCCESS, "查询成功", order);
} else {
return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
}
} catch (Exception e) {
logger.error("parse orderId failed");
return new ResultData(ErrorCode.DB_FAIL.getCode(), "订单查询未成功,e:" + e.getMessage());
} }
return new ResultData(Result.SUCCESS, "查询成功", wxOrderService.getById(orderId));
} }






+ 1
- 2
mallinkCApi/src/main/java/com/simple/controller/WxPayOrderController.java Wyświetl plik

@@ -92,7 +92,6 @@ public class WxPayOrderController extends BaseController {
try { try {
orderId = Long.valueOf(orderIdStr); orderId = Long.valueOf(orderIdStr);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
orderId = 0L;
logger.error("orderId参数不正确: " + paramMap.toString() + ", e:" + e.getMessage()); logger.error("orderId参数不正确: " + paramMap.toString() + ", e:" + e.getMessage());
return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId参数不正确"); return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId参数不正确");
} }
@@ -118,8 +117,8 @@ public class WxPayOrderController extends BaseController {
return new ResultData(e.getErrorCode(), e.getMessage()); return new ResultData(e.getErrorCode(), e.getMessage());
} catch (Exception e) { } catch (Exception e) {
logger.error("支付状态更新失败3: " + payOrder.toString() + ", e:" + e.getMessage()); logger.error("支付状态更新失败3: " + payOrder.toString() + ", e:" + e.getMessage());
return new ResultData(ErrorCode.PAY_ORDER_ERROR, "支付状态更新失败3: " + payOrder.toString() + ", e:" + e.getMessage());
} }
return new ResultData(ErrorCode.PAY_ORDER_ERROR);
} }


@ApiOperation("分页列表接口") @ApiOperation("分页列表接口")


+ 12
- 0
mallinkService/src/main/java/com/simple/domain/po/WxOrder.java Wyświetl plik

@@ -57,6 +57,9 @@ public class WxOrder implements Serializable {
/*操作券B端小程序用户ID**/ /*操作券B端小程序用户ID**/
@io.swagger.annotations.ApiModelProperty(value="操作券B端小程序用户ID",name="bUserId") @io.swagger.annotations.ApiModelProperty(value="操作券B端小程序用户ID",name="bUserId")
private Long bUserId; private Long bUserId;
/*券ID-产品ID将来的产品都会放在coupon表里**/
@io.swagger.annotations.ApiModelProperty(value="券ID",name="couponId")
private Long couponId;
/*0: 付款 1: 退款**/ /*0: 付款 1: 退款**/
@io.swagger.annotations.ApiModelProperty(value="0: 付款 1: 退款",name="paymentType") @io.swagger.annotations.ApiModelProperty(value="0: 付款 1: 退款",name="paymentType")
private Integer paymentType; private Integer paymentType;
@@ -108,6 +111,14 @@ public class WxOrder implements Serializable {
public void setBUserId(Long _bUserId) { public void setBUserId(Long _bUserId) {
bUserId = _bUserId; bUserId = _bUserId;
} }
public Long getCouponId() {
return couponId;
}

public void setCouponId(Long _couponId) {
this.couponId = _couponId;
}

public Integer getPaymentType() { public Integer getPaymentType() {
return paymentType; return paymentType;
} }
@@ -161,6 +172,7 @@ public class WxOrder implements Serializable {
,CUserId_ASC("`cUserId` ASC"),CUserId_DESC("`cUserId` DESC") ,CUserId_ASC("`cUserId` ASC"),CUserId_DESC("`cUserId` DESC")
,MerchantId_ASC("`merchantId` ASC"),MerchantId_DESC("`merchantId` DESC") ,MerchantId_ASC("`merchantId` ASC"),MerchantId_DESC("`merchantId` DESC")
,BUserId_ASC("`bUserId` ASC"),BUserId_DESC("`bUserId` DESC") ,BUserId_ASC("`bUserId` ASC"),BUserId_DESC("`bUserId` DESC")
,CouponId_ASC("`couponId` ASC"),CouponId_DESC("`couponId` DESC")
,PaymentType_ASC("`paymentType` ASC"),PaymentType_DESC("`paymentType` DESC") ,PaymentType_ASC("`paymentType` ASC"),PaymentType_DESC("`paymentType` DESC")
,Payment_ASC("`payment` ASC"),Payment_DESC("`payment` DESC") ,Payment_ASC("`payment` ASC"),Payment_DESC("`payment` DESC")
,PaymentTime_ASC("`paymentTime` ASC"),PaymentTime_DESC("`paymentTime` DESC") ,PaymentTime_ASC("`paymentTime` ASC"),PaymentTime_DESC("`paymentTime` DESC")


+ 1
- 5
mallinkService/src/main/java/com/simple/enums/EnumCouponStatus.java Wyświetl plik

@@ -5,15 +5,11 @@ package com.simple.enums;
*/ */
public enum EnumCouponStatus { public enum EnumCouponStatus {


// 0-草稿/待生效;1-已生效/已发布/已投放;2-已下架;3-已领取/已购买/未核销/未使用,4:已核销/已使用,5:已过期,6:已退券
// 0-草稿/待生效;1-已生效/已发布/已投放;2-已下架;


COUPON_STATUS_DRAFT(0, "草稿"), COUPON_STATUS_DRAFT(0, "草稿"),
COUPON_STATUS_THROW_IN(1, "已投放"), COUPON_STATUS_THROW_IN(1, "已投放"),
COUPON_STATUS_TAKE_OFFF(2, "已下架"), COUPON_STATUS_TAKE_OFFF(2, "已下架"),
COUPON_STATUS_NOT_USED(3, "未使用"),
COUPON_STATUS_USED(4,"已使用"),
COUPON_STATUS_OVER_TIME(5, "已过期"),
COUPON_STATUS_BACKED(6, "已退券")
; ;


public static EnumCouponStatus getEnum(Integer code) { public static EnumCouponStatus getEnum(Integer code) {


+ 10
- 3
mallinkService/src/main/java/com/simple/service/WxOrderService.java Wyświetl plik

@@ -53,11 +53,18 @@ public interface WxOrderService {
WxOrder sendUserFreeCoupon(Long userId, Long couponId); WxOrder sendUserFreeCoupon(Long userId, Long couponId);


/** /**
* 更新订单状态
* @param orderId
* orderSuccess 订单已支付
* @param updateOrder
* @return
*/
int orderSuccess(WxOrder updateOrder);

/**
* updateOrderStatus 更新订单状态, 只针对已取消及已退款
* @param updateOrder
* @param enumOrderStatus * @param enumOrderStatus
*/ */
int updateOrderStatus(Long orderId, EnumOrderStatus enumOrderStatus);
int updateOrderStatus(WxOrder updateOrder, EnumOrderStatus enumOrderStatus);
/** /**
* 根据Id获得实体 * 根据Id获得实体


+ 210
- 178
mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java Wyświetl plik

@@ -8,14 +8,12 @@ import com.github.pagehelper.PageInfo;
import com.simple.common.ErrorCode; import com.simple.common.ErrorCode;
import com.simple.domain.po.*; import com.simple.domain.po.*;
import com.simple.domain.vo.OrderVo; import com.simple.domain.vo.OrderVo;
import com.simple.enums.EnumCouponOrderStatus;
import com.simple.enums.EnumOrderStatus;
import com.simple.enums.EnumPayType;
import com.simple.enums.EnumValidStatus;
import com.simple.enums.*;
import com.simple.exception.MallinkException; import com.simple.exception.MallinkException;
import com.simple.mapper.*; import com.simple.mapper.*;
import com.simple.service.WxOrderService; import com.simple.service.WxOrderService;
import com.simple.service.WxPayAccountService; import com.simple.service.WxPayAccountService;
import com.simple.service.WxUserCouponService;
import com.simple.utils.RedisLock; import com.simple.utils.RedisLock;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -71,29 +69,32 @@ public class WxOrderServiceImpl implements WxOrderService {
return wxOrderMapper.queryObject(orderVo); return wxOrderMapper.queryObject(orderVo);
} }


@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public WxOrder saveOrder(WxCUser user, Long couponId) {
String couponIdStr = String.valueOf(couponId);
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId);
if (coupon == null) {
logger.error("券不存在, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
private int getUserCouponOrderCount(WxCUser user, WxCoupon counpon) {
// 查找
int count = 0;
WxCouponOrder query = new WxCouponOrder();
query.setCouponId(counpon.getId());
query.setCUserId(user.getId());
query.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode());
try {
count = wxCouponOrderMapper.selectCount(query);
}catch (Exception e) {
logger.error("购买是否超限-DB, couponId: " + counpon.getId() + ", e:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
} }
//加锁

return count;
}

private void stockReduce(WxCUser user, WxCoupon coupon, String couponIdStr) {
long time = System.currentTimeMillis() + RedisLock.TIMEOUT; long time = System.currentTimeMillis() + RedisLock.TIMEOUT;
String timeStr = String.valueOf(time); String timeStr = String.valueOf(time);
// 库存加锁
if(!redisLock.lock(couponIdStr, timeStr)) { if(!redisLock.lock(couponIdStr, timeStr)) {
logger.error("此券被锁定, couponId: " + couponIdStr); logger.error("此券被锁定, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.TOO_MANY_REQUEST); throw new MallinkException(ErrorCode.TOO_MANY_REQUEST);
} }


int payPrice = 0;
int payment = 0;

Date curr = new Date();
Date valid_date = null;

// 检查 优惠券 库存 // 检查 优惠券 库存
if (coupon.getRemainInventory() <= 0) { if (coupon.getRemainInventory() <= 0) {
//解锁 //解锁
@@ -105,17 +106,13 @@ public class WxOrderServiceImpl implements WxOrderService {
// check 购买是否超限 // check 购买是否超限
int count = 0; int count = 0;
try { try {
WxCouponOrder query = new WxCouponOrder();
query.setCouponId(couponId);
query.setCUserId(user.getId());
query.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode());
count = wxCouponOrderMapper.selectCount(query);
}catch (Exception e) {
count = getUserCouponOrderCount(user, coupon);
} catch (Exception e) {
//解锁 //解锁
redisLock.unlock(couponIdStr, timeStr); redisLock.unlock(couponIdStr, timeStr);
logger.error("购买是否超限-DB, couponId: " + couponIdStr + ", e:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
throw new MallinkException(ErrorCode.DB_FAIL);
} }

if (count > coupon.getUseLimitQuantity()) { if (count > coupon.getUseLimitQuantity()) {
//解锁 //解锁
redisLock.unlock(couponIdStr, timeStr); redisLock.unlock(couponIdStr, timeStr);
@@ -135,69 +132,146 @@ public class WxOrderServiceImpl implements WxOrderService {
logger.error("此券减库存失败, couponId: " + couponIdStr); logger.error("此券减库存失败, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
} }
}


payPrice = coupon.getSalePrice();
payment = coupon.getSalePrice();
private void stockBack(WxOrder updateOrder) {
// 已取消/已退款,库存加1
// 获取订单相关coupon
Long couponId = updateOrder.getCouponId();
String couponIdStr = "" + couponId;
//加锁
long time = System.currentTimeMillis() + RedisLock.TIMEOUT;
String timeStr = String.valueOf(time);


valid_date = (coupon.getValidType() == EnumValidStatus.VALID_RANGE.getCode())?
coupon.getValidEndDate():
new Date((curr.getTime()/1000+coupon.getValidDays()*24*60*60)*1000);
if(!redisLock.lock(couponIdStr, timeStr)) {
logger.error("此券被锁定, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.TOO_MANY_REQUEST);
}
try {
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId);
coupon.setRemainInventory(coupon.getRemainInventory() + 1);
wxCouponMapper.updateByPrimaryKeySelective(coupon);
} catch (Exception e) {
logger.error("库存+1失败, e:" + e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "库存+1失败, e:" + e.getMessage());
} finally {
//解锁
redisLock.unlock(couponIdStr, timeStr);
}
}

@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public WxOrder saveOrder(WxCUser user, Long couponId) {
// 检查用户
if (user == null) {
logger.error("用户不存在");
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}
// 获取券信息
String couponIdStr = String.valueOf(couponId);
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId);
if (coupon == null) {
logger.error("券不存在, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
}
if (coupon.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) {
logger.error("券已下架, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID);
}

/*
WxMerchant wxMerchant = wxMerchantMapper.selectByPrimaryKey(coupon.getMerchantId());
if (wxMerchant == null) {
logger.error("商户不存在, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND);
}
if (wxMerchant.getStatus() == EnumMerchantStatus.NOT_VALID.getCode()) {
logger.error("商户已禁用, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_VALID);
}
*/

// 减库存操作
try {
stockReduce(user, coupon, couponIdStr);
} catch (Exception e) {
logger.error("减库存失败, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "减库存失败, couponId: " + couponIdStr);
}

Date curr = new Date();

int payment = coupon.getSalePrice();


final IdWorker idWorker = IdWorker.get(); final IdWorker idWorker = IdWorker.get();
Long orderNumber = idWorker.nextId(); Long orderNumber = idWorker.nextId();


// body // body
// tenant_id + merchant_id + title + subtitle // tenant_id + merchant_id + title + subtitle
String bodyStr = coupon.getTitle() + "/" + coupon.getSubTitle();
String bodyStr = coupon.getTitle() + "-" + coupon.getSubTitle();


WxOrder record = new WxOrder(); WxOrder record = new WxOrder();
record.setId(orderNumber);
record.setTenantId(user.getTenantId());
record.setOrderNumber(orderNumber);
record.setCUserId(user.getId());
record.setMerchantId(coupon.getMerchantId());
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode());
record.setPayment(payment);
record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode());
record.setDetail(bodyStr);
record.setCreateDate(curr);
record.setUpdateDate(curr);


try { try {
// 保存订单 // 保存订单
record.setId(orderNumber);
record.setTenantId(user.getTenantId());
record.setOrderNumber(orderNumber);
record.setCUserId(user.getId());
record.setMerchantId(coupon.getMerchantId());
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode());
record.setPayment(payment);
record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode());
record.setDetail(bodyStr);
record.setCreateDate(curr);
record.setUpdateDate(curr);
wxOrderMapper.insertSelective(record); wxOrderMapper.insertSelective(record);
} catch (RuntimeException e) { } catch (RuntimeException e) {
// TODO 增库存
// 加库存
stockBack(record);
logger.error("保存订单:" + e.getMessage()); logger.error("保存订单:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "保存订单失败:" + record.toString());
} }


return record;
}

/**
* 创建 couponOrder
* @param user
* @param order
* @param coupon
*/
private void createCouponOrder(WxCUser user, WxOrder order, WxCoupon coupon) {
Date curr = new Date();
Date valid_date = (coupon.getValidType() == EnumValidStatus.VALID_RANGE.getCode())?
coupon.getValidEndDate():
new Date((curr.getTime()/1000+coupon.getValidDays()*24*60*60)*1000);
final IdWorker idWorker = IdWorker.get();
try { try {
WxCouponOrder couponOrder = new WxCouponOrder(); WxCouponOrder couponOrder = new WxCouponOrder();
couponOrder.setId(idWorker.nextId()); couponOrder.setId(idWorker.nextId());
couponOrder.setTenantId(user.getTenantId()); couponOrder.setTenantId(user.getTenantId());
couponOrder.setCouponId(couponId);
couponOrder.setCouponId(order.getCouponId());
couponOrder.setCUserId(user.getId()); couponOrder.setCUserId(user.getId());
couponOrder.setOrderId(orderNumber);
couponOrder.setOrderId(order.getOrderNumber());
couponOrder.setExpiredTime(valid_date); couponOrder.setExpiredTime(valid_date);
couponOrder.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode()); couponOrder.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode());
couponOrder.setCreateDate(curr); couponOrder.setCreateDate(curr);
couponOrder.setUpdateDate(curr); couponOrder.setUpdateDate(curr);
couponOrder.setCouponPrice(payPrice);
couponOrder.setCouponPrice(order.getPayment());


wxCouponOrderMapper.insertSelective(couponOrder); wxCouponOrderMapper.insertSelective(couponOrder);

} catch (RuntimeException e) { } catch (RuntimeException e) {
// TODO 增库存
logger.error("WxCouponOrder:" + e.getMessage()); logger.error("WxCouponOrder:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "couponOrder保存失败!");
} }

return record;
} }


@Override @Override
public WxOrder sendUserFreeCoupon(Long userId, Long couponId) { public WxOrder sendUserFreeCoupon(Long userId, Long couponId) {
// check 用户状态
WxCUser user = null; WxCUser user = null;
try { try {
user = wxCUserMapper.selectByPrimaryKey(userId); user = wxCUserMapper.selectByPrimaryKey(userId);
@@ -209,78 +283,25 @@ public class WxOrderServiceImpl implements WxOrderService {
throw new MallinkException(ErrorCode.USER_IS_EMPTY); throw new MallinkException(ErrorCode.USER_IS_EMPTY);
} }


// 检查券状态
String couponIdStr = String.valueOf(couponId); String couponIdStr = String.valueOf(couponId);
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId); WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId);
if (coupon == null) { if (coupon == null) {
logger.error("券不存在, couponId: " + couponIdStr); logger.error("券不存在, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
} }
if (coupon.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) {
logger.error("券已下架, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID);
}
if (coupon.getSalePrice() != 0) { if (coupon.getSalePrice() != 0) {
logger.error("券不免费, couponId: " + couponIdStr); logger.error("券不免费, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.COUPON_IS_NOT_FREE); throw new MallinkException(ErrorCode.COUPON_IS_NOT_FREE);
} }
//加锁
long time = System.currentTimeMillis() + RedisLock.TIMEOUT;
String timeStr = String.valueOf(time);
if(!redisLock.lock(couponIdStr, timeStr)) {
logger.error("此券被锁定, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.TOO_MANY_REQUEST);
}


int payPrice = 0;
int payment = 0;
int payment = coupon.getSalePrice();


Date curr = new Date(); Date curr = new Date();
Date valid_date = null;

// 检查 优惠券 库存
if (coupon.getRemainInventory() <= 0) {
//解锁
redisLock.unlock(couponIdStr, timeStr);
logger.error("此券库存为0, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY);
}

// check 购买是否超限
int count = 0;
try {
WxCouponOrder query = new WxCouponOrder();
query.setCouponId(couponId);
query.setCUserId(user.getId());
query.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode());
count = wxCouponOrderMapper.selectCount(query);
}catch (Exception e) {
//解锁
redisLock.unlock(couponIdStr, timeStr);
logger.error("购买是否超限-DB, couponId: " + couponIdStr + ", e:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
}
if (count > coupon.getUseLimitQuantity()) {
//解锁
redisLock.unlock(couponIdStr, timeStr);
logger.error("此券购买数量已超限, couponId: " + couponIdStr + ", count: " + count);
throw new MallinkException(ErrorCode.ORDER_IS_LIMITED);
}

try {
// 减库存
coupon.setRemainInventory(coupon.getRemainInventory() - 1);
wxCouponMapper.updateByPrimaryKeySelective(coupon);
//解锁
redisLock.unlock(couponIdStr, timeStr);
} catch (RuntimeException e) {
//解锁
redisLock.unlock(couponIdStr, timeStr);
logger.error("此券减库存失败, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
}

payPrice = coupon.getSalePrice();
payment = coupon.getSalePrice();

valid_date = (coupon.getValidType() == EnumValidStatus.VALID_RANGE.getCode())?
coupon.getValidEndDate():
new Date((curr.getTime()/1000+coupon.getValidDays()*24*60*60)*1000);


final IdWorker idWorker = IdWorker.get(); final IdWorker idWorker = IdWorker.get();
Long orderNumber = idWorker.nextId(); Long orderNumber = idWorker.nextId();
@@ -290,95 +311,106 @@ public class WxOrderServiceImpl implements WxOrderService {
String bodyStr = coupon.getTitle() + "/" + coupon.getSubTitle(); String bodyStr = coupon.getTitle() + "/" + coupon.getSubTitle();


WxOrder record = new WxOrder(); WxOrder record = new WxOrder();

record.setId(orderNumber);
record.setTenantId(user.getTenantId());
record.setOrderNumber(orderNumber);
record.setCUserId(user.getId());
record.setMerchantId(coupon.getMerchantId());
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode());
record.setPayment(payment);
record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode());
record.setDetail(bodyStr);
record.setCreateDate(curr);
record.setUpdateDate(curr);

// 保存订单
try { try {
// 保存订单
record.setId(orderNumber);
record.setTenantId(user.getTenantId());
record.setOrderNumber(orderNumber);
record.setCUserId(user.getId());
record.setMerchantId(coupon.getMerchantId());
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode());
record.setPayment(payment);
record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode());
record.setDetail(bodyStr);
record.setCreateDate(curr);
record.setUpdateDate(curr);
wxOrderMapper.insertSelective(record); wxOrderMapper.insertSelective(record);
} catch (RuntimeException e) {
// TODO 增库存
} catch (Exception e) {
//加库存
stockBack(record);
logger.error("保存订单:" + e.getMessage()); logger.error("保存订单:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
} }


// 创建couponOrder
try { try {
WxCouponOrder couponOrder = new WxCouponOrder();
couponOrder.setId(idWorker.nextId());
couponOrder.setTenantId(user.getTenantId());
couponOrder.setCouponId(couponId);
couponOrder.setCUserId(user.getId());
couponOrder.setOrderId(orderNumber);
couponOrder.setExpiredTime(valid_date);
couponOrder.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode());
couponOrder.setCreateDate(curr);
couponOrder.setUpdateDate(curr);
couponOrder.setCouponPrice(payPrice);

wxCouponOrderMapper.insertSelective(couponOrder);
} catch (RuntimeException e) {
// TODO 增库存
logger.error("WxCouponOrder:" + e.getMessage());
createCouponOrder(user, record, coupon);
} catch (Exception e) {
logger.error("保存订单:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
} }



return record; return record;
} }


@Override @Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public int updateOrderStatus(Long orderId,EnumOrderStatus enumOrderStatus) {
WxOrder updateRecord = wxOrderMapper.selectByPrimaryKey(orderId);
if (updateRecord == null) {
logger.error("order updateStatus, order not found, orderId:" + updateRecord.getId());
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND);
}
public int orderSuccess(WxOrder updateOrder) {
Date currentDate = new Date(); Date currentDate = new Date();
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(updateOrder.getCouponId());
if (coupon == null) {
logger.error("券不存在, couponId: " + updateOrder.getCouponId());
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY);
}
if (coupon.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) {
logger.error("券已下架, couponId: " + updateOrder.getCouponId());
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID);
}
WxCUser user = wxCUserMapper.selectByPrimaryKey(updateOrder.getCUserId());
if (user == null) {
logger.error("用户不存在, userId: " + updateOrder.getCUserId());
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}
// 已支付,更新支付时间
updateOrder.setPaymentTime(currentDate);
updateOrder.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode());
updateOrder.setUpdateDate(currentDate);
int ret = 0;
try {
ret = wxOrderMapper.updateByPrimaryKey(updateOrder);
} catch (Exception e) {
logger.error("订单更新失败:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "订单更新失败:" + e.getMessage());
}
// 创建couponOrder
try {
createCouponOrder(user, updateOrder, coupon);
} catch (Exception e) {
logger.error("保存订单:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
}
return ret;
}


@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class})
public int updateOrderStatus(WxOrder updateOrder, EnumOrderStatus enumOrderStatus) {
Date currentDate = new Date();
switch (enumOrderStatus) { switch (enumOrderStatus) {
case ORDER_STATUS_PAYMENT_SUCCESS: {
// 已支付,更新支付时间
updateRecord.setPaymentTime(currentDate);
break;
}
case ORDER_STATUS_OVERTIME_CANCEL: case ORDER_STATUS_OVERTIME_CANCEL:
case ORDER_STATUS_REFUND_SUCCESS: case ORDER_STATUS_REFUND_SUCCESS:
{
// 已取消/已退款,库存加1
// 获取订单相关coupon
WxCouponOrder couponOrder = new WxCouponOrder();
couponOrder.setOrderId(orderId);
List<WxCouponOrder> colist = wxCouponOrderMapper.findList(couponOrder);
for(WxCouponOrder couponOrderX : colist) {
String couponIdStr = String.valueOf(couponOrderX.getCouponId());
//加锁
long time = System.currentTimeMillis() + RedisLock.TIMEOUT;
String timeStr = String.valueOf(time);
if(!redisLock.lock(couponIdStr, timeStr)) {
logger.error("此券被锁定, couponId: " + couponIdStr);
throw new MallinkException(ErrorCode.TOO_MANY_REQUEST);
}
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponOrderX.getCouponId());
coupon.setRemainInventory(coupon.getRemainInventory() + 1);
wxCouponMapper.updateByPrimaryKeySelective(coupon);
//解锁
redisLock.unlock(couponIdStr, timeStr);
{
try {
stockBack(updateOrder);
} catch (Exception e) {
logger.error("库存+1失败, e:" + e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "库存+1失败, e:" + e.getMessage());
} }
break; break;
} }
} }
updateRecord.setOrderStatus(enumOrderStatus.getCode());
updateRecord.setUpdateDate(currentDate);
return wxOrderMapper.updateByPrimaryKey(updateRecord);
int ret = 0;
try {
updateOrder.setOrderStatus(enumOrderStatus.getCode());
updateOrder.setUpdateDate(currentDate);
ret = wxOrderMapper.updateByPrimaryKey(updateOrder);
}catch (Exception e) {
logger.error("订单状态更新失败, e:" + e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单状态更新失败, e:" + e.getMessage());
}
return ret;
} }


@Override @Override


+ 37
- 25
mallinkService/src/main/java/com/simple/service/impl/WxPayOrderServiceImpl.java Wyświetl plik

@@ -16,10 +16,7 @@ import com.simple.enums.EnumPayStatus;
import com.simple.enums.EnumPayType; import com.simple.enums.EnumPayType;
import com.simple.enums.EnumPayWay; import com.simple.enums.EnumPayWay;
import com.simple.exception.MallinkException; import com.simple.exception.MallinkException;
import com.simple.mapper.WxAppinfoMapper;
import com.simple.mapper.WxOrderMapper;
import com.simple.mapper.WxPayAccountMapper;
import com.simple.mapper.WxPayOrderMapper;
import com.simple.mapper.*;
import com.simple.pay.WxPay; import com.simple.pay.WxPay;
import com.simple.pay.WxPayOrderP; import com.simple.pay.WxPayOrderP;
import com.simple.pay.WxPayment; import com.simple.pay.WxPayment;
@@ -44,6 +41,9 @@ public class WxPayOrderServiceImpl implements WxPayOrderService {
@Autowired @Autowired
WxAppinfoMapper wxAppinfoMapper; WxAppinfoMapper wxAppinfoMapper;


@Autowired
WxCUserMapper wxCUserMapper;

@Autowired @Autowired
WxPayAccountMapper wxPayAccountMapper; WxPayAccountMapper wxPayAccountMapper;


@@ -310,25 +310,32 @@ public class WxPayOrderServiceImpl implements WxPayOrderService {




// 修改支付订单状态 // 修改支付订单状态
WxPayOrder updateOrder = new WxPayOrder();
updateOrder.setId(record.getId());
updateOrder.setOrderId(record.getOrderId());
updateOrder.setUpdateTime(currentDate);
updateOrder.setPayOrderStatus(EnumPayStatus.PAY_WAY_SUCCESS.getCode());
updateOrder.setTransactionId(transactionId);
try { try {
WxPayOrder updateOrder = new WxPayOrder();
updateOrder.setId(record.getId());
updateOrder.setOrderId(record.getOrderId());
updateOrder.setUpdateTime(currentDate);
updateOrder.setPayOrderStatus(EnumPayStatus.PAY_WAY_SUCCESS.getCode());
updateOrder.setTransactionId(transactionId);
wxPayOrderMapper.updateByPrimaryKeySelective(updateOrder); wxPayOrderMapper.updateByPrimaryKeySelective(updateOrder);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL);
logger.error("支付订单数据库更新失败: " + e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "支付订单数据库更新失败: " + e.getMessage());
} }


// 修改订单状态 // 修改订单状态
try { try {
wxOrderService.updateOrderStatus(order.getId(), EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS);
wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS);
} catch (Exception e) {
logger.error("订单数据库更新失败: " + e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单数据库更新失败: " + e.getMessage());
}
// 创建couponOrder
try {
wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL);
logger.error("订单数据库更新失败: " + e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单数据库更新失败: " + e.getMessage());
} }


/* /*
@@ -360,19 +367,24 @@ public class WxPayOrderServiceImpl implements WxPayOrderService {
logger.error("pay handle, order " + record.getOrderId() + " not found , payOrderGid : " + record.getPayOrderNo()); logger.error("pay handle, order " + record.getOrderId() + " not found , payOrderGid : " + record.getPayOrderNo());
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND); throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND);
} }
WxCUser user = wxCUserMapper.selectByPrimaryKey(order.getCUserId());
if (user == null) {
logger.error("pay handle, order " + record.getOrderId() + " not found , payOrderGid : " + record.getPayOrderNo());
throw new MallinkException(ErrorCode.USER_IS_EMPTY);
}


Date currentDate = new Date(); Date currentDate = new Date();
if (record.getId() > 0) { if (record.getId() > 0) {
// 修改支付订单状态 // 修改支付订单状态
// 订单金额为0时,支付订单不创建,直接更改订单状态 // 订单金额为0时,支付订单不创建,直接更改订单状态
WxPayOrder updateOrder = new WxPayOrder();
updateOrder.setId(record.getId());
updateOrder.setOrderId(record.getOrderId());
updateOrder.setUpdateTime(currentDate);
updateOrder.setPayOrderStatus(record.getPayOrderStatus());
updateOrder.setPayTimeEnd(currentDate);
updateOrder.setFailReason(record.getFailReason());
try { try {
WxPayOrder updateOrder = new WxPayOrder();
updateOrder.setId(record.getId());
updateOrder.setOrderId(record.getOrderId());
updateOrder.setUpdateTime(currentDate);
updateOrder.setPayOrderStatus(record.getPayOrderStatus());
updateOrder.setPayTimeEnd(currentDate);
updateOrder.setFailReason(record.getFailReason());
wxPayOrderMapper.updateByPrimaryKeySelective(updateOrder); wxPayOrderMapper.updateByPrimaryKeySelective(updateOrder);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
@@ -383,17 +395,17 @@ public class WxPayOrderServiceImpl implements WxPayOrderService {
// 修改订单状态 // 修改订单状态
if (record.getPayOrderStatus() == EnumPayStatus.PAY_WAY_SUCCESS.getCode()) { if (record.getPayOrderStatus() == EnumPayStatus.PAY_WAY_SUCCESS.getCode()) {
try { try {
int _count = wxOrderService.updateOrderStatus(order.getId(), EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS);
int _count = wxOrderService.orderSuccess(order);
if (_count > 1) { if (_count > 1) {
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
} }
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL);
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单更新");
} }
} else if (record.getPayOrderStatus() == EnumPayStatus.PAY_WAY_CANCEL.getCode()) { } else if (record.getPayOrderStatus() == EnumPayStatus.PAY_WAY_CANCEL.getCode()) {
try { try {
int _count = wxOrderService.updateOrderStatus(order.getId(), EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL);
int _count = wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL);
if (_count > 1) { if (_count > 1) {
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
} }


+ 8
- 8
mallinkService/src/main/java/com/simple/service/impl/WxRefundOrderServiceImpl.java Wyświetl plik

@@ -493,15 +493,15 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService {
} }


Date currentDate = new Date(); Date currentDate = new Date();
WxRefundOrder updateOrder = new WxRefundOrder();
updateOrder.setId(refundOrder.getId());
updateOrder.setOrderId(refundOrder.getOrderId());
updateOrder.setUpdateTime(currentDate);
updateOrder.setRefundOrderStatus(EnumRefundStatus.REFUND_SUCCESS.getCode());
updateOrder.setTransactionId(transactionId);
updateOrder.setRefundId(refundId);
// 修改退款订单状态 // 修改退款订单状态
try { try {
WxRefundOrder updateOrder = new WxRefundOrder();
updateOrder.setId(refundOrder.getId());
updateOrder.setOrderId(refundOrder.getOrderId());
updateOrder.setUpdateTime(currentDate);
updateOrder.setRefundOrderStatus(EnumRefundStatus.REFUND_SUCCESS.getCode());
updateOrder.setTransactionId(transactionId);
updateOrder.setRefundId(refundId);
wxRefundOrderMapper.updateByPrimaryKeySelective(updateOrder); wxRefundOrderMapper.updateByPrimaryKeySelective(updateOrder);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
@@ -510,7 +510,7 @@ public class WxRefundOrderServiceImpl implements WxRefundOrderService {


// 修改订单状态 // 修改订单状态
try { try {
wxOrderService.updateOrderStatus(order.getId(), EnumOrderStatus.ORDER_STATUS_REFUND_SUCCESS);
wxOrderService.updateOrderStatus(order, EnumOrderStatus.ORDER_STATUS_REFUND_SUCCESS);
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage()); logger.error(e.getMessage());
throw new MallinkException(ErrorCode.DB_FAIL); throw new MallinkException(ErrorCode.DB_FAIL);


Ładowanie…
Anuluj
Zapisz