# InternalEventSourceController.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.EventSourceSetting;
    import cloud.wconnect.rateshop.entity.rateshop.EventSourceUrl;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.internal.InternalEventSourceUrlDTO;
    import cloud.wconnect.rateshop.service.rateshop.IEventSourceSettingService;
    import cloud.wconnect.rateshop.service.rateshop.IEventSourceUrlService;
    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.*;

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

    /**
     * 抓取事件数据源 控制层
     *
     * @author lichen
     * @date 2023/6/19
     */
    @RestController
    @RequestMapping("/internal/event-sources")
    public class InternalEventSourceController {

        @Value("${internal.service.auth-key.basic}")
        private String authKey;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private IEventSourceSettingService eventSourceSettingService;
        @Autowired
        private IEventSourceUrlService eventSourceUrlService;
        @Autowired
        private PropertyInternalService propertyInternalService;

        /**
         * 查询事件源信息
         *
         * @param sign 签名
         * @param page 页码
         * @param size 每页大小
         * @return cloud.wconnect.commonlib.pojo.dto.ListPagedDTO<cloud.wconnect.rateshop.pojo.dto.internal.InternalEventSourceDTO>
         * @author lichen
         * @date 2023/6/19
         */
        @GetMapping
        public ListPagedDTO<InternalEventSourceUrlDTO> list(@RequestHeader String sign,
                                                            @RequestParam(required = false) Integer page,
                                                            @RequestParam(required = false) Integer size,
                                                            @RequestParam Integer datasourceId) {
            //计算签名
            String actualSign;
            if (Objects.nonNull(page) && Objects.nonNull(size)) {
                actualSign = SignUtils.signWithKey(authKey, page, size, datasourceId);
            } else {
                actualSign = SignUtils.signWithKey(authKey, "", "", datasourceId);
            }

            //校验签名
            if (!actualSign.equals(sign)) {
                String logMsg = MessageFormat.format("The parameters may be modified illegally; expectedSign:{0}, "
                        + "actualSign:{1}, authKey:{2}", sign, actualSign, authKey);
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(), logMsg);
            }

            //资产配置的event source setting
            List<EventSourceSetting> eventSourceSettingList = eventSourceSettingService.list();
            if(CollectionUtils.isEmpty(eventSourceSettingList)) return new ListPagedDTO<>();

            //资产ID集合
            List<Integer> propertyIds = eventSourceSettingList.stream().map(EventSourceSetting::getPropertyId).toList();
            //开通功能的的资产ID
            OpenFunctionPropertyQuery openFunctionPropertyQuery = new OpenFunctionPropertyQuery();
            openFunctionPropertyQuery.setIds(propertyIds);
            openFunctionPropertyQuery.setOpenFunctionIds(Lists.newArrayList(FunctionIdConstant.RATE_SHOP));
            List<Integer> openPropertyIds = propertyInternalService.getOpenFunctionProperty(openFunctionPropertyQuery)
                    .stream().filter(PropertyDTO::getStatus).map(PropertyDTO::getId).toList();
            if(CollectionUtils.isEmpty(openPropertyIds)) return new ListPagedDTO<>();

            //过滤出开通的资产配置的url
            Set<Integer> allUrlId = eventSourceSettingList.stream()
                    .filter(setting -> openPropertyIds.contains(setting.getPropertyId()))
                    .map(EventSourceSetting::getUrlId).collect(Collectors.toSet());

            //查询事件源信息
            long total;
            List<EventSourceUrl> records;
            if (Objects.nonNull(page) && Objects.nonNull(size)) {
                Page<EventSourceUrl> pageResult = eventSourceUrlService.page(new Page<>(page, size),
                        new LambdaQueryWrapper<EventSourceUrl>()
                                .in(EventSourceUrl::getId, allUrlId)
                                .eq(EventSourceUrl::getDatasourceId, datasourceId)
                                .orderByDesc(EventSourceUrl::getId));
                total = pageResult.getTotal();
                records = pageResult.getRecords();
            } else {
                records = eventSourceUrlService.list(new LambdaQueryWrapper<EventSourceUrl>()
                        .in(EventSourceUrl::getId, allUrlId)
                        .eq(EventSourceUrl::getDatasourceId, datasourceId));
                total = records.size();
            }

            if (total == 0L) return new ListPagedDTO<>();
            return new ListPagedDTO<>(total, beanMapper.mapList(records, InternalEventSourceUrlDTO.class));
        }

    }