/// <summary> /// 添加一个部门 /// </summary> /// <param name="adepartment"></param> /// <returns></returns> public ReturnViewModel <Department> AddDepartment(Department adepartment) { if (db.Department.Where(x => x.DepartmentName == adepartment.DepartmentName).Any()) { return(new ReturnViewModel <Department>() { code = (int)codes.AddDepartmentError, data = adepartment, message = $"\"{adepartment.DepartmentName}\"已存在,请更改后重试" }); } try { db.Department.Add(adepartment); db.SaveChanges(); } catch (Exception) { return(new ReturnViewModel <Department>() { code = (int)codes.AddDepartmentError, data = adepartment, message = $"\"{adepartment.DepartmentName}\"添加失败" //message = i.Message }); } return(new ReturnViewModel <Department>() { code = (int)codes.Success, data = adepartment, message = $"\"{adepartment.DepartmentName}\"添加成功" }); }
/// <summary> /// 申请请假 /// </summary> /// <param name="leave"></param> /// <param name="user"></param> /// <returns></returns> public ReturnViewModel <bool> AddLeave(LeaveViewModel leave, NewUser user) { try { DateTime LeaveStartTime = Convert.ToDateTime(leave.LeaveTimeSpan.First()); DateTime LeaveEndTime = Convert.ToDateTime(leave.LeaveTimeSpan.Last()); TimeSpan days = LeaveEndTime - LeaveStartTime; Leave aleave = new Leave { UserId = user.Id, RealName = user.RealName, DepartmentId = user.DepartmentId, LeaveStartTime = LeaveStartTime, LeaveEndTime = LeaveEndTime, LeaveReason = leave.LeaveReason, LeaveTime = leave.LeaveTime, LeaveHalfDay = leave.LeaveHalfDay, LeaveState = 3, LeaveDays = days.Days }; db.Leave.Add(aleave); db.SaveChanges(); } catch (Exception) { return(new ReturnViewModel <bool> { code = (int)codes.ApplyLeaveError, data = false, message = "申请失败,请检查是否填写正确" }); } return(new ReturnViewModel <bool> { code = (int)codes.Success, data = true, message = "申请成功" }); }