# InternalAirportController.java

    package cloud.wconnect.rateshop.controller.internal.rateshop;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.FunctionIdConstant;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.query.OpenFunctionPropertyQuery;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.SignUtils;
    import cloud.wconnect.rateshop.entity.rateshop.AirportSourceSetting;
    import cloud.wconnect.rateshop.entity.rateshop.AirportSourceUrl;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.internal.InternalAirportSourceUrlDTO;
    import cloud.wconnect.rateshop.pojo.query.rateshop.AirportSourceQuery;
    import cloud.wconnect.rateshop.service.rateshop.IAirportSourceSettingService;
    import cloud.wconnect.rateshop.service.rateshop.IAirportSourceUrlService;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestHeader;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import java.text.MessageFormat;
    import java.util.List;
    import java.util.Objects;
    import java.util.Set;
    import java.util.stream.Collectors;

    /**
     * Internal Controller: 抓取机场源
     *
     * @author zc
     * @date 2024/03/23
     */
    @RestController
    @RequestMapping("/internal/airport-sources")
    public class InternalAirportController {

        @Autowired
        private IAirportSourceSettingService settingService;
        @Autowired
        private IAirportSourceUrlService urlService;
        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private BeanMapper beanMapper;

        @Value("${internal.service.auth-key.basic}")
        private String authKey;

        /**
         * 需要抓取的机场数据源
         *
         * @param sign  签名
         * @param query 查询条件
         * @return InternalAirportSourceUrlDTO 集合
         * @author zc
         * @date 2024/3/23
         */
        @GetMapping
        public ListPagedDTO<InternalAirportSourceUrlDTO> list(@RequestHeader String sign, AirportSourceQuery query) {
            Integer page = query.getPage();
            Integer size = query.getSize();
            // 校验签名
            boolean isPaged = Objects.nonNull(page) && Objects.nonNull(size);
            String actualSign = isPaged ? SignUtils.signWithKey(authKey, page, size) : SignUtils.signWithKey(authKey, "");
            if (!actualSign.equals(sign)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        MessageFormat.format("The parameters may be modified illegally; expectedSign:{0}, "
                                + "actualSign:{1}, authKey:{2}", sign, actualSign, authKey));
            }

            // 获取所有配置数据
            List<AirportSourceSetting> sourceSettingList = settingService.list();
            if (CollectionUtils.isEmpty(sourceSettingList)) {
                return new ListPagedDTO<>();
            }

            Set<Integer> propertyIds = sourceSettingList.stream()
                    .map(AirportSourceSetting::getPropertyId).collect(Collectors.toSet());

            //开通模块的的资产ID
            OpenFunctionPropertyQuery openFunctionPropertyQuery = new OpenFunctionPropertyQuery();
            openFunctionPropertyQuery.setIds(Lists.newArrayList(propertyIds));
            openFunctionPropertyQuery.setOpenFunctionIds(Lists.newArrayList(FunctionIdConstant.RATE_SHOP));
            List<Integer> openPropertyIds = propertyInternalService.getOpenFunctionProperty(openFunctionPropertyQuery)
                    .stream().filter(PropertyDTO::getStatus).map(PropertyDTO::getId).toList();

            // 状态为开启的资产使用的url
            Set<Integer> allUrlId = sourceSettingList.stream()
                    .filter(item -> openPropertyIds.contains(item.getPropertyId()))
                    .map(AirportSourceSetting::getUrlId).collect(Collectors.toSet());
            if (CollectionUtils.isEmpty(allUrlId)) {
                return new ListPagedDTO<>();
            }

            long total;
            List<AirportSourceUrl> records;
            if (isPaged) {
                Page<AirportSourceUrl> pageResult = urlService.page(new Page<>(page, size),
                        new LambdaQueryWrapper<AirportSourceUrl>()
                                .in(AirportSourceUrl::getId, allUrlId)
                                .orderByDesc(AirportSourceUrl::getId));
                total = pageResult.getTotal();
                records = pageResult.getRecords();
            } else {
                records = urlService.list(new LambdaQueryWrapper<AirportSourceUrl>()
                        .in(AirportSourceUrl::getId, allUrlId));
                total = records.size();
            }
            if (CollectionUtils.isEmpty(records)) {
                return new ListPagedDTO<>();
            }

            return new ListPagedDTO<>(total, beanMapper.mapList(records, InternalAirportSourceUrlDTO.class));
        }
    }