| | |
| | | package cn.iocoder.yudao.module.bpm.service.definition; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupImportExcelVO; |
| | | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupImportReqVO; |
| | | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupImportRespVO; |
| | | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupPageReqVO; |
| | | import cn.iocoder.yudao.module.bpm.controller.admin.definition.vo.group.BpmUserGroupSaveReqVO; |
| | | import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmUserGroupDO; |
| | | import cn.iocoder.yudao.module.bpm.dal.mysql.definition.BpmUserGroupMapper; |
| | | import cn.iocoder.yudao.module.system.api.user.AdminUserApi; |
| | | import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collection; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.LinkedHashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Set; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.USER_GROUP_IMPORT_EXISTS; |
| | | import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.USER_GROUP_IMPORT_LIST_IS_EMPTY; |
| | | import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.USER_GROUP_IMPORT_USER_NOT_EXISTS; |
| | | import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.USER_GROUP_IS_DISABLE; |
| | | import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.USER_GROUP_NOT_EXISTS; |
| | | |
| | |
| | | |
| | | @Resource |
| | | private BpmUserGroupMapper userGroupMapper; |
| | | @Resource |
| | | private AdminUserApi adminUserApi; |
| | | |
| | | @Override |
| | | public Long createUserGroup(BpmUserGroupSaveReqVO createReqVO) { |
| | |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public BpmUserGroupImportRespVO importUserGroupList(List<BpmUserGroupImportExcelVO> importUserGroups, |
| | | BpmUserGroupImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importUserGroups = filterList(importUserGroups, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importUserGroups)) { |
| | | throw exception(USER_GROUP_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 逐条处理 |
| | | BpmUserGroupImportRespVO respVO = BpmUserGroupImportRespVO.builder() |
| | | .createUserGroupNames(new ArrayList<>()) |
| | | .updateUserGroupNames(new ArrayList<>()) |
| | | .failureUserGroupNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importUserGroups.forEach(importUserGroup -> { |
| | | try { |
| | | // 2.1 解析成员 |
| | | Set<Long> userIds = parseUserIds(importUserGroup.getUserNames()); |
| | | // 2.2 按名字匹配已存在记录 |
| | | BpmUserGroupDO existUserGroup = userGroupMapper.selectOne( |
| | | BpmUserGroupDO::getName, importUserGroup.getName()); |
| | | if (existUserGroup == null) { |
| | | BpmUserGroupDO userGroup = BeanUtils.toBean(importUserGroup, BpmUserGroupDO.class); |
| | | userGroup.setUserIds(userIds); |
| | | userGroupMapper.insert(userGroup); |
| | | respVO.getCreateUserGroupNames().add(importUserGroup.getName()); |
| | | return; |
| | | } |
| | | // 2.3 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureUserGroupNames().put(importUserGroup.getName(), |
| | | StrUtil.format(USER_GROUP_IMPORT_EXISTS.getMsg(), importUserGroup.getName())); |
| | | return; |
| | | } |
| | | BpmUserGroupDO updateUserGroup = BeanUtils.toBean(importUserGroup, BpmUserGroupDO.class) |
| | | .setId(existUserGroup.getId()); |
| | | updateUserGroup.setUserIds(userIds); |
| | | userGroupMapper.updateById(updateUserGroup); |
| | | respVO.getUpdateUserGroupNames().add(importUserGroup.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureUserGroupNames().put(importUserGroup.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 解析成员姓名,转换为用户编号数组 |
| | | * |
| | | * @param userNames 成员姓名,多个以英文逗号、中文逗号或顿号分隔 |
| | | * @return 用户编号数组 |
| | | */ |
| | | private Set<Long> parseUserIds(String userNames) { |
| | | Set<Long> userIds = new LinkedHashSet<>(); |
| | | if (StrUtil.isBlank(userNames)) { |
| | | return userIds; |
| | | } |
| | | for (String name : userNames.split("[,,、]")) { |
| | | String trimmedName = name.trim(); |
| | | if (StrUtil.isBlank(trimmedName)) { |
| | | continue; |
| | | } |
| | | List<AdminUserRespDTO> users = adminUserApi.getUserListByNickname(trimmedName); |
| | | AdminUserRespDTO match = users.stream() |
| | | .filter(user -> Objects.equals(user.getNickname(), trimmedName)) |
| | | .findFirst().orElse(null); |
| | | if (match == null) { |
| | | throw exception(USER_GROUP_IMPORT_USER_NOT_EXISTS, trimmedName); |
| | | } |
| | | userIds.add(match.getId()); |
| | | } |
| | | return userIds; |
| | | } |
| | | |
| | | } |