예제 #1
0
        /// <summary>
        /// 保存部门
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public SaveDepartmentResponse SaveDeparment(SaveDepartmentRequest request)
        {
            var response = new SaveDepartmentResponse();

            if (request.Entity == null)
            {
                response.IsSuccess   = false;
                response.MessageCode = "1";
                response.MessageText = "保存部门信息不存在";
                return(response);
            }

            try
            {
                var entity = request.Entity.As <DepartmentPo>();

                if (entity.Id == 0)
                {
                    #region 新增部门信息
                    if (_departmentRepository.Any <DepartmentPo>(e => e.IsValid == 1 && e.Name == request.Entity.Name))
                    {
                        throw new Exception("部门名称已存在");
                    }

                    EntityLogger.CreateEntity(entity);
                    entity.Id = _departmentRepository.InsertReturnIdentity(entity);
                    #endregion
                }
                else
                {
                    #region 更新部门信息
                    var entitytemp = _departmentRepository.FindSingle <DepartmentPo>(entity.Id);

                    if (entitytemp == null)
                    {
                        throw new Exception("更新部门信息不存在");
                    }

                    EntityLogger.UpdateEntity(entity);

                    _departmentRepository.UpdateIgnoreColumns(entity, e => new
                    {
                        e.IsValid,
                        e.CreateTime,
                        e.CreateUserId
                    });
                    #endregion
                }
            }
            catch (Exception ex)
            {
                response.IsSuccess   = false;
                response.MessageCode = "-1";
                response.MessageText = ex.ToString();
                LogManager.LogicLogger.ErrorFormat("保存部门出错:{0}", new { request, err = ex.ToString() }.ToJson());
            }
            return(response);
        }
예제 #2
0
        public ActionResult Save(SaveDepartmentRequest request)
        {
            var rs = SafeOptionHelper.Execute(request, new SaveDepartmentValidator(), () =>
            {
                var data = request.Data;
                if (request.OptionType == OptionType.Add)
                {
                    _departmentRepository.Add(new Department
                    {
                        Name     = data.Name,
                        ParentId = data.ParentId,
                        Remark   = data.Remark
                    });
                    _uow.Commit();
                    return(ResultObject.Success());
                }

                if (request.OptionType == OptionType.Edit)
                {
                    var dep = _departmentRepository.GetByKey(data.Id);
                    if (dep == null)
                    {
                        throw new Exception("部门信息未找到");
                    }

                    dep.Name     = data.Name;
                    dep.ParentId = data.ParentId;
                    dep.Remark   = data.Remark;
                    _uow.Commit();
                    return(ResultObject.Success());
                }
                return(ResultObject.Fail(500, "不支持的操作类型"));
            });

            return(Json(rs));
        }