防止一直在页面提交

This commit is contained in:
苏竹红
2024-06-03 21:28:39 +08:00
parent 9f2525cbdf
commit 7a221f7530

View File

@@ -9,6 +9,7 @@ import com.cool.store.dao.ShopStageInfoDAO;
import com.cool.store.entity.*;
import com.cool.store.enums.*;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.*;
import com.cool.store.request.LicenseListRequest;
import com.cool.store.request.SubmitLicenseRequest;
@@ -27,11 +28,13 @@ import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service
@@ -80,6 +83,9 @@ public class ApplyLicenseServiceImpl implements ApplyLicenseService {
@Resource
LineInfoMapper lineInfoMapper;
@Resource
StringRedisTemplate stringRedisTemplate;
@Override
@Transactional(rollbackFor = Exception.class)
@@ -88,6 +94,13 @@ public class ApplyLicenseServiceImpl implements ApplyLicenseService {
if (Objects.isNull(request)) {
return false;
}
String lockKey = "submitLicense:" + request.getShopId();
String lockValue = UUID.randomUUID().toString();
boolean acquired = Boolean.FALSE;
acquired = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, 1, TimeUnit.SECONDS);
if (!acquired) {
throw new ServiceException(ErrorCodeEnum.DUPLICATE_SUBMISSION);
}
LicenseTransactDO licenseTransactDO = request.toLicenseTransactDO();
//防止一直在页面提交
if (licenseTransactDO.getId() == null){