2026-08-31 08:06:55 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/common/httpx"
|
|
|
|
|
"bj_power_mes/internal/logic"
|
|
|
|
|
"bj_power_mes/internal/svc"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-31 12:58:28 +08:00
|
|
|
// ListProcessFlowsHandler GET /process-flows?stationNo=
|
2026-08-31 08:06:55 +08:00
|
|
|
func ListProcessFlowsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2026-08-31 12:58:28 +08:00
|
|
|
stationNo := atoiDefault(r.URL.Query().Get("stationNo"), 0)
|
|
|
|
|
data, err := logic.New(svcCtx).ListFlows(r.Context(), stationNo)
|
2026-08-31 08:06:55 +08:00
|
|
|
if err != nil {
|
|
|
|
|
httpx.Fail(w, 2201, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-31 12:58:28 +08:00
|
|
|
// SaveProcessFlowHandler POST /process-flows(新建或更新工艺流程,含工序步骤)
|
2026-08-31 08:06:55 +08:00
|
|
|
func SaveProcessFlowHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req logic.FlowReq
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := logic.New(svcCtx).SaveFlow(r.Context(), req, operator(r, "")); err != nil {
|
|
|
|
|
httpx.Fail(w, 2202, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.OkMessage(w, "工艺流程已保存", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteProcessFlowHandler DELETE /process-flows/:id
|
|
|
|
|
func DeleteProcessFlowHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if err := logic.New(svcCtx).DeleteFlow(r.Context(), pathId(r), operator(r, "")); err != nil {
|
|
|
|
|
httpx.Fail(w, 2203, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.OkMessage(w, "工艺流程已删除", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UploadPdfHandler POST /process-flows/upload(工艺图纸PDF上传,返回文件名)
|
|
|
|
|
func UploadPdfHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
|
|
|
|
httpx.BadRequest(w, "上传文件过大或格式错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
name, err := saveUploadFile(r, "file", svcCtx.Config.Upload.Dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.Fail(w, 2204, "文件保存失败:"+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.Ok(w, map[string]any{"filename": name, "url": "/api/v1/files/" + name})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FileDownloadHandler JWT 保护的文件下载(MES 前端展示 PDF/照片)
|
|
|
|
|
func FileDownloadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
serveUploadFile(w, r, svcCtx.Config.Upload.Dir, r.URL.Query().Get("name"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListStationsHandler GET /stations
|
|
|
|
|
func ListStationsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
data, err := logic.New(svcCtx).ListStations(r.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpx.Fail(w, 2205, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.Ok(w, data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SaveStationHandler POST /stations(工位绑定工艺流程图)
|
|
|
|
|
func SaveStationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req logic.StationReq
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := logic.New(svcCtx).SaveStation(r.Context(), req, operator(r, "")); err != nil {
|
|
|
|
|
httpx.Fail(w, 2206, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.OkMessage(w, "工位绑定已保存", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-31 12:58:28 +08:00
|
|
|
|
|
|
|
|
// SetFlowStatusHandler POST /process-flows/status(工艺流程启用/停用)
|
|
|
|
|
func SetFlowStatusHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req struct {
|
|
|
|
|
Id int `json:"id"`
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
}
|
|
|
|
|
if err := httpx.ParseJSON(r, &req); err != nil {
|
|
|
|
|
httpx.BadRequest(w, "请求体解析失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := logic.New(svcCtx).SetFlowStatus(r.Context(), req.Id, req.Status, operator(r, "")); err != nil {
|
|
|
|
|
httpx.Fail(w, 2207, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
httpx.OkMessage(w, "操作成功", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|