BAOLH
2026-07-15 511bf59bf1b6d2d1a3865ddabd3a973267147ce2
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
/*
*  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 lombok.RequiredArgsConstructor;
import me.zhengjie.modules.contract.domain.Contract;
import me.zhengjie.modules.contract.domain.ContractFirst;
import me.zhengjie.modules.contract.domain.ContractMail;
import me.zhengjie.modules.contract.domain.ContractSecond;
import me.zhengjie.modules.contract.repository.ContractFirstRepository;
import me.zhengjie.modules.contract.repository.ContractMailRepository;
import me.zhengjie.modules.contract.repository.ContractRepository;
import me.zhengjie.modules.contract.repository.ContractSecondRepository;
import me.zhengjie.modules.contract.service.ContractMailService;
import me.zhengjie.modules.contract.service.dto.ContractMailDto;
import me.zhengjie.modules.contract.service.dto.ContractMailQueryCriteria;
import me.zhengjie.modules.contract.service.mapstruct.ContractMailMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
 
/**
* @website https://eladmin.vip
* @description 服务实现
* @author baolh
* @date 2026-02-26
**/
@Service
@RequiredArgsConstructor
public class ContractMailServiceImpl implements ContractMailService {
 
    private final ContractMailRepository contractMailRepository;
    private final ContractRepository contractRepository;
    private final ContractFirstRepository contractFirstRepository;
    private final ContractSecondRepository contractSecondRepository;
    private final ContractMailMapper contractMailMapper;
 
    @Override
    public Map<String,Object> queryAll(ContractMailQueryCriteria criteria, Pageable pageable){
        Page<ContractMail> page = contractMailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Function<ContractMail, ContractMailDto> func = new Function<ContractMail, ContractMailDto>(){
            @Override
            public ContractMailDto apply(ContractMail t) {
                // 将实体类转换成DTO
                ContractMailDto contractMailDto = contractMailMapper.toDto(t);
                // 查询合同数据
                Contract contract = Optional.ofNullable(t.getContractId()).map(contractId -> contractRepository.findById(contractId).orElseGet(Contract::new)).orElseGet(Contract::new);
                // 查询合同数据
                ContractFirst contractFirst = Optional.ofNullable(t.getFirstId()).map(firstId -> contractFirstRepository.findById(firstId).orElseGet(ContractFirst::new)).orElseGet(ContractFirst::new);
                // 查询合同数据
                ContractSecond contractSecond = Optional.ofNullable(t.getSecondId()).map(secondId -> contractSecondRepository.findById(secondId).orElseGet(ContractSecond::new)).orElseGet(ContractSecond::new);
 
                // DTO赋值
                contractMailDto.setContractName(contract.getContractName());
                contractMailDto.setFirstName(contractFirst.getFirstName());
                contractMailDto.setSecondName(contractSecond.getSecondName());
                // 返回处理好的DTO
                return contractMailDto;
            }
        };
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<ContractMailDto> queryAll(ContractMailQueryCriteria criteria){
        return contractMailMapper.toDto(contractMailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public ContractMailDto findById(Long id) {
        ContractMail contractMail = contractMailRepository.findById(id).orElseGet(ContractMail::new);
        ValidationUtil.isNull(contractMail.getId(),"ContractMail","id",id);
        return contractMailMapper.toDto(contractMail);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ContractMailDto create(ContractMail resources) {
        return contractMailMapper.toDto(contractMailRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ContractMail resources) {
        ContractMail contractMail = contractMailRepository.findById(resources.getId()).orElseGet(ContractMail::new);
        ValidationUtil.isNull( contractMail.getId(),"ContractMail","id",resources.getId());
        contractMail.copy(resources);
        contractMailRepository.save(contractMail);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            contractMailRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<ContractMailDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ContractMailDto contractMail : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("合同ID", contractMail.getContractId());
            map.put("邮件发送时间", contractMail.getMailSendTime());
            map.put("邮件运单号", contractMail.getMailTrackingNo());
            map.put("邮寄方式", contractMail.getMailMethod());
            map.put("邮件内容", contractMail.getMailContent());
            map.put("备注信息", contractMail.getRemarks());
            map.put("创建者", contractMail.getCreateBy());
            map.put("更新者", contractMail.getUpdateBy());
            map.put("创建日期", contractMail.getCreateTime());
            map.put("更新时间", contractMail.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}