using System.Diagnostics;
using System.Text.Json;
using Admin.NET.Plugin.AiDOP.Entity;
using Admin.NET.Plugin.AiDOP.Manufacturing;
using Admin.NET.Plugin.ApprovalFlow;
using Admin.NET.Plugin.ApprovalFlow.Service;
using Yitter.IdGenerator;
using ApprovalFlowEntity = Admin.NET.Plugin.ApprovalFlow.ApprovalFlow;
namespace Admin.NET.Plugin.AiDOP.Infrastructure;
///
/// S6 过程检验单(IPQC)检验员+主管流程种子(幂等)。
///
/// 顺序:① 扩展表 ado_s6_ipqc_inspection_flow_state(CodeFirst 建表,无 SQL)
/// ② 角色 ROLE_S6_IPQC_INSPECTOR / ROLE_S6_IPQC_SUPERVISOR ③ UAT 成员绑定(superAdmin.NET 同绑两角色,仅 UAT 自审)
/// ④ ApprovalBizType(IPQC_INSPECTION) ⑤ ApprovalFlow 已发布定义(start→N1_INSPECT→N2_SUP_REVIEW→end,节点绑角色)。
/// 全部幂等:存在则跳过,不覆盖、不删除既有角色/流程。superAdmin.NET 不存在时仅告警不崩溃。
/// 本批不含网关/SQE/不合格处置。
///
public static class IpqcInspectionFlowSeed
{
public static void EnsureSeed(ISqlSugarClient db)
{
try
{
db.CodeFirst.InitTables(
typeof(ApprovalBizType),
typeof(ApprovalFlowEntity),
typeof(ApprovalFlowVersion));
// 流程状态轻量扩展表(CodeFirst 建表,无 SQL)
db.CodeFirst.InitTables(typeof(AdoS6IpqcInspectionFlowState));
EnsureRoles(db);
EnsureUatMembers(db);
EnsureBizType(db);
EnsurePublishedFlow(db);
}
catch (Exception ex)
{
Trace.TraceWarning("S6 IPQC inspection flow seed: " + ex);
}
}
private static void EnsureRoles(ISqlSugarClient db)
{
EnsureRole(db, IpqcInspectionFlowConst.RoleInspectorId, IpqcInspectionFlowConst.RoleInspectorCode, "过程检验员", 820);
EnsureRole(db, IpqcInspectionFlowConst.RoleSupervisorId, IpqcInspectionFlowConst.RoleSupervisorCode, "过程检验主管", 821);
}
private static void EnsureRole(ISqlSugarClient db, long id, string code, string name, int orderNo)
{
var exists = db.Queryable().ClearFilter().Any(x => x.Code == code);
if (exists) return;
db.Insertable(new SysRole
{
Id = id,
TenantId = SqlSugarConst.DefaultTenantId,
Name = name,
Code = code,
OrderNo = orderNo,
DataScope = DataScopeEnum.Self,
Status = StatusEnum.Enable,
Remark = "S6 过程检验流程角色",
CreateTime = DateTime.Now,
}).ExecuteCommand();
}
private static void EnsureUatMembers(ISqlSugarClient db)
{
var user = db.Queryable().ClearFilter()
.Where(x => x.Account == IpqcInspectionFlowConst.UatAccount)
.First();
if (user == null)
{
Trace.TraceWarning($"S6 IPQC flow seed: UAT 账号 {IpqcInspectionFlowConst.UatAccount} 不存在,跳过成员绑定(阶段2 冒烟前须指定成员)");
return;
}
EnsureUserRole(db, user.Id, IpqcInspectionFlowConst.RoleInspectorId);
EnsureUserRole(db, user.Id, IpqcInspectionFlowConst.RoleSupervisorId);
}
private static void EnsureUserRole(ISqlSugarClient db, long userId, long roleId)
{
var exists = db.Queryable().ClearFilter().Any(x => x.UserId == userId && x.RoleId == roleId);
if (exists) return;
db.Insertable(new SysUserRole
{
Id = YitIdHelper.NextId(),
UserId = userId,
RoleId = roleId,
}).ExecuteCommand();
}
private static void EnsureBizType(ISqlSugarClient db)
{
var exists = db.Queryable().Any(x => x.Code == IpqcInspectionFlowConst.BizType);
if (exists) return;
db.Insertable(new ApprovalBizType
{
Id = YitIdHelper.NextId(),
Code = IpqcInspectionFlowConst.BizType,
Name = "过程检验单审批",
Remark = "S6 过程检验单 检验员→主管 审批流",
IsEnabled = true,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
}).ExecuteCommand();
}
private static void EnsurePublishedFlow(ISqlSugarClient db)
{
var orgIds = db.Queryable()
.Select(x => x.Id)
.ToList()
.Where(x => x > 0)
.Distinct()
.ToList();
if (orgIds.Count == 0) orgIds.Add(0);
var now = DateTime.Now;
var flowJson = BuildFlowJson();
foreach (var orgId in orgIds)
{
var existing = db.Queryable()
.Where(x => x.BizType == IpqcInspectionFlowConst.BizType && x.OrgId == orgId && x.IsPublished && !x.IsDelete)
.OrderByDescending(x => x.Version)
.First();
// 已有已发布定义则跳过(幂等,不覆盖)。历史实例用各自 FlowJsonSnapshot。
if (existing != null) continue;
var flow = new ApprovalFlowEntity
{
Id = YitIdHelper.NextId(),
Code = $"S6IPQC_{orgId}",
Name = "过程检验单审批",
BizType = IpqcInspectionFlowConst.BizType,
FormJson = "{}",
FlowJson = flowJson,
Status = 1,
Remark = "系统默认流程 v1:开始-检验员检验-检验主管审核-结束",
Version = 1,
IsPublished = true,
OrgId = orgId,
CreateTime = now,
UpdateTime = now,
};
db.Insertable(flow).ExecuteCommand();
db.Insertable(new ApprovalFlowVersion
{
Id = YitIdHelper.NextId(),
FlowId = flow.Id,
Version = flow.Version,
FlowJson = flow.FlowJson,
FormJson = flow.FormJson,
PublishTime = now,
PublisherName = "系统初始化",
CreateTime = now,
UpdateTime = now,
}).ExecuteCommand();
}
}
private static string BuildFlowJson()
{
var item = new ApprovalFlowItem
{
Nodes = new List
{
new()
{
Id = "start",
Type = "bpmn:startEvent",
X = 100,
Y = 120,
Properties = new FlowProperties { NodeName = "开始" },
Text = new FlowTextItem { X = 100, Y = 154, Value = "开始" },
},
new()
{
Id = IpqcInspectionFlowConst.NodeInspect,
Type = "bpmn:userTask",
X = 300,
Y = 120,
Properties = new FlowProperties
{
NodeName = "检验员检验",
ApproverType = nameof(ApproverTypeEnum.Role),
ApproverIds = IpqcInspectionFlowConst.RoleInspectorId.ToString(),
ApproverNames = "过程检验员",
MultiApproveMode = "Any",
},
Text = new FlowTextItem { X = 300, Y = 154, Value = "检验员检验" },
},
new()
{
Id = IpqcInspectionFlowConst.NodeSupervisor,
Type = "bpmn:userTask",
X = 500,
Y = 120,
Properties = new FlowProperties
{
NodeName = "检验主管审核",
ApproverType = nameof(ApproverTypeEnum.Role),
ApproverIds = IpqcInspectionFlowConst.RoleSupervisorId.ToString(),
ApproverNames = "过程检验主管",
MultiApproveMode = "Any",
},
Text = new FlowTextItem { X = 500, Y = 154, Value = "检验主管审核" },
},
new()
{
Id = "end",
Type = "bpmn:endEvent",
X = 700,
Y = 120,
Properties = new FlowProperties { NodeName = "结束" },
Text = new FlowTextItem { X = 700, Y = 154, Value = "结束" },
},
},
Edges = new List
{
BuildEdge("edge-start-n1", "start", IpqcInspectionFlowConst.NodeInspect, 140, 120, 260, 120),
BuildEdge("edge-n1-n2", IpqcInspectionFlowConst.NodeInspect, IpqcInspectionFlowConst.NodeSupervisor, 340, 120, 460, 120),
BuildEdge("edge-n2-end", IpqcInspectionFlowConst.NodeSupervisor, "end", 540, 120, 660, 120),
}
};
return JsonSerializer.Serialize(item);
}
private static ApprovalFlowEdgeItem BuildEdge(string id, string source, string target, float sx, float sy, float ex, float ey)
{
return new ApprovalFlowEdgeItem
{
Id = id,
Type = "bpmn:sequenceFlow",
SourceNodeId = source,
TargetNodeId = target,
StartPoint = new FlowEdgePointItem { X = sx, Y = sy },
EndPoint = new FlowEdgePointItem { X = ex, Y = ey },
Properties = new FlowProperties(),
PointsList = new List
{
new() { X = sx, Y = sy },
new() { X = ex, Y = ey },
},
};
}
}