# PastReservationCreditLimitServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.internationalization.InternationalizationNumberDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.frontoffice.entity.pastreservation.PastReservationCreditLimit;
    import cloud.wconnect.frontoffice.mapper.pastreservation.PastReservationCreditLimitMapper;
    import cloud.wconnect.frontoffice.pojo.bo.pastreservation.CreditLimitInfoBO;
    import cloud.wconnect.frontoffice.service.pastreservation.IPastReservationCreditLimitService;
    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.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.math.RoundingMode;
    import java.util.List;
    import java.util.stream.Collectors;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-12-12
     */
    @Service
    @DS("logbook")
    public class PastReservationCreditLimitServiceImpl extends ServiceImpl<PastReservationCreditLimitMapper, PastReservationCreditLimit> implements IPastReservationCreditLimitService {

        /**
         * 查询 credit limit
         *
         * @param propertyId 资产 id
         * @param fullNames  full name
         * @return credit limit
         * @author yyt
         * @date 2024/12/12
         **/
        @Override
        public List<PastReservationCreditLimit> list(Integer propertyId, List<String> fullNames) {
            return list(new QueryWrapper<PastReservationCreditLimit>().lambda()
                    .eq(PastReservationCreditLimit::getPropertyId, propertyId)
                    .in(PastReservationCreditLimit::getFullName, fullNames));
        }

        /**
         * 处理 Credit Limit Data
         *
         * @param property 资产
         * @param dataBO   credit limit
         * @param uid      操作人
         * @author yyt
         * @date 2024/12/12
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void handleCreditLimitData(PropertyDTO property, List<CreditLimitInfoBO> dataBO, Integer uid) {
            Integer propertyId = property.getId();
            // 先清除，再保存
            remove(new QueryWrapper<PastReservationCreditLimit>().lambda().eq(PastReservationCreditLimit::getPropertyId, propertyId));

            InternationalizationNumberDTO number = property.getInternationalization().getNumber();
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(number.getCalculationRule());
            int dataPrecision = BaseConstant.DATA_PRECISION;

            // 保存
            if (CollectionUtils.isNotEmpty(dataBO)) {
                long now = DateTimeUtils.nowTimestamp();
                saveBatch(dataBO.stream().map(item -> PastReservationCreditLimit.builder()
                                .propertyId(propertyId)
                                .fullName(item.getFullName())
                                .roomNumber(item.getRoomNumber())
                                .totalBalance(item.getTotalBalance().setScale(dataPrecision, roundingMode))
                                .totalApprovalAmount(item.getTotalApprovalAmount().setScale(dataPrecision, roundingMode))
                                .createdBy(uid)
                                .createdTime(now)
                                .build())
                        .collect(Collectors.toList()));
            }
        }

    }