Department.razor.cs 8.8 KB

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