001/*
002 *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).
003 *
004 *    Licensed under the Apache License, Version 2.0 (the "License");
005 *    you may not use this file except in compliance with the License.
006 *    You may obtain a copy of the License at
007 *
008 *       https://www.apache.org/licenses/LICENSE-2.0
009 *
010 *    Unless required by applicable law or agreed to in writing, software
011 *    distributed under the License is distributed on an "AS IS" BASIS,
012 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *    See the License for the specific language governing permissions and
014 *    limitations under the License.
015 */
016package org.dromara.warm.flow.core.service.impl;
017
018import org.dromara.warm.flow.core.FlowFactory;
019import org.dromara.warm.flow.core.dao.FlowUserDao;
020import org.dromara.warm.flow.core.entity.Task;
021import org.dromara.warm.flow.core.entity.User;
022import org.dromara.warm.flow.core.enums.UserType;
023import org.dromara.warm.flow.core.orm.service.impl.WarmServiceImpl;
024import org.dromara.warm.flow.core.service.UserService;
025import org.dromara.warm.flow.core.utils.ArrayUtil;
026import org.dromara.warm.flow.core.utils.CollUtil;
027import org.dromara.warm.flow.core.utils.StreamUtils;
028
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.List;
032
033/**
034 * 流程用户Service业务层处理
035 *
036 * @author xiarg
037 * @since 2024/5/10 13:57
038 */
039public class UserServiceImpl extends WarmServiceImpl<FlowUserDao<User>, User> implements UserService {
040
041    @Override
042    public UserService setDao(FlowUserDao<User> warmDao) {
043        this.warmDao = warmDao;
044        return this;
045    }
046
047    @Override
048    public List<User> taskAddUsers(List<Task> addTasks) {
049        List<User> taskUserList = new ArrayList<>();
050        if (CollUtil.isNotEmpty(addTasks)) {
051            StreamUtils.toList(addTasks, task -> taskUserList.addAll(taskAddUser(task)));
052        }
053        return taskUserList;
054    }
055
056    @Override
057    public List<User> setSkipUser(List<Task> addTasks, Long taskId) {
058        // 删除已执行的待办任务的权限人
059        deleteByTaskIds(CollUtil.toList(taskId));
060        return taskAddUsers(addTasks);
061    }
062
063    @Override
064    public List<User> taskAddUser(Task task) {
065        // 遍历权限集合,生成流程节点的权限
066        List<User> userList = StreamUtils.toList(task.getPermissionList()
067                , permission -> structureUser(task.getId(), permission, UserType.APPROVAL.getKey()));
068        task.setUserList(userList);
069        return userList;
070    }
071
072    @Override
073    public void deleteByTaskIds(List<Long> ids) {
074        getDao().deleteByTaskIds(ids);
075    }
076
077    @Override
078    public List<String> getPermission(Long associated, String... types) {
079        if (ArrayUtil.isEmpty(types)) {
080            return StreamUtils.toList(list(FlowFactory.newUser().setAssociated(associated)), User::getProcessedBy);
081        }
082        if (types.length == 1) {
083            return StreamUtils.toList(list(FlowFactory.newUser().setAssociated(associated).setType(types[0]))
084                    , User::getProcessedBy);
085        }
086        return StreamUtils.toList(getDao().listByAssociatedAndTypes(Collections.singletonList(associated), types)
087                , User::getProcessedBy);
088    }
089
090    @Override
091    public List<User> listByAssociatedAndTypes(Long associated, String... types) {
092        if (ArrayUtil.isEmpty(types)) {
093            return list(FlowFactory.newUser().setAssociated(associated));
094        }
095        if (types.length == 1) {
096            return list(FlowFactory.newUser().setAssociated(associated).setType(types[0]));
097        }
098        return getDao().listByAssociatedAndTypes(Collections.singletonList(associated), types);
099    }
100
101    @Override
102    public List<User> getByAssociateds(List<Long> associateds, String... types) {
103        if (CollUtil.isNotEmpty(associateds) && associateds.size() == 1) {
104            return listByAssociatedAndTypes(associateds.get(0), types);
105        }
106        return getDao().listByAssociatedAndTypes(associateds, types);
107    }
108
109    @Override
110    public List<User> listByProcessedBys(Long associated, String processedBy, String... types) {
111        if (ArrayUtil.isEmpty(types)) {
112            return list(FlowFactory.newUser().setAssociated(associated).setProcessedBy(processedBy));
113        }
114        if (types.length == 1) {
115            return list(FlowFactory.newUser().setAssociated(associated).setProcessedBy(processedBy).setType(types[0]));
116        }
117        return getDao().listByProcessedBys(associated, Collections.singletonList(processedBy), types);
118    }
119
120    @Override
121    public List<User> getByProcessedBys(Long associated, List<String> processedBys, String... types) {
122        if (CollUtil.isNotEmpty(processedBys) && processedBys.size() == 1) {
123            return listByProcessedBys(associated, processedBys.get(0), types);
124        }
125        return getDao().listByProcessedBys(associated, processedBys, types);
126    }
127
128
129    @Override
130    public boolean updatePermission(Long associated, List<String> permissions, String type, boolean clear,
131                                    String handler) {
132        // 判断是否clear,如果是true,则先删除当前关联id用户数据
133        if (clear) {
134            getDao().delete(FlowFactory.newUser().setAssociated(associated).setCreateBy(handler));
135        }
136        // 再新增权限人
137        saveBatch(StreamUtils.toList(permissions, permission -> structureUser(associated, permission, type, handler)));
138        return true;
139    }
140
141    @Override
142    public List<User> structureUser(Long associated, List<String> permissionList, String type) {
143        return StreamUtils.toList(permissionList, permission -> structureUser(associated, permission, type, null));
144    }
145
146    @Override
147    public User structureUser(Long associated, String permission, String type) {
148        return structureUser(associated, permission, type, null);
149    }
150
151    @Override
152    public List<User> structureUser(Long associated, List<String> permissionList, String type, String handler) {
153        return StreamUtils.toList(permissionList, permission -> structureUser(associated, permission, type, handler));
154    }
155
156    @Override
157    public User structureUser(Long associated, String permission, String type, String handler) {
158        User user = FlowFactory.newUser()
159                .setType(type)
160                .setProcessedBy(permission)
161                .setAssociated(associated)
162                .setCreateBy(handler);
163        FlowFactory.dataFillHandler().idFill(user);
164        return user;
165    }
166
167}