# RevenueOtbRevenueLastYearServiceImpl.java

    package cloud.wconnect.rateshop.service.revenue.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    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.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.rateshop.entity.revenue.RevenueOtbRevenueLastYear;
    import cloud.wconnect.rateshop.mapper.revenue.RevenueOtbRevenueLastYearMapper;
    import cloud.wconnect.rateshop.pojo.dto.revenue.LastYearOtbDTO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueOtbRevenueLastYearService;
    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.RoundingMode;
    import java.time.ZonedDateTime;
    import java.util.Objects;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2024-03-23
     */
    @Service
    @DS("rate_shop")
    public class RevenueOtbRevenueLastYearServiceImpl extends ServiceImpl<RevenueOtbRevenueLastYearMapper, RevenueOtbRevenueLastYear> implements IRevenueOtbRevenueLastYearService {

        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;

        /**
         * 获取 LastYearOtb
         *
         * @param propertyDTO 资产
         * @param date        日期
         * @return budget
         * @author yyt
         * @date 2024/3/23
         */
        @Override
        public LastYearOtbDTO getLastYearOtbDTO(PropertyDTO propertyDTO, long date) {
            ZonedDateTime targetDate = DateTimeUtils.toZonedDateTime(date, propertyDTO.getTimezone());
            RevenueOtbRevenueLastYear lastYearOtbRevenue = getOne(new QueryWrapper<RevenueOtbRevenueLastYear>().lambda()
                    .eq(RevenueOtbRevenueLastYear::getPropertyId, propertyDTO.getId())
                    .eq(RevenueOtbRevenueLastYear::getTheYear, targetDate.getYear() - 1)
                    .eq(RevenueOtbRevenueLastYear::getTheMonth, targetDate.getMonthValue()));
            if (Objects.isNull(lastYearOtbRevenue)) {
                return new LastYearOtbDTO();
            }

            LastYearOtbDTO lastYearOtbDTO = beanMapper.map(lastYearOtbRevenue, LastYearOtbDTO.class);
            lastYearOtbDTO.setOtbRevPar(lastYearOtbRevenue.getOtbRevpar());
            return lastYearOtbDTO;
        }

        /**
         * 保存 LastYearOtb
         *
         * @param propertyDTO    资产
         * @param year           年
         * @param month          月
         * @param lastYearOtbDTO LastYearOtb
         * @param userId         操作人
         * @author yyt
         * @date 2024/3/23
         **/
        @Override
        public void save(PropertyDTO propertyDTO, Integer year, Integer month, LastYearOtbDTO lastYearOtbDTO, int userId) {
            Integer propertyId = propertyDTO.getId();
            long now = DateTimeUtils.nowTimestamp();

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

            RevenueOtbRevenueLastYear revenueLastYear = new RevenueOtbRevenueLastYear();
            revenueLastYear.setOtbRevenue(Objects.isNull(lastYearOtbDTO.getOtbRevenue()) ? null :
                            lastYearOtbDTO.getOtbRevenue().setScale(dataPrecision, roundingMode))
                    .setOtbAdr(Objects.isNull(lastYearOtbDTO.getOtbAdr()) ? null
                            : lastYearOtbDTO.getOtbAdr().setScale(dataPrecision, roundingMode))
                    .setOtbOccPercent(Objects.isNull(lastYearOtbDTO.getOtbOccPercent()) ? null :
                            lastYearOtbDTO.getOtbOccPercent().setScale(dataPrecision, roundingMode))
                    .setBanquetRevenue(Objects.isNull(lastYearOtbDTO.getBanquetRevenue()) ? null :
                            lastYearOtbDTO.getBanquetRevenue().setScale(dataPrecision, roundingMode))
                    .setFbOtbRevenue(Objects.isNull(lastYearOtbDTO.getFbOtbRevenue()) ? null :
                            lastYearOtbDTO.getFbOtbRevenue().setScale(dataPrecision, roundingMode))
                    .setPropertyId(propertyId).setTheYear(year).setTheMonth(month).setCreatedBy(userId)
                    .setCreatedTime(now).setUpdatedBy(userId).setUpdatedTime(now);

            // 计算 revpar
            revenueLastYear.calculateRevpar(number);

            baseMapper.saveOrUpdate(revenueLastYear);
        }

        /**
         * 查询 last year
         *
         * @param propertyId 资产 id
         * @param year       年
         * @param month      月
         * @return last year
         * @author yyt
         * @date 2024/6/14
         **/
        @Override
        public RevenueOtbRevenueLastYear getLastYear(Integer propertyId, int year, int month) {
            return getOne(new QueryWrapper<RevenueOtbRevenueLastYear>().lambda()
                    .eq(RevenueOtbRevenueLastYear::getPropertyId, propertyId)
                    .eq(RevenueOtbRevenueLastYear::getTheYear, year).eq(RevenueOtbRevenueLastYear::getTheMonth, month));
        }

    }