using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; 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(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(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(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(d => d.Id == dept.parentId); } if (dept.order_num == null) { if(parentDept == null) { var temOrderNum = Context.Departments.Where(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(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 GetDepartments() { return Context.Departments.ToList(); } public bool DeleteDept(int Id) { try { var delObj = Context.Departments.FirstOrDefault(d => d.Id == Id); Context.Departments.Remove(delObj); Context.SaveChanges(); return true; } catch(Exception ex) { return false; } } public List GetPositions() { return Context.Positions.ToList(); } public Position GetPosition(int deptId,int staffId) { var data = Context.DepartmentPositions.Include(pi => pi.Position).FirstOrDefault(s => s.StaffId == staffId); if(data != null) { return data.Position; } else { return null; } } public List GetCustomers() { return Context.Customers.ToList(); } #endregion } }