/* * 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.BusinessExpense; import me.zhengjie.modules.business.domain.BusinessExpenseReimbursement; import me.zhengjie.modules.business.repository.BusinessExpenseReimbursementRepository; import me.zhengjie.modules.business.service.BusinessPlanService; import me.zhengjie.modules.business.service.dto.BusinessExpenseReimbursementDto; import me.zhengjie.modules.business.service.dto.BusinessPlanDto; import me.zhengjie.modules.business.service.mapstruct.BusinessExpenseReimbursementMapper; import me.zhengjie.modules.project.service.ProjectService; import me.zhengjie.modules.project.service.dto.ProjectDto; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.business.repository.BusinessExpenseRepository; import me.zhengjie.modules.business.service.BusinessExpenseService; import me.zhengjie.modules.business.service.dto.BusinessExpenseDto; import me.zhengjie.modules.business.service.dto.BusinessExpenseQueryCriteria; import me.zhengjie.modules.business.service.mapstruct.BusinessExpenseMapper; 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.math.BigDecimal; 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-13 **/ @Service @RequiredArgsConstructor public class BusinessExpenseServiceImpl implements BusinessExpenseService { private final BusinessExpenseRepository businessExpenseRepository; private final BusinessExpenseMapper businessExpenseMapper; private final BusinessPlanService businessPlanService; private final ProjectService projectService; private final BusinessExpenseReimbursementRepository businessExpenseReimbursementRepository; private final BusinessExpenseReimbursementMapper businessExpenseReimbursementMapper; @Override public Map queryAll(BusinessExpenseQueryCriteria criteria, Pageable pageable){ Page page = businessExpenseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @Override public BusinessExpenseDto apply(BusinessExpense t) { // 将实体类转换成DTO BusinessExpenseDto businessExpenseDto = businessExpenseMapper.toDto(t); // 获取营业计划 BusinessPlanDto businessPlanDto = businessPlanService.findById(businessExpenseDto.getBusinessPlanId()); businessExpenseDto.setBusinessPlanContent(businessPlanDto.getBusinessContent()); // 获取项目 if (businessExpenseDto.getProjectId() != null) { ProjectDto projectDto = projectService.findById(businessExpenseDto.getProjectId()); businessExpenseDto.setProjectName(projectDto.getName()); } // 获取营业费用明细 List businessExpenseReimbursementList = businessExpenseReimbursementRepository.findByBusinessExpenseId(businessExpenseDto.getId()); businessExpenseDto.setBusinessExpenseReimbursementDtoList(businessExpenseReimbursementMapper.toDto(businessExpenseReimbursementList)); // 获取报销金额总计 BigDecimal amountReimbursementAmount = businessExpenseReimbursementRepository.amountReimbursementAmountByBusinessExpenseId(businessExpenseDto.getId()); businessExpenseDto.setAmountReimbursementAmount(amountReimbursementAmount); // 返回处理好的DTO return businessExpenseDto; } }; return PageUtil.toPage(page.map(func)); } @Override public List queryAll(BusinessExpenseQueryCriteria criteria){ return businessExpenseMapper.toDto(businessExpenseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public BusinessExpenseDto findById(Long id) { BusinessExpense businessExpense = businessExpenseRepository.findById(id).orElseGet(BusinessExpense::new); ValidationUtil.isNull(businessExpense.getId(),"BusinessExpense","id",id); return businessExpenseMapper.toDto(businessExpense); } @Override @Transactional(rollbackFor = Exception.class) public BusinessExpenseDto create(BusinessExpenseDto resources) { // 保存营业费用 BusinessExpense businessExpense = businessExpenseRepository.save(businessExpenseMapper.toEntity(resources)); // 保存相关营业费用明细 saveBusinessExpenseReimbursement(resources.getBusinessExpenseReimbursementDtoList(), businessExpense.getId()); return findById(businessExpense.getId()); } @Override @Transactional(rollbackFor = Exception.class) public void update(BusinessExpenseDto resources) { BusinessExpense businessExpense = businessExpenseRepository.findById(resources.getId()).orElseGet(BusinessExpense::new); ValidationUtil.isNull( businessExpense.getId(),"BusinessExpense","id",resources.getId()); businessExpense.copy(businessExpenseMapper.toEntity(resources)); businessExpenseRepository.save(businessExpense); // 保存相关营业费用明细 saveBusinessExpenseReimbursement(resources.getBusinessExpenseReimbursementDtoList(), businessExpense.getId()); } /** * 保存相关营业费用明细 * @param businessExpenseReimbursementDtoList * @param businessExpenseId */ private void saveBusinessExpenseReimbursement(List businessExpenseReimbursementDtoList, Long businessExpenseId) { // 根据营业费用ID删除营业费用明细 businessExpenseReimbursementRepository.deleteByBusinessExpenseId(businessExpenseId); // 营业费用明细列表 List businessExpenseReimbursementList = businessExpenseReimbursementMapper.toEntity(businessExpenseReimbursementDtoList); // 循环营业费用明细列表 for (BusinessExpenseReimbursement businessExpenseReimbursement : businessExpenseReimbursementList) { // 营业费用明细 businessExpenseReimbursement.setBusinessExpenseId(businessExpenseId); // 保存营业费用明细 businessExpenseReimbursementRepository.save(businessExpenseReimbursement); } } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { businessExpenseRepository.deleteById(id); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (BusinessExpenseDto businessExpense : all) { Map map = new LinkedHashMap<>(); map.put("费用类型", businessExpense.getExpenseType()); map.put("所属营业计划", businessExpense.getBusinessPlanId()); map.put("所属项目", businessExpense.getProjectId()); map.put("备注", businessExpense.getRemarks()); map.put("创建者", businessExpense.getCreateBy()); map.put("创建时间", businessExpense.getCreateTime()); map.put("更新者", businessExpense.getUpdateBy()); map.put("更新时间", businessExpense.getUpdateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } }