/// <summary>
        /// 添加
        /// </summary>
        /// <param name="requestModel"></param>
        /// <returns></returns>
        public BusinessBaseViewModel <string> Insert(AddSystemSectionRequestModel requestModel)
        {
            var res = new BusinessBaseViewModel <string>()
            {
                Status = ResponseStatus.Fail
            };

            if (requestModel == null)
            {
                res.ErrorMessage = "参数错误";
                return(res);
            }
            if (requestModel.Name.IsNullOrWhiteSpace())
            {
                res.ErrorMessage = "部门名称不能为空";
                return(res);
            }
            var entity = _systemSectionRepository.FirstOrDefault(x => x.Name == requestModel.Name && x.ParentId == requestModel.ParentId);

            if (entity != null)
            {
                res.ErrorMessage = "部门名称已存在,不能重复添加";
                return(res);
            }
            int level = 1;

            if (requestModel.ParentId > 0)
            {
                level = _systemSectionRepository.FirstOrDefault(x => x.Id == requestModel.ParentId).Level + 1;
            }

            var Code = 0;

            var query = _systemSectionRepository.Find(x => x.ParentCode == requestModel.ParentCode);

            if (query.Any())
            {
                var maxCode = query.Max(x => x.Code);

                //如果不是第一级
                if (requestModel.ParentCode != 0)
                {
                    maxCode = requestModel.ParentCode * 100 + 1;
                }
                maxCode += 1;

                Code = maxCode;
            }
            else
            {
                Code = requestModel.ParentCode * 100 + 1;
            }

            var model = new SystemSection()
            {
                Name       = requestModel.Name,
                Person     = requestModel.Person,
                ParentId   = requestModel.ParentId,
                Code       = Code,
                ParentCode = requestModel.ParentCode,
                Enabled    = requestModel.Enabled,
                Sort       = requestModel.Sort,
                Level      = level,
                CreateTime = DateTime.Now,
                ModifyTime = DateTime.Now,
                Remark     = requestModel.Remark
            };

            _systemSectionRepository.Insert(model);
            _systemSectionRepository.SaveChanges();

            res.Status = ResponseStatus.Success;
            return(res);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="requestModel"></param>
        /// <returns></returns>
        public BusinessPagedBaseViewModel <QuerySystemStaffResponseModel> Query(QuerySystemStaffRequestModel requestModel)
        {
            var res = new BusinessPagedBaseViewModel <QuerySystemStaffResponseModel>()
            {
                Status = ResponseStatus.Fail
            };

            var filter = PredicateBuilder.True <SystemStaff>();

            filter = filter.And(x => x.Status != (int)SystemStaffStatus.Del);

            if (!requestModel.KeyWord.IsNullOrWhiteSpace())
            {
                filter = filter.And(x => x.LoginName.Contains(requestModel.KeyWord) || x.NickName.Contains(requestModel.KeyWord) ||
                                    x.Mobile.Contains(requestModel.KeyWord));
            }

            var order = new IOrderByBuilder <SystemStaff>[]
            {
                new OrderByBuilder <SystemStaff, DateTime?>(x => x.CreateTime, true),
            };

            var query = _staffRepostory.Find(out int totalCount, requestModel.PageIndex, requestModel.PageSize, filter, order);

            if (query.Any())
            {
                var viewModel = query.MapToList <QuerySystemStaffResponseModel>();

                //获取部门名称
                var sectionIds  = viewModel.Select(x => x.SectionId).ToList();
                var sectionList = _systemSectionRepository.Find(x => sectionIds.Contains(x.Id.ToString()));

                if (sectionList.Any())
                {
                    viewModel.ForEach(m =>
                    {
                        var section = sectionList.Where(x => m.SectionId == x.Id + "").Select(x => x.Name).ToList();

                        if (!section.IsNull())
                        {
                            m.SectionName = string.Join(",", section);
                        }
                    });
                }

                //获取角色名称
                var StaffIds = viewModel.Select(x => x.Id);

                var roleList = _systemRoleService.FindStaffRoleNameByStaffId(StaffIds);

                if (roleList.Any())
                {
                    viewModel.ForEach(m =>
                    {
                        var role = roleList.Where(x => m.Id == x.StaffId).Select(x => x.RoleName).ToList();

                        if (!role.IsNull())
                        {
                            m.RoleName = string.Join(",", role);
                        }
                    });
                }

                res.BusinessData = viewModel;
            }

            res.Total  = totalCount;
            res.Status = ResponseStatus.Success;

            return(res);
        }