分布式锁,十秒过期

This commit is contained in:
guohb
2024-05-31 18:45:15 +08:00
parent 5e8408d74f
commit 0959ae0dd4
3 changed files with 86 additions and 37 deletions

View File

@@ -53,7 +53,6 @@ public class CoolStoreStartFlowServiceImpl implements CoolStoreStartFlowService
log.info("newStore API response:{}", JSONObject.toJSONString(initiatingResponse));
if (initiatingResponse.getCode() != 0L) {
throw new ServiceException(ErrorCodeEnum.FRANCHISE_AGREEMENT_FALSE, initiatingResponse.getMsg(),initiatingResponse.getData());
// return new ResponseResult(500, initiatingResponse.getMsg(), initiatingResponse.getData());
} else {
//更新阶段信息
shopStageInfoDAO.updateShopStageAndAuditInfo(shopId, ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_31, null);
@@ -79,7 +78,6 @@ public class CoolStoreStartFlowServiceImpl implements CoolStoreStartFlowService
InitiatingResponse initiatingResponse = httpRestTemplateService.postForObject(url, request, InitiatingResponse.class);
log.info("franchiseAgreement API response:{}", JSONObject.toJSONString(initiatingResponse));
if (initiatingResponse.getCode() != 0L) {
// throw new ServiceException(500, initiatingResponse.getMsg(), initiatingResponse.getData());
throw new ServiceException(ErrorCodeEnum.FRANCHISE_AGREEMENT_FALSE, initiatingResponse.getMsg(),initiatingResponse.getData());
} else {
return new ResponseResult(200000, initiatingResponse.getMsg(), initiatingResponse.getData());

View File

@@ -22,12 +22,14 @@ import com.cool.store.service.SignFranchiseService;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.utils.poi.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
@Service
@Slf4j
@@ -80,6 +82,11 @@ public class SignFranchiseServiceImpl implements SignFranchiseService, AuditResu
@Resource
CommonService commonService;
@Resource
private StringRedisTemplate redisTemplate;
@Override
public Boolean auditResult(AuditResultRequest request) {
log.info("SignFranchiseServiceImpl auditResult request{}", JSONObject.toJSONString(request));
@@ -135,24 +142,45 @@ public class SignFranchiseServiceImpl implements SignFranchiseService, AuditResu
if (Objects.isNull(request.getShopId())) {
throw new ServiceException(ErrorCodeEnum.SHOP_ID_NOT_EXIST);
}
SignFranchiseDO signFranchiseDO = request.toSignFranchiseDO();
if (Objects.isNull(request.getId())) {
signFranchiseMapper.insertSelective(signFranchiseDO);
} else {
signFranchiseMapper.updateByPrimaryKeySelective(signFranchiseDO);
String lockKey = "submitSignFranchise:" + request.getShopId();
//流水
String lockValue = UUID.randomUUID().toString();
boolean acquired = false;
try {
//10s过期
acquired = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, 10, TimeUnit.SECONDS);
if (Boolean.TRUE.equals(acquired)) {
SignFranchiseDO signFranchiseDO = request.toSignFranchiseDO();
if (Objects.isNull(request.getId())) {
signFranchiseMapper.insertSelective(signFranchiseDO);
} else {
signFranchiseMapper.updateByPrimaryKeySelective(signFranchiseDO);
}
//店铺信息
ShopInfoDO shopInfoDO = shopInfoMapper.selectByPrimaryKey(request.getShopId());
FranchiseAgreementRequest franchiseAgreementRequest = convertFranchiseAgreement(request, shopInfoDO, user);
MemberQuestionDO memberQuestionDO = joinIntentionMapper.getByLineId(shopInfoDO.getLineId());
log.info("submitSignFranchise franchiseAgreementRequest :{}",JSONObject.toJSONString(franchiseAgreementRequest));
ResponseResult responseResult = coolStoreStartFlowService.franchiseAgreement(franchiseAgreementRequest, memberQuestionDO.getJoinType());
//更新状态为加盟商
LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(shopInfoDO.getLineId());
lineInfoDO.setJoinStatus(2);
lineInfoMapper.insertOrUpdate(lineInfoDO);
shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_83);
return responseResult;
}else {
throw new ServiceException(ErrorCodeEnum.DUPLICATE_SUBMISSION);
}
}finally {
if (Boolean.TRUE.equals(acquired)) {
String currentValue = redisTemplate.opsForValue().get(lockKey);
if (lockValue.equals(currentValue)) {
redisTemplate.delete(lockKey);
}
}
}
//店铺信息
ShopInfoDO shopInfoDO = shopInfoMapper.selectByPrimaryKey(request.getShopId());
FranchiseAgreementRequest franchiseAgreementRequest = convertFranchiseAgreement(request, shopInfoDO, user);
MemberQuestionDO memberQuestionDO = joinIntentionMapper.getByLineId(shopInfoDO.getLineId());
log.info("submitSignFranchise franchiseAgreementRequest :{}",JSONObject.toJSONString(franchiseAgreementRequest));
ResponseResult responseResult = coolStoreStartFlowService.franchiseAgreement(franchiseAgreementRequest, memberQuestionDO.getJoinType());
//更新状态为加盟商
LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(shopInfoDO.getLineId());
lineInfoDO.setJoinStatus(2);
lineInfoMapper.insertOrUpdate(lineInfoDO);
shopStageInfoDAO.updateShopStageInfo(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_83);
return responseResult;
}

View File

@@ -23,17 +23,17 @@ import com.cool.store.service.CoolStoreStartFlowService;
import com.cool.store.service.PreparationService;
import com.cool.store.service.SysStoreAppService;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.utils.RedisUtilPool;
import com.cool.store.utils.poi.StringUtils;
import com.cool.store.utils.poi.constant.Constants;
import lombok.extern.slf4j.Slf4j;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -86,6 +86,12 @@ public class SysStoreAppServiceImpl implements SysStoreAppService, AuditResultSe
@Resource
PointDetailInfoMapper pointDetailInfoMapper;
@Resource
private StringRedisTemplate redisTemplate;
@Resource
RedisUtilPool redisUtilPool;
@Override
@Transactional(rollbackFor = Exception.class)
public ResponseResult submitSysBuildStore(SysStoreAppRequest request, LoginUserInfo user) {
@@ -93,21 +99,38 @@ public class SysStoreAppServiceImpl implements SysStoreAppService, AuditResultSe
if (Objects.isNull(request)) {
throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
}
//1.操作数据库
SystemBuildingShopDO systemBuildingShopDO = SystemBuildingShopDO.convertToSystemBuildingShopDO(request);
if (request.getId() == null) {
systemBuildingShopMapper.insertSelective(systemBuildingShopDO);
} else {
systemBuildingShopDO.setId(request.getId());
systemBuildingShopMapper.updateByPrimaryKeySelective(systemBuildingShopDO);
String lockKey = "submitSysBuildStore:" + request.getShopId();
String lockValue = UUID.randomUUID().toString();
boolean acquired = false;
try {
acquired = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, 10, TimeUnit.SECONDS);
if (Boolean.TRUE.equals(acquired)) {
//1.操作数据库
SystemBuildingShopDO systemBuildingShopDO = SystemBuildingShopDO.convertToSystemBuildingShopDO(request);
if (request.getId() == null) {
systemBuildingShopMapper.insertSelective(systemBuildingShopDO);
} else {
systemBuildingShopDO.setId(request.getId());
systemBuildingShopMapper.updateByPrimaryKeySelective(systemBuildingShopDO);
}
//2.查找、组装数组
LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(request.getLineId());//线索信息
PointInfoDO pointInfoDO = pointInfoMapper.getDataByShopIdAndLineId(request.getLineId(), request.getShopId());//查铺位信息确定铺位所在大区、战区、门店所在省市区街道地址
//3.请求鲜丰接口
NewStoreRequest apiRequest = convertToNewStoreRequest(request, lineInfoDO, pointInfoDO, user);
log.info("submitSysBuildStore apiRequest{}",JSONObject.toJSONString(apiRequest));
return coolStoreStartFlowService.newStore(apiRequest, request.getShopId());
}else {
throw new ServiceException(ErrorCodeEnum.DUPLICATE_SUBMISSION);
}
}finally {
if (Boolean.TRUE.equals(acquired)) {
String currentValue = redisTemplate.opsForValue().get(lockKey);
if (lockValue.equals(currentValue)) {
redisTemplate.delete(lockKey);
}
}
}
//2.查找、组装数组
LineInfoDO lineInfoDO = lineInfoMapper.getByLineId(request.getLineId());//线索信息
PointInfoDO pointInfoDO = pointInfoMapper.getDataByShopIdAndLineId(request.getLineId(), request.getShopId());//查铺位信息确定铺位所在大区、战区、门店所在省市区街道地址
//3.请求鲜丰接口
NewStoreRequest apiRequest = convertToNewStoreRequest(request, lineInfoDO, pointInfoDO, user);
log.info("submitSysBuildStore apiRequest{}",JSONObject.toJSONString(apiRequest));
return coolStoreStartFlowService.newStore(apiRequest, request.getShopId());
}
@Override