churf
2023-08-21 fe1c1002a5a2b1f9ea5eec95a40c9bbbcfe17450
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
/*
*  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.contract.domain.ContractSecond;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.contract.repository.ContractSecondRepository;
import me.zhengjie.modules.contract.service.ContractSecondService;
import me.zhengjie.modules.contract.service.dto.ContractSecondDto;
import me.zhengjie.modules.contract.service.dto.ContractSecondQueryCriteria;
import me.zhengjie.modules.contract.service.mapstruct.ContractSecondMapper;
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;
 
/**
* @website https://eladmin.vip
* @description 服务实现
* @author 赵士淞
* @date 2023-02-02
**/
@Service
@RequiredArgsConstructor
public class ContractSecondServiceImpl implements ContractSecondService {
 
    private final ContractSecondRepository contractSecondRepository;
    private final ContractSecondMapper contractSecondMapper;
 
    @Override
    public Map<String,Object> queryAll(ContractSecondQueryCriteria criteria, Pageable pageable){
        Page<ContractSecond> page = contractSecondRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(contractSecondMapper::toDto));
    }
 
    @Override
    public List<ContractSecondDto> queryAll(ContractSecondQueryCriteria criteria){
        return contractSecondMapper.toDto(contractSecondRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public ContractSecondDto findById(Long secondId) {
        ContractSecond contractSecond = contractSecondRepository.findById(secondId).orElseGet(ContractSecond::new);
        ValidationUtil.isNull(contractSecond.getSecondId(),"ContractSecond","secondId",secondId);
        return contractSecondMapper.toDto(contractSecond);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ContractSecondDto create(ContractSecond resources) {
        return contractSecondMapper.toDto(contractSecondRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ContractSecond resources) {
        ContractSecond contractSecond = contractSecondRepository.findById(resources.getSecondId()).orElseGet(ContractSecond::new);
        ValidationUtil.isNull( contractSecond.getSecondId(),"ContractSecond","id",resources.getSecondId());
        contractSecond.copy(resources);
        contractSecondRepository.save(contractSecond);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long secondId : ids) {
            contractSecondRepository.deleteById(secondId);
        }
    }
 
    @Override
    public void download(List<ContractSecondDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ContractSecondDto contractSecond : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("乙方名称", contractSecond.getSecondName());
            map.put("乙方联系人", contractSecond.getSecondPerson());
            map.put("乙方联系电话", contractSecond.getSecondPhone());
            map.put("乙方邮件", contractSecond.getSecondEmail());
            map.put("乙方传真", contractSecond.getSecondFax());
            map.put("乙方所在地区", contractSecond.getSecondArea());
            map.put("乙方详细地址", contractSecond.getSecondAddress());
            map.put("备注信息", contractSecond.getRemarks());
            map.put("创建者", contractSecond.getCreateBy());
            map.put("创建日期", contractSecond.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}