Пример #1
0
        public async Task Save(DictSaveInputDto saveDto)
        {
            if (string.IsNullOrWhiteSpace(saveDto.DictName))
            {
                throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "请输入字典名称"));
            }

            //add
            if (saveDto.ID == 0)
            {
                //long Id = new Snowflake(1, 1).NextId();
                long Id       = IdGeneraterHelper.GetNextId(IdGeneraterKey.DICT);
                var  subDicts = GetSubDicts(Id, saveDto.DictValues);
                await _dictRepository.InsertRangeAsync(subDicts.Append(new SysDict {
                    ID = Id, Pid = 0, Name = saveDto.DictName, Tips = saveDto.Tips, Num = "0"
                }));
            }
            //update
            else
            {
                var dict = new SysDict {
                    Name = saveDto.DictName, Tips = saveDto.Tips, ID = saveDto.ID, Pid = 0
                };
                var subDicts = GetSubDicts(saveDto.ID, saveDto.DictValues);
                await _maintManagerService.UpdateDicts(dict, subDicts);
            }
        }
Пример #2
0
        public async Task Save(DeptSaveInputDto saveDto)
        {
            if (string.IsNullOrWhiteSpace(saveDto.FullName))
            {
                throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "请输入部门全称"));
            }

            if (string.IsNullOrWhiteSpace(saveDto.SimpleName))
            {
                throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "请输入部门简称"));
            }

            if (saveDto.ID == 0)
            {
                var dept = _mapper.Map <SysDept>(saveDto);
                dept.ID = IdGeneraterHelper.GetNextId(IdGeneraterKey.DEPT);
                await this.SetDeptPids(dept);

                await _deptRepository.InsertAsync(dept);
            }
            else
            {
                var oldDept = await _deptRepository.FetchAsync(d => d, x => x.ID == saveDto.ID);

                oldDept.Pid        = saveDto.Pid;
                oldDept.SimpleName = saveDto.SimpleName;
                oldDept.FullName   = saveDto.FullName;
                oldDept.Num        = saveDto.Num;
                oldDept.Tips       = saveDto.Tips;

                var dept = await this.SetDeptPids(oldDept);

                await _deptRepository.UpdateAsync(dept);
            }
        }
Пример #3
0
        public async Task Save(UserSaveInputDto saveDto)
        {
            var user = _mapper.Map <SysUser>(saveDto);

            if (user.ID < 1)
            {
                if (await _userRepository.ExistAsync(x => x.Account == user.Account))
                {
                    throw new BusinessException(new ErrorModel(ErrorCode.Forbidden, "用户已存在"));
                }

                user.ID       = IdGeneraterHelper.GetNextId(IdGeneraterKey.USER);
                user.Salt     = SecurityHelper.GenerateRandomCode(5);
                user.Password = HashHelper.GetHashedString(HashType.MD5, user.Password, user.Salt);
                await _usrManager.AddUser(user);
            }
            else
            {
                await _userRepository.UpdateAsync(user,
                                                  x => x.Name,
                                                  x => x.DeptId,
                                                  x => x.Sex,
                                                  x => x.Phone,
                                                  x => x.Email,
                                                  x => x.Birthday,
                                                  x => x.Status);
            }
        }
Пример #4
0
        public async Task Save(RoleSaveInputDto roleDto)
        {
            var role = _mapper.Map <SysRole>(roleDto);

            if (roleDto.ID < 1)
            {
                role.ID = IdGeneraterHelper.GetNextId(IdGeneraterKey.ROLE);
                await _roleRepository.InsertAsync(role);
            }
            else
            {
                await _roleRepository.UpdateAsync(role);
            }
        }
Пример #5
0
        public async Task <int> Save(CfgSaveInputDto inputDto)
        {
            if (string.IsNullOrWhiteSpace(inputDto.CfgName))
            {
                throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "请输入参数名称"));
            }

            if (string.IsNullOrWhiteSpace(inputDto.CfgValue))
            {
                throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "请输入参数值"));
            }

            //add
            if (inputDto.ID == 0)
            {
                var exist = await _cfgRepository.ExistAsync(c => c.CfgName == inputDto.CfgName);

                if (exist)
                {
                    throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "参数名称已经存在"));
                }

                var enity = _mapper.Map <SysCfg>(inputDto);

                //Vue处理大数字有问题,暂时不用Snowflake算法,以后完善。
                //enity.ID = new Snowflake(1, 1).NextId();
                enity.ID = IdGeneraterHelper.GetNextId(IdGeneraterKey.CFG);

                return(await _cfgRepository.InsertAsync(enity));
            }
            //update
            else
            {
                var exist = await _cfgRepository.ExistAsync(c => c.CfgName == inputDto.CfgName && c.ID != inputDto.ID);

                if (exist)
                {
                    throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "参数名称已经存在"));
                }

                var enity = _mapper.Map <SysCfg>(inputDto);

                return(await _cfgRepository.UpdateAsync(enity));
            }
        }
