# GstLedgTotalAmountServiceImpl.java

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

    import cloud.wconnect.frontoffice.entity.guestledgercheck.GstLedgTotalAmount;
    import cloud.wconnect.frontoffice.mapper.guestledgercheck.GstLedgTotalAmountMapper;
    import cloud.wconnect.frontoffice.service.guestledgercheck.IGstLedgTotalAmountService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-03-17
     */
    @Service
    @DS("logbook")
    public class GstLedgTotalAmountServiceImpl extends ServiceImpl<GstLedgTotalAmountMapper, GstLedgTotalAmount> implements IGstLedgTotalAmountService {

        @Autowired
        private GstLedgTotalAmountMapper gstLedgTotalAmountMapper;

        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(Integer propertyId, BigDecimal writeOffAmount) {
            //利用排它锁 获取数据
            GstLedgTotalAmount one = gstLedgTotalAmountMapper.getOneForUpdate(propertyId);
            if (Objects.isNull(one)) {
                //新增
                GstLedgTotalAmount insertEntity = new GstLedgTotalAmount();
                insertEntity.setPropertyId(propertyId).setWriteOffAmount(writeOffAmount);
                save(insertEntity);
            } else {
                //累计并更新
                one.setWriteOffAmount(one.getWriteOffAmount().add(writeOffAmount));
                updateById(one);
            }
        }

        /**
         * 获取totalAmount
         *
         * @param propertyId 资产Id
         * @return java.math.BigDecimal
         * @author lichen
         * @date 2024/3/16
         */
        @Override
        public BigDecimal getTotalAmountByPropertyId(Integer propertyId) {
            GstLedgTotalAmount gstLedgTotalAmount = getOne(new LambdaQueryWrapper<GstLedgTotalAmount>()
                    .eq(GstLedgTotalAmount::getPropertyId, propertyId).last("limit 1"));
            return Objects.isNull(gstLedgTotalAmount) ? BigDecimal.ZERO : gstLedgTotalAmount.getWriteOffAmount();
        }

        /**
         * 重置totalAmount为0
         *
         * @param propertyId 资产Id
         * @return void
         * @author lichen
         * @date 2024/3/16
         */
        @Override
        public void resetZero(int propertyId) {
            update(new LambdaUpdateWrapper<GstLedgTotalAmount>()
                    .eq(GstLedgTotalAmount::getPropertyId, propertyId)
                    .set(GstLedgTotalAmount::getWriteOffAmount, BigDecimal.ZERO));
        }
    }