|
|
|
@@ -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 |
|
|
|
|