69 lines
1.9 KiB
Go
69 lines
1.9 KiB
Go
package handler
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"net/http"
|
||
|
|
"sort"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"bj_power_wms/internal/svc"
|
||
|
|
)
|
||
|
|
|
||
|
|
// 视为"已完成"的工单状态(查询时排到最后,未完成的优先显示)
|
||
|
|
var mesDoneStatuses = map[string]bool{
|
||
|
|
"FINISHED": true, "COMPLETED": true, "DONE": true,
|
||
|
|
"CLOSED": true, "已完结": true, "已完成": true,
|
||
|
|
}
|
||
|
|
|
||
|
|
type mesOrder struct {
|
||
|
|
WorkOrderNo string `json:"workOrderNo"`
|
||
|
|
ProductCode string `json:"productCode"`
|
||
|
|
ProductName string `json:"productName"`
|
||
|
|
Quantity int `json:"quantity"`
|
||
|
|
Status string `json:"status"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// ordersHandler 调用 MES 内部接口获取工单列表,把未完成工单排前面,供备料出库下拉选择
|
||
|
|
func ordersHandler(ctx *svc.ServiceContext) http.HandlerFunc {
|
||
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
url := ctx.Config.Mes.BaseURL + "/api/internal/order/query?orderNo=" + r.URL.Query().Get("orderNo")
|
||
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||
|
|
if err != nil {
|
||
|
|
fail(w, http.StatusBadGateway, "构造 MES 请求失败")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
req.Header.Set("X-API-TOKEN", ctx.Config.Mes.Token)
|
||
|
|
cli := &http.Client{Timeout: 5 * time.Second}
|
||
|
|
resp, err := cli.Do(req)
|
||
|
|
if err != nil {
|
||
|
|
fail(w, http.StatusBadGateway, "无法连接 MES 服务")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
if resp.StatusCode != http.StatusOK {
|
||
|
|
fail(w, http.StatusBadGateway, "MES 返回异常")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var body struct {
|
||
|
|
Data []mesOrder `json:"data"`
|
||
|
|
}
|
||
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
|
||
|
|
fail(w, http.StatusBadGateway, "解析 MES 响应失败")
|
||
|
|
return
|
||
|
|
}
|
||
|
|
list := body.Data
|
||
|
|
if list == nil {
|
||
|
|
list = []mesOrder{}
|
||
|
|
}
|
||
|
|
sort.SliceStable(list, func(i, j int) bool {
|
||
|
|
di := mesDoneStatuses[list[i].Status]
|
||
|
|
dj := mesDoneStatuses[list[j].Status]
|
||
|
|
if di != dj {
|
||
|
|
return !di // 未完成在前
|
||
|
|
}
|
||
|
|
return list[i].WorkOrderNo < list[j].WorkOrderNo
|
||
|
|
})
|
||
|
|
ok(w, list)
|
||
|
|
}
|
||
|
|
}
|