diff --git a/mallinkService/src/main/java/com/simple/common/ErrorCode.java b/mallinkService/src/main/java/com/simple/common/ErrorCode.java index 0c10fe3af..17ca543de 100644 --- a/mallinkService/src/main/java/com/simple/common/ErrorCode.java +++ b/mallinkService/src/main/java/com/simple/common/ErrorCode.java @@ -40,7 +40,18 @@ public enum ErrorCode{ USER_IS_EMPTY(2000, "用户不存在"), PASSWORD_ERROR(2001, "密码错误"), LOGIN_USER_OR_PWD_ERROR(2002, "用户名或密码错误"), - USER_IS_LOCKED(2003, "用户已经被锁定不能登录,请与管理员联系"); + USER_IS_LOCKED(2003, "用户已经被锁定不能登录,请与管理员联系"), + + /** + * 订单 + */ + REMAIN_IS_EMPTY(12000, "库存不足"), + ORDER_IS_FAIL(12001, "订单创建失败"), + ORDER_IS_NOT_FIND(12002, "订单不存在"); + + /** + * 支付 + */ diff --git a/mallinkService/src/main/java/com/simple/exception/BizMessageException.java b/mallinkService/src/main/java/com/simple/exception/BizMessageException.java new file mode 100644 index 000000000..6e6f0b477 --- /dev/null +++ b/mallinkService/src/main/java/com/simple/exception/BizMessageException.java @@ -0,0 +1,14 @@ +package com.simple.exception; + +/** + * Created by Stormeye on 2018/8/10. + */ +public class BizMessageException extends RuntimeException { + public BizMessageException() { + super(); + } + + public BizMessageException(String bizMessage) { + super(bizMessage); + } +} diff --git a/mallinkService/src/main/java/com/simple/exception/MallinkException.java b/mallinkService/src/main/java/com/simple/exception/MallinkException.java new file mode 100644 index 000000000..0396cb0f5 --- /dev/null +++ b/mallinkService/src/main/java/com/simple/exception/MallinkException.java @@ -0,0 +1,36 @@ +package com.simple.exception; + +import com.simple.common.ErrorCode; + +/** + * Created by Stormeye on 2018/8/10. + */ +public class MallinkException extends RuntimeException { + + private int errorCode; + private String message; + + public MallinkException(int errorC, String message) { + this.errorCode = errorC; + this.message = message; + } + + public MallinkException(ErrorCode errCode) { + this.errorCode = errCode.getCode(); + this.message = errCode.getMessage(); + } + + public MallinkException() { + super(); + } + + public int getErrorCode() { + return errorCode; + } + + @Override + public String getMessage() { + return message; + } + +} diff --git a/mallinkService/src/main/java/com/simple/service/WxOrderService.java b/mallinkService/src/main/java/com/simple/service/WxOrderService.java index 984612d9d..a3d9a6bbd 100644 --- a/mallinkService/src/main/java/com/simple/service/WxOrderService.java +++ b/mallinkService/src/main/java/com/simple/service/WxOrderService.java @@ -29,7 +29,7 @@ public interface WxOrderService { * @param orderId * @param enumOrderStatus */ - void updateOrderStatus(Long orderId,EnumOrderStatus enumOrderStatus); + int updateOrderStatus(Long orderId, EnumOrderStatus enumOrderStatus); /** * 根据Id获得实体 diff --git a/mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java b/mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java index 3024a8394..34a5073e1 100644 --- a/mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java +++ b/mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java @@ -1,19 +1,32 @@ package com.simple.service.impl; import java.util.*; + import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; +import com.simple.common.ErrorCode; +import com.simple.domain.po.WxCoupon; import com.simple.domain.po.WxOrder; import com.simple.enums.EnumOrderStatus; +import com.simple.exception.MallinkException; +import com.simple.mapper.WxCouponMapper; import com.simple.mapper.WxOrderMapper; import com.simple.service.WxOrderService; +import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.simple.common.IdWorker; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; @Service public class WxOrderServiceImpl implements WxOrderService { - + + private Logger logger = Logger.getLogger(getClass()); + + @Autowired + WxCouponMapper wxCouponMapper; + @Autowired WxOrderMapper wxOrderMapper; @@ -24,25 +37,60 @@ public class WxOrderServiceImpl implements WxOrderService { } @Override + @Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) public WxOrder saveOrder(WxOrder record) { - // 检查 优惠券 库存 - // 保存订单 - final IdWorker idWorker = IdWorker.get(); - record.setId(idWorker.nextId()); - record.setStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); - Date curr = new Date(); - record.setCreateDate(curr); - record.setUpdateDate(curr); - wxOrderMapper.insertSelective(record); - return record; + try { + // 检查 优惠券 库存 + WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(record.getCouponId()); + if (coupon.getRemainInventory() <= 0) { + logger.error("coupon not found, couponId: " + record.getCouponId()); + throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY); + } + // 减库存 + coupon.setRemainInventory(coupon.getRemainInventory() - 1); + wxCouponMapper.updateByPrimaryKeySelective(coupon); + + // 保存订单 + final IdWorker idWorker = IdWorker.get(); + record.setId(idWorker.nextId()); + record.setStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); + Date curr = new Date(); + record.setCreateDate(curr); + record.setUpdateDate(curr); + wxOrderMapper.insertSelective(record); + // 返回 record + return record; + } catch (RuntimeException e) { + throw new MallinkException(ErrorCode.ORDER_IS_FAIL); + } } @Override - public void updateOrderStatus(Long orderId,EnumOrderStatus enumOrderStatus) { - WxOrder record = wxOrderMapper.selectByPrimaryKey(orderId); - record.setStatus(enumOrderStatus.getCode()); - record.setUpdateDate(new Date()); - wxOrderMapper.updateByPrimaryKey(record); + @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); + } + Date currentDate = new Date(); + // 支付成功 + if (enumOrderStatus == EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS) { + updateRecord.setPaymentTime(currentDate); + } + if (enumOrderStatus == EnumOrderStatus.ORDER_STATUS_OVERTIME_CANCEL) { + //取消订单, 库存加1 + WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(updateRecord.getCouponId()); + if (coupon.getRemainInventory() <= 0) { + logger.error("coupon not found, couponId: " + updateRecord.getCouponId()); + throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY); + } + coupon.setRemainInventory(coupon.getRemainInventory() + 1); + wxCouponMapper.updateByPrimaryKeySelective(coupon); + } + updateRecord.setStatus(enumOrderStatus.getCode()); + updateRecord.setUpdateDate(currentDate); + return wxOrderMapper.updateByPrimaryKey(updateRecord); } @Override