Files
bj_power/bj_power_mes/internal/handler/inspection.go
T
SunYF f1cf57e9b2 feat: 新增工序签字功能,移除移料指令模块
新增操作工/检验员工序签字功能,支持工艺步骤配置签字要求;删除移料指令相关代码,将移料页签重命名为产线点位(上料/下料);新增成品入库功能及相关权限;优化备料单状态、质量状态逻辑,新增批次序号字段;修复AGV取消后备料单回退状态问题。
2026-10-05 20:15:38 +08:00

209 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handler
import (
"net/http"
"net/url"
"bj_power_mes/common/httpx"
"bj_power_mes/internal/logic"
"bj_power_mes/internal/svc"
"github.com/xuri/excelize/v2"
)
// CreateInspectionHandler POST /inspections(PAD 巡检终端提交记录)
func CreateInspectionHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req logic.InspectionReq
if err := httpx.ParseJSON(r, &req); err != nil {
httpx.BadRequest(w, "请求体解析失败")
return
}
// 按钮级权限:按记录类型校验(块2/8)
categoryPerm := map[string]string{
"CHECKIN": "sys.inspect:checkin",
"POINT": "sys.inspect:point",
"PROCESS": "sys.inspect:process",
"DONE": "sys.inspect:done",
"ALARM": "sys.inspect:alarm",
}
// 质量检验三类(过程检 QC_PROCESS / 完工检 FINAL / 来料检 INCOMING)与 PAD 巡检共用本接口,
// 必须持有质量检验录入权限 produce.quality:edit 方可提交(质检员专属,操作工仅做完工确认)。
// 注:PAD 巡检「过程巡检」仍用 PROCESS(走 categoryPerm 的 sys.inspect:process),与质检隔离(P1-8)。
qualityCats := map[string]bool{"QC_PROCESS": true, "FINAL": true, "INCOMING": true}
if qualityCats[req.Category] {
if !userHasPerm(r, svcCtx, "produce.quality:edit") {
httpx.FailHTTP(w, http.StatusForbidden, "无操作权限:produce.quality:edit")
return
}
} else if code, ok := categoryPerm[req.Category]; ok {
if !userHasPerm(r, svcCtx, code) {
httpx.FailHTTP(w, http.StatusForbidden, "无操作权限:"+code)
return
}
} else {
// 未识别的检验类型,拒绝提交,避免越权放行
httpx.FailHTTP(w, http.StatusBadRequest, "未知的检验类型:"+req.Category)
return
}
msg, err := logic.New(svcCtx).CreateInspection(r.Context(), req, operator(r, ""))
if err != nil {
httpx.Fail(w, 2301, err.Error())
return
}
// message 放 data 返回:质量检验页需读取「库存联动失败/已联动」提示(含 WMS 原文原因)
httpx.Ok(w, map[string]any{"message": msg})
}
}
// ListInspectionsHandler GET /inspections?category=&operator=&from=&to=&materialCode=&materialName=&reportNo=&page=&pageSize=
// 返回 {list, total},后端真分页(created_at desc / id desc,最新置顶)
// materialCode/materialName/reportNo:质量检验模糊查询(每框内模糊、多框取交集)
func ListInspectionsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
page := atoiDefault(q.Get("page"), 1)
pageSize := atoiDefault(q.Get("pageSize"), 50)
if page < 1 {
page = 1
}
if pageSize < 1 || pageSize > 1000 {
pageSize = 50
}
data, total, err := logic.New(svcCtx).ListInspectionsPaged(r.Context(),
q.Get("category"), q.Get("operator"), q.Get("from"), q.Get("to"),
q.Get("orderNo"), q.Get("stationNo"),
q.Get("materialCode"), q.Get("materialName"), q.Get("reportNo"), page, pageSize)
if err != nil {
httpx.Fail(w, 2302, err.Error())
return
}
httpx.Ok(w, map[string]any{"list": data, "total": total})
}
}
// UploadPhotoHandler POST /inspections/upload(巡检/异常拍照上传)
// 统一存储:<Upload.Dir>/年/月/日/INSPECTION_PHOTO/<uuid>.<ext>;返回相对路径 + 免鉴权直链。
func UploadPhotoHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseMultipartForm(svcCtx.Config.Upload.MaxMB << 20); err != nil {
httpx.BadRequest(w, "上传文件过大或格式错误")
return
}
res, err := saveTypedUpload(r, "file", svcCtx.Config.Upload.Dir, "inspection", FileTypeInspectionPhoto,
uploadMaxBytes(svcCtx.Config.Upload.MaxSize, svcCtx.Config.Upload.MaxMB, FileTypeInspectionPhoto))
if err != nil {
httpx.Fail(w, 2303, "照片保存失败:"+err.Error())
return
}
httpx.Ok(w, map[string]any{"filename": res.RelPath, "url": "/api/v1/files/" + res.RelPath})
}
}
// FinishedPendingHandler GET /quality/finished-pending?page=&pageSize=
// 待完工检成品:完工确认(DONE)后尚无对应完工检记录的成品 SN,只读分页(完工时间倒序)
func FinishedPendingHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
page := atoiDefault(q.Get("page"), 1)
pageSize := atoiDefault(q.Get("pageSize"), 20)
list, total, err := logic.New(svcCtx).FinishedPendingList(r.Context(), page, pageSize)
if err != nil {
httpx.Fail(w, 2308, err.Error())
return
}
httpx.Ok(w, map[string]any{"list": list, "total": total})
}
}
// InspectionDisposalHandler POST /quality/disposal 质量检验处置(退货/返修/退换)
func InspectionDisposalHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
Id int `json:"id"`
DisposalType string `json:"disposalType"`
DisposalRemark string `json:"disposalRemark"`
}
if err := httpx.ParseJSON(r, &req); err != nil || req.Id <= 0 {
httpx.BadRequest(w, "缺少 id")
return
}
msg, err := logic.New(svcCtx).SetInspectionDisposal(r.Context(), req.Id, req.DisposalType, req.DisposalRemark, operator(r, ""))
if err != nil {
httpx.Fail(w, 2305, err.Error())
return
}
httpx.OkMessage(w, msg, nil)
}
}
// InspectionBindsHandler GET /inspections/binds?sn= 检验记录详情附带的装配物料绑定明细
// (按 SN 跨工艺查 workpiece_bind,标注工艺/工位;客户需求:检验记录附装配明细)
func InspectionBindsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
sn := r.URL.Query().Get("sn")
if sn == "" {
httpx.BadRequest(w, "缺少 sn")
return
}
httpx.Ok(w, logic.New(svcCtx).InspectionBinds(r.Context(), sn))
}
}
// UnqualifiedReportHandler GET /quality/unqualified-report?id= 一键生成《不合格单》xlsx 模板(预填检验数据)
func UnqualifiedReportHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := atoiDefault(r.URL.Query().Get("id"), 0)
if id <= 0 {
httpx.Fail(w, 2306, "缺少 id")
return
}
rec, err := logic.New(svcCtx).GetInspectionRecord(r.Context(), id)
if err != nil {
httpx.Fail(w, 2307, err.Error())
return
}
f := excelize.NewFile()
sheet := "不合格单"
f.SetSheetName("Sheet1", sheet)
headers := []string{"报检单号", "图号", "物料名称", "生产厂家", "检验单号", "结论", "处置方式", "处置说明", "记录时间"}
for c, h := range headers {
cell, _ := excelize.CoordinatesToCellName(c+1, 1)
_ = f.SetCellValue(sheet, cell, h)
}
row := []any{
rec.ReportNo, rec.MaterialCode, rec.MaterialName, rec.Manufacturer, rec.InspectionNo,
rec.Result, rec.DisposalType, rec.DisposalRemark, rec.CreatedAt.Format("2006-01-02 15:04:05"),
}
for c, v := range row {
cell, _ := excelize.CoordinatesToCellName(c+1, 2)
_ = f.SetCellValue(sheet, cell, v)
}
// 装配物料绑定明细区(明细下方加区块,照主表风格):该 SN 跨工艺全部装机绑定
if rec.Sn != "" {
if binds := logic.New(svcCtx).InspectionBinds(r.Context(), rec.Sn); len(binds) > 0 {
titleCell, _ := excelize.CoordinatesToCellName(1, 4)
_ = f.SetCellValue(sheet, titleCell, "装配物料绑定明细")
bindHeaders := []string{"工件SN", "工艺", "工位", "物料编码", "物料名称", "规格", "绑定值(SN/批次)", "绑定方式", "绑定人", "绑定时间"}
for c, h := range bindHeaders {
cell, _ := excelize.CoordinatesToCellName(c+1, 5)
_ = f.SetCellValue(sheet, cell, h)
}
for i, b := range binds {
bindRow := []any{
b.Sn, b.FlowName, b.StationNo, b.MaterialCode, b.MaterialName, b.Spec,
b.BindValue, b.BindMode, b.Operator, b.CreatedAt.Format("2006-01-02 15:04:05"),
}
for c, v := range bindRow {
cell, _ := excelize.CoordinatesToCellName(c+1, 6+i)
_ = f.SetCellValue(sheet, cell, v)
}
}
}
}
w.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
w.Header().Set("Content-Disposition", "attachment; filename*=UTF-8''"+url.PathEscape("不合格单.xlsx"))
_, _ = f.WriteTo(w)
}
}