| @@ -1,5 +1,6 @@ | |||||
| package com.iformall.service.impl; | package com.iformall.service.impl; | ||||
| import com.alibaba.fastjson.JSON; | |||||
| import com.alibaba.fastjson.JSONObject; | import com.alibaba.fastjson.JSONObject; | ||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||||
| import com.github.pagehelper.PageHelper; | import com.github.pagehelper.PageHelper; | ||||
| @@ -19,9 +20,12 @@ import com.iformall.mapper.*; | |||||
| import com.iformall.mq.MqBaseProducer; | import com.iformall.mq.MqBaseProducer; | ||||
| import com.iformall.service.*; | import com.iformall.service.*; | ||||
| import com.iformall.utils.*; | import com.iformall.utils.*; | ||||
| import org.apache.commons.lang3.StringUtils; | |||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.beans.factory.annotation.Qualifier; | |||||
| import org.springframework.data.redis.core.RedisTemplate; | |||||
| import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | ||||
| import org.springframework.transaction.annotation.Propagation; | import org.springframework.transaction.annotation.Propagation; | ||||
| import org.springframework.transaction.annotation.Transactional; | import org.springframework.transaction.annotation.Transactional; | ||||
| @@ -32,6 +36,7 @@ import java.math.BigDecimal; | |||||
| import java.text.ParseException; | import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | import java.text.SimpleDateFormat; | ||||
| import java.util.*; | import java.util.*; | ||||
| import java.util.concurrent.TimeUnit; | |||||
| import java.util.stream.Collectors; | import java.util.stream.Collectors; | ||||
| @Service | @Service | ||||
| @@ -660,7 +665,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| } | } | ||||
| record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | ||||
| record.setPayment(payment); | record.setPayment(payment); | ||||
| record.setTotalPayment(payment); | |||||
| record.setTotalPayment(payment); | |||||
| record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | ||||
| record.setDetail(bodyStr); | record.setDetail(bodyStr); | ||||
| record.setCreateDate(curr); | record.setCreateDate(curr); | ||||
| @@ -697,7 +702,7 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| record.setCUserId(cUserId); | record.setCUserId(cUserId); | ||||
| record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); | ||||
| record.setPayment(payment); | record.setPayment(payment); | ||||
| record.setTotalPayment(payment); | |||||
| record.setTotalPayment(payment); | |||||
| record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode()); | ||||
| record.setDetail(bodyStr); | record.setDetail(bodyStr); | ||||
| record.setCreateDate(curr); | record.setCreateDate(curr); | ||||
| @@ -1618,11 +1623,26 @@ public class WxOrderServiceImpl implements WxOrderService { | |||||
| wxOrderMapper.updateStatusByPressCouponId(updateOrder); | wxOrderMapper.updateStatusByPressCouponId(updateOrder); | ||||
| } | } | ||||
| @Autowired | |||||
| @Qualifier("cuserTokenRedisTemplate") | |||||
| RedisTemplate<String, WxCUser> cUserTokenRedisTemplate; | |||||
| @Override | @Override | ||||
| public WxOrder saveOrderForCoupon(WxCUser user, WxCoupon coupon, OrderSaveDto orderSaveDto, boolean isPress) { | public WxOrder saveOrderForCoupon(WxCUser user, WxCoupon coupon, OrderSaveDto orderSaveDto, boolean isPress) { | ||||
| WxOrder order = null; | WxOrder order = null; | ||||
| // 防止同一用户重复抽奖(一分钟内) | |||||
| // 1. 订单保存时 加入redis缓存,key --> action:userId:couponId , 时间1000ms | |||||
| // 2. 如果存在,说明该用户正在下单,返回异常提示 | |||||
| // 3. 方法结束时清除 key | |||||
| if (checkCouponIsFree(coupon)) { | if (checkCouponIsFree(coupon)) { | ||||
| // 免费券 | // 免费券 | ||||
| String key = StringUtils.join("action", ":", user.getId(), coupon.getId()); | |||||
| boolean hasKey = cUserTokenRedisTemplate.hasKey(key); | |||||
| if (hasKey) { | |||||
| logger.error("用户正在下单,请求异常返回,user: {} " + JSON.toJSONString(user)); | |||||
| throw new MallinkException(ErrorCode.SYS_REPEAT_SUBMIT_EXCEPTION); | |||||
| } | |||||
| cUserTokenRedisTemplate.opsForValue().set(key, user, 1000, TimeUnit.MILLISECONDS); | |||||
| order = saveFreeOrderForCoupon(user, coupon, orderSaveDto.getCouponChannelId(), orderSaveDto.getFormId(), null); | order = saveFreeOrderForCoupon(user, coupon, orderSaveDto.getCouponChannelId(), orderSaveDto.getFormId(), null); | ||||
| if (order != null) { | if (order != null) { | ||||
| // 6. 下订单完成,发送内部消息 | // 6. 下订单完成,发送内部消息 | ||||