BAOLH
2025-12-02 d8eddeba2456ad1c89df68979112ae98d82c7948
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
211
212
213
214
215
/*
*  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.capitalFlow.service.impl;
 
import me.zhengjie.modules.capital.service.dto.CapitalAccountCompanyDto;
import me.zhengjie.modules.capital.service.dto.CapitalAccountMoneyDto;
import me.zhengjie.modules.capital.service.impl.CapitalAccountCompanyServiceImpl;
import me.zhengjie.modules.capital.service.impl.CapitalAccountMoneyServiceImpl;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlowExpend;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowDto;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowExpendRepository;
import me.zhengjie.modules.capitalFlow.service.CapitalFlowExpendService;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowExpendDto;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowExpendQueryCriteria;
import me.zhengjie.modules.capitalFlow.service.mapstruct.CapitalFlowExpendMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.function.Function;
 
/**
* @website https://eladmin.vip
* @description 服务实现
* @author 赵士淞
* @date 2023-02-13
**/
@Service
@RequiredArgsConstructor
public class CapitalFlowExpendServiceImpl implements CapitalFlowExpendService {
 
    private final CapitalFlowExpendRepository capitalFlowExpendRepository;
    private final CapitalFlowExpendMapper capitalFlowExpendMapper;
    private final CapitalFlowServiceImpl capitalFlowServiceImpl;
    private final CapitalAccountMoneyServiceImpl capitalAccountMoneyServiceImpl;
    private final CapitalAccountCompanyServiceImpl capitalAccountCompanyServiceImpl;
 
