IqcInspBillFlowSeed.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System.Diagnostics;
  2. using System.Text.Json;
  3. using Admin.NET.Plugin.AiDOP.Entity;
  4. using Admin.NET.Plugin.AiDOP.MaterialWarehouse;
  5. using Admin.NET.Plugin.ApprovalFlow;
  6. using Admin.NET.Plugin.ApprovalFlow.Service;
  7. using Yitter.IdGenerator;
  8. using ApprovalFlowEntity = Admin.NET.Plugin.ApprovalFlow.ApprovalFlow;
  9. namespace Admin.NET.Plugin.AiDOP.Infrastructure;
  10. /// <summary>
  11. /// S5 来料检验单 检验员+主管流程种子(幂等)。
  12. ///
  13. /// 顺序:① 角色 ROLE_S5_IQC_INSPECTOR / ROLE_S5_IQC_SUPERVISOR ② UAT 成员绑定(superAdmin.NET 同绑两角色)
  14. /// ③ ApprovalBizType(S5_IQC_INSPBILL) ④ ApprovalFlow 已发布定义(start→N1_INSPECT→N2_SUP_REVIEW→end,节点绑角色)。
  15. /// 全部幂等:存在则跳过,不覆盖、不删除既有角色/流程。superAdmin.NET 不存在时仅告警不崩溃。
  16. /// </summary>
  17. public static class IqcInspBillFlowSeed
  18. {
  19. public static void EnsureSeed(ISqlSugarClient db)
  20. {
  21. try
  22. {
  23. db.CodeFirst.InitTables(
  24. typeof(ApprovalBizType),
  25. typeof(ApprovalFlowEntity),
  26. typeof(ApprovalFlowVersion));
  27. // D' SQE 处置表(CodeFirst 建表,无 SQL)
  28. db.CodeFirst.InitTables(typeof(AdoS5IqcSqeDisposition));
  29. EnsureRoles(db);
  30. EnsureUatMembers(db);
  31. EnsureBizType(db);
  32. EnsurePublishedFlow(db);
  33. }
  34. catch (Exception ex)
  35. {
  36. Trace.TraceWarning("S5 IQC inspbill flow seed: " + ex);
  37. }
  38. }
  39. private static void EnsureRoles(ISqlSugarClient db)
  40. {
  41. EnsureRole(db, IqcInspBillFlowConst.RoleInspectorId, IqcInspBillFlowConst.RoleInspectorCode, "来料检验员", 810);
  42. EnsureRole(db, IqcInspBillFlowConst.RoleSupervisorId, IqcInspBillFlowConst.RoleSupervisorCode, "来料检验主管", 811);
  43. EnsureRole(db, IqcInspBillFlowConst.RoleSqeId, IqcInspBillFlowConst.RoleSqeCode, "来料检验SQE", 812);
  44. }
  45. private static void EnsureRole(ISqlSugarClient db, long id, string code, string name, int orderNo)
  46. {
  47. var exists = db.Queryable<SysRole>().ClearFilter().Any(x => x.Code == code);
  48. if (exists) return;
  49. db.Insertable(new SysRole
  50. {
  51. Id = id,
  52. TenantId = SqlSugarConst.DefaultTenantId,
  53. Name = name,
  54. Code = code,
  55. OrderNo = orderNo,
  56. DataScope = DataScopeEnum.Self,
  57. Status = StatusEnum.Enable,
  58. Remark = "S5 来料检验流程角色",
  59. CreateTime = DateTime.Now,
  60. }).ExecuteCommand();
  61. }
  62. private static void EnsureUatMembers(ISqlSugarClient db)
  63. {
  64. var user = db.Queryable<SysUser>().ClearFilter()
  65. .Where(x => x.Account == IqcInspBillFlowConst.UatAccount)
  66. .First();
  67. if (user == null)
  68. {
  69. Trace.TraceWarning($"S5 IQC flow seed: UAT 账号 {IqcInspBillFlowConst.UatAccount} 不存在,跳过成员绑定(阶段2 冒烟前须指定成员)");
  70. return;
  71. }
  72. EnsureUserRole(db, user.Id, IqcInspBillFlowConst.RoleInspectorId);
  73. EnsureUserRole(db, user.Id, IqcInspBillFlowConst.RoleSupervisorId);
  74. EnsureUserRole(db, user.Id, IqcInspBillFlowConst.RoleSqeId);
  75. }
  76. private static void EnsureUserRole(ISqlSugarClient db, long userId, long roleId)
  77. {
  78. var exists = db.Queryable<SysUserRole>().ClearFilter().Any(x => x.UserId == userId && x.RoleId == roleId);
  79. if (exists) return;
  80. db.Insertable(new SysUserRole
  81. {
  82. Id = YitIdHelper.NextId(),
  83. UserId = userId,
  84. RoleId = roleId,
  85. }).ExecuteCommand();
  86. }
  87. private static void EnsureBizType(ISqlSugarClient db)
  88. {
  89. var exists = db.Queryable<ApprovalBizType>().Any(x => x.Code == IqcInspBillFlowConst.BizType);
  90. if (exists) return;
  91. db.Insertable(new ApprovalBizType
  92. {
  93. Id = YitIdHelper.NextId(),
  94. Code = IqcInspBillFlowConst.BizType,
  95. Name = "来料检验单审批",
  96. Remark = "S5 来料检验单 检验员→主管 审批流",
  97. IsEnabled = true,
  98. CreateTime = DateTime.Now,
  99. UpdateTime = DateTime.Now,
  100. }).ExecuteCommand();
  101. }
  102. private static void EnsurePublishedFlow(ISqlSugarClient db)
  103. {
  104. var orgIds = db.Queryable<SysOrg>()
  105. .Select(x => x.Id)
  106. .ToList()
  107. .Where(x => x > 0)
  108. .Distinct()
  109. .ToList();
  110. if (orgIds.Count == 0) orgIds.Add(0);
  111. var now = DateTime.Now;
  112. var flowJson = BuildFlowJson();
  113. foreach (var orgId in orgIds)
  114. {
  115. var existing = db.Queryable<ApprovalFlowEntity>()
  116. .Where(x => x.BizType == IqcInspBillFlowConst.BizType && x.OrgId == orgId && x.IsPublished && !x.IsDelete)
  117. .OrderByDescending(x => x.Version)
  118. .First();
  119. // v1→v2 幂等升级:已发布定义若已含 N3_SQE(v2)则跳过;若仍为 v1(无 N3)则下架旧版、发布 v2。
  120. // 历史 Running/Approved 实例使用各自 FlowJsonSnapshot,不受本次 republish 影响。
  121. if (existing != null)
  122. {
  123. var isV2 = (existing.FlowJson ?? "").Contains(IqcInspBillFlowConst.NodeSqe)
  124. && (existing.FlowJson ?? "").Contains(IqcInspBillFlowConst.GatewayPd);
  125. if (isV2) continue;
  126. db.Updateable<ApprovalFlowEntity>()
  127. .SetColumns(x => x.IsPublished == false)
  128. .SetColumns(x => x.UpdateTime == now)
  129. .Where(x => x.Id == existing.Id)
  130. .ExecuteCommand();
  131. }
  132. var nextVersion = (existing?.Version ?? 0) + 1;
  133. var flow = new ApprovalFlowEntity
  134. {
  135. Id = YitIdHelper.NextId(),
  136. Code = $"S5IQC_{orgId}",
  137. Name = "来料检验单审批",
  138. BizType = IqcInspBillFlowConst.BizType,
  139. FormJson = "{}",
  140. FlowJson = flowJson,
  141. Status = 1,
  142. Remark = "系统默认流程 v2:开始-检验员检验-检验主管审核-网关(pd)-SQE处置-结束",
  143. Version = nextVersion,
  144. IsPublished = true,
  145. OrgId = orgId,
  146. TenantId = ApprovalFlowTenantSeed.ResolveTenantId(db, orgId),
  147. CreateTime = now,
  148. UpdateTime = now,
  149. };
  150. db.Insertable(flow).ExecuteCommand();
  151. db.Insertable(new ApprovalFlowVersion
  152. {
  153. Id = YitIdHelper.NextId(),
  154. FlowId = flow.Id,
  155. Version = flow.Version,
  156. FlowJson = flow.FlowJson,
  157. FormJson = flow.FormJson,
  158. PublishTime = now,
  159. PublisherName = "系统初始化",
  160. CreateTime = now,
  161. UpdateTime = now,
  162. }).ExecuteCommand();
  163. }
  164. }
  165. private static string BuildFlowJson()
  166. {
  167. var item = new ApprovalFlowItem
  168. {
  169. Nodes = new List<ApprovalFlowNodeItem>
  170. {
  171. new()
  172. {
  173. Id = "start",
  174. Type = "bpmn:startEvent",
  175. X = 100,
  176. Y = 120,
  177. Properties = new FlowProperties { NodeName = "开始" },
  178. Text = new FlowTextItem { X = 100, Y = 154, Value = "开始" },
  179. },
  180. new()
  181. {
  182. Id = IqcInspBillFlowConst.NodeInspect,
  183. Type = "bpmn:userTask",
  184. X = 300,
  185. Y = 120,
  186. Properties = new FlowProperties
  187. {
  188. NodeName = "检验员检验",
  189. ApproverType = nameof(ApproverTypeEnum.Role),
  190. ApproverIds = IqcInspBillFlowConst.RoleInspectorId.ToString(),
  191. ApproverNames = "来料检验员",
  192. MultiApproveMode = "Any",
  193. },
  194. Text = new FlowTextItem { X = 300, Y = 154, Value = "检验员检验" },
  195. },
  196. new()
  197. {
  198. Id = IqcInspBillFlowConst.NodeSupervisor,
  199. Type = "bpmn:userTask",
  200. X = 500,
  201. Y = 120,
  202. Properties = new FlowProperties
  203. {
  204. NodeName = "检验主管审核",
  205. ApproverType = nameof(ApproverTypeEnum.Role),
  206. ApproverIds = IqcInspBillFlowConst.RoleSupervisorId.ToString(),
  207. ApproverNames = "来料检验主管",
  208. MultiApproveMode = "Any",
  209. },
  210. Text = new FlowTextItem { X = 500, Y = 154, Value = "检验主管审核" },
  211. },
  212. new()
  213. {
  214. Id = IqcInspBillFlowConst.GatewayPd,
  215. Type = "bpmn:exclusiveGateway",
  216. X = 660,
  217. Y = 120,
  218. Properties = new FlowProperties
  219. {
  220. NodeName = "判定分流",
  221. Conditions = new List<GatewayCondition>
  222. {
  223. new()
  224. {
  225. Expression = "pd == 1",
  226. TargetNodeId = IqcInspBillFlowConst.NodeSqe,
  227. IsDefault = false,
  228. Label = "不合格转SQE",
  229. },
  230. new()
  231. {
  232. TargetNodeId = "end",
  233. IsDefault = true,
  234. Label = "合格完成",
  235. },
  236. },
  237. },
  238. Text = new FlowTextItem { X = 660, Y = 154, Value = "判定分流" },
  239. },
  240. new()
  241. {
  242. Id = IqcInspBillFlowConst.NodeSqe,
  243. Type = "bpmn:userTask",
  244. X = 820,
  245. Y = 120,
  246. Properties = new FlowProperties
  247. {
  248. NodeName = "SQE处置",
  249. ApproverType = nameof(ApproverTypeEnum.Role),
  250. ApproverIds = IqcInspBillFlowConst.RoleSqeId.ToString(),
  251. ApproverNames = "来料检验SQE",
  252. MultiApproveMode = "Any",
  253. },
  254. Text = new FlowTextItem { X = 820, Y = 154, Value = "SQE处置" },
  255. },
  256. new()
  257. {
  258. Id = "end",
  259. Type = "bpmn:endEvent",
  260. X = 980,
  261. Y = 120,
  262. Properties = new FlowProperties { NodeName = "结束" },
  263. Text = new FlowTextItem { X = 980, Y = 154, Value = "结束" },
  264. },
  265. },
  266. Edges = new List<ApprovalFlowEdgeItem>
  267. {
  268. BuildEdge("edge-start-n1", "start", IqcInspBillFlowConst.NodeInspect, 140, 120, 260, 120),
  269. BuildEdge("edge-n1-n2", IqcInspBillFlowConst.NodeInspect, IqcInspBillFlowConst.NodeSupervisor, 340, 120, 460, 120),
  270. BuildEdge("edge-n2-gw", IqcInspBillFlowConst.NodeSupervisor, IqcInspBillFlowConst.GatewayPd, 540, 120, 640, 120),
  271. BuildEdge("edge-gw-n3", IqcInspBillFlowConst.GatewayPd, IqcInspBillFlowConst.NodeSqe, 680, 120, 780, 120),
  272. BuildEdge("edge-gw-end", IqcInspBillFlowConst.GatewayPd, "end", 680, 120, 960, 120),
  273. BuildEdge("edge-n3-end", IqcInspBillFlowConst.NodeSqe, "end", 860, 120, 960, 120),
  274. }
  275. };
  276. return JsonSerializer.Serialize(item);
  277. }
  278. private static ApprovalFlowEdgeItem BuildEdge(string id, string source, string target, float sx, float sy, float ex, float ey)
  279. {
  280. return new ApprovalFlowEdgeItem
  281. {
  282. Id = id,
  283. Type = "bpmn:sequenceFlow",
  284. SourceNodeId = source,
  285. TargetNodeId = target,
  286. StartPoint = new FlowEdgePointItem { X = sx, Y = sy },
  287. EndPoint = new FlowEdgePointItem { X = ex, Y = ey },
  288. Properties = new FlowProperties(),
  289. PointsList = new List<FlowEdgePointItem>
  290. {
  291. new() { X = sx, Y = sy },
  292. new() { X = ex, Y = ey },
  293. },
  294. };
  295. }
  296. }