churf
2023-09-07 1b978faa6df37feb92ea84f08fc63dbf0d120efb
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
/*
*  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.impl.CapitalAccountCompanyServiceImpl;
import me.zhengjie.modules.capitalFlow.domain.CapitalFlowIncome;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowDto;
import me.zhengjie.modules.system.service.dto.SysCompanyDto;
import me.zhengjie.modules.system.service.impl.SysCompanyServiceImpl;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.capitalFlow.repository.CapitalFlowIncomeRepository;
import me.zhengjie.modules.capitalFlow.service.CapitalFlowIncomeService;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowIncomeDto;
import me.zhengjie.modules.capitalFlow.service.dto.CapitalFlowIncomeQueryCriteria;
import me.zhengjie.modules.capitalFlow.service.mapstruct.CapitalFlowIncomeMapper;
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.text.SimpleDateFormat;
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-02-13
**/
@Service
@RequiredArgsConstructor
public class CapitalFlowIncomeServiceImpl implements CapitalFlowIncomeService {
 
    private final CapitalFlowIncomeRepository capitalFlowIncomeRepository;
    private final CapitalFlowIncomeMapper capitalFlowIncomeMapper;
    private final CapitalFlowServiceImpl capitalFlowServiceImpl;
    private final CapitalAccountCompanyServiceImpl capitalAccountCompanyServiceImpl;
 
    @Override
    public Map<String,Object> queryAll(CapitalFlowIncomeQueryCriteria criteria, Pageable pageable){
        Page<CapitalFlowIncome> page = capitalFlowIncomeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        // 将实体类转换为DTO(重写)
        Function<CapitalFlowIncome, CapitalFlowIncomeDto> func = new Function<CapitalFlowIncome, CapitalFlowIncomeDto>(){
            @Override
            public CapitalFlowIncomeDto apply(CapitalFlowIncome t) {
                // 将实体类转换成DTO
                CapitalFlowIncomeDto capitalFlowIncomeDto = capitalFlowIncomeMapper.toDto(t);
                // 流水名称
                capitalFlowIncomeDto.setFlowName(flowName(t));
                // 返回处理好的DTO
                return capitalFlowIncomeDto;
            }
        };
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<CapitalFlowIncomeDto> queryAll(CapitalFlowIncomeQueryCriteria criteria){
        return capitalFlowIncomeMapper.toDto(capitalFlowIncomeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public CapitalFlowIncomeDto findById(Long id) {
        CapitalFlowIncome capitalFlowIncome = capitalFlowIncomeRepository.findById(id).orElseGet(CapitalFlowIncome::new);
        ValidationUtil.isNull(capitalFlowIncome.getId(),"CapitalFlowIncome","id",id);
        return capitalFlowIncomeMapper.toDto(capitalFlowIncome);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CapitalFlowIncomeDto create(CapitalFlowIncome resources) {
        return capitalFlowIncomeMapper.toDto(capitalFlowIncomeRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CapitalFlowIncome resources) {
        CapitalFlowIncome capitalFlowIncome = capitalFlowIncomeRepository.findById(resources.getId()).orElseGet(CapitalFlowIncome::new);
        ValidationUtil.isNull( capitalFlowIncome.getId(),"CapitalFlowIncome","id",resources.getId());
        capitalFlowIncome.copy(resources);
        capitalFlowIncomeRepository.save(capitalFlowIncome);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            capitalFlowIncomeRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<CapitalFlowIncomeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CapitalFlowIncomeDto capitalFlowIncome : all) {
            String flowName = flowName(capitalFlowIncomeMapper.toEntity(capitalFlowIncome));
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("所属流水", flowName);
            map.put("标题", capitalFlowIncome.getTitle());
            map.put("金额", capitalFlowIncome.getAmount());
            map.put("备注", capitalFlowIncome.getRemarks());
            map.put("创建者", capitalFlowIncome.getCreateBy());
            map.put("创建时间", capitalFlowIncome.getCreateTime());
            map.put("更新者", capitalFlowIncome.getUpdateBy());
            map.put("更新时间", capitalFlowIncome.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    /**
     * 流水名称
     * @param resources
     * @return
     */
    private String flowName(CapitalFlowIncome resources) {
        // 查询资金流水数据
        CapitalFlowDto capitalFlowDto = capitalFlowServiceImpl.findById(resources.getFlowId());
        // 查询年度账户数据
        CapitalAccountCompanyDto capitalAccountCompanyDto = capitalAccountCompanyServiceImpl.findById(capitalFlowDto.getAccountCompanyId());
        return new SimpleDateFormat("yyyy-MM").format(capitalFlowDto.getFlowYearMonth()) + " - " + capitalAccountCompanyDto.getAccountName();
    }
}