weicd
2024-12-27 0766c42639d0e9b12d8dc2f93589b4713ced57a6
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
*  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.salary.service.impl;
 
import java.io.IOException;
import java.math.BigDecimal;
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.attendance.service.AttendanceService;
import me.zhengjie.modules.attendance.service.dto.AttendanceQueryCriteria;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.project.domain.Project;
import me.zhengjie.modules.project.domain.ProjectCostOther;
import me.zhengjie.modules.project.service.dto.ProjectCostOtherDto;
import me.zhengjie.modules.salary.domain.PayMonthlySalary;
import me.zhengjie.modules.salary.repository.PayMonthlySalaryRepository;
import me.zhengjie.modules.salary.service.PayMonthlySalaryService;
import me.zhengjie.modules.salary.service.dto.PayMonthlySalaryDto;
import me.zhengjie.modules.salary.service.dto.PayMonthlySalaryGenerateParam;
import me.zhengjie.modules.salary.service.dto.PayMonthlySalaryQueryCriteria;
import me.zhengjie.modules.salary.service.mapstruct.PayMonthlySalaryMapper;
import me.zhengjie.modules.system.domain.Job;
import me.zhengjie.modules.system.domain.PerformanceCoefficient;
import me.zhengjie.modules.system.domain.SysConfigHoliday;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.DeptRepository;
import me.zhengjie.modules.system.repository.PerformanceCoefficientRepository;
import me.zhengjie.modules.system.repository.SysConfigHolidayRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.SysConfigHolidayQueryCriteria;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.task.service.TaskDaliyService;
import me.zhengjie.modules.task.service.dto.TaskDaliyQueryCriteria;
import me.zhengjie.modules.treatment.domain.UserInsurance;
import me.zhengjie.modules.treatment.domain.UserLevelWage;
import me.zhengjie.modules.treatment.repository.UserInsuranceRepository;
import me.zhengjie.modules.treatment.repository.UserLevelWageRepository;
import me.zhengjie.utils.DateUtil;
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 PayMonthlySalaryServiceImpl implements PayMonthlySalaryService {
 
    private final PayMonthlySalaryRepository payMonthlySalaryRepository;
    private final PayMonthlySalaryMapper payMonthlySalaryMapper;
    private final UserRepository userRepository;
    private final UserService userService;
    private final DeptRepository deptRepository;
    private final DeptService deptService;
    private final SysConfigHolidayRepository sysConfigHolidayRepository;
    private final AttendanceService attendanceService;
    private final TaskDaliyService taskDaliyService;
    private final UserLevelWageRepository userLevelWageRepository;
    private final UserInsuranceRepository userInsuranceRepository;
    private final PerformanceCoefficientRepository performanceCoefficientRepository;
 
    @Override
    public Map<String,Object> queryAll(PayMonthlySalaryQueryCriteria criteria, Pageable pageable){
        Page<PayMonthlySalary> page = payMonthlySalaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
 
        // 将实体类转换为DTO(重写)
        Function<PayMonthlySalary, PayMonthlySalaryDto> func = new Function<PayMonthlySalary, PayMonthlySalaryDto>() {
            @Override
            public PayMonthlySalaryDto apply(PayMonthlySalary payMonthlySalary) {
                // 将实体类转换成DTO
                PayMonthlySalaryDto payMonthlySalaryDto = payMonthlySalaryMapper.toDto(payMonthlySalary);
                // 根据员工ID查询用户信息
                UserDto userDto = userService.findById(payMonthlySalary.getUserId());
                // 员工名称
                payMonthlySalaryDto.setNickName(userDto.getNickName());
                return payMonthlySalaryDto;
            }
        };
 
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<PayMonthlySalaryDto> queryAll(PayMonthlySalaryQueryCriteria criteria){
        return payMonthlySalaryMapper.toDto(payMonthlySalaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public PayMonthlySalaryDto findById(Long id) {
        PayMonthlySalary payMonthlySalary = payMonthlySalaryRepository.findById(id).orElseGet(PayMonthlySalary::new);
        ValidationUtil.isNull(payMonthlySalary.getId(),"PayMonthlySalary","id",id);
        return payMonthlySalaryMapper.toDto(payMonthlySalary);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PayMonthlySalaryDto create(PayMonthlySalary resources) {
        return payMonthlySalaryMapper.toDto(payMonthlySalaryRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PayMonthlySalary resources) {
        PayMonthlySalary payMonthlySalary = payMonthlySalaryRepository.findById(resources.getId()).orElseGet(PayMonthlySalary::new);
        ValidationUtil.isNull( payMonthlySalary.getId(),"PayMonthlySalary","id",resources.getId());
        payMonthlySalary.copy(resources);
        payMonthlySalaryRepository.save(payMonthlySalary);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            payMonthlySalaryRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<PayMonthlySalaryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PayMonthlySalaryDto payMonthlySalary : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("员工ID", payMonthlySalary.getUserId());
            map.put("基本工资", payMonthlySalary.getBasicWage());
            map.put("业绩工资", payMonthlySalary.getPerformanceWage());
            map.put("公积金基数", payMonthlySalary.getAccumulationRadix());
            map.put("公积金个人缴纳比例", payMonthlySalary.getAccumulationPersonalProportion());
            map.put("公积金个人缴纳金额", payMonthlySalary.getAccumulationPersonalAmount());
            map.put("公积金公司缴纳比例", payMonthlySalary.getAccumulationCompanyProportion());
            map.put("公积金公司缴纳金额", payMonthlySalary.getAccumulationCompanyAmount());
            map.put("养老险基数", payMonthlySalary.getPensionRadix());
            map.put("养老险个人缴纳比例", payMonthlySalary.getPensionPersonalProportion());
            map.put("养老险个人缴纳金额", payMonthlySalary.getPensionPersonalAmount());
            map.put("养老险公司缴纳比例", payMonthlySalary.getPensionCompanyProportion());
            map.put("养老险公司缴纳金额", payMonthlySalary.getPensionCompanyAmount());
            map.put("医疗险基数", payMonthlySalary.getMedicalRadix());
            map.put("医疗险个人缴纳比例", payMonthlySalary.getMedicalPersonalProportion());
            map.put("医疗险个人缴纳金额", payMonthlySalary.getMedicalPersonalAmount());
            map.put("医疗险公司缴纳比例", payMonthlySalary.getMedicalCompanyProportion());
            map.put("医疗险公司缴纳金额", payMonthlySalary.getMedicalCompanyAmount());
            map.put("失业险基数", payMonthlySalary.getUnemploymentRadix());
            map.put("失业险个人缴纳比例", payMonthlySalary.getUnemploymentPersonalProportion());
            map.put("失业险个人缴纳金额", payMonthlySalary.getUnemploymentPersonalAmount());
            map.put("失业险公司缴纳比例", payMonthlySalary.getUnemploymentCompanyProportion());
            map.put("失业险公司缴纳金额", payMonthlySalary.getUnemploymentCompanyAmount());
            map.put("生育险基数", payMonthlySalary.getMaternityRadix());
            map.put("生育险个人缴纳比例", payMonthlySalary.getMaternityPersonalProportion());
            map.put("生育险个人缴纳金额", payMonthlySalary.getMaternityPersonalAmount());
            map.put("生育险公司缴纳比例", payMonthlySalary.getMaternityCompanyProportion());
            map.put("生育险公司缴纳金额", payMonthlySalary.getMaternityCompanyAmount());
            map.put("工伤险基数", payMonthlySalary.getIndustrialInjuryRadix());
            map.put("工伤险个人缴纳比例", payMonthlySalary.getIndustrialInjuryPersonalProportion());
            map.put("工伤险个人缴纳金额", payMonthlySalary.getIndustrialInjuryPersonalAmount());
            map.put("工伤险公司缴纳比例", payMonthlySalary.getIndustrialInjuryCompanyProportion());
            map.put("工伤险公司缴纳金额", payMonthlySalary.getIndustrialInjuryCompanyAmount());
            map.put("备注", payMonthlySalary.getRemarks());
            map.put("公示状态", payMonthlySalary.getPublicityStatus());
            map.put("承认状态", payMonthlySalary.getApproveStatus());
            map.put("创建者", payMonthlySalary.getCreateBy());
            map.put("创建时间", payMonthlySalary.getCreateTime());
            map.put("更新者", payMonthlySalary.getUpdateBy());
            map.put("更新时间", payMonthlySalary.getUpdateTime());
            map.put("工资年月", payMonthlySalary.getYearMonth());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    @Override
    @Transactional
    public void generate(PayMonthlySalaryGenerateParam 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);
 
        // 节日查询类
        SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria();
        sysConfigHolidayQueryCriteria.setHolidayDateLike(param.getTargetYearMonth());
        // 获取某月节日数据
        List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,sysConfigHolidayQueryCriteria,criteriaBuilder));
        // 查询某月工作天数
        Integer fullHours = DateUtil.getWorkDayOfMonth(sysConfigHolidayList, param.getTargetYearMonth()) * 8;
 
        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 -> {
            PayMonthlySalary payMonthlySalary = payMonthlySalaryRepository.findByUserIdAndYearMonth(user.getId(), param.getTargetYearMonth()).orElseGet(PayMonthlySalary::new);
            payMonthlySalary.setUserId(user.getId());
            payMonthlySalary.setYearMonth(param.getTargetYearMonth());
            payMonthlySalary.setFullHours(new Double(fullHours));
 
            // 考勤查询类
            AttendanceQueryCriteria attendanceQueryCriteria = new AttendanceQueryCriteria();
            attendanceQueryCriteria.setUserId(user.getId());
            attendanceQueryCriteria.setTargetYearMonth(param.getTargetYearMonth());
            // 查询某月出勤小时数
            Double attendanceHours = attendanceService.queryAttendanceHours(attendanceQueryCriteria);
            payMonthlySalary.setAttendanceHours(attendanceHours);
 
            UserLevelWage userLevelWage = userLevelWageRepository.findByUserIdAndYearMonth(user.getId(), param.getTargetYearMonth());
 
            if (userLevelWage != null) {
                payMonthlySalary.setBasicWage(userLevelWage.getBasicWage().add(userLevelWage.getRankWage()));
                BigDecimal attendanceWage = payMonthlySalary.getBasicWage().multiply(BigDecimal.valueOf(payMonthlySalary.getAttendanceHours())).divide(BigDecimal.valueOf(payMonthlySalary.getFullHours()));
                payMonthlySalary.setActAttendanceWage(attendanceWage);
 
                // 任务日报查询类
                TaskDaliyQueryCriteria taskDaliyQueryCriteria = new TaskDaliyQueryCriteria();
                taskDaliyQueryCriteria.setUserId(user.getId());
                taskDaliyQueryCriteria.setTargetYearMonth(param.getTargetYearMonth());
                // 查询某月已完成工时
                Double performanceHours = taskDaliyService.queryCompletedWork(taskDaliyQueryCriteria, false);
                payMonthlySalary.setPerformanceHours(performanceHours);
                payMonthlySalary.setPerformanceWage(userLevelWage.getPerformanceWage());
                Double coefficient = performanceHours / fullHours;
 
                Long jobId = user.getJobs().toArray(new Job[0])[0].getId();
                List<PerformanceCoefficient> performanceCoefficientList = performanceCoefficientRepository.findByYearAndJobIdOrderByMultiplesAsc(param.getTargetYearMonth().substring(0, 4), jobId);
                for (PerformanceCoefficient performanceCoefficient : performanceCoefficientList) {
                    if (performanceCoefficient.getMultiples().compareTo(BigDecimal.valueOf(coefficient)) > 0) {
                        payMonthlySalary.setActPerformanceWage(payMonthlySalary.getPerformanceWage().multiply(performanceCoefficient.getMonthCoefficient()));
                    }
                }
            }
 
            Optional<UserInsurance> userInsuranceOpt = userInsuranceRepository.findByUserIdAndYearMonth(user.getId(), param.getTargetYearMonth());
            if (userInsuranceOpt.isPresent()) {
                UserInsurance userInsurance = userInsuranceOpt.get();
                payMonthlySalary.setAccumulationRadix(userInsurance.getAccumulationFund());
                payMonthlySalary.setAccumulationCompanyProportion(userInsurance.getAccumulationFundCompany());
                payMonthlySalary.setAccumulationCompanyAmount(userInsurance.getAccumulationFund().multiply(BigDecimal.valueOf(userInsurance.getAccumulationFundCompany() / 100)));
                payMonthlySalary.setAccumulationPersonalProportion(userInsurance.getAccumulationFundPersonal());
                payMonthlySalary.setAccumulationPersonalAmount(userInsurance.getAccumulationFund().multiply(BigDecimal.valueOf(userInsurance.getAccumulationFundPersonal() / 100)));
 
                payMonthlySalary.setMaternityRadix(userInsurance.getBirthInsurance());
                payMonthlySalary.setMaternityCompanyProportion(userInsurance.getBirthInsuranceCompany());
                payMonthlySalary.setMaternityCompanyAmount(userInsurance.getBirthInsurance().multiply(BigDecimal.valueOf(userInsurance.getBirthInsuranceCompany() / 100)));
                payMonthlySalary.setMaternityPersonalProportion(userInsurance.getBirthInsurancePersonal());
                payMonthlySalary.setMaternityPersonalAmount(userInsurance.getBirthInsurance().multiply(BigDecimal.valueOf(userInsurance.getBirthInsurancePersonal() / 100)));
 
                payMonthlySalary.setIndustrialInjuryRadix(userInsurance.getEmploymentInsurance());
                payMonthlySalary.setIndustrialInjuryCompanyProportion(userInsurance.getEmploymentInsuranceCompany());
                payMonthlySalary.setIndustrialInjuryCompanyAmount(userInsurance.getEmploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getEmploymentInsuranceCompany() / 100)));
                payMonthlySalary.setIndustrialInjuryPersonalProportion(userInsurance.getEmploymentInsurancePersonal());
                payMonthlySalary.setIndustrialInjuryPersonalAmount(userInsurance.getEmploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getEmploymentInsurancePersonal() / 100)));
 
                payMonthlySalary.setMedicalRadix(userInsurance.getMedicalInsurance());
                payMonthlySalary.setMedicalCompanyProportion(userInsurance.getMedicalInsuranceCompany());
                payMonthlySalary.setMedicalCompanyAmount(userInsurance.getMedicalInsurance().multiply(BigDecimal.valueOf(userInsurance.getMedicalInsuranceCompany() / 100)));
                payMonthlySalary.setMedicalPersonalProportion(userInsurance.getMedicalInsurancePersonal());
                payMonthlySalary.setMedicalPersonalAmount(userInsurance.getMedicalInsurance().multiply(BigDecimal.valueOf(userInsurance.getMedicalInsurancePersonal() / 100)));
 
                payMonthlySalary.setPensionRadix(userInsurance.getRetirementInsurance());
                payMonthlySalary.setPensionCompanyProportion(userInsurance.getRetirementInsuranceCompany());
                payMonthlySalary.setPensionCompanyAmount(userInsurance.getRetirementInsurance().multiply(BigDecimal.valueOf(userInsurance.getRetirementInsuranceCompany() / 100)));
                payMonthlySalary.setPensionPersonalProportion(userInsurance.getRetirementInsurancePersonal());
                payMonthlySalary.setPensionPersonalAmount(userInsurance.getRetirementInsurance().multiply(BigDecimal.valueOf(userInsurance.getRetirementInsurancePersonal() / 100)));
 
                payMonthlySalary.setUnemploymentRadix(userInsurance.getUnemploymentInsurance());
                payMonthlySalary.setUnemploymentCompanyProportion(userInsurance.getUnemploymentInsuranceCompany());
                payMonthlySalary.setUnemploymentCompanyAmount(userInsurance.getUnemploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getUnemploymentInsuranceCompany() / 100)));
                payMonthlySalary.setUnemploymentPersonalProportion(userInsurance.getUnemploymentInsurancePersonal());
                payMonthlySalary.setUnemploymentPersonalAmount(userInsurance.getUnemploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getUnemploymentInsurancePersonal() / 100)));
            }
 
            payMonthlySalary.setPublicityStatus(Constant.PUBLICITY_STATUS_NOT);
            payMonthlySalary.setApproveStatus(Constant.APPROVE_STATUS_UNRECOGNIZED);
            payMonthlySalaryRepository.save(payMonthlySalary);
        });
    }
}