Search in sources :

Example 1 with BusinessException

use of com.hccake.ballcat.common.core.exception.BusinessException in project ballcat by ballcat-projects.

the class AnnouncementServiceImpl method updateAnnouncement.

/**
 * 更新公告信息
 * @param announcementDTO announcementDTO
 * @return boolean
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateAnnouncement(AnnouncementDTO announcementDTO) {
    Announcement oldAnnouncement = baseMapper.selectById(announcementDTO.getId());
    if (oldAnnouncement.getStatus() != AnnouncementStatusEnum.UNPUBLISHED.getValue()) {
        throw new BusinessException(SystemResultCode.BAD_REQUEST.getCode(), "不允许修改已经发布过的公告!");
    }
    Announcement announcement = AnnouncementConverter.INSTANCE.dtoToPo(announcementDTO);
    // 不允许修改为《发布中》以外的状态
    boolean isPublishStatus = announcement.getStatus() == AnnouncementStatusEnum.ENABLED.getValue();
    if (!isPublishStatus) {
        announcement.setStatus(null);
    }
    // 保证当前状态未被修改过
    boolean isUpdated = baseMapper.updateIfUnpublished(announcement);
    // 公告发布事件
    if (isUpdated && isPublishStatus) {
        this.onAnnouncementPublish(announcement);
    }
    return isUpdated;
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) Announcement(com.hccake.ballcat.notify.model.entity.Announcement) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with BusinessException

use of com.hccake.ballcat.common.core.exception.BusinessException in project ballcat by ballcat-projects.

the class AnnouncementServiceImpl method publish.

/**
 * 发布公告信息
 * @param announcementId 公告ID
 * @return boolean
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean publish(Long announcementId) {
    Announcement announcement = baseMapper.selectById(announcementId);
    if (announcement.getStatus() != AnnouncementStatusEnum.UNPUBLISHED.getValue()) {
        throw new BusinessException(SystemResultCode.BAD_REQUEST.getCode(), "不允许修改已经发布过的公告!");
    }
    if (BooleanEnum.TRUE.getValue() != announcement.getImmortal() && LocalDateTime.now().isAfter(announcement.getDeadline())) {
        throw new BusinessException(SystemResultCode.BAD_REQUEST.getCode(), "公告失效时间必须迟于当前时间!");
    }
    // 更新公共至发布状态
    Announcement entity = new Announcement();
    entity.setId(announcementId);
    entity.setStatus(AnnouncementStatusEnum.ENABLED.getValue());
    boolean isUpdated = baseMapper.updateIfUnpublished(entity);
    if (isUpdated) {
        announcement.setStatus(AnnouncementStatusEnum.ENABLED.getValue());
        this.onAnnouncementPublish(announcement);
    }
    return isUpdated;
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) Announcement(com.hccake.ballcat.notify.model.entity.Announcement) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with BusinessException

use of com.hccake.ballcat.common.core.exception.BusinessException in project ballcat by ballcat-projects.

the class SysOrganizationServiceImpl method update.

/**
 * 更新一个已有的组织机构
 * @param sysOrganizationDTO 组织机构DTO
 * @return boolean 更新成功/失败
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean update(SysOrganizationDTO sysOrganizationDTO) {
    // TODO 防止并发问题
    SysOrganization newSysOrganization = SysOrganizationConverter.INSTANCE.dtoToPo(sysOrganizationDTO);
    Integer organizationId = newSysOrganization.getId();
    SysOrganization originSysOrganization = baseMapper.selectById(organizationId);
    // 如果没有移动父节点,则直接更新
    Integer targetParentId = sysOrganizationDTO.getParentId();
    if (originSysOrganization.getParentId().equals(targetParentId)) {
        return SqlHelper.retBool(baseMapper.updateById(newSysOrganization));
    }
    // 移动了父节点,先判断不是选择自己作为父节点
    Assert.isFalse(targetParentId.equals(organizationId), "父节点不能是自己!");
    // 再判断是否是自己的子节点,根节点跳过判断
    if (!GlobalConstants.TREE_ROOT_ID.equals(targetParentId)) {
        SysOrganization targetParentOrganization = baseMapper.selectById(targetParentId);
        String[] targetParentHierarchy = targetParentOrganization.getHierarchy().split("-");
        if (ArrayUtil.contains(targetParentHierarchy, String.valueOf(organizationId))) {
            throw new BusinessException(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), "父节点不能是自己的子节点!");
        }
    }
    // 填充目标层级和深度
    fillDepthAndHierarchy(newSysOrganization, targetParentId);
    // 更新其子节点的数据
    OrganizationMoveChildParam param = getOrganizationMoveChildParam(newSysOrganization, originSysOrganization);
    baseMapper.followMoveChildNode(param);
    // 更新组织节点信息
    return SqlHelper.retBool(baseMapper.updateById(newSysOrganization));
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) SysOrganization(com.hccake.ballcat.system.model.entity.SysOrganization) OrganizationMoveChildParam(com.hccake.ballcat.system.model.dto.OrganizationMoveChildParam) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with BusinessException

use of com.hccake.ballcat.common.core.exception.BusinessException in project ballcat by ballcat-projects.

the class SysUserServiceImpl method addSysUser.

/**
 * 新增系统用户
 * @param sysUserDto 系统用户DTO
 * @return 添加成功:true , 失败:false
 */
