muzd
2023-04-28 22c4d7be0f25a6b9b1a4047ac6dd5bb85543316c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
*  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<String,Object> queryAll(ContractPaymentQueryCriteria criteria, Pageable pageable){
        Page<ContractPayment> page = contractPaymentRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        // 将实体类转换为DTO(重写)
        Function<ContractPayment, ContractPaymentDto> func = new Function<ContractPayment, ContractPaymentDto>(){
            @Override
            public ContractPaymentDto apply(ContractPayment t) {
                // 将实体类转换成DTO
                ContractPaymentDto contractPaymentDto = contractPaymentMapper.toDto(t);
                // 查询付款相关发票数据
                List<ContractInvoicePayment> contractInvoicePayments = contractInvoicePaymentRepository.findByPaymentId(t.getPaymentId());
                // 相关发票列表
                List<Long> 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<ContractPaymentDto> 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<Long>());
            // 查询合同数据
            ContractDto contractDto = contractServiceImpl.findById(contractPayment.getContractId());
            // 更新合同付款状态
            Contract contract = contractServiceImpl.updatePaymentStatus(contractMapper.toEntity(contractDto));
            // 保存合同
            contractRepository.save(contract);
        }
    }
 
    @Override
    public void download(List<ContractPaymentDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> 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<String,Object> 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);
    }
}