    @Override
    public Map<String,Object> queryAll(CapitalFlowExpendQueryCriteria criteria, Pageable pageable){
        Page<CapitalFlowExpend> page = capitalFlowExpendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        // 将实体类转换为DTO(重写)
        Function<CapitalFlowExpend, CapitalFlowExpendDto> func = new Function<CapitalFlowExpend, CapitalFlowExpendDto>(){
            @Override
            public CapitalFlowExpendDto apply(CapitalFlowExpend t) {
                // 将实体类转换成DTO
                CapitalFlowExpendDto capitalFlowExpendDto = capitalFlowExpendMapper.toDto(t);
                // 流水名称
                capitalFlowExpendDto.setFlowName(flowName(t));
                // 返回处理好的DTO
                return capitalFlowExpendDto;
            }
        };
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<CapitalFlowExpendDto> queryAll(CapitalFlowExpendQueryCriteria criteria){
        return capitalFlowExpendMapper.toDto(capitalFlowExpendRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public CapitalFlowExpendDto findById(Long id) {
        CapitalFlowExpend capitalFlowExpend = capitalFlowExpendRepository.findById(id).orElseGet(CapitalFlowExpend::new);
        ValidationUtil.isNull(capitalFlowExpend.getId(),"CapitalFlowExpend","id",id);
        return capitalFlowExpendMapper.toDto(capitalFlowExpend);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CapitalFlowExpendDto create(CapitalFlowExpend resources) {
        return capitalFlowExpendMapper.toDto(capitalFlowExpendRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CapitalFlowExpend resources) {
        CapitalFlowExpend capitalFlowExpend = capitalFlowExpendRepository.findById(resources.getId()).orElseGet(CapitalFlowExpend::new);
        ValidationUtil.isNull( capitalFlowExpend.getId(),"CapitalFlowExpend","id",resources.getId());
        capitalFlowExpend.copy(resources);
        capitalFlowExpendRepository.save(capitalFlowExpend);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            capitalFlowExpendRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<CapitalFlowExpendDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CapitalFlowExpendDto capitalFlowExpend : all) {
            String flowName = flowName(capitalFlowExpendMapper.toEntity(capitalFlowExpend));
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("所属流水", flowName);
            map.put("标题", capitalFlowExpend.getTitle());
            map.put("金额", capitalFlowExpend.getAmount());
            map.put("备注", capitalFlowExpend.getRemarks());
            map.put("创建者", capitalFlowExpend.getCreateBy());
            map.put("创建时间", capitalFlowExpend.getCreateTime());
            map.put("更新者", capitalFlowExpend.getUpdateBy());
            map.put("更新时间", capitalFlowExpend.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    /**
     * 流水名称
     * @param resources
     * @return
     */
    private String flowName(CapitalFlowExpend resources) {
        // 查询资金流水数据
        CapitalFlowDto capitalFlowDto = capitalFlowServiceImpl.findById(resources.getFlowId());
        // 查询年度账号数据
        CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyServiceImpl.findById(capitalFlowDto.getAccountCompanyId());
        return new SimpleDateFormat("yyyy-MM").format(capitalFlowDto.getFlowYearMonth()) + " - " + capitalAccountCompanyDto.getAccountName();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void flowableCreate(String accountType, String title, BigDecimal amount) {
        // 关联年度资金现金流账户
        CapitalAccountMoneyDto capitalAccountMoneyDto = capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoney();
        // 关联年度资金现金流账户(减少金额)
        capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoneySubtract(capitalAccountMoneyDto, amount);
        // 关联年度资金公司流账户
        CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompany(accountType);
        // 关联年度资金公司流账户(减少金额)
        capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompanySubtract(capitalAccountCompanyDto, amount);
        // 关联月度资金公司流账户
        CapitalFlowDto capitalFlowDto = capitalFlowServiceImpl.relevanceCapitalFlow(capitalAccountCompanyDto.getId());
        // 关联资金流水支出(增加金额)
        relevanceCapitalFlowExpendAdd(capitalFlowDto.getId(), title, amount);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void flowableDelete(String accountType, String title, BigDecimal amount) {
        // 关联年度资金现金流账户
        CapitalAccountMoneyDto capitalAccountMoneyDto = capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoney();
        // 关联年度资金现金流账户(增加金额)
        capitalAccountMoneyServiceImpl.relevanceCapitalAccountMoneyAdd(capitalAccountMoneyDto, amount);
        // 关联年度资金公司流账户
        CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompany(accountType);
        // 关联年度资金公司流账户(增加金额)
        capitalAccountCompanyServiceImpl.relevanceCapitalAccountCompanyAdd(capitalAccountCompanyDto, amount);
        // 关联月度资金公司流账户
        CapitalFlowDto capitalFlowDto = capitalFlowServiceImpl.relevanceCapitalFlow(capitalAccountCompanyDto.getId());
        // 关联资金流水支出(减少金额)
        relevanceCapitalFlowExpendSubtract(capitalFlowDto.getId(), title, amount);
    }
 
    /**
     * 关联资金流水支出(增加金额)
     * @param capitalFlowId
     * @param title
     * @param amount
     */
    public void relevanceCapitalFlowExpendAdd(Long capitalFlowId, String title, BigDecimal amount) {
        // 临时金额
        BigDecimal tempAmount = new BigDecimal(0);
        tempAmount = tempAmount.add(amount);
        // 流水支出实体类
        CapitalFlowExpend capitalFlowExpend = new CapitalFlowExpend();
        capitalFlowExpend.setFlowId(capitalFlowId);
        capitalFlowExpend.setTitle(title);
        capitalFlowExpend.setAmount(tempAmount);
        // 保存
        capitalFlowExpendRepository.save(capitalFlowExpend);
    }
 
    /**
     * 关联资金流水支出(减少金额)
     * @param capitalFlowId
     * @param title
     * @param amount
     */
    public void relevanceCapitalFlowExpendSubtract(Long capitalFlowId, String title, BigDecimal amount) {
        // 临时金额
        BigDecimal tempAmount = new BigDecimal(0);
        tempAmount = tempAmount.subtract(amount);
        // 流水支出实体类
        CapitalFlowExpend capitalFlowExpend = new CapitalFlowExpend();
        capitalFlowExpend.setFlowId(capitalFlowId);
        capitalFlowExpend.setTitle(title);
        capitalFlowExpend.setAmount(tempAmount);
        // 保存
        capitalFlowExpendRepository.save(capitalFlowExpend);
    }
}