| @@ -1,11 +1,13 @@ | |||||
| package com.simple.service.impl; | package com.simple.service.impl; | ||||
| import java.util.*; | |||||
| import com.github.pagehelper.PageHelper; | import com.github.pagehelper.PageHelper; | ||||
| import com.github.pagehelper.PageInfo; | import com.github.pagehelper.PageInfo; | ||||
| import com.simple.common.ErrorCode; | import com.simple.common.ErrorCode; | ||||
| import com.simple.domain.po.*; | |||||
| import com.simple.common.IdWorker; | |||||
| import com.simple.domain.po.WxCUser; | |||||
| import com.simple.domain.po.WxCoupon; | |||||
| import com.simple.domain.po.WxCouponOrder; | |||||
| import com.simple.domain.po.WxOrder; | |||||
| import com.simple.domain.vo.OrderVo; | import com.simple.domain.vo.OrderVo; | ||||
| import com.simple.enums.*; | import com.simple.enums.*; | ||||
| import com.simple.exception.MallinkException; | import com.simple.exception.MallinkException; | ||||
| @@ -15,10 +17,13 @@ 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; | ||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| import com.simple.common.IdWorker; | |||||
| import org.springframework.transaction.annotation.Propagation; | import org.springframework.transaction.annotation.Propagation; | ||||
| import org.springframework.transaction.annotation.Transactional; | import org.springframework.transaction.annotation.Transactional; | ||||
| import java.util.Date; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| @Service | @Service | ||||
| public class WxOrderServiceImpl implements WxOrderService { | public class WxOrderServiceImpl implements WxOrderService { | ||||
| @@ -42,13 +47,13 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| @Autowired | @Autowired | ||||
| WxCUserMapper wxCUserMapper; | WxCUserMapper wxCUserMapper; | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxCouponMapper wxCouponMapper; | WxCouponMapper wxCouponMapper; | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxOrderMapper wxOrderMapper; | WxOrderMapper wxOrderMapper; | ||||
| @Autowired | |||||
| @Autowired | |||||
| WxCouponOrderMapper wxCouponOrderMapper; | WxCouponOrderMapper wxCouponOrderMapper; | ||||
| @Override | @Override | ||||
| @@ -57,12 +62,12 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| } | } | ||||
| @Override | @Override | ||||
| public PageInfo<Map<String,Object>> bQueryListMap(OrderVo orderVo, Integer pageIndex, Integer pageSize) { | |||||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(()->wxOrderMapper.queryListMap(orderVo)); | |||||
| public PageInfo<Map<String, Object>> bQueryListMap(OrderVo orderVo, Integer pageIndex, Integer pageSize) { | |||||
| return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> wxOrderMapper.queryListMap(orderVo)); | |||||
| } | } | ||||
| @Override | @Override | ||||
| public Map<String,Object> queryObject(OrderVo orderVo) { | |||||
| public Map<String, Object> queryObject(OrderVo orderVo) { | |||||
| return wxOrderMapper.queryObject(orderVo); | return wxOrderMapper.queryObject(orderVo); | ||||
| } | } | ||||
| @@ -77,7 +82,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| orderQ.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | orderQ.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | ||||
| try { | try { | ||||
| countOrder = wxOrderMapper.selectCount(orderQ); | countOrder = wxOrderMapper.selectCount(orderQ); | ||||
| }catch (Exception e) { | |||||
| } catch (Exception e) { | |||||
| logger.error("购买是否超限-DB, couponId: " + counpon.getId() + ", e:" + e.getMessage()); | logger.error("购买是否超限-DB, couponId: " + counpon.getId() + ", e:" + e.getMessage()); | ||||
| throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | ||||
| } | } | ||||
| @@ -88,7 +93,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| couponOrderQ.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode()); | couponOrderQ.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode()); | ||||
| try { | try { | ||||
| countCouponOrder = wxCouponOrderMapper.selectCount(couponOrderQ); | countCouponOrder = wxCouponOrderMapper.selectCount(couponOrderQ); | ||||
| }catch (Exception e) { | |||||
| } catch (Exception e) { | |||||
| logger.error("购买是否超限-DB, couponId: " + counpon.getId() + ", e:" + e.getMessage()); | logger.error("购买是否超限-DB, couponId: " + counpon.getId() + ", e:" + e.getMessage()); | ||||
| throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | throw new MallinkException(ErrorCode.ORDER_IS_FAIL); | ||||
| } | } | ||||
| @@ -100,7 +105,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| 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); | ||||
| } | } | ||||
| @@ -153,13 +158,25 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| 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); | ||||
| } | } | ||||
| WxCoupon coupon = null; | |||||
| try { | |||||
| coupon = wxCouponMapper.selectByPrimaryKey(couponId); | |||||
| } catch (Exception e) { | |||||
| logger.error("券未找到, e:" + e.getMessage()); | |||||
| throw new MallinkException(ErrorCode.COUPON_ORDER_IS_NULL.getCode(), "券未找到:" + couponIdStr); | |||||
| } | |||||
| if (coupon == null) { | |||||
| logger.error("券未找到, " + couponIdStr); | |||||
| throw new MallinkException(ErrorCode.COUPON_ORDER_IS_NULL.getCode(), "券未找到:" + couponIdStr); | |||||
| } | |||||
| coupon.setRemainInventory(coupon.getRemainInventory() + 1); | |||||
| try { | try { | ||||
| WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId); | |||||
| coupon.setRemainInventory(coupon.getRemainInventory() + 1); | |||||
| wxCouponMapper.updateByPrimaryKeySelective(coupon); | wxCouponMapper.updateByPrimaryKeySelective(coupon); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| logger.error("库存+1失败, e:" + e.getMessage()); | logger.error("库存+1失败, e:" + e.getMessage()); | ||||
| @@ -249,15 +266,16 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| /** | /** | ||||
| * 创建 couponOrder | * 创建 couponOrder | ||||
| * | |||||
| * @param user | * @param user | ||||
| * @param order | * @param order | ||||
| * @param coupon | * @param coupon | ||||
| */ | */ | ||||
| private void createCouponOrder(WxCUser user, WxOrder order, WxCoupon coupon) { | private void createCouponOrder(WxCUser user, WxOrder order, WxCoupon coupon) { | ||||
| Date curr = new Date(); | 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); | |||||
| 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(); | final IdWorker idWorker = IdWorker.get(); | ||||
| try { | try { | ||||
| WxCouponOrder couponOrder = new WxCouponOrder(); | WxCouponOrder couponOrder = new WxCouponOrder(); | ||||
| @@ -400,8 +418,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| Date currentDate = new Date(); | Date currentDate = new Date(); | ||||
| switch (enumOrderStatus) { | switch (enumOrderStatus) { | ||||
| case ORDER_STATUS_OVERTIME_CANCEL: | case ORDER_STATUS_OVERTIME_CANCEL: | ||||
| case ORDER_STATUS_REFUND_SUCCESS: | |||||
| { | |||||
| case ORDER_STATUS_REFUND_SUCCESS: { | |||||
| try { | try { | ||||
| stockBack(updateOrder); | stockBack(updateOrder); | ||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| @@ -416,7 +433,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| updateOrder.setOrderStatus(enumOrderStatus.getCode()); | updateOrder.setOrderStatus(enumOrderStatus.getCode()); | ||||
| updateOrder.setUpdateDate(currentDate); | updateOrder.setUpdateDate(currentDate); | ||||
| ret = wxOrderMapper.updateByPrimaryKey(updateOrder); | ret = wxOrderMapper.updateByPrimaryKey(updateOrder); | ||||
| }catch (Exception e) { | |||||
| } catch (Exception e) { | |||||
| logger.error("订单状态更新失败, e:" + e.getMessage()); | logger.error("订单状态更新失败, e:" + e.getMessage()); | ||||
| throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单状态更新失败, e:" + e.getMessage()); | throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单状态更新失败, e:" + e.getMessage()); | ||||
| } | } | ||||
| @@ -433,7 +450,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| if (record.getId() == null) { | if (record.getId() == null) { | ||||
| //record.setId(UUID.randomUUID().toString().replaceAll("-", "")); | //record.setId(UUID.randomUUID().toString().replaceAll("-", "")); | ||||
| final IdWorker idWorker = IdWorker.get(); | final IdWorker idWorker = IdWorker.get(); | ||||
| record.setId(idWorker.nextId()); | |||||
| record.setId(idWorker.nextId()); | |||||
| wxOrderMapper.insertSelective(record); | wxOrderMapper.insertSelective(record); | ||||
| } else { | } else { | ||||
| wxOrderMapper.updateByPrimaryKeySelective(record); | wxOrderMapper.updateByPrimaryKeySelective(record); | ||||