package org.example.controller;

import org.example.entity.Terminal;
import org.example.service.TerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/terminals")
public class TerminalController {

    @Autowired
    private TerminalService terminalService;

    /**
     * 获取所有设备列表
     * @return 设备列表
     */
    @GetMapping
    public List<Terminal> getAllTerminals(
            @RequestParam(required = false) String terminalType,
            @RequestParam(required = false) String deptCode,
            @RequestParam(required = false) String wardCode,
            @RequestParam(required = false) Boolean isOnline,
            @RequestParam(required = false) String terminalDesc) {
        List<Terminal> terminals;
        if (terminalType != null) {
            terminals = terminalService.findByTerminalTypeContaining(terminalType);
        } else if (deptCode != null) {
            terminals = terminalService.findByDeptCodeContaining(deptCode);
        } else if (wardCode != null) {
            terminals = terminalService.findByWardCodeContaining(wardCode);
        } else if (isOnline != null) {
            terminals = terminalService.findByIsOnline(isOnline);
        } else if (terminalDesc != null) {
            terminals = terminalService.findByTerminalDescContaining(terminalDesc);
        } else {
            // 否则返回所有设备
            terminals = terminalService.findAllTerminals();
        }
        return terminals;
    }

    /**
     * 根据设备编号获取设备信息
     * @param terminalNumber 设备编号
     * @return 设备信息
     */
    @GetMapping("/{terminalNumber}")
    public Map<String, Object> getTerminalByNumber(@PathVariable String terminalNumber) {
        Map<String, Object> result = new HashMap<>();
        try {
            Terminal terminal = terminalService.findByTerminalNumber(terminalNumber);
            if (terminal != null) {
                result.put("success", true);
                result.put("data", terminal);
            } else {
                result.put("success", false);
                result.put("message", "设备不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询设备信息失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 添加新设备
     * @param terminal 设备信息
     * @return 操作结果
     */
    @PostMapping
    public Map<String, Object> addTerminal(@RequestBody Terminal terminal) {
        Map<String, Object> result = new HashMap<>();
        try {
            terminalService.addTerminal(terminal);
            result.put("success", true);
            result.put("message", "设备添加成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "设备添加失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 更新设备信息
     * @param terminalNumber 设备编号
     * @param terminal 设备信息
     * @return 操作结果
     */
    @PutMapping("/{terminalNumber}")
    public Map<String, Object> updateTerminal(@PathVariable String terminalNumber, @RequestBody Terminal terminal) {
        Map<String, Object> result = new HashMap<>();
        try {
            terminalService.updateTerminal(terminalNumber, terminal);
            result.put("success", true);
            result.put("message", "设备更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "设备更新失败: " + e.getMessage());
        }
        return result;
    }

    /**
     * 删除设备
     * @param terminalNumber 设备编号
     * @return 操作结果
     */
    @DeleteMapping("/{terminalNumber}")
    public Map<String, Object> deleteTerminal(@PathVariable String terminalNumber) {
        Map<String, Object> result = new HashMap<>();
        try {
            terminalService.deleteTerminal(terminalNumber);
            result.put("success", true);
            result.put("message", "设备删除成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "设备删除失败: " + e.getMessage());
        }
        return result;
    }
}