# AirportController.java

    package cloud.wconnect.rateshop.controller.rateshop;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.entity.rateshop.AirportSourceLocation;
    import cloud.wconnect.rateshop.entity.rateshop.AirportSourceSetting;
    import cloud.wconnect.rateshop.entity.rateshop.AirportSourceUrl;
    import cloud.wconnect.rateshop.entity.rateshop.CancellationFlights;
    import cloud.wconnect.rateshop.pojo.query.rateshop.CancellationFlightsQuery;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.AirportCancellationFlightsVO;
    import cloud.wconnect.rateshop.service.rateshop.IAirportSourceLocationService;
    import cloud.wconnect.rateshop.service.rateshop.IAirportSourceSettingService;
    import cloud.wconnect.rateshop.service.rateshop.IAirportSourceUrlService;
    import cloud.wconnect.rateshop.service.rateshop.ICancellationFlightsService;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;

    import jakarta.validation.Valid;
    import java.time.ZoneId;
    import java.time.ZonedDateTime;
    import java.util.Collections;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.function.Function;
    import java.util.stream.Collectors;

    /**
     * 机场数据 控制层
     *
     * @author zc
     * @date 2024/03/25
     */
    @RestController
    @RequestMapping("/rate-shop/airport")
    public class AirportController {

        @Autowired
        private IAirportSourceSettingService airportSourceSettingService;
        @Autowired
        private IAirportSourceLocationService airportSourceLocationService;
        @Autowired
        private IAirportSourceUrlService airportSourceUrlService;
        @Autowired
        private ICancellationFlightsService cancellationFlightsService;
        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;

        /**
         * 获取机场取消航班数
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return AirportCancellationFlightVO 集合
         * @author zc
         * @date 2024/3/25
         */
        @ApiId(1116)
        @GetMapping("/cancellation-flights")
        public List<AirportCancellationFlightsVO> list(@RequestParam String property, @Valid CancellationFlightsQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);

            List<AirportSourceSetting> airportSourceSettingList = airportSourceSettingService.listByPropertyId(propertyDTO.getId());
            if (CollectionUtils.isEmpty(airportSourceSettingList)) {
                return Collections.emptyList();
            }

            // 查询机场 location 信息
            Set<Integer> locationIds = airportSourceSettingList.stream().map(AirportSourceSetting::getLocationId).collect(Collectors.toSet());
            Map<Integer, String> locationId2Location = airportSourceLocationService.listByIds(locationIds)
                    .stream().collect(Collectors.toMap(AirportSourceLocation::getId, AirportSourceLocation::getLocation));

            // 查询机场 url 信息
            List<Integer> urlIds = airportSourceSettingList.stream().map(AirportSourceSetting::getUrlId).collect(Collectors.toList());
            Map<Integer, AirportSourceUrl> urlId2SourceUrl = airportSourceUrlService.listByIds(urlIds)
                    .stream().collect(Collectors.toMap(AirportSourceUrl::getId, Function.identity()));

            // 查询机场取消航班数量
            ZonedDateTime queryZonedDate = DateTimeUtils.strToLocalDate(query.getTheDate(),
                    BaseConstant.DATE_FORMAT_YYYY_MM_DD).atStartOfDay(ZoneId.of(propertyDTO.getTimezone()));
            List<CancellationFlights> cancellationFlightsList = cancellationFlightsService.list(new QueryWrapper<CancellationFlights>().lambda()
                    .eq(CancellationFlights::getTheDate, DateTimeUtils.parseToLong(queryZonedDate, BaseConstant.DATE_FORMAT_YYYYMMDD))
                    .in(CancellationFlights::getUrlId, urlIds));

            return beanMapper.mapList(cancellationFlightsList, AirportCancellationFlightsVO.class, (entity, vo) -> {
                // 设置机场 url 信息
                AirportSourceUrl airportSourceUrl = urlId2SourceUrl.get(entity.getUrlId());
                // 设置机场 location 信息
                vo.setLocation(locationId2Location.get(airportSourceUrl.getLocationId()));
                vo.setUrlSymbol(entity.getSymbol());
                vo.setUpdateTime(entity.getUpdatedTime());
            });
        }
    }