package com.aiotagro.cattletrade.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aiotagro.cattletrade.business.service.IJbqServerService;
import com.aiotagro.common.core.web.domain.AjaxResult;
import com.aiotagro.common.core.web.domain.PageResultResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 主机设备控制器
 *
 * @author System
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/jbqServer")
public class JbqServerController {

    @Autowired
    private IJbqServerService jbqServerService;

    /**
     * 主机设备列表查询
     */
    @SaCheckPermission("delivery:view")
    @PostMapping("/jbqCheckList")
    public PageResultResponse<Map<String, Object>> jbqCheckList(@RequestBody Map<String, Object> params) {
        try {
            Integer pageNum = params.get("pageNum") != null ? (Integer) params.get("pageNum") : 1;
            Integer pageSize = params.get("pageSize") != null ? (Integer) params.get("pageSize") : 10;
            String deviceId = (String) params.get("deviceId");

            Page<Map<String, Object>> result = PageHelper.startPage(pageNum, pageSize);
            
            // 查询主机设备列表
            // 这里可以根据实际需求实现主机设备查询逻辑
            // 暂时返回空列表，避免编译错误
            return new PageResultResponse<>(0L, java.util.Collections.emptyList());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResultResponse<>(0L, java.util.Collections.emptyList());
        }
    }

    @SaCheckPermission("delivery:view")
    @PostMapping("/serverList")
    public PageResultResponse<Map<String, Object>> serverList(@RequestBody Map<String, Object> params) {
        try {
            Integer pageNum = params.get("pageNum") != null ? (Integer) params.get("pageNum") : 1;
            Integer pageSize = params.get("pageSize") != null ? (Integer) params.get("pageSize") : 10;
            String deviceId = (String) params.get("deviceId");

            Page<Map<String, Object>> result = PageHelper.startPage(pageNum, pageSize);
            
            // 调用服务层查询主机设备列表
            com.aiotagro.cattletrade.business.dto.DeviceDto dto = new com.aiotagro.cattletrade.business.dto.DeviceDto();
            dto.setDeviceId(deviceId);
            
            PageResultResponse response = jbqServerService.serverList(dto);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResultResponse<>(0L, java.util.Collections.emptyList());
        }
    }

    /**
     * 查询分页列表（通用）
     */
    @PostMapping(value = "/pageQuery")
    public PageResultResponse pageQuery(@RequestBody Map<String, Object> params) {
        try {
            Integer pageNum = params.get("pageNum") != null ? (Integer) params.get("pageNum") : 1;
            Integer pageSize = params.get("pageSize") != null ? (Integer) params.get("pageSize") : 10;
            String deviceId = (String) params.get("deviceId");

            Page<Map<String, Object>> result = PageHelper.startPage(pageNum, pageSize);
            
            // 调用服务层查询主机设备列表
            com.aiotagro.cattletrade.business.dto.DeviceDto dto = new com.aiotagro.cattletrade.business.dto.DeviceDto();
            dto.setDeviceId(deviceId);
            dto.setPageNum(pageNum);
            dto.setPageSize(pageSize);
            
            PageResultResponse response = jbqServerService.serverList(dto);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResultResponse<>(0L, java.util.Collections.emptyList());
        }
    }

    /**
     * 测试智能主机查询
     */
    @GetMapping("/testServerList")
    public AjaxResult testServerList() {
        try {
            com.aiotagro.cattletrade.business.dto.DeviceDto dto = new com.aiotagro.cattletrade.business.dto.DeviceDto();
            PageResultResponse response = jbqServerService.serverList(dto);
            return AjaxResult.success("测试结果", response);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("测试失败: " + e.getMessage());
        }
    }
}