@Override
@Transactional(rollbackFor = Exception.class)
public boolean addSysUser(SysUserDTO sysUserDto) {
    SysUser sysUser = SysUserConverter.INSTANCE.dtoToPo(sysUserDto);
    sysUser.setStatus(SysUserConst.Status.NORMAL.getValue());
    sysUser.setType(SysUserConst.Type.SYSTEM.getValue());
    // 对密码进行加密
    String rawPassword = sysUserDto.getPassword();
    String encodedPassword = PasswordUtils.encode(rawPassword);
    sysUser.setPassword(encodedPassword);
    // 保存用户
    boolean insertSuccess = SqlHelper.retBool(baseMapper.insert(sysUser));
    Assert.isTrue(insertSuccess, () -> {
        log.error("[addSysUser] 数据插入系统用户表失败,user:{}", sysUserDto);
        return new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "数据插入系统用户表失败");
    });
    // 新增用户角色关联
    List<String> roleCodes = sysUserDto.getRoleCodes();
    if (CollectionUtil.isNotEmpty(roleCodes)) {
        boolean addUserRoleSuccess = sysUserRoleService.addUserRoles(sysUser.getUserId(), roleCodes);
        Assert.isTrue(addUserRoleSuccess, () -> {
            log.error("[addSysUser] 更新用户角色信息失败,user:{}, roleCodes: {}", sysUserDto, roleCodes);
            return new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "更新用户角色信息失败");
        });
    }
    // 发布用户创建事件
    publisher.publishEvent(new UserCreatedEvent(sysUser, sysUserDto.getRoleCodes()));
    return true;
}
Also used : UserCreatedEvent(com.hccake.ballcat.system.event.UserCreatedEvent) BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) SysUser(com.hccake.ballcat.system.model.entity.SysUser) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with BusinessException

use of com.hccake.ballcat.common.core.exception.BusinessException in project ballcat by ballcat-projects.

the class SysDictManager method removeDictById.

/**
 * 删除字典
 * @param id 字典id
 */
@Transactional(rollbackFor = Exception.class)
public void removeDictById(Integer id) {
    // 查询现有数据
    SysDict dict = sysDictService.getById(id);
    // 字典标识
    String dictCode = dict.getCode();
    // 有关联字典项则不允许删除
    Assert.isFalse(sysDictItemService.exist(dictCode), () -> new BusinessException(BaseResultCode.LOGIC_CHECK_ERROR.getCode(), "该字典下存在字典项,不允许删除!"));
    // 删除字典
    Assert.isTrue(sysDictService.removeById(id), () -> new BusinessException(BaseResultCode.UPDATE_DATABASE_ERROR.getCode(), "字典删除异常"));
}
Also used : BusinessException(com.hccake.ballcat.common.core.exception.BusinessException) SysDict(com.hccake.ballcat.system.model.entity.SysDict) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BusinessException (com.hccake.ballcat.common.core.exception.BusinessException)11 Transactional (org.springframework.transaction.annotation.Transactional)9 SysMenu (com.hccake.ballcat.system.model.entity.SysMenu)3 Announcement (com.hccake.ballcat.notify.model.entity.Announcement)2 ArrayList (java.util.ArrayList)2 TemplateModelConverter (com.hccake.ballcat.codegen.converter.TemplateModelConverter)1 TemplateEntry (com.hccake.ballcat.codegen.model.entity.TemplateEntry)1 I18nMessage (com.hccake.ballcat.common.i18n.I18nMessage)1 I18nMessageCreateEvent (com.hccake.ballcat.common.i18n.I18nMessageCreateEvent)1 DictChangeEvent (com.hccake.ballcat.system.event.DictChangeEvent)1 UserCreatedEvent (com.hccake.ballcat.system.event.UserCreatedEvent)1 OrganizationMoveChildParam (com.hccake.ballcat.system.model.dto.OrganizationMoveChildParam)1 SysDict (com.hccake.ballcat.system.model.entity.SysDict)1 SysDictItem (com.hccake.ballcat.system.model.entity.SysDictItem)1 SysOrganization (com.hccake.ballcat.system.model.entity.SysOrganization)1 SysUser (com.hccake.ballcat.system.model.entity.SysUser)1 IOException (java.io.IOException)1 List (java.util.List)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1