# GstLedgOverBalanceConfigServiceImpl.java

    package cloud.wconnect.frontoffice.service.guestledgercheck.impl;

    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.frontoffice.cons.guestledgercheck.GuestLedgerCheckErrorI18nConstant;
    import cloud.wconnect.frontoffice.entity.guestledgercheck.GstLedgOverBalanceConfig;
    import cloud.wconnect.frontoffice.mapper.guestledgercheck.GstLedgOverBalanceConfigMapper;
    import cloud.wconnect.frontoffice.pojo.dto.guestledgercheck.OverBalanceConfigDTO;
    import cloud.wconnect.frontoffice.service.guestledgercheck.IGstLedgOverBalanceConfigService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    import java.math.BigDecimal;
    import java.util.Objects;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author zc
     * @since 2023-12-14
     */
    @Service
    @DS("logbook")
    public class GstLedgOverBalanceConfigServiceImpl extends ServiceImpl<GstLedgOverBalanceConfigMapper, GstLedgOverBalanceConfig> implements IGstLedgOverBalanceConfigService {

        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 获取余额下限
         *
         * @param propertyId 资产 id
         * @return 余额下限
         * @author zc
         * @date 2023/12/14
         */
        @Override
        public BigDecimal get(int propertyId) {
            GstLedgOverBalanceConfig overBalanceConfig = getOne(new QueryWrapper<GstLedgOverBalanceConfig>().lambda()
                    .eq(GstLedgOverBalanceConfig::getPropertyId, propertyId));
            return Objects.isNull(overBalanceConfig) ? BigDecimal.ZERO : overBalanceConfig.getBalance();
        }

        /**
         * 保存余额下限配置
         *
         * @param propertyId           资产 id
         * @param operatorId           操作人 id
         * @param overBalanceConfigDTO 余额下限
         * @author zc
         * @date 2023/12/14
         */
        @Override
        public void save(int propertyId, int operatorId, OverBalanceConfigDTO overBalanceConfigDTO) {
            GstLedgOverBalanceConfig config = new GstLedgOverBalanceConfig();
            config.setPropertyId(propertyId);
            config.setBalance(overBalanceConfigDTO.getOverBalance());
            config.setUpdateBy(operatorId);
            config.setUpdateTime(DateTimeUtils.nowTimestamp());
            baseMapper.insertOrUpdateByUniqueKey(config);
        }

        /**
         * 校验资产余额是否超过余额下限
         *
         * @param propertyId 资产 id
         * @param amount     金额
         * @return void
         * @author lichen
         * @date 2025/8/25
         */
        @Override
        public void checkOverBalance(int propertyId, BigDecimal amount) {
            BigDecimal overBalanceByConfig = get(propertyId);
            if (amount.compareTo(overBalanceByConfig) <= 0) {
                throw new BadRequestException(GuestLedgerCheckErrorI18nConstant.GREATER_THAN_BALANCE_SETTING,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate(overBalanceByConfig.toString()),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The currentBalance must be greater than balance setting, balanceSetting:" + overBalanceByConfig);
            }
        }

    }