182 lines
6.5 KiB
Go
182 lines
6.5 KiB
Go
package handler
|
||
|
||
import (
|
||
"errors"
|
||
"net/http"
|
||
|
||
"bj_power_mes/common/httpx"
|
||
"bj_power_mes/internal/logic"
|
||
"bj_power_mes/internal/svc"
|
||
|
||
"github.com/zeromicro/go-zero/rest/pathvar"
|
||
)
|
||
|
||
// ListProcessFlowsHandler GET /process-flows?stationNo=&pp=(pp=按产品工艺名称反查过滤)
|
||
func ListProcessFlowsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
stationNo := atoiDefault(r.URL.Query().Get("stationNo"), 0)
|
||
page := atoiDefault(r.URL.Query().Get("page"), 0)
|
||
pageSize := atoiDefault(r.URL.Query().Get("pageSize"), 20)
|
||
data, err := logic.New(svcCtx).ListFlows(r.Context(), stationNo, r.URL.Query().Get("pp"),
|
||
r.URL.Query().Get("name"), r.URL.Query().Get("status"), atoiDefault(r.URL.Query().Get("stepMin"), 0), page, pageSize)
|
||
if err != nil {
|
||
httpx.Fail(w, 2201, err.Error())
|
||
return
|
||
}
|
||
httpx.Ok(w, data)
|
||
}
|
||
}
|
||
|
||
// SaveProcessFlowHandler POST /process-flows(新建或更新工艺流程,含工序步骤)
|
||
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
|
||
}
|
||
res, err := saveTypedUpload(r, "file", svcCtx.Config.Upload.Dir, "process_flow", FileTypeProcessPDF,
|
||
uploadMaxBytes(svcCtx.Config.Upload.MaxSize, svcCtx.Config.Upload.MaxMB, FileTypeProcessPDF))
|
||
if err != nil {
|
||
httpx.Fail(w, 2204, "文件保存失败:"+err.Error())
|
||
return
|
||
}
|
||
httpx.Ok(w, map[string]any{"filename": res.RelPath, "url": "/api/v1/files/" + res.RelPath})
|
||
}
|
||
}
|
||
|
||
// 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"))
|
||
}
|
||
}
|
||
|
||
// FilePathHandler GET /files/:date/:name —— 路径式文件 URL(巡检拍照上传返回的
|
||
// url 形如 /api/v1/files/2026-09-07/xxx.png,此前无此路由导致照片 404)
|
||
func FilePathHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
vars := pathvar.Vars(r)
|
||
serveUploadFile(w, r, svcCtx.Config.Upload.Dir, vars["date"]+"/"+vars["name"])
|
||
}
|
||
}
|
||
|
||
// FileDeepPathHandler GET /files/:y/:m/:d/:t/:f —— 统一存储格式的路径式 URL
|
||
// (年/月/日/文件类型/uuid.ext)。go-zero 路由不支持多段通配,故按固定 5 段显式声明。
|
||
func FileDeepPathHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
vars := pathvar.Vars(r)
|
||
rel := vars["y"] + "/" + vars["m"] + "/" + vars["d"] + "/" + vars["t"] + "/" + vars["f"]
|
||
serveUploadFile(w, r, svcCtx.Config.Upload.Dir, rel)
|
||
}
|
||
}
|
||
|
||
// 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 {
|
||
// 规则3:PLC 离线未二次确认 → 哨兵错误映射为 code=3021,前端弹「已现场确认,继续」二次确认后重发
|
||
if errors.Is(err, logic.ErrPlcOfflineConfirmRequired) {
|
||
httpx.Fail(w, 3021, err.Error())
|
||
return
|
||
}
|
||
// 收集式校验(code=3015)经 FailErr 结构化输出,前端弹 ErrorListDialog 一次列全
|
||
httpx.FailErr(w, err)
|
||
return
|
||
}
|
||
httpx.OkMessage(w, "工位绑定已保存", nil)
|
||
}
|
||
}
|
||
|
||
// DeleteStationHandler DELETE /stations/:id(删除工位:内置工位不允许删除)
|
||
func DeleteStationHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
if err := logic.New(svcCtx).DeleteStation(r.Context(), pathId(r), operator(r, "")); err != nil {
|
||
httpx.Fail(w, 2208, err.Error())
|
||
return
|
||
}
|
||
httpx.OkMessage(w, "工位已删除", nil)
|
||
}
|
||
}
|
||
|
||
// 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)
|
||
}
|
||
}
|
||
|
||
// SetStationStatusHandler POST /stations/status(工位启用/停用,对齐 /process-flows/status 模式)
|
||
func SetStationStatusHandler(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).SetStationStatus(r.Context(), req.Id, req.Status, operator(r, "")); err != nil {
|
||
httpx.Fail(w, 2209, err.Error())
|
||
return
|
||
}
|
||
httpx.OkMessage(w, "操作成功", nil)
|
||
}
|
||
}
|