package com.ruoyi.pda.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.device.domain.BaseInfo;
import com.ruoyi.device.service.IBaseInfoService;
import com.ruoyi.pda.service.IPdaApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/api")
public class PdaApiController {
    @Autowired
    private IPdaApiService service;
    @Autowired
    private IBaseInfoService baseInfoService;

    @PostMapping("/home/selectDeviceInfo")
    public AjaxResult selectDeviceInfo(String code) {
        BaseInfo baseInfo = baseInfoService.selectDeviceInfo(code);
        if (baseInfo == null) return error("查询失败,条码扫描错误");
        String deviceState = baseInfo.getDeviceState();
        String deviceName = baseInfo.getDeviceName();
        if (deviceState.equals("0")) return error(deviceName + "已停用，使用其他设备吧");
        if (deviceState.equals("2")) return error(deviceName + "正在维修，使用其他设备吧");
        if (baseInfo.getUseState().equals("1")) return error(deviceName + "在使用中，更换其他设备吧");
        return success(baseInfo);
    }


    public AjaxResult open() throws IOException {
        URL url = new URL("https://api.example.com/data");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        int responseCode = conn.getResponseCode();
        if (responseCode == 200) {
            // BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            // String inputLine;
            // StringBuilder response = new StringBuilder();
            // while ((inputLine = in.readLine()) != null) {
            //     response.append(inputLine);
            // }
            // in.close();
            // System.out.println(response);
        }
        return success(conn.getInputStream());
    }
}
