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

com.higgschain.trust.slave.dao.rocks.account.AccountInfoRocksDao Maven / Gradle / Ivy

package com.higgschain.trust.slave.dao.rocks.account;

import com.higgschain.trust.common.dao.RocksBaseDao;
import com.higgschain.trust.common.utils.ThreadLocalUtils;
import com.higgschain.trust.slave.common.enums.SlaveErrorEnum;
import com.higgschain.trust.slave.common.exception.SlaveException;
import com.higgschain.trust.slave.dao.po.account.AccountInfoPO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.rocksdb.Transaction;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * The type Account info rocks dao.
 *
 * @author tangfashuang
 * @desc key : accountNo, value: AccountInfoPO
 */
@Service
@Slf4j
public class AccountInfoRocksDao extends RocksBaseDao {

    @Override protected String getColumnFamilyName() {
        return "accountInfo";
    }

    /**
     * Freeze.
     *
     * @param accountNo the account no
     * @param amount    the amount
     */
    public void freeze(String accountNo, BigDecimal amount) {
        AccountInfoPO po = get(accountNo);
        if (null == po) {
            throw new SlaveException(SlaveErrorEnum.SLAVE_ROCKS_KEY_IS_NOT_EXIST);
        }

        BigDecimal balance = po.getBalance();
        BigDecimal freezeAmount = po.getFreezeAmount();
        BigDecimal newFreezeAmount = freezeAmount.add(amount);

        if (BigDecimal.ZERO.compareTo(balance.subtract(amount)) > 0) {
            throw new SlaveException(SlaveErrorEnum.SLAVE_ACCOUNT_BALANCE_IS_NOT_ENOUGH_ERROR);
        }

        if ( BigDecimal.ZERO.compareTo(newFreezeAmount) > 0) {
            throw new SlaveException(SlaveErrorEnum.SLAVE_ACCOUNT_FREEZE_AMOUNT_ERROR);
        }

        po.setFreezeAmount(newFreezeAmount);
        po.setDetailFreezeNo(po.getDetailNo() + 1);
        po.setUpdateTime(new Date());

        put(accountNo, po);
    }

    /**
     * Un freeze.
     *
     * @param accountNo the account no
     * @param amount    the amount
     */
    public void unFreeze(String accountNo, BigDecimal amount) {
        AccountInfoPO po = get(accountNo);
        if (null == po) {
            throw new SlaveException(SlaveErrorEnum.SLAVE_ROCKS_KEY_IS_NOT_EXIST);
        }

        BigDecimal freezeAmount = po.getFreezeAmount();
        BigDecimal newFreezeAmount = freezeAmount.subtract(amount);

        if (BigDecimal.ZERO.compareTo(newFreezeAmount) > 0) {
            throw new SlaveException(SlaveErrorEnum.SLAVE_ACCOUNT_UNFREEZE_AMOUNT_ERROR);
        }

        po.setFreezeAmount(newFreezeAmount);
        po.setDetailFreezeNo(po.getDetailNo() + 1);
        po.setUpdateTime(new Date());

        put(accountNo, po);
    }

    /**
     * Batch insert.
     *
     * @param pos the pos
     */
    public void batchInsert(List pos) {
        if (CollectionUtils.isEmpty(pos)) {
            return;
        }

        Transaction tx = ThreadLocalUtils.getRocksTx();
        if (null == tx) {
            log.error("[AccountInfoRocksDao.batchInsert] transaction is null");
            throw new SlaveException(SlaveErrorEnum.SLAVE_ROCKS_TRANSACTION_IS_NULL);
        }

        for (AccountInfoPO po : pos) {
            if (null == po.getCreateTime()) {
                po.setCreateTime(new Date());
            } else {
                po.setUpdateTime(new Date());
            }
            txPut(tx, po.getAccountNo(), po);
        }

    }

    /**
     * Query by account nos list.
     *
     * @param accountNos the account nos
     * @return the list
     */
    public List queryByAccountNos(List accountNos) {
        if (CollectionUtils.isEmpty(accountNos)) {
            return null;
        }

        Map resultMap = multiGet(accountNos);
        if (MapUtils.isEmpty(resultMap)) {
            return null;
        }

        List pos = new ArrayList<>(resultMap.size());
        for (String key : resultMap.keySet()) {
            pos.add(resultMap.get(key));
        }

        return pos;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy