/* * 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.business.service.impl; import me.zhengjie.modules.business.domain.BusinessPlan; import me.zhengjie.modules.business.domain.BusinessPlanPersonnel; import me.zhengjie.modules.business.repository.BusinessPlanPersonnelRepository; import me.zhengjie.modules.business.service.dto.BusinessPlanPersonnelDto; import me.zhengjie.modules.business.service.mapstruct.BusinessPlanPersonnelMapper; import me.zhengjie.modules.system.service.UserService; import me.zhengjie.modules.system.service.dto.UserDto; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.business.repository.BusinessPlanRepository; import me.zhengjie.modules.business.service.BusinessPlanService; import me.zhengjie.modules.business.service.dto.BusinessPlanDto; import me.zhengjie.modules.business.service.dto.BusinessPlanQueryCriteria; import me.zhengjie.modules.business.service.mapstruct.BusinessPlanMapper; 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 2023-04-12 **/ @Service @RequiredArgsConstructor public class BusinessPlanServiceImpl implements BusinessPlanService { private final BusinessPlanRepository businessPlanRepository; private final BusinessPlanMapper businessPlanMapper; private final BusinessPlanPersonnelRepository businessPlanPersonnelRepository; private final BusinessPlanPersonnelMapper businessPlanPersonnelMapper; private final UserService userService; @Override public Map queryAll(BusinessPlanQueryCriteria criteria, Pageable pageable){ Page page = businessPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @Override public BusinessPlanDto apply(BusinessPlan t) { // 将实体类转换成DTO BusinessPlanDto businessPlanDto = businessPlanMapper.toDto(t); // 根据营业计划ID查询营业计划人员 List businessPlanPersonnelDtoList = businessPlanPersonnelMapper.toDto(businessPlanPersonnelRepository.findByBusinessPlanId(businessPlanDto.getId())); // 循环营业计划人员 for (BusinessPlanPersonnelDto businessPlanPersonnelDto : businessPlanPersonnelDtoList) { // 获取用户 UserDto userDto = userService.findById(businessPlanPersonnelDto.getUserId()); businessPlanPersonnelDto.setUserNickName(userDto.getNickName()); } // DTO businessPlanDto.setBusinessPlanPersonnelDtoList(businessPlanPersonnelDtoList); // 返回处理好的DTO return businessPlanDto; } }; return PageUtil.toPage(page.map(func)); } @Override public List queryAll(BusinessPlanQueryCriteria criteria){ return businessPlanMapper.toDto(businessPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public BusinessPlanDto findById(Long id) { BusinessPlan businessPlan = businessPlanRepository.findById(id).orElseGet(BusinessPlan::new); ValidationUtil.isNull(businessPlan.getId(),"BusinessPlan","id",id); return businessPlanMapper.toDto(businessPlan); } @Override @Transactional(rollbackFor = Exception.class) public BusinessPlanDto create(BusinessPlanDto resources) { // 保存营业计划 BusinessPlan businessPlan = businessPlanRepository.save(businessPlanMapper.toEntity(resources)); // 保存相关营业计划人员 saveBusinessPlanPersonnel(resources.getBusinessPlanPersonnelDtoList(), businessPlan.getId()); return findById(businessPlan.getId()); } @Override @Transactional(rollbackFor = Exception.class) public void update(BusinessPlanDto resources) { BusinessPlan businessPlan = businessPlanRepository.findById(resources.getId()).orElseGet(BusinessPlan::new); ValidationUtil.isNull( businessPlan.getId(),"BusinessPlan","id",resources.getId()); businessPlan.copy(businessPlanMapper.toEntity(resources)); businessPlanRepository.save(businessPlan); // 保存相关营业计划人员 saveBusinessPlanPersonnel(resources.getBusinessPlanPersonnelDtoList(), businessPlan.getId()); } /** * 保存相关营业计划人员 * @param businessPlanPersonnelDtoList * @param businessPlanId */ private void saveBusinessPlanPersonnel(List businessPlanPersonnelDtoList, Long businessPlanId) { // 根据营业计划ID删除营业计划人员 businessPlanPersonnelRepository.deleteByBusinessPlanId(businessPlanId); // 营业计划人员列表 List businessPlanPersonnelList = businessPlanPersonnelMapper.toEntity(businessPlanPersonnelDtoList); // 循环营业计划人员列表 for (BusinessPlanPersonnel businessPlanPersonnel : businessPlanPersonnelList) { // 营业计划人员 businessPlanPersonnel.setBusinessPlanId(businessPlanId); // 保存营业计划人员 businessPlanPersonnelRepository.save(businessPlanPersonnel); } } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { businessPlanRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (BusinessPlanDto businessPlan : all) { Map map = new LinkedHashMap<>(); map.put("营业地", businessPlan.getBusinessPlace()); map.put("营业内容", businessPlan.getBusinessContent()); map.put("营业状态", businessPlan.getBusinessStatus()); map.put("备注", businessPlan.getRemarks()); map.put("创建者", businessPlan.getCreateBy()); map.put("创建时间", businessPlan.getCreateTime()); map.put("更新者", businessPlan.getUpdateBy()); map.put("更新时间", businessPlan.getUpdateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } }