Organize.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Data;
  2. namespace DataTransmission.Joint
  3. {
  4. public class Organize
  5. {
  6. private readonly CommonTool tool = new CommonTool();
  7. public DataTable GetOrganizeBase(string Pid ="")
  8. {
  9. var conn = tool.GetConSqlServer();
  10. var and = string.IsNullOrEmpty(Pid) ? "" : $" AND SBM_PID = {Pid} ";
  11. var sql = $@" SELECT *,'' AS NewId, 1 as Frequency FROM dbo.SYS09_BMDIR WHERE 1=1 AND SBM_ID != 1 AND SBM_Name not in ('热线中心','运营维护') {and} ORDER BY SBM_ID ";
  12. return tool.GetDataTable(sql, conn);
  13. }
  14. public string GetOrganize() {
  15. DataTable oldData = GetOrganizeBase();
  16. DataTable oneData = oldData.AsEnumerable().Where(x=>x.Field<int>("SBM_PID") == 1).CopyToDataTable();
  17. DataTable Data2 = oldData.Clone();
  18. DataTable Data3= oldData.Clone();
  19. DataTable Data4 = oldData.Clone();
  20. DataTable Data5 = oldData.Clone();
  21. DataTable Data6 = oldData.Clone();
  22. var insertSql = $@"INSERT INTO ""public"".""system_organize""
  23. (
  24. ""Id"",""Name"",""ShortName"",""Level"", ""OrgType"",""ParentId"",""ParentName"",""IsEnable"",""IsCenter"",""IsDeleted"",""CreationTime"",""oldBmid""
  25. )
  26. VALUES";
  27. insertSql = GetOrganizeSql( ref oneData, insertSql, true,null, oldData,ref Data2);
  28. if (Data2!= null && Data2.Rows.Count > 0) insertSql = GetOrganizeSql(Data2, insertSql, oldData, oneData,ref Data3,2);
  29. if (Data3 != null && Data3.Rows.Count > 0) insertSql = GetOrganizeSql(Data3, insertSql, oldData, Data2,ref Data4,3);
  30. if (Data4 != null && Data4.Rows.Count > 0) insertSql = GetOrganizeSql(Data4, insertSql, oldData, Data3, ref Data5,4);
  31. if (Data5 != null && Data5.Rows.Count > 0) insertSql = GetOrganizeSql(Data5, insertSql, oldData, Data4,ref Data6,5);
  32. return insertSql ;
  33. }
  34. public string GetOrganizeSql(ref DataTable data,string insertSql,bool isOneGrade,DataRow row,DataTable oldData,ref DataTable Data2) {
  35. if (data is { Rows.Count: > 0 })
  36. {
  37. int num = 0;
  38. int levelNum = 1;
  39. foreach (DataRow item in data.Rows)
  40. {
  41. item["NewId"] ="001"+ ((num + 1).ToString("D3"));
  42. var type = item["SBM_TypeName"].ToString() == "市直部门" ? 1 : item["SBM_TypeName"].ToString() == "区县部门" ? 2 : 0;
  43. var parentId = string.Empty;
  44. var parentName = string.Empty;
  45. var pgConn = tool.GetConPgSql();
  46. var pgOrg = tool.GetDataTable($@" SELECT * FROM ""system_organize"" where ""oldBmid"" = '{item["SBM_ID"]}'; ", pgConn);
  47. if (num == 0 && isOneGrade)
  48. {
  49. var ccdata = oldData.AsEnumerable().Where(x => x.Field<int>("SBM_PID") == int.Parse(item["SBM_ID"].ToString()));
  50. if (ccdata.Any())
  51. {
  52. DataTable newData = ccdata.CopyToDataTable();
  53. foreach (DataRow x in newData.Rows)
  54. {
  55. Data2.ImportRow(x);
  56. }
  57. }
  58. if (pgOrg is { Rows.Count: > 0 })
  59. {
  60. item["NewId"] = pgOrg.Rows[0]["Id"];
  61. num++;
  62. continue;
  63. }
  64. else {
  65. var maxPgOrg = tool.GetDataTable($@" SELECT * FROM ""system_organize"" WHERE ""Level"" =1 AND (""ParentId"" IS NULL OR ""ParentId"" = '') ORDER BY ""Id"" DESC LIMIT 1 ", pgConn);
  66. if (maxPgOrg is { Rows.Count: > 0 })
  67. {
  68. item["NewId"] = maxPgOrg.Rows[0]["Id"] + (levelNum.ToString("D3"));
  69. }
  70. else
  71. {
  72. item["NewId"] = levelNum.ToString("D3");
  73. }
  74. //item["NewId"] = "00" + maxPgOrg.Rows[0]["Id"] + levelNum;
  75. levelNum++;
  76. }
  77. insertSql += $@"('{item["NewId"]}','{item["SBM_Name"]}','{item["SBM_Name"]}',{item["SBM_Grade"]},{type},'{parentId}','{parentName}','t','f','f','2023-11-14 00:00:00','{item["SBM_ID"]}')";
  78. num++;
  79. continue;
  80. }
  81. var cdata = oldData.AsEnumerable().Where(x => x.Field<int>("SBM_PID") == int.Parse(item["SBM_ID"].ToString()));
  82. if (cdata.Any())
  83. {
  84. DataTable newData = cdata.CopyToDataTable();
  85. foreach (DataRow x in newData.Rows)
  86. {
  87. Data2.ImportRow(x);
  88. }
  89. }
  90. if (pgOrg is { Rows.Count: > 0 })
  91. {
  92. item["NewId"] = pgOrg.Rows[0]["Id"];
  93. num++;
  94. continue;
  95. }
  96. else
  97. {
  98. var maxPgOrg = tool.GetDataTable($@" SELECT * FROM ""system_organize"" WHERE ""Level"" =1 AND (""ParentId"" IS NULL OR ""ParentId"" = '') ORDER BY ""Id"" DESC LIMIT 1 ", pgConn);
  99. if (maxPgOrg is { Rows.Count: > 0 })
  100. {
  101. //item["NewId"] = maxPgOrg.Rows[0]["Id"] + (levelNum.ToString("D3"));
  102. item["NewId"] = "00" + (int.Parse(maxPgOrg.Rows[0]["Id"].ToString()) + levelNum).ToString("D3");
  103. }
  104. else
  105. {
  106. item["NewId"] = levelNum.ToString("D3");
  107. }
  108. levelNum++;
  109. }
  110. insertSql += $@",('{item["NewId"]}', '{item["SBM_Name"]}','{item["SBM_Name"]}',{item["SBM_Grade"]},{type},'{parentId}','{parentName}','t','f','f','2023-11-14 00:00:00','{item["SBM_ID"]}')";
  111. num++;
  112. }
  113. }
  114. return insertSql;
  115. }
  116. public string GetOrganizeSql(DataTable data, string insertSql, DataTable oldData, DataTable pdata,ref DataTable zdata,int level)
  117. {
  118. if (data is { Rows.Count: > 0 })
  119. {
  120. int num = 0;
  121. int levelNum = 1;
  122. foreach (DataRow item in data.Rows)
  123. {
  124. var cdata = oldData.AsEnumerable().Where(x => x.Field<int>("SBM_PID") == int.Parse(item["SBM_ID"].ToString()));
  125. if (cdata.Any())
  126. {
  127. DataTable newData = cdata.CopyToDataTable();
  128. foreach (DataRow x in newData.Rows)
  129. {
  130. zdata.ImportRow(x);
  131. }
  132. }
  133. var type = item["SBM_TypeName"].ToString() == "市直部门" ? 1 : item["SBM_TypeName"].ToString() == "区县部门" ? 2 : 0;
  134. var parentId = string.Empty;
  135. var parentName = string.Empty;
  136. var pRow = pdata.AsEnumerable().First(x => x.Field<int>("SBM_ID") == int.Parse(item["SBM_PID"].ToString()));
  137. var Frequency = int.Parse(pRow["Frequency"].ToString());
  138. item["NewId"] = pRow["NewId"] + Frequency.ToString("D3");
  139. parentId = pRow["NewId"].ToString();
  140. parentName = pRow["SBM_Name"].ToString();
  141. pRow["Frequency"] = Frequency + 1;
  142. var pgConn = tool.GetConPgSql();
  143. var pgOrg = tool.GetDataTable($@" SELECT * FROM ""system_organize"" where ""oldBmid"" = '{item["SBM_ID"]}'; ", pgConn);
  144. if (pgOrg is { Rows.Count: > 0 })
  145. {
  146. item["NewId"] = pgOrg.Rows[0]["Id"];
  147. num++;
  148. continue;
  149. }
  150. else
  151. {
  152. var maxPgOrg = tool.GetDataTable($@" SELECT * FROM ""system_organize"" WHERE ""Level"" ={level} AND ""ParentId"" = '{parentId}' ORDER BY ""Id"" DESC LIMIT 1 ", pgConn);
  153. if (maxPgOrg is { Rows.Count: > 0 })
  154. {
  155. item["NewId"] = "00" + (int.Parse(maxPgOrg.Rows[0]["Id"].ToString()) + levelNum);
  156. }
  157. else
  158. {
  159. item["NewId"] = parentId + "00" + levelNum;
  160. }
  161. levelNum++;
  162. }
  163. insertSql += $@",('{item["NewId"]}', '{item["SBM_Name"]}','{item["SBM_Name"]}',{item["SBM_Grade"]},{type},'{parentId}','{parentName}','t','f','f','2023-11-14 00:00:00','{item["SBM_ID"]}')";
  164. num++;
  165. }
  166. }
  167. return insertSql;
  168. }
  169. }
  170. }