赵士淞
2023-04-12 dbfecd6423a625edac78aaafeea3d8a9bd2b0928
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
/*
*  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.capital.service.impl;
 
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.capital.domain.CapitalAccountCompany;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.capital.repository.CapitalAccountCompanyRepository;
import me.zhengjie.modules.capital.service.CapitalAccountCompanyService;
import me.zhengjie.modules.capital.service.dto.CapitalAccountCompanyDto;
import me.zhengjie.modules.capital.service.dto.CapitalAccountCompanyQueryCriteria;
import me.zhengjie.modules.capital.service.mapstruct.CapitalAccountCompanyMapper;
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-04-12
**/
@Service
@RequiredArgsConstructor
public class CapitalAccountCompanyServiceImpl implements CapitalAccountCompanyService {
 
    private final CapitalAccountCompanyRepository capitalAccountCompanyRepository;
    private final CapitalAccountCompanyMapper capitalAccountCompanyMapper;
 
    @Override
    public Map<String,Object> queryAll(CapitalAccountCompanyQueryCriteria criteria, Pageable pageable){
        Page<CapitalAccountCompany> page = capitalAccountCompanyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(capitalAccountCompanyMapper::toDto));
    }
 
    @Override
    public List<CapitalAccountCompanyDto> queryAll(CapitalAccountCompanyQueryCriteria criteria){
        return capitalAccountCompanyMapper.toDto(capitalAccountCompanyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public CapitalAccountCompanyDto findById(Long id) {
        CapitalAccountCompany capitalAccountCompany = capitalAccountCompanyRepository.findById(id).orElseGet(CapitalAccountCompany::new);
        ValidationUtil.isNull(capitalAccountCompany.getId(),"CapitalAccountCompany","id",id);
        return capitalAccountCompanyMapper.toDto(capitalAccountCompany);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CapitalAccountCompanyDto create(CapitalAccountCompany resources) {
        // 拷贝后校验
        copyAfterCheck(resources);
        return capitalAccountCompanyMapper.toDto(capitalAccountCompanyRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CapitalAccountCompany resources) {
        CapitalAccountCompany capitalAccountCompany = capitalAccountCompanyRepository.findById(resources.getId()).orElseGet(CapitalAccountCompany::new);
        ValidationUtil.isNull( capitalAccountCompany.getId(),"CapitalAccountCompany","id",resources.getId());
        // 拷贝前校验
        copyBeforeCheck(capitalAccountCompany);
        capitalAccountCompany.copy(resources);
        // 拷贝后校验
        copyAfterCheck(capitalAccountCompany);
        capitalAccountCompanyRepository.save(capitalAccountCompany);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            capitalAccountCompanyRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<CapitalAccountCompanyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CapitalAccountCompanyDto capitalAccountCompany : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("项目区分", capitalAccountCompany.getProjectDistinguish());
            map.put("账户类型", capitalAccountCompany.getAccountType());
            map.put("账户名称", capitalAccountCompany.getAccountName());
            map.put("账户年度", capitalAccountCompany.getAccountYear());
            map.put("期初金额", capitalAccountCompany.getOpeningAmount());
            map.put("警戒金额", capitalAccountCompany.getWarnAmount());
            map.put("账户金额", capitalAccountCompany.getAccountAmount());
            map.put("结算状态", capitalAccountCompany.getSettlementStatus());
            map.put("备注", capitalAccountCompany.getRemarks());
            map.put("创建者", capitalAccountCompany.getCreateBy());
            map.put("创建时间", capitalAccountCompany.getCreateTime());
            map.put("更新者", capitalAccountCompany.getUpdateBy());
            map.put("更新时间", capitalAccountCompany.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    /**
     * 拷贝前校验
     * @param resources
     */
    private void copyBeforeCheck(CapitalAccountCompany resources) {
        // 判断结算状态
        if (Constant.SETTLEMENT_STATUS_SETTLED.equals(resources.getSettlementStatus())) {
            throw new BadRequestException("已结算的公司流账户无法进行修改!");
        }
    }
 
    /**
     * 拷贝后校验
     * @param resources
     */
    private void copyAfterCheck(CapitalAccountCompany resources) {
        // 根据项目区分、账户类型、账户年度查询
        List<CapitalAccountCompany> capitalAccountCompanyList = capitalAccountCompanyRepository.findByProjectDistinguishAndAccountTypeAndAccountYear(resources.getProjectDistinguish(), resources.getAccountType(), resources.getAccountYear());
        // 判断结果集长度
        if (capitalAccountCompanyList.size() > 1) {
            throw new BadRequestException("每个年度同类型公司流账户只能有一个!");
        } else if (capitalAccountCompanyList.size() == 1) {
            if (resources.getId() == null || capitalAccountCompanyList.get(0).getId().longValue() != resources.getId().longValue()) {
                throw new BadRequestException("每个年度同类型公司流账户只能有一个!");
            }
        }
    }
 
}