All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.boot.idempotent.enhance.db.MySqlIdempotentRepositoryImpl Maven / Gradle / Ivy

The newest version!
/*
 * 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.boot.idempotent.enhance.db;

import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.idempotent.enhance.core.constants.IdempotentConstant;
import com.taotao.boot.idempotent.enhance.core.pojo.IdempotentEntity;
import com.taotao.boot.idempotent.enhance.core.repository.IdempotentRepository;
import com.taotao.boot.idempotent.enhance.db.mapper.BusinessIdempotentMapper;
import org.springframework.dao.DuplicateKeyException;

import java.util.Optional;

/** 基于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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy