package org.wfc.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.wfc.omada.api.client.OmadaClientApi;
import org.wfc.omada.api.client.model.ClientInfo;
import org.wfc.omada.api.client.model.OperationResponseClientGridVoClientInfo;
import org.wfc.omada.api.organization.OmadaSiteApi;
import org.wfc.omada.api.organization.model.OperationResponseGridVoSiteSummaryInfo;
import org.wfc.omada.api.organization.model.SiteSummaryInfo;
import org.wfc.system.domain.UClient;
import org.wfc.system.mapper.UClientMapper;
import org.wfc.system.service.IUClientService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 用户平台_用户设备表 服务实现类
 * </p>
 *
 * @author sys
 * @since 2025-01-03
 */
@Service
public class UClientServiceImpl extends ServiceImpl<UClientMapper, UClient> implements IUClientService {

    @Autowired
    private OmadaSiteApi omadaSiteApi;

    @Autowired
    private OmadaClientApi omadaClientApi;

    public List<ClientInfo> clientList(String searchKey) {
        ResponseEntity<OperationResponseGridVoSiteSummaryInfo> siteResp = omadaSiteApi.getSiteList(1, 1000);
        if (ObjectUtil.isNull(siteResp.getBody())) {
            return Collections.emptyList();
        }
        List<SiteSummaryInfo> sites = siteResp.getBody().getResult().getData();
        List<ClientInfo> clients = new ArrayList<>();
        for (SiteSummaryInfo site : sites) {
            ResponseEntity<OperationResponseClientGridVoClientInfo> clientResp = omadaClientApi.getGridActiveClients(site.getSiteId(), 1, 1000, searchKey);
            if (ObjectUtil.isNull(clientResp.getBody())) {
                continue;
            }
            clients.addAll(clientResp.getBody().getResult().getData());
        }
        return clients;
    }
}
