242 lines
5.8 KiB
Go
242 lines
5.8 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
client *http.Client
|
|
}
|
|
|
|
func NewClient(baseURL string) *Client {
|
|
return &Client{
|
|
baseURL: baseURL,
|
|
client: &http.Client{},
|
|
}
|
|
}
|
|
|
|
type APIResponse struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
type WorkOrderInfo struct {
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|
WorkpieceType string `json:"workpieceType"`
|
|
WorkpieceTypeName string `json:"workpieceTypeName"`
|
|
WorkpieceCount int `json:"workpieceCount"`
|
|
Status string `json:"status"`
|
|
StatusName string `json:"statusName"`
|
|
CreatedAt string `json:"createdAt"`
|
|
}
|
|
|
|
type MeasurementRecord struct {
|
|
ID int `json:"id"`
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|
WorkpieceNo string `json:"workpieceNo"`
|
|
Category string `json:"category"`
|
|
WorkpieceType string `json:"workpieceType"`
|
|
ValueA float64 `json:"valueA"`
|
|
ValueB float64 `json:"valueB"`
|
|
ValueC float64 `json:"valueC"`
|
|
Unit string `json:"unit"`
|
|
CreatedAt string `json:"createdAt"`
|
|
}
|
|
|
|
type MeasurementListResponse struct {
|
|
List []MeasurementRecord `json:"list"`
|
|
Total int `json:"total"`
|
|
Page int `json:"page"`
|
|
Limit int `json:"limit"`
|
|
}
|
|
|
|
type SubmitRequest struct {
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|
WorkpieceNo string `json:"workpieceNo"`
|
|
Category string `json:"category"`
|
|
WorkpieceType string `json:"workpieceType"`
|
|
ValueA float64 `json:"valueA"`
|
|
ValueB *float64 `json:"valueB,omitempty"`
|
|
ValueC *float64 `json:"valueC,omitempty"`
|
|
Unit string `json:"unit"`
|
|
Serial string `json:"serial"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|
|
|
|
type SubmitResponse struct {
|
|
ID int `json:"id"`
|
|
}
|
|
|
|
func (c *Client) QueryWorkOrder(ctx context.Context, workOrderNo string) (*WorkOrderInfo, error) {
|
|
u, err := url.Parse(c.baseURL + "/api/v1/micrometer/work-order/query")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
q := u.Query()
|
|
q.Set("workOrderNo", workOrderNo)
|
|
u.RawQuery = q.Encode()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := c.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var apiResp APIResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
|
|
return nil, err
|
|
}
|
|
if apiResp.Code != 0 {
|
|
return nil, fmt.Errorf("API error: %s", apiResp.Message)
|
|
}
|
|
|
|
data, err := json.Marshal(apiResp.Data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result WorkOrderInfo
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (c *Client) QueryMeasurements(ctx context.Context, workOrderNo, workpieceNo string, page, limit int) (*MeasurementListResponse, error) {
|
|
u, err := url.Parse(c.baseURL + "/api/v1/micrometer/measurements")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
q := u.Query()
|
|
q.Set("workOrderNo", workOrderNo)
|
|
if workpieceNo != "" {
|
|
q.Set("workpieceNo", workpieceNo)
|
|
}
|
|
q.Set("page", fmt.Sprintf("%d", page))
|
|
q.Set("limit", fmt.Sprintf("%d", limit))
|
|
u.RawQuery = q.Encode()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := c.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var apiResp APIResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
|
|
return nil, err
|
|
}
|
|
if apiResp.Code != 0 {
|
|
return nil, fmt.Errorf("API error: %s", apiResp.Message)
|
|
}
|
|
|
|
data, err := json.Marshal(apiResp.Data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result MeasurementListResponse
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (c *Client) SubmitMeasurement(ctx context.Context, req SubmitRequest) (*SubmitResponse, error) {
|
|
body, err := json.Marshal(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/api/v1/micrometer/measurements", bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
httpReq.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := c.client.Do(httpReq)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var apiResp APIResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
|
|
return nil, err
|
|
}
|
|
if apiResp.Code != 0 {
|
|
return nil, fmt.Errorf("API error: %s", apiResp.Message)
|
|
}
|
|
|
|
data, err := json.Marshal(apiResp.Data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result SubmitResponse
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (c *Client) QueryWorkOrderList(ctx context.Context, keyword string, limit int) ([]WorkOrderOption, error) {
|
|
u, err := url.Parse(c.baseURL + "/api/v1/micrometer/work-orders")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
q := u.Query()
|
|
if keyword != "" {
|
|
q.Set("keyword", keyword)
|
|
}
|
|
q.Set("limit", fmt.Sprintf("%d", limit))
|
|
u.RawQuery = q.Encode()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := c.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var apiResp APIResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
|
|
return nil, err
|
|
}
|
|
if apiResp.Code != 0 {
|
|
return nil, fmt.Errorf("API error: %s", apiResp.Message)
|
|
}
|
|
|
|
data, err := json.Marshal(apiResp.Data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var result []WorkOrderOption
|
|
if err := json.Unmarshal(data, &result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
type WorkOrderOption struct {
|
|
WorkOrderNo string `json:"workOrderNo"`
|
|
Description string `json:"description"`
|
|
} |