|
@@ -0,0 +1,162 @@
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using wispro.sp.entity;
|
|
|
+
|
|
|
+namespace wispro.sp.api.Controllers
|
|
|
+{
|
|
|
+ [Route("api/[controller]/[action]")]
|
|
|
+ [ApiController]
|
|
|
+ public class OrganizationController : ControllerBase
|
|
|
+ {
|
|
|
+ spDbContext Context;
|
|
|
+
|
|
|
+ public OrganizationController(spDbContext context)
|
|
|
+ {
|
|
|
+ Context = context;
|
|
|
+ }
|
|
|
+ #region 部门API
|
|
|
+ [HttpPost]
|
|
|
+ public Department SaveDept(Department dept)
|
|
|
+ {
|
|
|
+ if(dept.Id > 0)
|
|
|
+ {
|
|
|
+ var inDbObj = Context.Departments.FirstOrDefault<Department>(d=>d.Id == dept.Id);
|
|
|
+
|
|
|
+ inDbObj.Name = dept.Name;
|
|
|
+ inDbObj.Memo = dept.Memo;
|
|
|
+
|
|
|
+ if(dept.order_num != inDbObj.order_num)
|
|
|
+ {
|
|
|
+ inDbObj.order_num = dept.order_num;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(dept.parentId != inDbObj.parentId)
|
|
|
+ {
|
|
|
+ inDbObj.parentId = dept.parentId;
|
|
|
+ Department parentDept = null;
|
|
|
+ if (dept.parentId != null)
|
|
|
+ {
|
|
|
+ parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (parentDept != null || !string.IsNullOrEmpty(parentDept.ancestors))
|
|
|
+ {
|
|
|
+
|
|
|
+ inDbObj.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ inDbObj.ancestors = parentDept.Id.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dept.order_num == null)
|
|
|
+ {
|
|
|
+ var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.parentId).Max(d => d.order_num);
|
|
|
+ if (temOrderNum == null)
|
|
|
+ {
|
|
|
+ inDbObj.order_num = temOrderNum + 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ inDbObj.order_num = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ inDbObj.order_num = dept.order_num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Context.SaveChanges();
|
|
|
+
|
|
|
+ return inDbObj;
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Department parentDept = null;
|
|
|
+ if (dept.parentId != null)
|
|
|
+ {
|
|
|
+ parentDept = Context.Departments.FirstOrDefault<Department>(d => d.Id == dept.parentId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (dept.order_num == null)
|
|
|
+ {
|
|
|
+ if(parentDept == null)
|
|
|
+ {
|
|
|
+ var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == null).Max(d => d.order_num);
|
|
|
+ if (temOrderNum != null)
|
|
|
+ {
|
|
|
+ dept.order_num = temOrderNum + 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ dept.order_num = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ var temOrderNum = Context.Departments.Where<Department>(d => d.parentId == parentDept.Id ).Max(d => d.order_num);
|
|
|
+ if (temOrderNum != null)
|
|
|
+ {
|
|
|
+ dept.order_num = temOrderNum + 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ dept.order_num = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if(parentDept != null && !string.IsNullOrEmpty(parentDept.ancestors ))
|
|
|
+ {
|
|
|
+
|
|
|
+ dept.ancestors = $"{parentDept.ancestors},{parentDept.Id}";
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (parentDept != null)
|
|
|
+ {
|
|
|
+ dept.ancestors = parentDept.Id.ToString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Context.Departments.Add(dept);
|
|
|
+ Context.SaveChanges();
|
|
|
+ return dept;
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Department> GetDepartments()
|
|
|
+ {
|
|
|
+ return Context.Departments.ToList();
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool DeleteDept(int Id)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var delObj = Context.Departments.FirstOrDefault<Department>(d => d.Id == Id);
|
|
|
+ Context.Departments.Remove(delObj);
|
|
|
+ Context.SaveChanges();
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ catch(Exception ex)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|