IqcInspBillFlowSeed.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. CreateTime = now,
  147. UpdateTime = now,
  148. };
  149. db.Insertable(flow).ExecuteCommand();
  150. db.Insertable(new ApprovalFlowVersion
  151. {
  152. Id = YitIdHelper.NextId(),
  153. FlowId = flow.Id,
  154. Version = flow.Version,
  155. FlowJson = flow.FlowJson,
  156. FormJson = flow.FormJson,
  157. PublishTime = now,
  158. PublisherName = "系统初始化",
  159. CreateTime = now,
  160. UpdateTime = now,
  161. }).ExecuteCommand();
  162. }
  163. }
  164. private static string BuildFlowJson()
  165. {
  166. var item = new ApprovalFlowItem
  167. {
  168. Nodes = new List<ApprovalFlowNodeItem>
  169. {
  170. new()
  171. {
  172. Id = "start",
  173. Type = "bpmn:startEvent",
  174. X = 100,
  175. Y = 120,
  176. Properties = new FlowProperties { NodeName = "开始" },
  177. Text = new FlowTextItem { X = 100, Y = 154, Value = "开始" },
  178. },
  179. new()
  180. {
  181. Id = IqcInspBillFlowConst.NodeInspect,
  182. Type = "bpmn:userTask",
  183. X = 300,
  184. Y = 120,
  185. Properties = new FlowProperties
  186. {
  187. NodeName = "检验员检验",
  188. ApproverType = nameof(ApproverTypeEnum.Role),
  189. ApproverIds = IqcInspBillFlowConst.RoleInspectorId.ToString(),
  190. ApproverNames = "来料检验员",
  191. MultiApproveMode = "Any",
  192. },
  193. Text = new FlowTextItem { X = 300, Y = 154, Value = "检验员检验" },
  194. },
  195. new()
  196. {
  197. Id = IqcInspBillFlowConst.NodeSupervisor,
  198. Type = "bpmn:userTask",
  199. X = 500,
  200. Y = 120,
  201. Properties = new FlowProperties
  202. {
  203. NodeName = "检验主管审核",
  204. ApproverType = nameof(ApproverTypeEnum.Role),
  205. ApproverIds = IqcInspBillFlowConst.RoleSupervisorId.ToString(),
  206. ApproverNames = "来料检验主管",
  207. MultiApproveMode = "Any",
  208. },
  209. Text = new FlowTextItem { X = 500, Y = 154, Value = "检验主管审核" },
  210. },
  211. new()
  212. {
  213. Id = IqcInspBillFlowConst.GatewayPd,
  214. Type = "bpmn:exclusiveGateway",
  215. X = 660,
  216. Y = 120,
  217. Properties = new FlowProperties
  218. {
  219. NodeName = "判定分流",
  220. Conditions = new List<GatewayCondition>
  221. {
  222. new()
  223. {
  224. Expression = "pd == 1",
  225. TargetNodeId = IqcInspBillFlowConst.NodeSqe,
  226. IsDefault = false,
  227. Label = "不合格转SQE",
  228. },
  229. new()
  230. {
  231. TargetNodeId = "end",
  232. IsDefault = true,
  233. Label = "合格完成",
  234. },
  235. },
  236. },
  237. Text = new FlowTextItem { X = 660, Y = 154, Value = "判定分流" },
  238. },
  239. new()
  240. {
  241. Id = IqcInspBillFlowConst.NodeSqe,
  242. Type = "bpmn:userTask",
  243. X = 820,
  244. Y = 120,
  245. Properties = new FlowProperties
  246. {
  247. NodeName = "SQE处置",
  248. ApproverType = nameof(ApproverTypeEnum.Role),
  249. ApproverIds = IqcInspBillFlowConst.RoleSqeId.ToString(),
  250. ApproverNames = "来料检验SQE",
  251. MultiApproveMode = "Any",
  252. },
  253. Text = new FlowTextItem { X = 820, Y = 154, Value = "SQE处置" },
  254. },
  255. new()
  256. {
  257. Id = "end",
  258. Type = "bpmn:endEvent",
  259. X = 980,
  260. Y = 120,
  261. Properties = new FlowProperties { NodeName = "结束" },
  262. Text = new FlowTextItem { X = 980, Y = 154, Value = "结束" },
  263. },
  264. },
  265. Edges = new List<ApprovalFlowEdgeItem>
  266. {
  267. BuildEdge("edge-start-n1", "start", IqcInspBillFlowConst.NodeInspect, 140, 120, 260, 120),
  268. BuildEdge("edge-n1-n2", IqcInspBillFlowConst.NodeInspect, IqcInspBillFlowConst.NodeSupervisor, 340, 120, 460, 120),
  269. BuildEdge("edge-n2-gw", IqcInspBillFlowConst.NodeSupervisor, IqcInspBillFlowConst.GatewayPd, 540, 120, 640, 120),
  270. BuildEdge("edge-gw-n3", IqcInspBillFlowConst.GatewayPd, IqcInspBillFlowConst.NodeSqe, 680, 120, 780, 120),
  271. BuildEdge("edge-gw-end", IqcInspBillFlowConst.GatewayPd, "end", 680, 120, 960, 120),
  272. BuildEdge("edge-n3-end", IqcInspBillFlowConst.NodeSqe, "end", 860, 120, 960, 120),
  273. }
  274. };
  275. return JsonSerializer.Serialize(item);
  276. }
  277. private static ApprovalFlowEdgeItem BuildEdge(string id, string source, string target, float sx, float sy, float ex, float ey)
  278. {
  279. return new ApprovalFlowEdgeItem
  280. {
  281. Id = id,
  282. Type = "bpmn:sequenceFlow",
  283. SourceNodeId = source,
  284. TargetNodeId = target,
  285. StartPoint = new FlowEdgePointItem { X = sx, Y = sy },
  286. EndPoint = new FlowEdgePointItem { X = ex, Y = ey },
  287. Properties = new FlowProperties(),
  288. PointsList = new List<FlowEdgePointItem>
  289. {
  290. new() { X = sx, Y = sy },
  291. new() { X = ex, Y = ey },
  292. },
  293. };
  294. }
  295. }