/* * 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.contract.service.impl; import me.zhengjie.modules.constant.Constant; import me.zhengjie.modules.contract.domain.ContractInvoice; import me.zhengjie.modules.contract.domain.ContractInvoicePayment; import me.zhengjie.modules.contract.repository.ContractInvoicePaymentRepository; import me.zhengjie.modules.contract.repository.ContractRepository; import me.zhengjie.modules.contract.service.dto.ContractDto; import me.zhengjie.modules.system.repository.DictDetailRepository; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.contract.repository.ContractInvoiceRepository; import me.zhengjie.modules.contract.service.ContractInvoiceService; import me.zhengjie.modules.contract.service.dto.ContractInvoiceDto; import me.zhengjie.modules.contract.service.dto.ContractInvoiceQueryCriteria; import me.zhengjie.modules.contract.service.mapstruct.ContractInvoiceMapper; 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-02-02 **/ @Service @RequiredArgsConstructor public class ContractInvoiceServiceImpl implements ContractInvoiceService { private final ContractInvoiceRepository contractInvoiceRepository; private final ContractInvoiceMapper contractInvoiceMapper; private final DictDetailRepository dictDetailRepository; private final ContractInvoicePaymentRepository contractInvoicePaymentRepository; private final ContractInvoicePaymentServiceImpl contractInvoicePaymentServiceImpl; private final ContractServiceImpl contractServiceImpl; @Override public Map queryAll(ContractInvoiceQueryCriteria criteria, Pageable pageable){ Page page = contractInvoiceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @Override public ContractInvoiceDto apply(ContractInvoice t) { // 将实体类转换成DTO ContractInvoiceDto contractInvoiceDto = contractInvoiceMapper.toDto(t); // 查询发票相关付款数据 List contractInvoicePayments = contractInvoicePaymentRepository.findByInvoiceId(t.getInvoiceId()); // 相关付款列表 List contractPaymentIds = new ArrayList<>(); // 循环发票相关付款数据 for(int i = 0; i < contractInvoicePayments.size(); i ++) { // 相关付款列表 contractPaymentIds.add(contractInvoicePayments.get(i).getPaymentId()); } // DTO赋值 contractInvoiceDto.setContractPaymentIds(contractPaymentIds); // 查询合同数据 ContractDto contractDto = contractServiceImpl.findById(t.getContractId()); // DTO赋值 contractInvoiceDto.setContractName(contractDto.getContractName()); // 返回处理好的DTO return contractInvoiceDto; } }; return PageUtil.toPage(page.map(func)); } @Override public List queryAll(ContractInvoiceQueryCriteria criteria){ return contractInvoiceMapper.toDto(contractInvoiceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public ContractInvoiceDto findById(Long invoiceId) { ContractInvoice contractInvoice = contractInvoiceRepository.findById(invoiceId).orElseGet(ContractInvoice::new); ValidationUtil.isNull(contractInvoice.getInvoiceId(),"ContractInvoice","invoiceId",invoiceId); return contractInvoiceMapper.toDto(contractInvoice); } @Override @Transactional(rollbackFor = Exception.class) public ContractInvoiceDto create(ContractInvoiceDto resources) { // DTO转实体类 ContractInvoice ciEntity = contractInvoiceMapper.toEntity(resources); // 保存 ContractInvoice contractInvoice = contractInvoiceRepository.save(ciEntity); // 操作付款发票关联表 contractInvoicePaymentServiceImpl.operationMainInvoiceId(contractInvoice.getInvoiceId(), resources.getContractPaymentIds()); // 返回 return contractInvoiceMapper.toDto(contractInvoice); } @Override @Transactional(rollbackFor = Exception.class) public void update(ContractInvoiceDto resources) { // DTO转实体类 ContractInvoice ciEntity = contractInvoiceMapper.toEntity(resources); // 查询旧数据 ContractInvoice contractInvoice = contractInvoiceRepository.findById(ciEntity.getInvoiceId()).orElseGet(ContractInvoice::new); ValidationUtil.isNull( contractInvoice.getInvoiceId(),"ContractInvoice","id",ciEntity.getInvoiceId()); // 新旧数据合并 contractInvoice.copy(ciEntity); // 保存 contractInvoiceRepository.save(contractInvoice); // 操作付款发票关联表 contractInvoicePaymentServiceImpl.operationMainInvoiceId(contractInvoice.getInvoiceId(), resources.getContractPaymentIds()); } @Override public void deleteAll(Long[] ids) { for (Long invoiceId : ids) { // 删除 contractInvoiceRepository.deleteById(invoiceId); // 操作付款发票关联表 contractInvoicePaymentServiceImpl.operationMainInvoiceId(invoiceId, new ArrayList()); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (ContractInvoiceDto contractInvoice : all) { ContractDto contractDto = contractServiceImpl.findById(contractInvoice.getContractId()); String invoiceType = contractInvoice.getInvoiceType() != null ? dictDetailRepository.findByDictNameAndValue(Constant.INVOICE_TYPE, contractInvoice.getInvoiceType()).getLabel() : ""; Map map = new LinkedHashMap<>(); map.put("所属合同", contractDto.getContractName()); map.put("开票时间", contractInvoice.getBillingTime()); map.put("发票类型", invoiceType); map.put("税前金额", contractInvoice.getBeforeTaxAmount()); map.put("税率", contractInvoice.getTaxRate() + '%'); map.put("税额", contractInvoice.getTaxAmount()); map.put("税后金额", contractInvoice.getAfterTaxAmount()); map.put("备注信息", contractInvoice.getRemarks()); map.put("创建者", contractInvoice.getCreateBy()); map.put("创建日期", contractInvoice.getCreateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } }