Form1.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using NPOI.SS.Formula.Functions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using wispro.sp.api;
  12. using wispro.sp.entity;
  13. namespace UpdateUserDepartment
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. var ofd = new System.Windows.Forms.OpenFileDialog();
  24. if(ofd.ShowDialog() == DialogResult.OK)
  25. {
  26. string strPath = ofd.FileName;
  27. var dt = wispro.sp.utility.NPOIExcel.ExcelToTable(strPath);
  28. wispro.sp.api.spDbContext db = new wispro.sp.api.spDbContext();
  29. var depList = db.Departments.ToList();
  30. foreach ( DataRow row in dt.Rows )
  31. {
  32. string strId = row[0].ToString();
  33. string strDepart = row["部门"].ToString();
  34. System.Diagnostics.Debug.WriteLine(string.Format( "部门:{0}",strDepart));
  35. if(!string.IsNullOrEmpty(strDepart))
  36. {
  37. string[] deps = GetDepartmentPath(strDepart);
  38. if (deps != null && deps.Length > 0)
  39. {
  40. Department department = GetUserDepartment(deps, db);
  41. UpdateDepartmentPosition(db, strId, department);
  42. }
  43. }
  44. }
  45. }
  46. static Department GetUserDepartment(string[] deps, wispro.sp.api.spDbContext db)
  47. {
  48. Department temDept = null;
  49. for (int i=0;i<deps.Length;i++)
  50. {
  51. Department dep = null;
  52. if(temDept == null)
  53. {
  54. dep = db.Departments.Where<Department>(d => d.Name == deps[i].Trim()).FirstOrDefault();
  55. }
  56. else
  57. {
  58. dep = db.Departments.Where<Department>(d => d.Name == deps[i].Trim() && d.parentId == temDept.Id).FirstOrDefault();
  59. }
  60. if (dep == null)
  61. {
  62. dep = new Department()
  63. {
  64. Name = deps[i].Trim(),
  65. parentId = temDept == null ? null : temDept.Id,
  66. ancestors = temDept.ancestors + "," + temDept.Id.ToString()
  67. };
  68. db.Departments.Add(dep);
  69. db.SaveChanges();
  70. }
  71. temDept = dep;
  72. }
  73. return temDept;
  74. }
  75. static string[] GetDepartmentPath(string strDepart)
  76. {
  77. if(string.IsNullOrEmpty(strDepart) || strDepart =="NULL")
  78. {
  79. return null;
  80. }
  81. string[] deps = strDepart.Trim().Split(new char[] { ',' });
  82. if (deps.Length > 0)
  83. {
  84. System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("(.*)\\((.*)\\)");
  85. System.Text.RegularExpressions.Match m = r.Match(deps[0]);
  86. if (m.Success)
  87. {
  88. string[] temDeps = new string[deps.Length + 1];
  89. temDeps[0] = m.Groups[1].Value;
  90. temDeps[1] = m.Groups[2].Value;
  91. if (deps.Length > 1)
  92. {
  93. for (int i = 1; i < deps.Length; i++)
  94. {
  95. temDeps[i + 1] = deps[i];
  96. }
  97. }
  98. deps = temDeps;
  99. }
  100. }
  101. return deps;
  102. }
  103. }
  104. private static void UpdateDepartmentPosition(spDbContext db, string strId, Department department)
  105. {
  106. var dpList = db.DepartmentPositions.Where(dp => dp.StaffId == int.Parse(strId)).ToList<DepartmentPosition>();
  107. bool isExist = false;
  108. foreach (var dp in dpList)
  109. {
  110. if (dp.departmentId != department.Id)
  111. {
  112. db.DepartmentPositions.Remove(dp);
  113. }
  114. else
  115. {
  116. isExist = true;
  117. }
  118. }
  119. if (!isExist)
  120. {
  121. db.DepartmentPositions.Add(
  122. new DepartmentPosition()
  123. {
  124. departmentId = department.Id,
  125. PositionId = 5,
  126. StaffId = int.Parse(strId)
  127. }
  128. );
  129. }
  130. db.SaveChanges();
  131. }
  132. }
  133. }