/* * 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.Contract; import me.zhengjie.modules.contract.domain.ContractInvoicePayment; import me.zhengjie.modules.contract.domain.ContractPayment; 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.contract.service.mapstruct.ContractMapper; import me.zhengjie.modules.system.domain.SysCompany; import me.zhengjie.modules.system.repository.DictDetailRepository; import me.zhengjie.modules.system.repository.SysCompanyRepository; import me.zhengjie.utils.ValidationUtil; import me.zhengjie.utils.FileUtil; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.contract.repository.ContractPaymentRepository; import me.zhengjie.modules.contract.service.ContractPaymentService; import me.zhengjie.modules.contract.service.dto.ContractPaymentDto; import me.zhengjie.modules.contract.service.dto.ContractPaymentQueryCriteria; import me.zhengjie.modules.contract.service.mapstruct.ContractPaymentMapper; 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.text.SimpleDateFormat; 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 ContractPaymentServiceImpl implements ContractPaymentService { private final ContractPaymentRepository contractPaymentRepository; private final ContractPaymentMapper contractPaymentMapper; private final DictDetailRepository dictDetailRepository; private final ContractInvoicePaymentRepository contractInvoicePaymentRepository; private final ContractInvoicePaymentServiceImpl contractInvoicePaymentServiceImpl; private final ContractServiceImpl contractServiceImpl; private final ContractMapper contractMapper; private final ContractRepository contractRepository; private final SysCompanyRepository sysCompanyRepository; @Override public Map queryAll(ContractPaymentQueryCriteria criteria, Pageable pageable){ Page page = contractPaymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @Override public ContractPaymentDto apply(ContractPayment t) { // 将实体类转换成DTO ContractPaymentDto contractPaymentDto = contractPaymentMapper.toDto(t); // 查询付款相关发票数据 List contractInvoicePayments = contractInvoicePaymentRepository.findByPaymentId(t.getPaymentId()); // 相关发票列表 List contractInvoiceIds = new ArrayList<>(); // 循环付款相关发票数据 for(int i = 0; i < contractInvoicePayments.size(); i ++) { // 相关发票列表 contractInvoiceIds.add(contractInvoicePayments.get(i).getInvoiceId()); } // DTO赋值 contractPaymentDto.setContractInvoiceIds(contractInvoiceIds); // 查询合同数据 Contract contract = contractRepository.findById(t.getContractId()).orElseGet(Contract::new); // DTO赋值 contractPaymentDto.setContractName(contract.getContractName()); // 魏传栋 - 始 if (contractPaymentDto.getCompanyId() != null) { SysCompany sysCompany = sysCompanyRepository.findById(contractPaymentDto.getCompanyId()).orElseGet(SysCompany::new); contractPaymentDto.setCompanyName(sysCompany.getLabel()); } // 返回处理好的DTO return contractPaymentDto; } }; return PageUtil.toPage(page.map(func)); } @Override public List queryAll(ContractPaymentQueryCriteria criteria){ return contractPaymentMapper.toDto(contractPaymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); } @Override @Transactional public ContractPaymentDto findById(Long paymentId) { ContractPayment contractPayment = contractPaymentRepository.findById(paymentId).orElseGet(ContractPayment::new); ValidationUtil.isNull(contractPayment.getPaymentId(),"ContractPayment","paymentId",paymentId); return contractPaymentMapper.toDto(contractPayment); } @Override @Transactional(rollbackFor = Exception.class) public ContractPaymentDto create(ContractPaymentDto resources) { // DTO转实体类 ContractPayment cpEntity = contractPaymentMapper.toEntity(resources); // 保存 ContractPayment contractPayment = contractPaymentRepository.save(cpEntity); // 操作付款发票关联表 contractInvoicePaymentServiceImpl.operationMainPaymentId(contractPayment.getPaymentId(), resources.getContractInvoiceIds()); // 查询合同数据 ContractDto contractDto = contractServiceImpl.findById(resources.getContractId()); // 更新合同付款状态 Contract contract = contractServiceImpl.updatePaymentStatus(contractMapper.toEntity(contractDto)); // 保存合同 contractRepository.save(contract); // 返回 return contractPaymentMapper.toDto(contractPayment); } @Override @Transactional(rollbackFor = Exception.class) public void update(ContractPaymentDto resources) { // DTO转实体类 ContractPayment cpEntity = contractPaymentMapper.toEntity(resources); // 查询旧数据 ContractPayment contractPayment = contractPaymentRepository.findById(cpEntity.getPaymentId()).orElseGet(ContractPayment::new); ValidationUtil.isNull( contractPayment.getPaymentId(),"ContractPayment","id",cpEntity.getPaymentId()); // 新旧数据合并 contractPayment.copy(cpEntity); // 保存 contractPaymentRepository.save(contractPayment); // 操作付款发票关联表 contractInvoicePaymentServiceImpl.operationMainPaymentId(contractPayment.getPaymentId(), resources.getContractInvoiceIds()); // 查询合同数据 ContractDto contractDto = contractServiceImpl.findById(resources.getContractId()); // 更新合同付款状态 Contract contract = contractServiceImpl.updatePaymentStatus(contractMapper.toEntity(contractDto)); // 保存合同 contractRepository.save(contract); } @Override public void deleteAll(Long[] ids) { for (Long paymentId : ids) { // 查询旧数据 ContractPayment contractPayment = contractPaymentRepository.findById(paymentId).orElseGet(ContractPayment::new); // 删除 contractPaymentRepository.deleteById(paymentId); // 操作付款发票关联表 contractInvoicePaymentServiceImpl.operationMainPaymentId(paymentId, new ArrayList()); // 查询合同数据 ContractDto contractDto = contractServiceImpl.findById(contractPayment.getContractId()); // 更新合同付款状态 Contract contract = contractServiceImpl.updatePaymentStatus(contractMapper.toEntity(contractDto)); // 保存合同 contractRepository.save(contract); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (ContractPaymentDto contractPayment : all) { String fundType = contractPayment.getFundType() != null ? dictDetailRepository.findByDictNameAndValue(Constant.FUND_TYPE, contractPayment.getFundType()).getLabel() : ""; String fundBelong = null; if(contractPayment.getFundBelong() != null) { fundBelong = contractPayment.getFundBelong(); } else if(contractPayment.getFundBelongStart() != null && contractPayment.getFundBelongEnd() != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String fundBelongStart = sdf.format(contractPayment.getFundBelongStart()); String fundBelongEnd = sdf.format(contractPayment.getFundBelongEnd()); fundBelong = fundBelongStart.substring(0, 10) + " 至 " + fundBelongEnd.substring(0, 10) + " 服务费"; } Contract contract = contractRepository.findById(contractPayment.getContractId()).orElseGet(Contract::new); Map map = new LinkedHashMap<>(); map.put("所属合同", contract.getContractName()); map.put("付款时间", contractPayment.getPaymentTime()); map.put("款项类型", fundType); map.put("付款金额", contractPayment.getPaymentAmount()); map.put("款项所属", fundBelong); map.put("备注信息", contractPayment.getRemarks()); map.put("创建者", contractPayment.getCreateBy()); map.put("创建日期", contractPayment.getCreateTime()); list.add(map); } FileUtil.downloadExcel(list, response); } }