Пример #6
0
        public async Task SaveRolePermisson(long roleId, long[] permissionIds, CancellationToken cancellationToken = default)
        {
            await _relationRepository.DeleteRangeAsync(x => x.RoleId == roleId);

            var relations = new List <SysRelation>();

            foreach (var permissionId in permissionIds)
            {
                relations.Add(
                    new SysRelation
                {
                    ID     = IdGeneraterHelper.GetNextId(IdGeneraterKey.PEMS, permissionIds.IndexOf(permissionId)),
                    RoleId = roleId,
                    MenuId = permissionId
                }
                    );
            }
            await _relationRepository.InsertRangeAsync(relations);
        }
Пример #7
0
        public async Task <int> Save(MenuSaveInputDto saveDto)
        {
            if (saveDto.ID < 1)
            {
                if (await _menuRepository.ExistAsync(x => x.Code == saveDto.Code))
                {
                    throw new BusinessException(new ErrorModel(ErrorCode.Forbidden, "同名菜单已存在"));
                }
                saveDto.Status = true;
            }

            if (string.IsNullOrWhiteSpace(saveDto.PCode) || string.Equals(saveDto.PCode, "0"))
            {
                saveDto.PCode  = "0";
                saveDto.PCodes = "[0],";
                saveDto.Levels = 1;
            }
            else
            {
                var parentMenu = await _menuRepository.FetchAsync(m => new { m.Code, m.PCodes, m.Levels }, x => x.Code == saveDto.PCode);

                saveDto.PCode = parentMenu.Code;
                if (string.Equals(saveDto.Code, saveDto.PCode))
                {
                    throw new BusinessException(new ErrorModel(ErrorCode.Forbidden, "菜单编码冲突"));
                }

                saveDto.Levels = parentMenu.Levels + 1;
                saveDto.PCodes = $"{parentMenu.PCodes}[{parentMenu.Code}]";
            }

            var menu = _mapper.Map <SysMenu>(saveDto);

            if (menu.ID == 0)
            {
                menu.ID = IdGeneraterHelper.GetNextId(IdGeneraterKey.MENU);
                return(await _menuRepository.InsertAsync(menu));
            }
            else
            {
                return(await _menuRepository.UpdateAsync(menu));
            }
        }
Пример #8
0
        private List <SysDict> GetSubDicts(long pid, string dictValues)
        {
            List <SysDict> subDicts = new List <SysDict>();

            if (!string.IsNullOrWhiteSpace(dictValues))
            {
                //var snowflake = new Snowflake(1, 1);
                var values = dictValues.Split(";", StringSplitOptions.RemoveEmptyEntries);
                subDicts = values.Select((s, Index) => new SysDict
                {
                    //ID = snowflake.NextId()
                    ID = IdGeneraterHelper.GetNextId(IdGeneraterKey.DICT, Index)
                    ,
                    Pid = pid
                    ,
                    Name = s.Split(":", StringSplitOptions.RemoveEmptyEntries)[1]
                    ,
                    Num = s.Split(":", StringSplitOptions.RemoveEmptyEntries)[0]
                }).ToList();
            }
            return(subDicts);
        }
Пример #9
0
        public async Task Save(TaskSaveInputDto saveDto)
        {
            if (string.IsNullOrWhiteSpace(saveDto.Name))
            {
                throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "请输入任务名称"));
            }
            //add
            if (saveDto.ID == 0)
            {
                var exist = await _taskRepository.ExistAsync(c => c.Name == saveDto.Name);

                if (exist)
                {
                    throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "任务名称已经存在"));
                }

                var enity = _mapper.Map <SysTask>(saveDto);
                //enity.ID = new Snowflake(1, 1).NextId();
                enity.ID = IdGeneraterHelper.GetNextId(IdGeneraterKey.Task);

                await _taskRepository.InsertAsync(enity);
            }
            //update
            else
            {
                var exist = await _taskRepository.ExistAsync(c => c.Name == saveDto.Name && c.ID != saveDto.ID);

                if (exist)
                {
                    throw new BusinessException(new ErrorModel(ErrorCode.BadRequest, "任务名称已经存在"));
                }

                var enity = _mapper.Map <SysTask>(saveDto);

                await _taskRepository.UpdateAsync(enity);
            }
        }
Пример #10
0
        public async Task <int> SaveRolePermisson(long roleId, long[] permissionIds, CancellationToken cancellationToken = default)
        {
            int result = 0;
            var tran   = await _unitOfWork.BeginTransactionAsync();

            try
            {
                await _relationRepository.DeleteRangeAsync(x => x.RoleId == roleId);

                var relations = new List <SysRelation>();
                foreach (var permissionId in permissionIds)
                {
                    relations.Add(
                        new SysRelation
                    {
                        ID     = IdGeneraterHelper.GetNextId(IdGeneraterKey.PEMS, permissionIds.IndexOf(permissionId)),
                        RoleId = roleId,
                        MenuId = permissionId
                    }
                        );
                }
                await _relationRepository.InsertAsync(relations);

                await tran.CommitAsync();

                result = 1;
            }
            catch (Exception ex)
            {
                await tran.RollbackAsync();

                throw ex;
            }

            return(await Task.FromResult(result));
        }