weicd
2023-10-09 dcafe4c3b9644dc5b04d06bd08f7960cf3b23b43
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
216
/*
*  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.exception.BadRequestException;
import me.zhengjie.modules.capital.service.CapitalAccountCompanyService;
import me.zhengjie.modules.capital.service.dto.CapitalAccountCompanyDto;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlow;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlowExpend;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlowIncome;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowExpendRepository;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowIncomeRepository;
import me.zhengjie.modules.quartz.service.impl.CapitalServiceImpl;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowRepository;
import me.zhengjie.modules.capitalFlow.service.CapitalFlowService;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowDto;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowQueryCriteria;
import me.zhengjie.modules.capitalFlow.service.mapstruct.CapitalFlowMapper;
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.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
@Transactional
@RequiredArgsConstructor
public class CapitalFlowServiceImpl implements CapitalFlowService {
 
    private final CapitalFlowRepository capitalFlowRepository;
    private final CapitalFlowMapper capitalFlowMapper;
    private final CapitalFlowExpendRepository capitalFlowExpendRepository;
    private final CapitalFlowIncomeRepository capitalFlowIncomeRepository;
    private final CapitalServiceImpl capitalService;
    private final CapitalAccountCompanyService capitalAccountCompanyService;
 
    @Override
    public Map<String,Object> queryAll(CapitalFlowQueryCriteria criteria, Pageable pageable){
        // 判断资金流水查询类流水年月是否为空
        if (criteria.getFlowYearMonth() != null && !"".equals(criteria.getFlowYearMonth())) {
            // 流水年月:国际时间转中国时间
            criteria.setFlowYearMonth(DateUtil.internationalTimeToChinaTime(criteria.getFlowYearMonth()));
        }
        Page<CapitalFlow> page = capitalFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        // 将实体类转换为DTO(重写)
        Function<CapitalFlow, CapitalFlowDto> func = new Function<CapitalFlow, CapitalFlowDto>(){
            @Override
            public CapitalFlowDto apply(CapitalFlow t) {
                // 将实体类转换成DTO
                CapitalFlowDto capitalFlowDto = capitalFlowMapper.toDto(t);
                // 年度账户名称
                capitalFlowDto.setAccountCompanyName(accountCompanyName(t));
                // 流水支出
                capitalFlowDto.setCapitalFlowExpends(capitalFlowExpendRepository.findByFlowId(t.getId()));
                // 流水支出金额
                capitalFlowDto.setCapitalFlowExpendsAmount(capitalFlowExpendRepository.sumAmountByFlowId(t.getId()));
                // 流水收入
                capitalFlowDto.setCapitalFlowIncomes(capitalFlowIncomeRepository.findByFlowId(t.getId()));
                // 流水收入金额
                capitalFlowDto.setCapitalFlowIncomesAmount(capitalFlowIncomeRepository.sumAmountByFlowId(t.getId()));
                // 返回处理好的DTO
                return capitalFlowDto;
            }
        };
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<CapitalFlowDto> queryAll(CapitalFlowQueryCriteria criteria){
        List<CapitalFlowDto> capitalFlowDtoList = capitalFlowMapper.toDto(capitalFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
        // 循环资金流水列表
        for (int i = 0; i < capitalFlowDtoList.size(); i ++) {
            // 资金流水
            CapitalFlowDto capitalFlowDto = capitalFlowDtoList.get(i);
            // DTO赋值
            capitalFlowDto.setAccountCompanyName(accountCompanyName(capitalFlowMapper.toEntity(capitalFlowDto)));
        }
        return capitalFlowDtoList;
    }
 
    @Override
    @Transactional
    public CapitalFlowDto findById(Long id) {
        CapitalFlow capitalFlow = capitalFlowRepository.findById(id).orElseGet(CapitalFlow::new);
        ValidationUtil.isNull(capitalFlow.getId(),"CapitalFlow","id",id);
        return capitalFlowMapper.toDto(capitalFlow);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CapitalFlowDto create(CapitalFlow resources) {
        return capitalFlowMapper.toDto(capitalFlowRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CapitalFlow resources) {
        CapitalFlow capitalFlow = capitalFlowRepository.findById(resources.getId()).orElseGet(CapitalFlow::new);
        ValidationUtil.isNull( capitalFlow.getId(),"CapitalFlow","id",resources.getId());
        capitalFlow.copy(resources);
        capitalFlowRepository.save(capitalFlow);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            // 查询流水支出表
            List<CapitalFlowExpend> capitalFlowExpends = capitalFlowExpendRepository.findByFlowId(id);
            for(int i = 0; i < capitalFlowExpends.size(); i++){
                capitalFlowExpendRepository.deleteByFlowId(capitalFlowExpends.get(i).getFlowId());
            }
            // 查询流水收入表
            List<CapitalFlowIncome> capitalFlowIncomes = capitalFlowIncomeRepository.findByFlowId(id);
            for(int i = 0; i < capitalFlowIncomes.size(); i++){
                capitalFlowIncomeRepository.deleteByFlowId(capitalFlowIncomes.get(i).getFlowId());
            }
            capitalFlowRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<CapitalFlowDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CapitalFlowDto capitalFlow : all) {
            String accountCompanyName = accountCompanyName(capitalFlowMapper.toEntity(capitalFlow));
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("流水年月", capitalFlow.getFlowYearMonth());
            map.put("所属年度账户", accountCompanyName);
            map.put("备注", capitalFlow.getRemarks());
            map.put("创建者", capitalFlow.getCreateBy());
            map.put("创建时间", capitalFlow.getCreateTime());
            map.put("更新者", capitalFlow.getUpdateBy());
            map.put("更新时间", capitalFlow.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    /**
     * 流水生成
     * @param resources /
     */
    @Override
    public void generate(CapitalFlow resources) {
//        // 日历类
//        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.MONTH, -1);
//        if(resources.getFlowYearMonth().compareTo(cal.getTime()) > 0){
//            throw new BadRequestException("只能更新上个月的流水记录");
//        }
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 目标年月
        String targetYearMonth = sdf.format(resources.getFlowYearMonth().getTime());
        capitalService.getCapitalInfo(resources.getAccountCompanyId(), targetYearMonth);
    }
 
    /**
     * 年度账户名称
     * @param resources
     * @return
     */
    private String accountCompanyName(CapitalFlow resources) {
        // 查询公司数据
        CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyService.findById(resources.getAccountCompanyId());
        return capitalAccountCompanyDto.getAccountName();
    }
 
    /**
     * 关联月度资金公司流账户
     * @return
     */
    public CapitalFlowDto relevanceCapitalFlow(Long accountCompanyId) {
        // 当前系统时间
        Calendar calendar = Calendar.getInstance();
        // 资金流水检索类
        CapitalFlowQueryCriteria criteria = new CapitalFlowQueryCriteria();
        criteria.setAccountCompanyId(accountCompanyId);
        criteria.setFlowYearMonth(DateUtil.getYearMonth(calendar));
        // 查询资金流水
        List<CapitalFlowDto> capitalFlowDtoList = capitalFlowMapper.toDto(capitalFlowRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));;
        // 判断资金流水
        if (capitalFlowDtoList.size() == 0) {
            throw new BadRequestException("没有找到对应的月度资金公司流账户!");
        } else if (capitalFlowDtoList.size() > 1) {
            throw new BadRequestException("找到多条对应的月度资金公司流账户!");
        }
        return capitalFlowDtoList.get(0);
    }
}