2026-08-31 08:06:55 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
2026-08-31 12:58:28 +08:00
|
|
|
|
"fmt"
|
2026-08-31 08:06:55 +08:00
|
|
|
|
"sort"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"bj_power_mes/ent"
|
|
|
|
|
|
"bj_power_mes/ent/processflow"
|
|
|
|
|
|
"bj_power_mes/ent/processstep"
|
|
|
|
|
|
"bj_power_mes/ent/station"
|
|
|
|
|
|
"bj_power_mes/ent/stepcriterion"
|
|
|
|
|
|
"bj_power_mes/ent/workorder"
|
|
|
|
|
|
"bj_power_mes/ent/workpieceprocess"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 工艺流程(块3) ----------
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// FlowReq 工艺流程保存请求(流程=菜谱,只描述"怎么干")
|
|
|
|
|
|
// Stations:绑定工位列表(可多选,一个流程可绑多个工位)。
|
2026-08-31 08:06:55 +08:00
|
|
|
|
type FlowReq struct {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
Id int `json:"id"`
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
Stations []int `json:"stations"`
|
|
|
|
|
|
PdfFile string `json:"pdfFile"`
|
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
Remark string `json:"remark"`
|
|
|
|
|
|
Steps []StepItemReq `json:"steps"`
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 12:58:28 +08:00
|
|
|
|
// FlowVO 工艺流程视图(含工序步骤 + 绑定该流程的工位号)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
type FlowVO struct {
|
2026-08-31 12:58:28 +08:00
|
|
|
|
Id int `json:"id"`
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
PdfFile string `json:"pdfFile"`
|
|
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
Remark string `json:"remark"`
|
|
|
|
|
|
Stations []int `json:"stations"`
|
|
|
|
|
|
Steps []*ProcessStepTemplate `json:"steps"`
|
|
|
|
|
|
CreatedAt time.Time `json:"createdAt"`
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// ListFlows 查询工艺流程(可按工位号过滤:只认「关联工位」页的绑定关系 station.flow_id)
|
2026-09-08 11:44:04 +08:00
|
|
|
|
// page>0 时真分页返回 map{total,list,page,pageSize}(管理页用);page=0 全量数组(工位终端兼容)
|
|
|
|
|
|
func (s *Service) ListFlows(ctx context.Context, stationNo, page, pageSize int) (any, error) {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
q := s.ctx.EntClient.ProcessFlow.Query().
|
2026-09-10 16:59:15 +08:00
|
|
|
|
Order(ent.Asc(processflow.FieldID))
|
2026-08-31 08:06:55 +08:00
|
|
|
|
flows, err := q.All(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
stations, _ := s.ctx.EntClient.Station.Query().All(ctx)
|
2026-09-04 14:18:53 +08:00
|
|
|
|
flowToStations := map[int][]int{}
|
|
|
|
|
|
for _, st := range stations {
|
|
|
|
|
|
if st.FlowId > 0 {
|
|
|
|
|
|
flowToStations[st.FlowId] = append(flowToStations[st.FlowId], st.StationNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
out := make([]*FlowVO, 0, len(flows))
|
|
|
|
|
|
for _, f := range flows {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// 过滤:指定工位号时,只返回绑定了该工位的流程
|
2026-09-04 14:18:53 +08:00
|
|
|
|
if stationNo > 0 {
|
|
|
|
|
|
hit := false
|
|
|
|
|
|
for _, st := range flowToStations[f.ID] {
|
|
|
|
|
|
if st == stationNo {
|
|
|
|
|
|
hit = true
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
if !hit {
|
2026-09-04 14:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
vo := &FlowVO{
|
2026-09-10 16:59:15 +08:00
|
|
|
|
Id: f.ID, Name: f.Name,
|
2026-08-31 08:06:55 +08:00
|
|
|
|
PdfFile: f.PdfFile, Status: f.Status, Remark: f.Remark,
|
2026-09-10 16:59:15 +08:00
|
|
|
|
Stations: []int{}, Steps: s.flowSteps(ctx, f.ID), CreatedAt: f.CreatedAt,
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
if list, ok := flowToStations[f.ID]; ok {
|
|
|
|
|
|
vo.Stations = list
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
out = append(out, vo)
|
|
|
|
|
|
}
|
2026-09-08 11:44:04 +08:00
|
|
|
|
if page > 0 {
|
|
|
|
|
|
total := len(out)
|
|
|
|
|
|
start := (page - 1) * pageSize
|
|
|
|
|
|
if start > total {
|
|
|
|
|
|
start = total
|
|
|
|
|
|
}
|
|
|
|
|
|
end := start + pageSize
|
|
|
|
|
|
if end > total {
|
|
|
|
|
|
end = total
|
|
|
|
|
|
}
|
|
|
|
|
|
return map[string]any{"total": total, "list": out[start:end], "page": page, "pageSize": pageSize}, nil
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
return out, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-04 14:18:53 +08:00
|
|
|
|
// SaveFlow 保存工艺流程(可一次绑定多个工位)。
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// 约束:① 绑定工位必须已存在(工位主数据由数据库维护,本接口不自动创建);
|
|
|
|
|
|
// ② 一个工位同时只能绑定一个流程(保存时直接覆盖旧绑定);
|
2026-09-04 14:18:53 +08:00
|
|
|
|
// ③ 停用流程不再自动解绑工位(绑定关系由本保存接口显式维护,启停只切流程状态)。
|
2026-08-31 08:06:55 +08:00
|
|
|
|
func (s *Service) SaveFlow(ctx context.Context, req FlowReq, operator string) error {
|
2026-08-31 12:58:28 +08:00
|
|
|
|
if req.Name == "" {
|
|
|
|
|
|
return errors.New("流程名称必填")
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// 解析绑定工位列表
|
2026-09-04 14:18:53 +08:00
|
|
|
|
seen := map[int]bool{}
|
|
|
|
|
|
stations := []int{}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
for _, no := range req.Stations {
|
|
|
|
|
|
if no < 1 || seen[no] {
|
2026-09-04 14:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
seen[no] = true
|
|
|
|
|
|
stations = append(stations, no)
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
status := req.Status
|
|
|
|
|
|
if status == "" {
|
|
|
|
|
|
status = "ACTIVE"
|
|
|
|
|
|
}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
if status != "ACTIVE" && status != "INACTIVE" {
|
|
|
|
|
|
return errors.New("非法状态")
|
2026-08-31 12:58:28 +08:00
|
|
|
|
}
|
2026-09-08 11:44:04 +08:00
|
|
|
|
// 绑定语义(2026-09-08 定稿):直接更换——一个工位只有一条关联数据,
|
|
|
|
|
|
// 保存时所选工位一律改绑到本流程(覆盖旧绑定),不再报"已被启用流程绑定"冲突。
|
2026-08-31 08:06:55 +08:00
|
|
|
|
var flowID int
|
|
|
|
|
|
if req.Id > 0 {
|
|
|
|
|
|
upd := s.ctx.EntClient.ProcessFlow.UpdateOneID(req.Id).
|
2026-09-10 16:59:15 +08:00
|
|
|
|
SetName(req.Name).SetStatus(status).SetRemark(req.Remark)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if req.PdfFile != "" {
|
|
|
|
|
|
upd.SetPdfFile(req.PdfFile)
|
|
|
|
|
|
}
|
|
|
|
|
|
if _, err := upd.Save(ctx); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
flowID = req.Id
|
|
|
|
|
|
} else {
|
|
|
|
|
|
f, err := s.ctx.EntClient.ProcessFlow.Create().
|
2026-09-10 16:59:15 +08:00
|
|
|
|
SetName(req.Name).SetPdfFile(req.PdfFile).
|
2026-08-31 08:06:55 +08:00
|
|
|
|
SetStatus(status).SetRemark(req.Remark).Save(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
flowID = f.ID
|
|
|
|
|
|
}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
// 绑定同步:把绑定列表整体指向该流程(旧绑定该流程但不在列表内的工位解绑)
|
|
|
|
|
|
boundNow, _ := s.ctx.EntClient.Station.Query().Where(station.FlowId(flowID)).All(ctx)
|
|
|
|
|
|
for _, st := range boundNow {
|
|
|
|
|
|
if !seen[st.StationNo] {
|
|
|
|
|
|
_, _ = s.ctx.EntClient.Station.UpdateOneID(st.ID).SetFlowId(0).Save(ctx)
|
|
|
|
|
|
}
|
2026-08-31 12:58:28 +08:00
|
|
|
|
}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
for _, no := range stations {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
if err := s.syncStationFlow(ctx, no, flowID); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-08-31 12:58:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 覆盖式重建该流程的工序步骤及考核标准
|
2026-08-31 08:06:55 +08:00
|
|
|
|
_, _ = s.ctx.EntClient.ProcessStep.Delete().Where(processstep.FlowId(flowID)).Exec(ctx)
|
|
|
|
|
|
for _, st := range req.Steps {
|
|
|
|
|
|
if st.Name == "" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
rec, err := s.ctx.EntClient.ProcessStep.Create().
|
2026-09-10 16:59:15 +08:00
|
|
|
|
SetFlowId(flowID).SetSeq(st.Seq).SetName(st.Name).
|
|
|
|
|
|
SetCollectType(st.CollectType).SetIsTorque(st.IsTorque).SetNeedCheck(st.NeedCheck).SetRemark(st.Remark).Save(ctx)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
_, _ = s.ctx.EntClient.StepCriterion.Delete().Where(stepcriterion.StepId(rec.ID)).Exec(ctx)
|
|
|
|
|
|
for _, c := range st.Criteria {
|
|
|
|
|
|
if c.Name == "" || c.Logic == "" || c.Logic == "NONE" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
b := s.ctx.EntClient.StepCriterion.Create().
|
|
|
|
|
|
SetStepId(rec.ID).SetName(c.Name).SetUnit(c.Unit).SetLogic(c.Logic).SetTarget(c.Target)
|
|
|
|
|
|
if c.Min != nil {
|
|
|
|
|
|
b = b.SetMin(*c.Min)
|
|
|
|
|
|
}
|
|
|
|
|
|
if c.Max != nil {
|
|
|
|
|
|
b = b.SetMax(*c.Max)
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := b.Exec(ctx); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "process.flow.save", "", operator, "process_flow", "", "维护工艺流程",
|
2026-09-10 16:59:15 +08:00
|
|
|
|
map[string]any{"flowId": flowID, "stations": stations, "steps": len(req.Steps)})
|
2026-08-31 08:06:55 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// syncStationFlow 同步工位绑定:工位 stationNo 绑定到 flowID(工位不存在时报错,不自动创建)
|
|
|
|
|
|
func (s *Service) syncStationFlow(ctx context.Context, stationNo, flowID int) error {
|
2026-08-31 12:58:28 +08:00
|
|
|
|
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(stationNo)).First(ctx)
|
|
|
|
|
|
if err != nil {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
return fmt.Errorf("工位 %d 不存在,请先在工位主数据中维护", stationNo)
|
2026-08-31 12:58:28 +08:00
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
_, err = s.ctx.EntClient.Station.UpdateOneID(st.ID).SetFlowId(flowID).Save(ctx)
|
|
|
|
|
|
return err
|
2026-08-31 12:58:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 08:06:55 +08:00
|
|
|
|
func (s *Service) DeleteFlow(ctx context.Context, id int, operator string) error {
|
2026-09-04 14:18:53 +08:00
|
|
|
|
// 删除前先解除所有工位绑定,再清步骤与流程本身
|
|
|
|
|
|
_, _ = s.ctx.EntClient.Station.Update().
|
|
|
|
|
|
Where(station.FlowId(id)).
|
|
|
|
|
|
SetFlowId(0).Save(ctx)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
_, _ = s.ctx.EntClient.ProcessStep.Delete().Where(processstep.FlowId(id)).Exec(ctx)
|
|
|
|
|
|
s.ctx.EventLog.Write(ctx, "process.flow.delete", "", operator, "process_flow", "", "删除工艺流程", map[string]any{"flowId": id})
|
|
|
|
|
|
return s.ctx.EntClient.ProcessFlow.DeleteOneID(id).Exec(ctx)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-31 12:58:28 +08:00
|
|
|
|
// flowSteps 查询某流程的工序步骤(含考核标准)
|
2026-09-10 16:59:15 +08:00
|
|
|
|
func (s *Service) flowSteps(ctx context.Context, flowId int) []*ProcessStepTemplate {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
steps, _ := s.ctx.EntClient.ProcessStep.Query().
|
|
|
|
|
|
Where(processstep.FlowId(flowId)).Order(ent.Asc(processstep.FieldSeq)).All(ctx)
|
|
|
|
|
|
out := make([]*ProcessStepTemplate, 0, len(steps))
|
|
|
|
|
|
for _, st := range steps {
|
|
|
|
|
|
t := &ProcessStepTemplate{
|
2026-09-10 16:59:15 +08:00
|
|
|
|
ID: st.ID, Seq: st.Seq,
|
|
|
|
|
|
Name: st.Name, CollectType: st.CollectType, IsTorque: st.IsTorque, NeedCheck: st.NeedCheck,
|
2026-08-31 08:06:55 +08:00
|
|
|
|
Remark: st.Remark, Criteria: []CriterionVO{},
|
|
|
|
|
|
}
|
|
|
|
|
|
crits, _ := s.ctx.EntClient.StepCriterion.Query().
|
|
|
|
|
|
Where(stepcriterion.StepId(st.ID)).All(ctx)
|
|
|
|
|
|
for _, c := range crits {
|
|
|
|
|
|
t.Criteria = append(t.Criteria, CriterionVO{
|
|
|
|
|
|
ID: c.ID, Name: c.Name, Unit: c.Unit, Logic: c.Logic,
|
|
|
|
|
|
Target: c.Target, Min: c.Min, Max: c.Max,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
out = append(out, t)
|
|
|
|
|
|
}
|
|
|
|
|
|
return out
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 工位主数据(块3) ----------
|
|
|
|
|
|
|
|
|
|
|
|
type StationReq struct {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
Id int `json:"id"`
|
|
|
|
|
|
StationNo int `json:"stationNo"`
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
StationType *string `json:"stationType"`
|
|
|
|
|
|
FlowId *int `json:"flowId"`
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type StationVO struct {
|
|
|
|
|
|
Id int `json:"id"`
|
|
|
|
|
|
StationNo int `json:"stationNo"`
|
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
|
FlowId int `json:"flowId"`
|
|
|
|
|
|
FlowName string `json:"flowName"`
|
2026-09-10 16:59:15 +08:00
|
|
|
|
StationType string `json:"stationType"`
|
2026-08-31 08:06:55 +08:00
|
|
|
|
Status string `json:"status"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) ListStations(ctx context.Context) ([]*StationVO, error) {
|
|
|
|
|
|
stas, err := s.ctx.EntClient.Station.Query().Order(ent.Asc(station.FieldStationNo)).All(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
flows, _ := s.ctx.EntClient.ProcessFlow.Query().All(ctx)
|
|
|
|
|
|
flowMap := map[int]*ent.ProcessFlow{}
|
|
|
|
|
|
for _, f := range flows {
|
|
|
|
|
|
flowMap[f.ID] = f
|
|
|
|
|
|
}
|
|
|
|
|
|
out := make([]*StationVO, 0, len(stas))
|
|
|
|
|
|
for _, st := range stas {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
vo := &StationVO{Id: st.ID, StationNo: st.StationNo, Name: st.Name, FlowId: st.FlowId, StationType: st.StationType, Status: st.Status}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if f, ok := flowMap[st.FlowId]; ok {
|
|
|
|
|
|
vo.FlowName = f.Name
|
|
|
|
|
|
}
|
|
|
|
|
|
out = append(out, vo)
|
|
|
|
|
|
}
|
|
|
|
|
|
return out, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// SaveStation 维护工位:只改名称/绑定流程。工位主数据由数据库维护,本接口不新增工位。
|
2026-08-31 08:06:55 +08:00
|
|
|
|
func (s *Service) SaveStation(ctx context.Context, req StationReq, operator string) error {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
if req.StationNo <= 0 {
|
|
|
|
|
|
return errors.New("工位号非法")
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(req.StationNo)).First(ctx)
|
|
|
|
|
|
if err != nil {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
return fmt.Errorf("工位 %d 不存在,请先在工位主数据中维护", req.StationNo)
|
|
|
|
|
|
}
|
|
|
|
|
|
// 仅改名称:flowId 不传(nil)保留原绑定;0=解绑;>0=绑定指定启用流程
|
|
|
|
|
|
upd := s.ctx.EntClient.Station.UpdateOneID(st.ID).SetName(req.Name)
|
|
|
|
|
|
if req.StationType != nil {
|
|
|
|
|
|
t := *req.StationType
|
|
|
|
|
|
if t != "LINE" && t != "OFFLINE" {
|
|
|
|
|
|
return errors.New("工位类型仅支持 LINE/OFFLINE")
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
upd.SetStationType(t)
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.FlowId != nil {
|
|
|
|
|
|
fid := *req.FlowId
|
|
|
|
|
|
if fid > 0 {
|
|
|
|
|
|
flow, fErr := s.ctx.EntClient.ProcessFlow.Get(ctx, fid)
|
2026-08-31 12:58:28 +08:00
|
|
|
|
if fErr != nil {
|
|
|
|
|
|
return errors.New("所选工艺流程不存在")
|
|
|
|
|
|
}
|
|
|
|
|
|
if flow.Status != "ACTIVE" {
|
|
|
|
|
|
return errors.New("该工艺流程已停用,请先启用后再绑定工位")
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
upd.SetFlowId(fid)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
upd.SetFlowId(0)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
if _, err := upd.Save(ctx); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-08-31 12:58:28 +08:00
|
|
|
|
s.ctx.EventLog.Write(ctx, "station.save", "", operator, "station", "", "维护工位",
|
|
|
|
|
|
map[string]any{"stationNo": req.StationNo})
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SetFlowStatus 工艺流程启用/停用。
|
2026-09-04 14:18:53 +08:00
|
|
|
|
// 新语义:绑定关系由「保存流程」显式维护(一个流程可绑多工位),启停只切换流程状态,
|
|
|
|
|
|
// 不再自动解绑/绑定工位;停用流程对已绑定工位不可用(工位取步骤时按 ACTIVE 过滤)。
|
2026-08-31 12:58:28 +08:00
|
|
|
|
func (s *Service) SetFlowStatus(ctx context.Context, id int, status, operator string) error {
|
|
|
|
|
|
if id <= 0 {
|
|
|
|
|
|
return errors.New("缺少 id")
|
|
|
|
|
|
}
|
|
|
|
|
|
if status != "ACTIVE" && status != "INACTIVE" {
|
|
|
|
|
|
return errors.New("非法状态")
|
|
|
|
|
|
}
|
|
|
|
|
|
flow, err := s.ctx.EntClient.ProcessFlow.Get(ctx, id)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return errors.New("工艺流程不存在")
|
|
|
|
|
|
}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
if flow.Status == status {
|
|
|
|
|
|
return nil
|
2026-08-31 12:58:28 +08:00
|
|
|
|
}
|
2026-09-04 14:18:53 +08:00
|
|
|
|
// 启用无需额外动作:绑定由保存流程时校验(一个工位不会被两个启用流程占用)
|
2026-08-31 12:58:28 +08:00
|
|
|
|
if err := s.ctx.EntClient.ProcessFlow.UpdateOneID(id).SetStatus(status).Exec(ctx); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
action := "停用工艺流程"
|
|
|
|
|
|
if status == "ACTIVE" {
|
|
|
|
|
|
action = "启用工艺流程"
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
s.ctx.EventLog.Write(ctx, "process.flow.status", "", operator, "process_flow", "", action, map[string]any{"flowId": id, "status": status, "name": flow.Name})
|
2026-08-31 08:06:55 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------- 工位任务(块4/5:工位终端拉取) ----------
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Service) StationTask(ctx context.Context, stationNo int) (map[string]any, error) {
|
|
|
|
|
|
st, err := s.ctx.EntClient.Station.Query().Where(station.StationNo(stationNo)).First(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, errors.New("工位不存在或未配置")
|
|
|
|
|
|
}
|
|
|
|
|
|
orderNos := []string{}
|
|
|
|
|
|
wos, _ := s.ctx.EntClient.WorkOrder.Query().
|
|
|
|
|
|
Where(workorder.StatusIn("CREATED", "RELEASED", "IN_PROGRESS")).
|
|
|
|
|
|
Order(ent.Asc(workorder.FieldID)).Limit(10).All(ctx)
|
|
|
|
|
|
for _, w := range wos {
|
|
|
|
|
|
orderNos = append(orderNos, w.WorkOrderNo)
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// M4:工位终端按"工艺路线段"驱动(段=并行工位组,谁空停谁)。
|
|
|
|
|
|
// 解析活跃工单的 routeSnapshot,找出本工位所属的路线段,供终端按段执行流程。
|
|
|
|
|
|
routeSegments := []map[string]any{}
|
|
|
|
|
|
for _, w := range wos {
|
|
|
|
|
|
if len(w.RouteSnapshot) == 0 {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, seg := range w.RouteSnapshot {
|
|
|
|
|
|
stations, _ := seg["stations"].([]any)
|
|
|
|
|
|
inSeg := false
|
|
|
|
|
|
for _, sv := range stations {
|
|
|
|
|
|
switch n := sv.(type) {
|
|
|
|
|
|
case float64:
|
|
|
|
|
|
if int(n) == stationNo {
|
|
|
|
|
|
inSeg = true
|
|
|
|
|
|
}
|
|
|
|
|
|
case int:
|
|
|
|
|
|
if n == stationNo {
|
|
|
|
|
|
inSeg = true
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if inSeg {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if !inSeg {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
flowId := 0
|
|
|
|
|
|
if fv, ok := seg["flowId"].(float64); ok {
|
|
|
|
|
|
flowId = int(fv)
|
|
|
|
|
|
} else if fv, ok := seg["flowId"].(int); ok {
|
|
|
|
|
|
flowId = fv
|
|
|
|
|
|
}
|
|
|
|
|
|
routeSegments = append(routeSegments, map[string]any{
|
|
|
|
|
|
"orderNo": w.WorkOrderNo,
|
|
|
|
|
|
"seq": seg["seq"],
|
|
|
|
|
|
"segmentType": seg["segmentType"],
|
|
|
|
|
|
"flowId": flowId,
|
|
|
|
|
|
"stations": stations,
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 按段驱动:本工位实际执行的工艺流程取自「本工位所属路线段」的 flowId;
|
|
|
|
|
|
// 工单未配置路线段(旧数据/未排路线)时回退到工位绑定的 flow_id。
|
|
|
|
|
|
activeFlowId := 0
|
|
|
|
|
|
currentSegment := map[string]any{}
|
|
|
|
|
|
minSeq := -1
|
|
|
|
|
|
for _, sg := range routeSegments {
|
|
|
|
|
|
seq, _ := sg["seq"].(float64)
|
|
|
|
|
|
seqInt := int(seq)
|
|
|
|
|
|
if sgv, ok := sg["seq"].(int); ok {
|
|
|
|
|
|
seqInt = sgv
|
|
|
|
|
|
}
|
|
|
|
|
|
if minSeq < 0 || seqInt < minSeq {
|
|
|
|
|
|
minSeq, currentSegment = seqInt, sg
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(currentSegment) > 0 {
|
|
|
|
|
|
switch v := currentSegment["flowId"].(type) {
|
|
|
|
|
|
case int:
|
|
|
|
|
|
activeFlowId = v
|
|
|
|
|
|
case float64:
|
|
|
|
|
|
activeFlowId = int(v)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if activeFlowId == 0 {
|
|
|
|
|
|
activeFlowId = st.FlowId
|
|
|
|
|
|
}
|
|
|
|
|
|
var flow *ent.ProcessFlow
|
|
|
|
|
|
if activeFlowId > 0 {
|
|
|
|
|
|
flow, _ = s.ctx.EntClient.ProcessFlow.Get(ctx, activeFlowId)
|
|
|
|
|
|
}
|
|
|
|
|
|
steps := []*ProcessStepTemplate{}
|
|
|
|
|
|
if flow != nil && flow.Status == "ACTIVE" {
|
|
|
|
|
|
steps = s.flowSteps(ctx, flow.ID)
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
return map[string]any{
|
2026-09-10 16:59:15 +08:00
|
|
|
|
"stationNo": st.StationNo,
|
|
|
|
|
|
"stationName": st.Name,
|
|
|
|
|
|
"flowId": activeFlowId,
|
|
|
|
|
|
"segment": currentSegment,
|
|
|
|
|
|
"flow": flow,
|
|
|
|
|
|
"flowActive": flow != nil && flow.Status == "ACTIVE",
|
|
|
|
|
|
"steps": steps,
|
|
|
|
|
|
"orderNos": orderNos,
|
|
|
|
|
|
"routeSegments": routeSegments,
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-10 16:59:15 +08:00
|
|
|
|
// WorkloadRow 绩效/工作量聚合行(按人/按工位/明细三视图共用)
|
2026-08-31 08:06:55 +08:00
|
|
|
|
type WorkloadRow struct {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
Operator string
|
|
|
|
|
|
StationNo string
|
|
|
|
|
|
Date string
|
|
|
|
|
|
ProcessCode int
|
|
|
|
|
|
ProcessName string
|
|
|
|
|
|
DoneCount int
|
|
|
|
|
|
OkCount int
|
|
|
|
|
|
NgCount int
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-09-08 11:44:04 +08:00
|
|
|
|
func (s *Service) Workload(ctx context.Context, operator, stationNo, from, to string,
|
|
|
|
|
|
opPage, opSize, stPage, stSize, detPage, detSize int) (map[string]any, error) {
|
2026-08-31 08:06:55 +08:00
|
|
|
|
q := s.ctx.EntClient.WorkpieceProcess.Query()
|
|
|
|
|
|
if operator != "" {
|
|
|
|
|
|
q = q.Where(workpieceprocess.Operator(operator))
|
|
|
|
|
|
}
|
2026-08-31 12:58:28 +08:00
|
|
|
|
if stationNo != "" {
|
|
|
|
|
|
q = q.Where(workpieceprocess.StationNo(stationNo))
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if from != "" {
|
|
|
|
|
|
if f, err := time.Parse("2006-01-02", from); err == nil {
|
|
|
|
|
|
q = q.Where(workpieceprocess.CreatedAtGTE(f))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if to != "" {
|
|
|
|
|
|
if t, err := time.Parse("2006-01-02", to); err == nil {
|
|
|
|
|
|
q = q.Where(workpieceprocess.CreatedAtLT(t.Add(24 * time.Hour)))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
rows, err := q.All(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2026-09-10 16:59:15 +08:00
|
|
|
|
type key struct {
|
|
|
|
|
|
op, station, date string
|
|
|
|
|
|
code int
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
agg := map[key]*WorkloadRow{}
|
|
|
|
|
|
for _, r := range rows {
|
2026-08-31 12:58:28 +08:00
|
|
|
|
k := key{r.Operator, r.StationNo, r.CreatedAt.Format("2006-01-02"), r.ProcessCode}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
row, ok := agg[k]
|
|
|
|
|
|
if !ok {
|
2026-08-31 12:58:28 +08:00
|
|
|
|
row = &WorkloadRow{Operator: r.Operator, StationNo: r.StationNo, Date: k.date, ProcessCode: r.ProcessCode, ProcessName: r.ProcessName}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
agg[k] = row
|
|
|
|
|
|
}
|
|
|
|
|
|
row.DoneCount++
|
|
|
|
|
|
if r.Result == "OK" {
|
|
|
|
|
|
row.OkCount++
|
|
|
|
|
|
} else {
|
|
|
|
|
|
row.NgCount++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
list := make([]*WorkloadRow, 0, len(agg))
|
|
|
|
|
|
for _, v := range agg {
|
|
|
|
|
|
list = append(list, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
sort.Slice(list, func(i, j int) bool {
|
|
|
|
|
|
if list[i].Operator != list[j].Operator {
|
|
|
|
|
|
return list[i].Operator < list[j].Operator
|
|
|
|
|
|
}
|
2026-08-31 12:58:28 +08:00
|
|
|
|
if list[i].StationNo != list[j].StationNo {
|
|
|
|
|
|
return list[i].StationNo < list[j].StationNo
|
|
|
|
|
|
}
|
2026-08-31 08:06:55 +08:00
|
|
|
|
if list[i].Date != list[j].Date {
|
|
|
|
|
|
return list[i].Date < list[j].Date
|
|
|
|
|
|
}
|
|
|
|
|
|
return list[i].ProcessCode < list[j].ProcessCode
|
|
|
|
|
|
})
|
2026-09-08 11:44:04 +08:00
|
|
|
|
|
|
|
|
|
|
// 三个视图分别聚合(2026-09-08 绩效报表改造:按人/按工位/明细,各自真分页)
|
|
|
|
|
|
byOpAgg := map[string]*WorkloadRow{}
|
|
|
|
|
|
byStAgg := map[string]*WorkloadRow{}
|
|
|
|
|
|
for _, v := range list {
|
|
|
|
|
|
opKey := v.Operator
|
|
|
|
|
|
if o, ok := byOpAgg[opKey]; ok {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
o.DoneCount += v.DoneCount
|
|
|
|
|
|
o.OkCount += v.OkCount
|
|
|
|
|
|
o.NgCount += v.NgCount
|
2026-09-08 11:44:04 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
byOpAgg[opKey] = &WorkloadRow{Operator: v.Operator, DoneCount: v.DoneCount, OkCount: v.OkCount, NgCount: v.NgCount}
|
|
|
|
|
|
}
|
|
|
|
|
|
stKey := v.StationNo
|
|
|
|
|
|
if o, ok := byStAgg[stKey]; ok {
|
2026-09-10 16:59:15 +08:00
|
|
|
|
o.DoneCount += v.DoneCount
|
|
|
|
|
|
o.OkCount += v.OkCount
|
|
|
|
|
|
o.NgCount += v.NgCount
|
2026-09-08 11:44:04 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
byStAgg[stKey] = &WorkloadRow{StationNo: v.StationNo, DoneCount: v.DoneCount, OkCount: v.OkCount, NgCount: v.NgCount}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
paginateRows := func(all []*ent.WorkpieceProcess, page, size int) (int, []*ent.WorkpieceProcess) {
|
|
|
|
|
|
total := len(all)
|
|
|
|
|
|
start := (page - 1) * size
|
|
|
|
|
|
if start > total {
|
|
|
|
|
|
start = total
|
|
|
|
|
|
}
|
|
|
|
|
|
end := start + size
|
|
|
|
|
|
if end > total {
|
|
|
|
|
|
end = total
|
|
|
|
|
|
}
|
|
|
|
|
|
return total, all[start:end]
|
|
|
|
|
|
}
|
|
|
|
|
|
paginate := func(all []*WorkloadRow, page, size int) (int, []*WorkloadRow) {
|
|
|
|
|
|
total := len(all)
|
|
|
|
|
|
start := (page - 1) * size
|
|
|
|
|
|
if start > total {
|
|
|
|
|
|
start = total
|
|
|
|
|
|
}
|
|
|
|
|
|
end := start + size
|
|
|
|
|
|
if end > total {
|
|
|
|
|
|
end = total
|
|
|
|
|
|
}
|
|
|
|
|
|
return total, all[start:end]
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
byOpList := make([]*WorkloadRow, 0, len(byOpAgg))
|
|
|
|
|
|
for _, v := range byOpAgg {
|
|
|
|
|
|
byOpList = append(byOpList, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
sort.Slice(byOpList, func(i, j int) bool { return byOpList[i].Operator < byOpList[j].Operator })
|
|
|
|
|
|
byStList := make([]*WorkloadRow, 0, len(byStAgg))
|
|
|
|
|
|
for _, v := range byStAgg {
|
|
|
|
|
|
byStList = append(byStList, v)
|
|
|
|
|
|
}
|
|
|
|
|
|
sort.Slice(byStList, func(i, j int) bool { return byStList[i].StationNo < byStList[j].StationNo })
|
|
|
|
|
|
|
|
|
|
|
|
opTotal, byOpPage := paginate(byOpList, opPage, opSize)
|
|
|
|
|
|
stTotal, byStPage := paginate(byStList, stPage, stSize)
|
|
|
|
|
|
detTotal, detRowsPage := paginateRows(rows, detPage, detSize)
|
|
|
|
|
|
|
|
|
|
|
|
return map[string]any{
|
|
|
|
|
|
"byOperator": map[string]any{"total": opTotal, "list": byOpPage},
|
|
|
|
|
|
"byStation": map[string]any{"total": stTotal, "list": byStPage},
|
|
|
|
|
|
"detail": map[string]any{"total": detTotal, "list": detRowsPage},
|
|
|
|
|
|
}, nil
|
2026-08-31 08:06:55 +08:00
|
|
|
|
}
|