com.taotao.cloud.idempotent.enhance.db.MySqlIdempotentRepositoryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-cloud-starter-idempotent Show documentation
Show all versions of taotao-cloud-starter-idempotent Show documentation
taotao-cloud-starter-idempotent
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.cloud.idempotent.enhance.db;
import com.taotao.cloud.common.utils.log.LogUtils;
import com.taotao.cloud.idempotent.enhance.core.constants.IdempotentConstant;
import com.taotao.cloud.idempotent.enhance.core.pojo.IdempotentEntity;
import com.taotao.cloud.idempotent.enhance.core.repository.IdempotentRepository;
import com.taotao.cloud.idempotent.enhance.db.mapper.BusinessIdempotentMapper;
import java.util.Optional;
import org.springframework.dao.DuplicateKeyException;
/** 基于MySQL的repository实现 */
public class MySqlIdempotentRepositoryImpl implements IdempotentRepository {
private final BusinessIdempotentMapper businessIdempotentMapper;
public MySqlIdempotentRepositoryImpl(BusinessIdempotentMapper businessIdempotentMapper) {
this.businessIdempotentMapper = businessIdempotentMapper;
}
@Override
public boolean create(IdempotentEntity idempotentEntity) {
try {
Integer insert = businessIdempotentMapper.insert(idempotentEntity);
return IdempotentConstant.Digit.ONE.equals(insert);
} catch (DuplicateKeyException ex) {
LogUtils.error("{} DuplicateKeyException.", idempotentEntity.getBusinessKey(), ex);
return false;
}
}
@Override
public Boolean changeIdempotentStatusProcessing(IdempotentEntity idempotentEntity) {
Integer update = businessIdempotentMapper.changeIdempotentStatusProcessing(idempotentEntity);
// 更新成功后将版本号同步 +1
if (IdempotentConstant.Digit.ONE.equals(update)) {
idempotentEntity.setObjectVersionNumber(idempotentEntity.getObjectVersionNumber() + 1);
return true;
}
return false;
}
@Override
public boolean changeIdempotentStatusSuccess(IdempotentEntity idempotentEntity) {
Integer update = businessIdempotentMapper.changeIdempotentStatusSuccess(idempotentEntity);
// 更新成功后将版本号同步 +1
if (IdempotentConstant.Digit.ONE.equals(update)) {
idempotentEntity.setObjectVersionNumber(idempotentEntity.getObjectVersionNumber() + 1);
return true;
}
return false;
}
@Override
public Boolean delete(IdempotentEntity idempotentEntity) {
Integer delete = businessIdempotentMapper.deleteByUniqueKey(idempotentEntity.getUniqueKey());
return IdempotentConstant.Digit.ONE.equals(delete);
}
@Override
public Optional query(IdempotentEntity idempotentEntity) {
// 通过唯一键查询
IdempotentEntity entity = businessIdempotentMapper.queryByUniqueKey(idempotentEntity.getUniqueKey());
return Optional.ofNullable(entity);
}
}