Пример #1
0
        public CommandResult <Dept> Add(DeptAddRequestModel requestModel)
        {
            var result = new CommandResult <Dept>();

            using (var db = new FrameContext())
            {
                if (Exist(a => a.Name == requestModel.DeptName))
                {
                    result.Message = "该部门名称在系统中已存在";
                }
                else if (_staffService.Exist(db, a => a.DeptId == requestModel.ParentId))
                {
                    var staff = _staffService.Find(db, a => a.DeptId == requestModel.ParentId);
                    result.Message = $"该部门信息已被分配到了员工“{staff.Name}”上,请先取消";
                }
                else
                {
                    var entity = new Dept
                    {
                        Name = requestModel.DeptName,
                        PId  = requestModel.ParentId
                    };
                    if (!BaseService.Add(db, entity))
                    {
                        result.Message = "未知原因";
                    }
                    else
                    {
                        result.ResultStatus = ResultStatus.Success;
                        result.Message      = "部门新增成功";
                        result.Data         = entity;
                    }
                }
            }
            return(result);
        }