Department.razor.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using AntDesign;
  2. using AntDesign.TableModels;
  3. using Microsoft.AspNetCore.Components;
  4. using Microsoft.AspNetCore.Components.Web;
  5. using Microsoft.JSInterop;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using wispro.sp.entity;
  12. using wispro.sp.share;
  13. using wispro.sp.web.Services;
  14. namespace wispro.sp.web.Pages.Organization
  15. {
  16. public partial class Department
  17. {
  18. private List<wispro.sp.entity.Department> departments;
  19. Tree<wispro.sp.entity.Department> tree;
  20. bool _loading = false;
  21. entity.Department _editDepartment;
  22. [Inject] OrganizationService orgService { get; set; }
  23. [Inject] MessageService _message { get; set; }
  24. [Inject] protected NavigationManager NavigationManager { get; set; }
  25. [Inject] protected IAuthService _authService { get; set; }
  26. [Inject] protected IUserService userService { get; set; }
  27. [Inject] protected IJSRuntime JSRuntime { get; set; }
  28. protected override async System.Threading.Tasks.Task OnInitializedAsync()
  29. {
  30. await _authService.CanVisitResource();
  31. _loading = true;
  32. departments =await orgService.GetDepartments();
  33. var data = await orgService.GetStaffs(null, 1, 10);
  34. _loading = false;
  35. forecasts = data.Results;
  36. _total = data.TotalCount;
  37. _StaffGrade = await orgService.GetStaffGrades();
  38. _allPositions = await orgService.getPositions(null);
  39. _loading = false;
  40. }
  41. #region 部门操作
  42. bool newModal = false;
  43. private void AddNew(entity.Department parentDept)
  44. {
  45. _editDepartment = new entity.Department();
  46. if (parentDept != null)
  47. {
  48. _editDepartment.parentId = parentDept.Id;
  49. //if(!string.IsNullOrEmpty(parentDept.ancestors))
  50. //{
  51. // _editDepartment.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
  52. //}
  53. //else
  54. //{
  55. // _editDepartment.ancestors = parentDept.Id.ToString();
  56. //}
  57. //_editDepartment.order_num = GetChildren(parentDept).Count + 1;
  58. }
  59. newModal = true;
  60. }
  61. private void EditDept(entity.Department department)
  62. {
  63. _editDepartment = department;
  64. newModal = true;
  65. }
  66. private async Task DeleteDept(entity.Department department)
  67. {
  68. var result = await orgService.DeleteDept(department.Id);
  69. departments = await orgService.GetDepartments();
  70. StateHasChanged();
  71. }
  72. private async System.Threading.Tasks.Task NewOk(MouseEventArgs e)
  73. {
  74. newModal = false;
  75. var retObj = await orgService.SaveDept(_editDepartment);
  76. departments = await orgService.GetDepartments();
  77. StateHasChanged();
  78. }
  79. private void NewCancel(MouseEventArgs e)
  80. {
  81. newModal = false;
  82. }
  83. #endregion
  84. async Task OnSelect(TreeEventArgs<entity.Department> e)
  85. {
  86. _editDepartment = e.Node.DataItem;
  87. _loading = true;
  88. var data = await orgService.GetStaffs(_editDepartment, 1, 1000);
  89. forecasts = data.Results;
  90. _pageIndex = 1;
  91. _total = data.TotalCount;
  92. _loading = false;
  93. StateHasChanged();
  94. }
  95. private List<wispro.sp.entity.Department> GetChildren(wispro.sp.entity.Department dept)
  96. {
  97. if(dept == null)
  98. {
  99. var retList = departments.Where<wispro.sp.entity.Department>(x => x.parentId == null).OrderBy(x=>x.order_num).ToList();
  100. return retList;
  101. }
  102. else
  103. {
  104. var retList = departments.Where<wispro.sp.entity.Department>(x => x.parentId == dept.Id).OrderBy(x => x.order_num).ToList();
  105. return retList;
  106. }
  107. }
  108. #region 职员操作
  109. private List<Staff> forecasts;
  110. IEnumerable<Staff> selectedRows;
  111. ITable table;
  112. int _pageIndex = 1;
  113. int _pageSize = 10;
  114. int _total = 0;
  115. Staff EditingStaff = null;
  116. bool _isAdd = false;
  117. List<wispro.sp.entity.StaffGrade> _StaffGrade;
  118. string[] _places = new string[] { "深圳", "苏州", "南通", "西安", "北京", "杭州", "武汉" };
  119. bool _newUserModal = false;
  120. private void AddNewUser()
  121. {
  122. if (_editDepartment == null)
  123. {
  124. }
  125. else
  126. {
  127. EditingStaff = new Staff();
  128. _isAdd = true;
  129. _newUserModal = true;
  130. }
  131. }
  132. private int serialNumber(int pageIndex, int pageSize, string name)
  133. {
  134. int iIndex = 0;
  135. foreach (Staff sf in forecasts)
  136. {
  137. iIndex++;
  138. if (sf.Name == name)
  139. {
  140. break;
  141. }
  142. }
  143. return iIndex;
  144. }
  145. private async Task Edit(Staff staff)
  146. {
  147. EditingStaff = staff;
  148. selectPosition = await orgService.getPosition(staff, _editDepartment);
  149. if(selectPosition == null)
  150. {
  151. selectPosition = new Position();
  152. }
  153. //Console.WriteLine($"Edit:{JsonSerializer.Serialize(selectPosition)}");
  154. PositionId = selectPosition.Id;
  155. _isAdd = false;
  156. _newUserModal = true;
  157. }
  158. private void Delete(Staff staff)
  159. {
  160. }
  161. Dictionary<string, object> OnRow(RowData<Staff> row)
  162. {
  163. Dictionary<string, object> ret = new Dictionary<string, object>();
  164. ret.Add("id", row.RowIndex);
  165. ret.Add("onclick", ((Action)delegate
  166. {
  167. //_message.Info($"row {row.Data.Name} was clicked");
  168. }));
  169. return ret;
  170. }
  171. public async System.Threading.Tasks.Task OnChange(QueryModel<Staff> queryModel)
  172. {
  173. if (!_loading)
  174. {
  175. _loading = true;
  176. ListApiResponse<Staff> data = await orgService.GetStaffs(_editDepartment, queryModel); //Http.GetFromJsonAsync<ListApiResponse<Staff>>("http://localhost:39476/api/Staff/Query?" + GetRandomuserParams(queryModel));
  177. forecasts = data.Results;
  178. _total = data.TotalCount;
  179. _loading = false;
  180. }
  181. }
  182. private void HandlePageChange(PaginationEventArgs args)
  183. {
  184. if (_pageIndex != args.Page)
  185. {
  186. _pageIndex = args.Page;
  187. }
  188. if (_pageSize != args.PageSize)
  189. {
  190. _pageSize = args.PageSize;
  191. }
  192. }
  193. private List<Position> _allPositions;
  194. Position selectPosition= new Position();
  195. int PositionId;
  196. private async System.Threading.Tasks.Task HandleOk(MouseEventArgs e)
  197. {
  198. //selectPosition = new Position() { Id = PositionId };
  199. Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(_editDepartment));
  200. Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(EditingStaff));
  201. var data = await orgService.SaveUser(_editDepartment, selectPosition, EditingStaff); // Http.PostAsJsonAsync<wispro.sp.entity.Staff>($"http://localhost:39476/api/Staff/Save", EditingStaff);
  202. if (data.Success)
  203. {
  204. await _message.Success("数据已保存!");
  205. _newUserModal = false;
  206. _loading = true;
  207. var data1 = await orgService.GetStaffs(_editDepartment, 1, 1000);
  208. forecasts = data1.Results;
  209. _total = data1.TotalCount;
  210. _loading = false;
  211. }
  212. else
  213. {
  214. await _message.Error($"{data.ErrorMessage}");
  215. }
  216. //_newUserModal = false;
  217. }
  218. private void HandleCancel(MouseEventArgs e)
  219. {
  220. _newUserModal = false;
  221. }
  222. private void OnSelectedItemChangedHandler(Position value)
  223. {
  224. selectPosition = value;
  225. //PositionIdChanged.InvokeAsync(_SelectedItem.Id);
  226. }
  227. void UserSelected(Staff staff)
  228. {
  229. EditingStaff = staff;
  230. }
  231. #endregion
  232. }
  233. }