yaozc
2023-04-28 bdf99bb9ab8b0250c073e85e4d676c3c745aed8b
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
*  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.business.service.impl;
 
import me.zhengjie.modules.business.domain.BusinessExpense;
import me.zhengjie.modules.business.domain.BusinessExpenseReimbursement;
import me.zhengjie.modules.business.repository.BusinessExpenseReimbursementRepository;
import me.zhengjie.modules.business.service.BusinessExpenseReimbursementService;
import me.zhengjie.modules.business.service.BusinessPlanService;
import me.zhengjie.modules.business.service.dto.*;
import me.zhengjie.modules.business.service.mapstruct.BusinessExpenseReimbursementMapper;
import me.zhengjie.modules.capitalFlow.service.CapitalFlowExpendService;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.project.service.ProjectService;
import me.zhengjie.modules.project.service.dto.ProjectDto;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.repository.DictDetailRepository;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.business.repository.BusinessExpenseRepository;
import me.zhengjie.modules.business.service.BusinessExpenseService;
import me.zhengjie.modules.business.service.mapstruct.BusinessExpenseMapper;
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.math.BigDecimal;
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;
import java.util.function.Function;
 
/**
* @website https://eladmin.vip
* @description 服务实现
* @author 赵士淞
* @date 2023-04-13
**/
@Service
@RequiredArgsConstructor
public class BusinessExpenseServiceImpl implements BusinessExpenseService {
 
    private final BusinessExpenseRepository businessExpenseRepository;
    private final BusinessExpenseMapper businessExpenseMapper;
    private final BusinessPlanService businessPlanService;
    private final ProjectService projectService;
    private final BusinessExpenseReimbursementRepository businessExpenseReimbursementRepository;
    private final BusinessExpenseReimbursementMapper businessExpenseReimbursementMapper;
    private final CapitalFlowExpendService capitalFlowExpendService;
    private final DictDetailRepository dictDetailRepository;
    private final BusinessExpenseReimbursementService businessExpenseReimbursementService;
 
