77 lines
2.3 KiB
Go
77 lines
2.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/scanrecord"
|
|
"bj_power_mes/ent/workorder"
|
|
"bj_power_mes/ent/workpiece"
|
|
)
|
|
|
|
type ScanReq struct {
|
|
Station string `json:"station"`
|
|
Sn string `json:"sn"`
|
|
OrderNo string `json:"orderNo"`
|
|
Type string `json:"type"` // ONLINE / PROCESS / DONE / TEMP_STORE
|
|
ProcessCode int `json:"processCode"`
|
|
Operator string `json:"operator"`
|
|
}
|
|
|
|
// ReportScan 扫码报工:记录 scan_record,并更新工单进度
|
|
func (s *Service) ReportScan(ctx context.Context, req ScanReq, operator string) error {
|
|
if req.Sn == "" {
|
|
return errors.New("sn 不能为空")
|
|
}
|
|
scanType := req.Type
|
|
if scanType == "" {
|
|
scanType = "PROCESS"
|
|
}
|
|
if req.Operator == "" {
|
|
req.Operator = operator
|
|
}
|
|
_, err := s.ctx.EntClient.ScanRecord.Create().
|
|
SetStation(req.Station).SetSn(req.Sn).SetOrderNo(req.OrderNo).
|
|
SetType(scanType).SetProcessCode(req.ProcessCode).SetOperator(req.Operator).
|
|
SetTime(time.Now()).Save(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.ctx.EventLog.Write(ctx, "scan.report", req.OrderNo, operator, "scan_record", req.Sn, "扫码报工", map[string]any{"station": req.Station, "type": scanType, "processCode": req.ProcessCode})
|
|
|
|
// 更新工单进度:调高 finished 进度(去重按 sn+工序)
|
|
wp, err := s.ctx.EntClient.Workpiece.Query().Where(workpiece.Sn(req.Sn)).First(ctx)
|
|
if err == nil && wp != nil && wp.WorkOrderId > 0 {
|
|
s.bumpWorkOrderProgress(ctx, wp.OrderNo, wp.Sn)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// bumpWorkOrderProgress 增加工单已完成数量
|
|
func (s *Service) bumpWorkOrderProgress(ctx context.Context, orderNo, sn string) {
|
|
wo, err := s.ctx.EntClient.WorkOrder.Query().Where(workorder.WorkOrderNo(orderNo)).First(ctx)
|
|
if err != nil || wo == nil {
|
|
return
|
|
}
|
|
if wo.FinishedNum >= wo.Quantity {
|
|
return
|
|
}
|
|
_, _ = s.ctx.EntClient.WorkOrder.UpdateOneID(wo.ID).
|
|
SetFinishedNum(wo.FinishedNum + 1).
|
|
SetStatus("IN_PROGRESS").Save(ctx)
|
|
}
|
|
|
|
// ListScanRecords 查询扫码报工记录
|
|
func (s *Service) ListScanRecords(ctx context.Context, sn, orderNo string) ([]*ent.ScanRecord, error) {
|
|
q := s.ctx.EntClient.ScanRecord.Query()
|
|
if sn != "" {
|
|
q = q.Where(scanrecord.Sn(sn))
|
|
}
|
|
if orderNo != "" {
|
|
q = q.Where(scanrecord.OrderNo(orderNo))
|
|
}
|
|
return q.Order(ent.Desc(scanrecord.FieldID)).Limit(1000).All(ctx)
|
|
}
|