| New file |
| | |
| | | /* |
| | | * Copyright 2019-2020 Zheng Jie |
| | | * |
| | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | | * you may not use this file except in compliance with the License. |
| | | * You may obtain a copy of the License at |
| | | * |
| | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | | * |
| | | * Unless required by applicable law or agreed to in writing, software |
| | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | * See the License for the specific language governing permissions and |
| | | * limitations under the License. |
| | | */ |
| | | package me.zhengjie.modules.task.service.impl; |
| | | |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.project.domain.Project; |
| | | import me.zhengjie.modules.project.repository.ProjectRepository; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.modules.task.domain.Task; |
| | | import me.zhengjie.modules.task.domain.TaskUser; |
| | | import me.zhengjie.modules.task.repository.TaskRepository; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import me.zhengjie.utils.FileUtil; |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.modules.task.repository.TaskUserRepository; |
| | | import me.zhengjie.modules.task.service.TaskUserService; |
| | | import me.zhengjie.modules.task.service.dto.TaskUserDto; |
| | | import me.zhengjie.modules.task.service.dto.TaskUserQueryCriteria; |
| | | import me.zhengjie.modules.task.service.mapstruct.TaskUserMapper; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import me.zhengjie.utils.PageUtil; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | | * @description 服务实现 |
| | | * @author 赵士淞 |
| | | * @date 2022-12-28 |
| | | **/ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class TaskUserServiceImpl implements TaskUserService { |
| | | |
| | | private final TaskUserRepository taskUserRepository; |
| | | private final TaskUserMapper taskUserMapper; |
| | | private final ProjectRepository projectRepository; |
| | | private final TaskRepository taskRepository; |
| | | private final UserRepository userRepository; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(TaskUserQueryCriteria criteria, Pageable pageable){ |
| | | Page<TaskUser> page = taskUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | // 将实体类转换为DTO(重写) |
| | | Function<TaskUser, TaskUserDto> func = new Function<TaskUser, TaskUserDto>(){ |
| | | @Override |
| | | public TaskUserDto apply(TaskUser t) { |
| | | // 将实体类转换成DTO |
| | | TaskUserDto taskUserDto = taskUserMapper.toDto(t); |
| | | // 根据项目ID查询项目信息 |
| | | Project project = projectRepository.findById(taskUserDto.getProjectId()).orElseGet(Project::new); |
| | | ValidationUtil.isNull(project.getId(),"Project","id",taskUserDto.getProjectId()); |
| | | // 项目名称 |
| | | taskUserDto.setProjectName(project.getName()); |
| | | // 根据任务ID查询任务信息 |
| | | Task task = taskRepository.findById(taskUserDto.getTaskId()).orElseGet(Task::new); |
| | | ValidationUtil.isNull(task.getId(),"Task","id",taskUserDto.getTaskId()); |
| | | // 任务名称 |
| | | taskUserDto.setTaskName(task.getName()); |
| | | // 根据用户ID查询用户信息 |
| | | User user = userRepository.findById(taskUserDto.getUserId()).orElseGet(User::new); |
| | | ValidationUtil.isNull(user.getId(),"User","id",taskUserDto.getUserId()); |
| | | // 用户昵称 |
| | | taskUserDto.setUserNickName(user.getNickName()); |
| | | // 返回处理好的DTO |
| | | return taskUserDto; |
| | | } |
| | | }; |
| | | return PageUtil.toPage(page.map(func)); |
| | | } |
| | | |
| | | @Override |
| | | public List<TaskUserDto> queryAll(TaskUserQueryCriteria criteria){ |
| | | return taskUserMapper.toDto(taskUserRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public TaskUserDto findById(Long id) { |
| | | TaskUser taskUser = taskUserRepository.findById(id).orElseGet(TaskUser::new); |
| | | ValidationUtil.isNull(taskUser.getId(),"TaskUser","id",id); |
| | | return taskUserMapper.toDto(taskUser); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public TaskUserDto create(TaskUser resources) { |
| | | return taskUserMapper.toDto(taskUserRepository.save(resources)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(TaskUser resources) { |
| | | TaskUser taskUser = taskUserRepository.findById(resources.getId()).orElseGet(TaskUser::new); |
| | | ValidationUtil.isNull( taskUser.getId(),"TaskUser","id",resources.getId()); |
| | | taskUser.copy(resources); |
| | | taskUserRepository.save(taskUser); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | taskUserRepository.deleteById(id); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void download(List<TaskUserDto> all, HttpServletResponse response) throws IOException { |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (TaskUserDto taskUser : all) { |
| | | String projectName = taskUser.getProjectId() != null ? projectRepository.findById(taskUser.getProjectId()).orElseGet(Project::new).getName() : ""; |
| | | String taskName = taskUser.getTaskId() != null ? taskRepository.findById(taskUser.getTaskId()).orElseGet(Task::new).getName() : ""; |
| | | String userNickName = taskUser.getUserId() != null ? userRepository.findById(taskUser.getUserId()).orElseGet(User::new).getNickName() : ""; |
| | | String taskStatus = taskUser.getTaskStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.TASK_STATUS, taskUser.getTaskStatus()).getLabel() : ""; |
| | | String taskWorkloadUnit = taskUser.getTaskWorkloadUnit() != null ? dictDetailRepository.findByDictNameAndValue(Constant.STANDARD_WORKLOAD_UNIT, taskUser.getTaskWorkloadUnit()).getLabel() : ""; |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | map.put("项目名称", projectName); |
| | | map.put("任务名称", taskName); |
| | | map.put("用户姓名", userNickName); |
| | | map.put("任务开始时间", taskUser.getTaskStartDate()); |
| | | map.put("任务结束时间", taskUser.getTaskEndDate()); |
| | | map.put("任务工时", taskUser.getTaskHours()); |
| | | map.put("任务工作量", taskUser.getTaskWorkload()); |
| | | map.put("任务工作量单位", taskWorkloadUnit); |
| | | map.put("任务状态", taskStatus); |
| | | map.put("备注", taskUser.getRemarks()); |
| | | map.put("创建者", taskUser.getCreateBy()); |
| | | map.put("创建时间", taskUser.getCreateTime()); |
| | | map.put("更新者", taskUser.getUpdateBy()); |
| | | map.put("更新时间", taskUser.getUpdateTime()); |
| | | list.add(map); |
| | | } |
| | | FileUtil.downloadExcel(list, response); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void divide(TaskUserDto resources) { |
| | | // 循环多用户ID |
| | | for (int i = 0; i < resources.getUserIds().size(); i ++) { |
| | | // 任务分配实体类 |
| | | TaskUser taskUser = new TaskUser(); |
| | | taskUser.setProjectId(resources.getProjectId()); |
| | | taskUser.setTaskId(resources.getTaskId()); |
| | | taskUser.setUserId(resources.getUserIds().get(i)); |
| | | taskUser.setTaskStartDate(resources.getTaskStartDate()); |
| | | taskUser.setTaskEndDate(resources.getTaskEndDate()); |
| | | taskUser.setTaskHours(resources.getTaskHours()); |
| | | taskUser.setTaskWorkload(resources.getTaskWorkload()); |
| | | taskUser.setTaskWorkloadUnit(resources.getTaskWorkloadUnit()); |
| | | taskUser.setTaskStatus(resources.getTaskStatus()); |
| | | // 保存 |
| | | taskUserRepository.save(taskUser); |
| | | } |
| | | } |
| | | } |