package com.ruoyi.base.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.base.domain.BaseSite;
import com.ruoyi.base.service.IBaseSiteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 站点管理Controller
 *
 * @author vinjor-m
 * @date 2025-06-19
 */
@RestController
@RequestMapping("/base/site")
public class BaseSiteController extends BaseController {
    @Autowired
    private IBaseSiteService baseSiteService;

    /**
     * 查询站点管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:site:list')")
    @GetMapping("/list")
    public AjaxResult list(BaseSite baseSite,
                           @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<BaseSite> page = new Page<>(pageNum, pageSize);
        IPage<BaseSite> list = baseSiteService.queryListPage(baseSite, page);
        return success(list);
    }

    /**
     * 查所有站点-不分页
     *
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @author vinjor-M
     * @date 14:40 2025/6/19
     **/
    @GetMapping("/listAll")
    public AjaxResult list() {
        return success(baseSiteService.list());
    }

    /**
     * 导出站点管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:site:export')")
    @Log(title = "站点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseSite baseSite) {
        List<BaseSite> list = baseSiteService.list();
        ExcelUtil<BaseSite> util = new ExcelUtil<BaseSite>(BaseSite.class);
        util.exportExcel(response, list, "站点管理数据");
    }

    /**
     * 获取站点管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:site:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        System.out.println(baseSiteService.getById(id));
        return success(baseSiteService.getById(id));
    }

    /**
     * 新增站点管理
     */
    @PreAuthorize("@ss.hasPermi('base:site:add')")
    @Log(title = "站点管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseSite baseSite) {
        return toAjax(baseSiteService.saveOrUpdate(baseSite));
    }

    /**
     * 修改站点管理
     */
    @PreAuthorize("@ss.hasPermi('base:site:edit')")
    @Log(title = "站点管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseSite baseSite) {
        return toAjax(baseSiteService.saveOrUpdate(baseSite));
    }

    /**
     * 删除站点管理
     */
    @PreAuthorize("@ss.hasPermi('base:site:remove')")
    @Log(title = "站点管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        List<String> list = new ArrayList<>(Arrays.asList(ids));
        return toAjax(baseSiteService.removeByIds(list));
    }
}