    @Override
    public Map<String,Object> queryAll(BusinessExpenseQueryCriteria criteria, Pageable pageable){
        Page<BusinessExpense> page = businessExpenseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        // 将实体类转换为DTO(重写)
        Function<BusinessExpense, BusinessExpenseDto> func = new Function<BusinessExpense, BusinessExpenseDto>(){
            @Override
            public BusinessExpenseDto apply(BusinessExpense t) {
                // 将实体类转换成DTO
                BusinessExpenseDto businessExpenseDto = businessExpenseMapper.toDto(t);
 
                // 获取营业计划
                BusinessPlanDto businessPlanDto = businessPlanService.findById(businessExpenseDto.getBusinessPlanId());
                businessExpenseDto.setBusinessPlanContent(businessPlanDto.getBusinessContent());
                // 获取项目
                if (businessExpenseDto.getProjectId() != null) {
                    ProjectDto projectDto = projectService.findById(businessExpenseDto.getProjectId());
                    businessExpenseDto.setProjectName(projectDto.getName());
                }
                // 获取营业费用明细
                List<BusinessExpenseReimbursement> businessExpenseReimbursementList = businessExpenseReimbursementRepository.findByBusinessExpenseId(businessExpenseDto.getId());
                businessExpenseDto.setBusinessExpenseReimbursementDtoList(businessExpenseReimbursementMapper.toDto(businessExpenseReimbursementList));
                // 获取报销金额总计
                BigDecimal amountReimbursementAmount = businessExpenseReimbursementRepository.amountReimbursementAmountByBusinessExpenseId(businessExpenseDto.getId());
                businessExpenseDto.setAmountReimbursementAmount(amountReimbursementAmount);
 
                // 返回处理好的DTO
                return businessExpenseDto;
            }
        };
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<BusinessExpenseDto> queryAll(BusinessExpenseQueryCriteria criteria){
        return businessExpenseMapper.toDto(businessExpenseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public BusinessExpenseDto findById(Long id) {
        BusinessExpense businessExpense = businessExpenseRepository.findById(id).orElseGet(BusinessExpense::new);
        ValidationUtil.isNull(businessExpense.getId(),"BusinessExpense","id",id);
        return businessExpenseMapper.toDto(businessExpense);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BusinessExpenseDto create(BusinessExpenseDto resources) {
        // 保存营业费用
        BusinessExpense businessExpense = businessExpenseRepository.save(businessExpenseMapper.toEntity(resources));
        // 保存相关营业费用明细
        saveBusinessExpenseReimbursement(resources.getBusinessExpenseReimbursementDtoList(), businessExpense.getId());
        // 关联新增
        accountCreate(resources);
        return findById(businessExpense.getId());
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BusinessExpenseDto resources) {
        BusinessExpense businessExpense = businessExpenseRepository.findById(resources.getId()).orElseGet(BusinessExpense::new);
        ValidationUtil.isNull( businessExpense.getId(),"BusinessExpense","id",resources.getId());
        // 关联删除
        accountDelete(businessExpense);
        businessExpense.copy(businessExpenseMapper.toEntity(resources));
        businessExpenseRepository.save(businessExpense);
        // 保存相关营业费用明细
        saveBusinessExpenseReimbursement(resources.getBusinessExpenseReimbursementDtoList(), businessExpense.getId());
        // 实体类 转 DTO
        BusinessExpenseDto businessExpenseDto = businessExpenseMapper.toDto(businessExpense);
        businessExpenseDto.setBusinessExpenseReimbursementDtoList(resources.getBusinessExpenseReimbursementDtoList());
        // 关联新增
        accountCreate(businessExpenseDto);
    }
 
    /**
     * 保存相关营业费用明细
     * @param businessExpenseReimbursementDtoList
     * @param businessExpenseId
     */
    private void saveBusinessExpenseReimbursement(List<BusinessExpenseReimbursementDto> businessExpenseReimbursementDtoList, Long businessExpenseId) {
        // 根据营业费用ID删除营业费用明细
        businessExpenseReimbursementRepository.deleteByBusinessExpenseId(businessExpenseId);
        // 营业费用明细列表
        List<BusinessExpenseReimbursement> businessExpenseReimbursementList = businessExpenseReimbursementMapper.toEntity(businessExpenseReimbursementDtoList);
        // 循环营业费用明细列表
        for (BusinessExpenseReimbursement businessExpenseReimbursement : businessExpenseReimbursementList) {
            // 营业费用明细
            businessExpenseReimbursement.setBusinessExpenseId(businessExpenseId);
            // 保存营业费用明细
            businessExpenseReimbursementRepository.save(businessExpenseReimbursement);
        }
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            BusinessExpense businessExpense = businessExpenseRepository.findById(id).orElseGet(BusinessExpense::new);
            ValidationUtil.isNull( businessExpense.getId(),"BusinessExpense","id",id);
            // 关联删除
            accountDelete(businessExpense);
            businessExpenseRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<BusinessExpenseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BusinessExpenseDto businessExpense : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("费用类型", businessExpense.getExpenseType());
            map.put("所属营业计划", businessExpense.getBusinessPlanId());
            map.put("所属项目", businessExpense.getProjectId());
            map.put("备注", businessExpense.getRemarks());
            map.put("创建者", businessExpense.getCreateBy());
            map.put("创建时间", businessExpense.getCreateTime());
            map.put("更新者", businessExpense.getUpdateBy());
            map.put("更新时间", businessExpense.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    /**
     * 关联创建
     * @param resources
     */
    private void accountCreate(BusinessExpenseDto resources) {
        // 营业计划
        BusinessPlanDto businessPlanDto = businessPlanService.findById(resources.getBusinessPlanId());
        // 费用类型
        DictDetail dictDetail = dictDetailRepository.findByDictNameAndValue(Constant.EXPENSE_TYPE, resources.getExpenseType());
        // 费用金额
        BigDecimal amount = new BigDecimal(0);
        for (BusinessExpenseReimbursementDto businessExpenseReimbursementDto : resources.getBusinessExpenseReimbursementDtoList()) {
            amount = amount.add(businessExpenseReimbursementDto.getReimbursementAmount());
        }
        // 关联创建
        capitalFlowExpendService.flowableCreate(Constant.ACCOUNT_TYPE_QIYUAN, businessPlanDto.getBusinessContent() + " - " + dictDetail.getLabel(), amount);
    }
 
    /**
     * 关联删除
     * @param resources
     */
    private void accountDelete(BusinessExpense resources) {
        // 营业计划
        BusinessPlanDto businessPlanDto = businessPlanService.findById(resources.getBusinessPlanId());
        // 费用类型
        DictDetail dictDetail = dictDetailRepository.findByDictNameAndValue(Constant.EXPENSE_TYPE, resources.getExpenseType());
        // 费用金额
        BigDecimal amount = new BigDecimal(0);
        BusinessExpenseReimbursementQueryCriteria businessExpenseReimbursementQueryCriteria = new BusinessExpenseReimbursementQueryCriteria();
        businessExpenseReimbursementQueryCriteria.setBusinessExpenseId(resources.getId());
        List<BusinessExpenseReimbursementDto> businessExpenseReimbursementDtoList = businessExpenseReimbursementService.queryAll(businessExpenseReimbursementQueryCriteria);
        for (BusinessExpenseReimbursementDto businessExpenseReimbursementDto : businessExpenseReimbursementDtoList) {
            amount = amount.add(businessExpenseReimbursementDto.getReimbursementAmount());
        }
        // 关联删除
        capitalFlowExpendService.flowableDelete(Constant.ACCOUNT_TYPE_QIYUAN, businessPlanDto.getBusinessContent() + " - " + dictDetail.getLabel(), amount);
    }
}