yaozc
2023-07-10 8d965e584b9be28a81d3f9fa587f6d0b18cd8dff
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
/*
*  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.treatment.service.impl;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
 
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.DeptRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.treatment.domain.CmnInsurance;
import me.zhengjie.modules.treatment.domain.UserInsurance;
import me.zhengjie.modules.treatment.repository.CmnInsuranceRepository;
import me.zhengjie.modules.treatment.repository.UserInsuranceRepository;
import me.zhengjie.modules.treatment.service.UserInsuranceService;
import me.zhengjie.modules.treatment.service.dto.UserInsuranceDto;
import me.zhengjie.modules.treatment.service.dto.UserInsuranceGenerateParam;
import me.zhengjie.modules.treatment.service.dto.UserInsuranceQueryCriteria;
import me.zhengjie.modules.treatment.service.mapstruct.UserInsuranceGenMapper;
import me.zhengjie.modules.treatment.service.mapstruct.UserInsuranceMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
 
/**
* @website https://eladmin.vip
* @description 服务实现
* @author 姚增春
* @date 2023-05-07
**/
@Service
@RequiredArgsConstructor
public class UserInsuranceServiceImpl implements UserInsuranceService {
 
    private final UserInsuranceRepository userInsuranceRepository;
    private final CmnInsuranceRepository cmnInsuranceRepository;
    private final UserInsuranceMapper userInsuranceMapper;
    private final UserInsuranceGenMapper userInsuranceGenMapper;
    private final UserRepository userRepository;
    private final DeptRepository deptRepository;
    private final DeptService deptService;
 
    @Override
    public Map<String, Object> queryAll(UserInsuranceQueryCriteria criteria, Pageable pageable) {
        Page<UserInsurance> page = userInsuranceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder), pageable);
        // 将实体类转换为DTO(重写)
        Function<UserInsurance, UserInsuranceDto> func = new Function<UserInsurance, UserInsuranceDto>() {
            @Override
            public UserInsuranceDto apply(UserInsurance t) {
                // 将实体类转换成DTO
                UserInsuranceDto userInsuranceDto = userInsuranceMapper.toDto(t);
                // 根据用户ID查询用户信息
                User user = userRepository.findById(userInsuranceDto.getUserId()).orElseGet(User::new);
                ValidationUtil.isNull(user.getId(),"User","id",userInsuranceDto.getUserId());
                // 用户昵称
                userInsuranceDto.setNickName(user.getNickName());
                // 返回处理好的DTO
                return userInsuranceDto;
            }
        };
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<UserInsuranceDto> queryAll(UserInsuranceQueryCriteria criteria){
        return userInsuranceMapper.toDto(userInsuranceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public UserInsuranceDto findById(Long id) {
        UserInsurance userInsurance = userInsuranceRepository.findById(id).orElseGet(UserInsurance::new);
        ValidationUtil.isNull(userInsurance.getId(),"UserInsurance","id",id);
        return userInsuranceMapper.toDto(userInsurance);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserInsuranceDto create(UserInsurance resources) {
        return userInsuranceMapper.toDto(userInsuranceRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserInsurance resources) {
        UserInsurance userInsurance = userInsuranceRepository.findById(resources.getId()).orElseGet(UserInsurance::new);
        ValidationUtil.isNull( userInsurance.getId(),"UserInsurance","id",resources.getId());
        userInsurance.copy(resources);
        userInsuranceRepository.save(userInsurance);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            userInsuranceRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<UserInsuranceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserInsuranceDto userInsurance : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户ID", userInsurance.getUserId());
            map.put("年月", userInsurance.getYearMonth());
            map.put("公积金基数", userInsurance.getAccumulationFund());
            map.put("公积金个人缴纳比例", userInsurance.getAccumulationFundPersonal());
            map.put("公积金公司缴纳比例", userInsurance.getAccumulationFundCompany());
            map.put("养老险基数", userInsurance.getRetirementInsurance());
            map.put("养老险个人缴纳比例", userInsurance.getRetirementInsurancePersonal());
            map.put("养老险公司缴纳比例", userInsurance.getRetirementInsuranceCompany());
            map.put("医疗险基数", userInsurance.getMedicalInsurance());
            map.put("医疗险个人缴纳比例", userInsurance.getMedicalInsurancePersonal());
            map.put("医疗险公司缴纳比例", userInsurance.getMedicalInsuranceCompany());
            map.put("失业险基数", userInsurance.getUnemploymentInsurance());
            map.put("失业险个人缴纳比例", userInsurance.getUnemploymentInsurancePersonal());
            map.put("失业险公司缴纳比例", userInsurance.getUnemploymentInsuranceCompany());
            map.put("生育险基数", userInsurance.getBirthInsurance());
            map.put("生育险个人缴纳比例", userInsurance.getBirthInsurancePersonal());
            map.put("生育险公司缴纳比例", userInsurance.getBirthInsuranceCompany());
            map.put("工伤险基数", userInsurance.getEmploymentInsurance());
            map.put("工伤险个人缴纳比例", userInsurance.getEmploymentInsurancePersonal());
            map.put("工伤险公司缴纳比例", userInsurance.getEmploymentInsuranceCompany());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    @Override
    @Transactional
    public void generate(UserInsuranceGenerateParam param) {
        List<User> userList = null;
        List<Long> deptIds = param.getNodes().stream().filter(node -> node.getLeaf() == false).map(node -> node.getId()).collect(Collectors.toList());
        List<Long> deptIdAlls = deptService.getDeptChildren(deptRepository.findByIdIn(deptIds));
        userList = userRepository.findByDeptIdInAndEnabled(deptIdAlls, true);
//        if (param.getUserIds() != null && param.getUserIds().size() > 0) {
//            userList = userRepository.findByIdIn(param.getUserIds());
//        } else {
//            userList = userRepository.findAllByEnabled(true);
//        }
        List<Long> userIds = param.getNodes().stream().filter(node -> node.getLeaf() == true).map(node -> node.getId()).collect(Collectors.toList());
        userList.addAll(userRepository.findByIdIn(userIds));
        userList.stream().forEach(user -> {
            Optional<CmnInsurance> cmnInsuranceOpt = cmnInsuranceRepository.findByCompany(user.getCompany());
            if (cmnInsuranceOpt.isPresent()) {
                UserInsurance userInsuranceTarget = userInsuranceRepository.findByUserIdAndYearMonth(user.getId(), param.getTargetYearMonth()).orElseGet(UserInsurance::new);
                UserInsurance userInsuranceSrc = userInsuranceGenMapper.cloneInsurance(cmnInsuranceOpt.get());
                userInsuranceTarget.copy(userInsuranceSrc);
                userInsuranceTarget.setUserId(user.getId());
                userInsuranceTarget.setYearMonth(param.getTargetYearMonth());
                userInsuranceRepository.save(userInsuranceTarget);
            }
        });
    }
}