赵士淞
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
/*
*  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.CapitalAccountMoney;
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.CapitalAccountMoneyRepository;
import me.zhengjie.modules.capital.service.CapitalAccountMoneyService;
import me.zhengjie.modules.capital.service.dto.CapitalAccountMoneyDto;
import me.zhengjie.modules.capital.service.dto.CapitalAccountMoneyQueryCriteria;
import me.zhengjie.modules.capital.service.mapstruct.CapitalAccountMoneyMapper;
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 CapitalAccountMoneyServiceImpl implements CapitalAccountMoneyService {
 
    private final CapitalAccountMoneyRepository capitalAccountMoneyRepository;
    private final CapitalAccountMoneyMapper capitalAccountMoneyMapper;
 
    @Override
    public Map<String,Object> queryAll(CapitalAccountMoneyQueryCriteria criteria, Pageable pageable){
        Page<CapitalAccountMoney> page = capitalAccountMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(capitalAccountMoneyMapper::toDto));
    }
 
    @Override
    public List<CapitalAccountMoneyDto> queryAll(CapitalAccountMoneyQueryCriteria criteria){
        return capitalAccountMoneyMapper.toDto(capitalAccountMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public CapitalAccountMoneyDto findById(Long id) {
        CapitalAccountMoney capitalAccountMoney = capitalAccountMoneyRepository.findById(id).orElseGet(CapitalAccountMoney::new);
        ValidationUtil.isNull(capitalAccountMoney.getId(),"CapitalAccountMoney","id",id);
        return capitalAccountMoneyMapper.toDto(capitalAccountMoney);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CapitalAccountMoneyDto create(CapitalAccountMoney resources) {
        // 拷贝后校验
        copyAfterCheck(resources);
        return capitalAccountMoneyMapper.toDto(capitalAccountMoneyRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CapitalAccountMoney resources) {
        CapitalAccountMoney capitalAccountMoney = capitalAccountMoneyRepository.findById(resources.getId()).orElseGet(CapitalAccountMoney::new);
        ValidationUtil.isNull( capitalAccountMoney.getId(),"CapitalAccountMoney","id",resources.getId());
        // 拷贝前校验
        copyBeforeCheck(capitalAccountMoney);
        capitalAccountMoney.copy(resources);
        // 拷贝后校验
        copyAfterCheck(capitalAccountMoney);
        capitalAccountMoneyRepository.save(capitalAccountMoney);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            capitalAccountMoneyRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<CapitalAccountMoneyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CapitalAccountMoneyDto capitalAccountMoney : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("账户名称", capitalAccountMoney.getAccountName());
            map.put("账户年度", capitalAccountMoney.getAccountYear());
            map.put("期初金额", capitalAccountMoney.getOpeningAmount());
            map.put("警戒金额", capitalAccountMoney.getWarnAmount());
            map.put("账户金额", capitalAccountMoney.getAccountAmount());
            map.put("结算状态", capitalAccountMoney.getSettlementStatus());
            map.put("备注", capitalAccountMoney.getRemarks());
            map.put("创建者", capitalAccountMoney.getCreateBy());
            map.put("创建时间", capitalAccountMoney.getCreateTime());
            map.put("更新者", capitalAccountMoney.getUpdateBy());
            map.put("更新时间", capitalAccountMoney.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    /**
     * 拷贝前校验
     * @param resources
     */
    private void copyBeforeCheck(CapitalAccountMoney resources) {
        // 判断结算状态
        if (Constant.SETTLEMENT_STATUS_SETTLED.equals(resources.getSettlementStatus())) {
            throw new BadRequestException("已结算的现金流账户无法进行修改!");
        }
    }
 
    /**
     * 拷贝后校验
     * @param resources
     */
    private void copyAfterCheck(CapitalAccountMoney resources) {
        // 根据账户年度查询
        List<CapitalAccountMoney> capitalAccountMoneyList = capitalAccountMoneyRepository.findByAccountYear(resources.getAccountYear());
        // 判断结果集长度
        if (capitalAccountMoneyList.size() > 1) {
            throw new BadRequestException("每个年度只能有一个现金流账户!");
        } else if (capitalAccountMoneyList.size() == 1) {
            if (resources.getId() == null || capitalAccountMoneyList.get(0).getId().longValue() != resources.getId().longValue()) {
                throw new BadRequestException("每个年度只能有一个现金流账户!");
            }
        }
    }
}