package org.wfc.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.wfc.common.core.utils.PageUtils;
import org.wfc.common.core.utils.ResponseUtils;
import org.wfc.common.core.web.page.TableDataInfo;
import org.wfc.omada.api.organization.OmadaSiteApi;
import org.wfc.omada.api.organization.model.CreateSiteEntity;
import org.wfc.omada.api.organization.model.OperationResponseGridVoSiteSummaryInfo;
import org.wfc.omada.api.organization.model.OperationResponseObject;
import org.wfc.omada.api.organization.model.OperationResponseSiteEntity;
import org.wfc.omada.api.organization.model.OperationResponseWithoutResult;
import org.wfc.omada.api.organization.model.SiteEntity;
import org.wfc.omada.api.organization.model.UpdateSiteEntity;
import org.wfc.system.service.ISysSiteService;

import java.util.Objects;

/**
 * @description: site service impl
 * @author: cyc
 * @since: 2025-02-18
 */
@Service
public class SysSiteServiceImpl implements ISysSiteService {

    @Autowired
    private OmadaSiteApi omadaSiteApi;

    @Override
    public TableDataInfo getSiteList(Integer pageNum, Integer pageSize) {
        ResponseEntity<OperationResponseGridVoSiteSummaryInfo> response = omadaSiteApi.getSiteList(pageNum, pageSize);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return PageUtils.getDataTable(response.getBody().getResult().getData(), response.getBody().getResult().getTotalRows());
    }

    @Override
    public SiteEntity getSiteEntity(String siteId) {
        ResponseEntity<OperationResponseSiteEntity> response = omadaSiteApi.getSiteEntity(siteId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return response.getBody().getResult();
    }

    @Override
    public boolean createNewSite(CreateSiteEntity createSiteEntity) {
        ResponseEntity<OperationResponseObject> response = omadaSiteApi.createNewSite(createSiteEntity);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }

    @Override
    public boolean updateSiteEntity(String siteId, UpdateSiteEntity updateSiteEntity) {
        ResponseEntity<OperationResponseWithoutResult> response = omadaSiteApi.updateSiteEntity(siteId, updateSiteEntity);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }

    @Override
    public boolean deleteSite(String siteId) {
        ResponseEntity<OperationResponseWithoutResult> response = omadaSiteApi.deleteSite(siteId);
        ResponseUtils.checkResponse(Objects.requireNonNull(response.getBody()).getErrorCode(), response.getBody().getMsg());
        return true;
    }
}
