赵士淞
2023-04-28 5a31047fa94abde105ee964a70891dd975403f89
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
*  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.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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.exception.BadRequestException;
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.service.ProjectPerformanceDetailService;
import me.zhengjie.modules.project.service.dto.ProjectPerformanceDetailQueryCriteria;
import me.zhengjie.modules.salary.domain.Salary;
import me.zhengjie.modules.salary.repository.SalaryRepository;
import me.zhengjie.modules.salary.service.SalaryService;
import me.zhengjie.modules.salary.service.dto.SalaryDto;
import me.zhengjie.modules.salary.service.dto.SalaryQueryCriteria;
import me.zhengjie.modules.salary.service.mapstruct.SalaryMapper;
import me.zhengjie.modules.system.domain.SysConfigHoliday;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.repository.DictDetailRepository;
import me.zhengjie.modules.system.repository.SysConfigHolidayRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.service.RoleService;
import me.zhengjie.modules.system.service.SysConfigIndividualIncomeTaxService;
import me.zhengjie.modules.system.service.SysUserLevelInsuranceService;
import me.zhengjie.modules.system.service.SysUserLevelOtherService;
import me.zhengjie.modules.system.service.SysUserLevelWageService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.modules.system.service.dto.SysConfigHolidayQueryCriteria;
import me.zhengjie.modules.system.service.dto.SysConfigIndividualIncomeTaxDto;
import me.zhengjie.modules.system.service.dto.SysConfigIndividualIncomeTaxQueryCriteria;
import me.zhengjie.modules.system.service.dto.SysUserLevelInsuranceDto;
import me.zhengjie.modules.system.service.dto.SysUserLevelInsuranceQueryCriteria;
import me.zhengjie.modules.system.service.dto.SysUserLevelOtherDto;
import me.zhengjie.modules.system.service.dto.SysUserLevelOtherQueryCriteria;
import me.zhengjie.modules.system.service.dto.SysUserLevelWageDto;
import me.zhengjie.modules.system.service.dto.SysUserLevelWageQueryCriteria;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.dto.UserQueryCriteria;
import me.zhengjie.modules.task.service.TaskDaliyService;
import me.zhengjie.modules.task.service.dto.TaskDaliyQueryCriteria;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.ValidationUtil;
 
/**
* @website https://eladmin.vip
* @description 服务实现
* @author 赵士淞
* @date 2023-01-03
**/
@Service
@RequiredArgsConstructor
public class SalaryServiceImpl implements SalaryService {
 
    private final SalaryRepository salaryRepository;
    private final SalaryMapper salaryMapper;
    private final UserRepository userRepository;
    private final SysConfigHolidayRepository sysConfigHolidayRepository;
    private final RoleService roleService;
    private final DictDetailRepository dictDetailRepository;
    // ==================== 以下为定时生成工资记录主要依赖 ====================
    private final UserService userService;
    private final SysUserLevelWageService sysUserLevelWageService;
    private final SysUserLevelInsuranceService sysUserLevelInsuranceService;
    private final SysUserLevelOtherService sysUserLevelOtherService;
    private final AttendanceService attendanceService;
    private final TaskDaliyService taskDaliyService;
    private final SysConfigIndividualIncomeTaxService sysConfigIndividualIncomeTaxService;
    private final ProjectPerformanceDetailService projectPerformanceDetailService;
    // ==================== 以上为定时生成工资记录主要依赖 ====================
 
    @Override
    public Map<String,Object> queryAll(SalaryQueryCriteria criteria, Pageable pageable){
        // 判断工资查询类工资年月是否为空
        if (criteria.getSalaryYearMonth() != null && !"".equals(criteria.getSalaryYearMonth())) {
            // 工资年月:国际时间转中国时间
            criteria.setSalaryYearMonth(DateUtil.internationalTimeToChinaTime(criteria.getSalaryYearMonth()));
        }
        // 获取登录用户角色等级
        List<Integer> levels = roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList());
        // 登录用户最大角色等级
        int min = Collections.min(levels);
        // 判断角色等级
        if (min == Constant.ROLE_LEVEL_MANAGER || min == Constant.ROLE_LEVEL_LEADER || min == Constant.ROLE_LEVEL_ORDINARY) {
            // 工资查询类
            criteria.setUserId(SecurityUtils.getCurrentUserId());
            criteria.setPublicityStatus(Constant.PUBLICITY_STATUS_YES);
        }
        Page<Salary> page = salaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        // 将实体类转换为DTO(重写)
        Function<Salary, SalaryDto> func = new Function<Salary, SalaryDto>(){
            @Override
            public SalaryDto apply(Salary t) {
                // 将实体类转换成DTO
                SalaryDto salaryDto = salaryMapper.toDto(t);
                // 根据用户ID查询用户信息
                User user = userRepository.findById(salaryDto.getUserId()).orElseGet(User::new);
                ValidationUtil.isNull(user.getId(),"User","id",salaryDto.getUserId());
                // 用户昵称
                salaryDto.setUserNickName(user.getNickName());
                // 返回处理好的DTO
                return salaryDto;
            }
        };
        return PageUtil.toPage(page.map(func));
    }
 
    @Override
    public List<SalaryDto> queryAll(SalaryQueryCriteria criteria){
        return salaryMapper.toDto(salaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }
 
    @Override
    @Transactional
    public SalaryDto findById(Long id) {
        Salary salary = salaryRepository.findById(id).orElseGet(Salary::new);
        ValidationUtil.isNull(salary.getId(),"Salary","id",id);
        return salaryMapper.toDto(salary);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SalaryDto create(Salary resources) {
        // 根据用户ID和工资年月查询
        List<Salary> salaryList = salaryRepository.findByUserIdAndSalaryYearMonth(resources.getUserId(), resources.getSalaryYearMonth());
        // 判断列表长度
        if (salaryList.size() > 0) {
            throw new BadRequestException("找到相同用户相同年月的工资记录!");
        }
        return salaryMapper.toDto(salaryRepository.save(resources));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Salary resources) {
        // 根据用户ID和工资年月查询
        List<Salary> salaryList = salaryRepository.findByUserIdAndSalaryYearMonth(resources.getUserId(), resources.getSalaryYearMonth());
        // 判断列表长度
        if (salaryList.size() > 1) {
            throw new BadRequestException("找到相同用户相同年月的工资记录!");
        } else if (salaryList.size() == 1) {
            // 判断是否是同一条记录
            if (salaryList.get(0).getId().longValue() != resources.getId().longValue()) {
                throw new BadRequestException("找到相同用户相同年月的工资记录!");
            }
        }
        Salary salary = salaryRepository.findById(resources.getId()).orElseGet(Salary::new);
        // 判断承认状态
        if (Constant.APPROVE_STATUS_ACKNOWLEDGED.equals(salary.getApproveStatus())) {
            throw new BadRequestException("已承认的工资记录无法进行修改!");
        }
        ValidationUtil.isNull( salary.getId(),"Salary","id",resources.getId());
        salary.copy(resources);
        salaryRepository.save(salary);
    }
 
    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            salaryRepository.deleteById(id);
        }
    }
 
    @Override
    public void download(List<SalaryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SalaryDto salary : all) {
            String userNickName = salary.getUserId() != null ? userRepository.findById(salary.getUserId()).orElseGet(User::new).getNickName() : "";
            String publicityStatus = salary.getPublicityStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.PUBLICITY_STATUS, salary.getPublicityStatus()).getLabel() : "";
            String approveStatus = salary.getApproveStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.APPROVE_STATUS, salary.getApproveStatus()).getLabel() : "";
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("员工姓名", userNickName);
            map.put("工资年月", salary.getSalaryYearMonth());
            map.put("级别工资", salary.getLevelWage());
            map.put("基本工资比例", salary.getBasicWageProportion());
            map.put("基本工资", salary.getBasicWage());
            map.put("全勤小时数", salary.getFullDays());
            map.put("缺勤小时数", salary.getAbsenceDays());
            map.put("实际基础工资", salary.getActualBasicWage());
            map.put("绩效工资比例", salary.getAchievementsWageProportion());
            map.put("绩效工资", salary.getAchievementsWage());
            map.put("应完成绩效工时", salary.getNeedCompletedAchievementsHours());
            map.put("已完成绩效工时", salary.getAlreadyCompletedAchievementsHours());
            map.put("实际绩效工资", salary.getActualAchievementsWage());
            map.put("业绩工资比例", salary.getPerformanceWageProportion());
            map.put("业绩工资", salary.getPerformanceWage());
            map.put("个人业绩比例", salary.getPersonalPerformanceProportion());
            map.put("应完成业绩工时", salary.getNeedCompletedPerformanceHours());
            map.put("已完成业绩工时", salary.getAlreadyCompletedPerformanceHours());
            map.put("实际业绩工资", salary.getActualPerformanceWage());
            map.put("团队工资比例", salary.getTeamWageProportion());
            map.put("团队工资", salary.getTeamWage());
            map.put("实达标团队工时", salary.getActualComplianceTeamHours());
            map.put("已完成团队工时", salary.getAlreadyCompletedTeamHours());
            map.put("实际团队工资", salary.getActualTeamWage());
            map.put("公积金基数", salary.getAccumulationRadix());
            map.put("公积金个人缴纳比例", salary.getAccumulationPersonalProportion());
            map.put("公积金个人缴纳金额", salary.getAccumulationPersonalAmount());
            map.put("公积金公司缴纳比例", salary.getAccumulationCompanyProportion());
            map.put("公积金公司缴纳金额", salary.getAccumulationCompanyAmount());
            map.put("养老险基数", salary.getPensionRadix());
            map.put("养老险个人缴纳比例", salary.getPensionPersonalProportion());
            map.put("养老险个人缴纳金额", salary.getPensionPersonalAmount());
            map.put("养老险公司缴纳比例", salary.getPensionCompanyProportion());
            map.put("养老险公司缴纳金额", salary.getPensionCompanyAmount());
            map.put("医疗险基数", salary.getMedicalRadix());
            map.put("医疗险个人缴纳比例", salary.getMedicalPersonalProportion());
            map.put("医疗险个人缴纳金额", salary.getMedicalPersonalAmount());
            map.put("医疗险公司缴纳比例", salary.getMedicalCompanyProportion());
            map.put("医疗险公司缴纳金额", salary.getMedicalCompanyAmount());
            map.put("失业险基数", salary.getUnemploymentRadix());
            map.put("失业险个人缴纳比例", salary.getUnemploymentPersonalProportion());
            map.put("失业险个人缴纳金额", salary.getUnemploymentPersonalAmount());
            map.put("失业险公司缴纳比例", salary.getUnemploymentCompanyProportion());
            map.put("失业险公司缴纳金额", salary.getUnemploymentCompanyAmount());
            map.put("生育险基数", salary.getMaternityRadix());
            map.put("生育险个人缴纳比例", salary.getMaternityPersonalProportion());
            map.put("生育险个人缴纳金额", salary.getMaternityPersonalAmount());
            map.put("生育险公司缴纳比例", salary.getMaternityCompanyProportion());
            map.put("生育险公司缴纳金额", salary.getMaternityCompanyAmount());
            map.put("工伤险基数", salary.getIndustrialInjuryRadix());
            map.put("工伤险个人缴纳比例", salary.getIndustrialInjuryPersonalProportion());
            map.put("工伤险个人缴纳金额", salary.getIndustrialInjuryPersonalAmount());
            map.put("工伤险公司缴纳比例", salary.getIndustrialInjuryCompanyProportion());
            map.put("工伤险公司缴纳金额", salary.getIndustrialInjuryCompanyAmount());
            map.put("税前金额", salary.getPreTaxAmount());
            map.put("个人所得税", salary.getIndividualIncomeTax());
            map.put("税后金额", salary.getAfterTaxAmount());
            map.put("备注", salary.getRemarks());
            map.put("公示状态", publicityStatus);
            map.put("承认状态", approveStatus);
            map.put("创建者", salary.getCreateBy());
            map.put("创建时间", salary.getCreateTime());
            map.put("更新者", salary.getUpdateBy());
            map.put("更新时间", salary.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
 
    @Override
    public Integer queryWorkDays(SalaryQueryCriteria criteria){
        // 国际时间转中国时间
        String targetYearMonth = DateUtil.internationalTimeToChinaTime(criteria.getTargetYearMonth());
        // 节日查询类
        SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria();
        sysConfigHolidayQueryCriteria.setHolidayDateLike(targetYearMonth.substring(0,7));
        // 获取某月节日数据
        List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,sysConfigHolidayQueryCriteria,criteriaBuilder));
        // 查询某月工作天数
        return DateUtil.getWorkDayOfMonth(sysConfigHolidayList, targetYearMonth);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByUserIdAndSalaryYearMonthAndCreateNew(Salary resources) {
        // 根据用户ID和工资年月删除
        salaryRepository.deleteByUserIdAndSalaryYearMonth(resources.getUserId(), resources.getSalaryYearMonth().toString().substring(0,7));
        // 保存
        salaryRepository.save(resources);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void publicity(Long[] ids) {
        // 循环ID列表
        for (Long id : ids) {
            // 查询工资
            Salary salary = salaryRepository.findById(id).orElseGet(Salary::new);
            ValidationUtil.isNull( salary.getId(),"Salary","id",id);
            // 公示状态
            salary.setPublicityStatus(Constant.PUBLICITY_STATUS_YES);
            // 保存
            salaryRepository.save(salary);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(Long[] ids) {
        // 循环ID列表
        for (Long id : ids) {
            // 查询工资
            Salary salary = salaryRepository.findById(id).orElseGet(Salary::new);
            ValidationUtil.isNull( salary.getId(),"Salary","id",id);
            // 承认状态
            salary.setApproveStatus(Constant.APPROVE_STATUS_ACKNOWLEDGED);
            // 保存
            salaryRepository.save(salary);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generate(SalaryQueryCriteria criteria) {
        // 用户工资结算
        userSalarySettlement(criteria.getUserId(), DateUtil.internationalTimeToChinaTime(criteria.getTargetYearMonth()));
    }
 
    // ==================== 以下为定时生成工资记录主要逻辑 ====================
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userSalarySettlement(Long userId, String targetYearMonth) {
        // 判断用户ID是否为空
        if (userId != null) {
            // 单个用户工资结算
            singleUserSalarySettlement(userId, targetYearMonth);
        } else {
            // 用户检索类
            UserQueryCriteria userQueryCriteria = new UserQueryCriteria();
            userQueryCriteria.setEnabled(true);
            // 获取用户列表
            List<UserDto> userDtoList = userService.queryAll(userQueryCriteria);
            // 循环用户列表
            for (int i = 0; i < userDtoList.size(); i ++) {
                try {
                    // 单个用户工资结算
                    singleUserSalarySettlement(userDtoList.get(i).getId(), targetYearMonth);
                } catch (Exception e) {
                    // 跳过本次循环
                    continue;
                }
            }
        }
    }
 
    // 单个用户工资结算
    private void singleUserSalarySettlement(Long userId, String targetYearMonth) {
        // 日历类
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, Integer.valueOf(targetYearMonth.substring(0,4)));
        // 设置月份
        cal.set(Calendar.MONTH, Integer.valueOf(targetYearMonth.substring(5,7)) - 1);
        // 设置日份
        cal.set(Calendar.DAY_OF_MONTH, 1);
        // 设置时数
        cal.set(Calendar.HOUR_OF_DAY, 0);
        // 设置分数
        cal.set(Calendar.MINUTE, 0);
        // 设置秒数
        cal.set(Calendar.SECOND, 0);
        // 设置毫秒数
        cal.set(Calendar.MILLISECOND, 0);
        // 工资实体类
        Salary salary = new Salary();
        salary.setUserId(userId);
        salary.setSalaryYearMonth(new Timestamp(cal.getTime().getTime()));
        salary.setPublicityStatus(Constant.PUBLICITY_STATUS_NOT);
        salary.setApproveStatus(Constant.APPROVE_STATUS_UNRECOGNIZED);
        // 获取用户级别工资
        salary = getUserLevelWage(userId, targetYearMonth, salary);
        // 获取用户级别保险
        salary = getUserLevelInsurance(userId, targetYearMonth, salary);
        // 获取用户级别其他
        salary = getUserLevelOther(userId, targetYearMonth, salary);
        // 获取工作天数
        salary = getWorkDays(targetYearMonth, salary);
        // 获取出勤小时数
        salary = getAttendanceHours(userId, targetYearMonth, salary);
        // 获取达标工时
        salary = getComplianceWork(userId, targetYearMonth, salary);
        // 获取已完成工时
        salary = getCompletedWork(userId, targetYearMonth, salary);
        try {
            // 保存
            create(salary);
        } catch (Exception e) {
            // 删除
            deleteByUserIdAndSalaryYearMonthAndCreateNew(salary);
        }
    }
 
    // 获取用户级别工资
    private Salary getUserLevelWage(Long userId, String targetYearMonth, Salary salary) {
        // 用户级别工资检索类
        SysUserLevelWageQueryCriteria sysUserLevelWageQueryCriteria = new SysUserLevelWageQueryCriteria();
        sysUserLevelWageQueryCriteria.setUserId(userId);
        sysUserLevelWageQueryCriteria.setTargetYearMonth(targetYearMonth);
        // 查询指定年月用户级别工资
        SysUserLevelWageDto sysUserLevelWageDto = sysUserLevelWageService.queryTargetYearMonth(sysUserLevelWageQueryCriteria);
        // 级别工资
        salary.setLevelWage(sysUserLevelWageDto.getLevelWage());
        // 基本工资比例
        salary.setBasicWageProportion(sysUserLevelWageDto.getBasicWageProportion());
        // 预计基本工资
        salary.setBasicWage(sysUserLevelWageDto.getLevelWage().multiply(new BigDecimal(sysUserLevelWageDto.getBasicWageProportion() / 100)));
        // 绩效工资比例
        salary.setAchievementsWageProportion(sysUserLevelWageDto.getAchievementsWageProportion());
        // 预计绩效工资
        salary.setAchievementsWage(sysUserLevelWageDto.getLevelWage().multiply(new BigDecimal(sysUserLevelWageDto.getAchievementsWageProportion() / 100)));
        // 业绩工资比例
        salary.setPerformanceWageProportion(sysUserLevelWageDto.getPerformanceWageProportion());
        // 预计业绩工资
        salary.setPerformanceWage(sysUserLevelWageDto.getLevelWage().multiply(new BigDecimal(sysUserLevelWageDto.getPerformanceWageProportion() / 100)));
        // 团队工资比例
        salary.setTeamWageProportion(sysUserLevelWageDto.getTeamWageProportion());
        // 预计团队工资
        salary.setTeamWage(sysUserLevelWageDto.getLevelWage().multiply(new BigDecimal(sysUserLevelWageDto.getTeamWageProportion() / 100)));
        // 返回
        return salary;
    }
 
    // 获取用户级别保险
    private Salary getUserLevelInsurance(Long userId, String targetYearMonth, Salary salary) {
        // 用户级别保险查询类
        SysUserLevelInsuranceQueryCriteria sysUserLevelInsuranceQueryCriteria = new SysUserLevelInsuranceQueryCriteria();
        sysUserLevelInsuranceQueryCriteria.setUserId(userId);
        sysUserLevelInsuranceQueryCriteria.setTargetYearMonth(targetYearMonth);
        // 查询指定年月用户级别保险
        SysUserLevelInsuranceDto sysUserLevelInsuranceDto = sysUserLevelInsuranceService.queryTargetYearMonth(sysUserLevelInsuranceQueryCriteria);
        // 公积金基数
        salary.setAccumulationRadix(sysUserLevelInsuranceDto.getAccumulationRadix());
        // 公积金个人缴纳比例
        salary.setAccumulationPersonalProportion(sysUserLevelInsuranceDto.getAccumulationPersonalProportion());
        // 公积金个人缴纳金额
        salary.setAccumulationPersonalAmount(sysUserLevelInsuranceDto.getAccumulationRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getAccumulationPersonalProportion() / 100)));
        // 公积金公司缴纳比例
        salary.setAccumulationCompanyProportion(sysUserLevelInsuranceDto.getAccumulationCompanyProportion());
        // 公积金公司缴纳金额
        salary.setAccumulationCompanyAmount(sysUserLevelInsuranceDto.getAccumulationRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getAccumulationCompanyProportion() / 100)));
        // 养老险基数
        salary.setPensionRadix(sysUserLevelInsuranceDto.getPensionRadix());
        // 养老险个人缴纳比例
        salary.setPensionPersonalProportion(sysUserLevelInsuranceDto.getPensionPersonalProportion());
        // 养老险个人缴纳金额
        salary.setPensionPersonalAmount(sysUserLevelInsuranceDto.getPensionRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getPensionPersonalProportion() / 100)));
        // 养老险公司缴纳比例
        salary.setPensionCompanyProportion(sysUserLevelInsuranceDto.getPensionCompanyProportion());
        // 养老险公司缴纳金额
        salary.setPensionCompanyAmount(sysUserLevelInsuranceDto.getPensionRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getPensionCompanyProportion() / 100)));
        // 医疗险基数
        salary.setMedicalRadix(sysUserLevelInsuranceDto.getMedicalRadix());
        // 医疗险个人缴纳比例
        salary.setMedicalPersonalProportion(sysUserLevelInsuranceDto.getMedicalPersonalProportion());
        // 医疗险个人缴纳金额
        salary.setMedicalPersonalAmount(sysUserLevelInsuranceDto.getMedicalRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMedicalPersonalProportion() / 100)));
        // 医疗险公司缴纳比例
        salary.setMedicalCompanyProportion(sysUserLevelInsuranceDto.getMedicalCompanyProportion());
        // 医疗险公司缴纳金额
        salary.setMedicalCompanyAmount(sysUserLevelInsuranceDto.getMedicalRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMedicalCompanyProportion() / 100)));
        // 失业险基数
        salary.setUnemploymentRadix(sysUserLevelInsuranceDto.getUnemploymentRadix());
        // 失业险个人缴纳比例
        salary.setUnemploymentPersonalProportion(sysUserLevelInsuranceDto.getUnemploymentPersonalProportion());
        // 失业险个人缴纳金额
        salary.setUnemploymentPersonalAmount(sysUserLevelInsuranceDto.getUnemploymentRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getUnemploymentPersonalProportion() / 100)));
        // 失业险公司缴纳比例
        salary.setUnemploymentCompanyProportion(sysUserLevelInsuranceDto.getUnemploymentCompanyProportion());
        // 失业险公司缴纳金额
        salary.setUnemploymentCompanyAmount(sysUserLevelInsuranceDto.getUnemploymentRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getUnemploymentCompanyProportion() / 100)));
        // 生育险基数
        salary.setMaternityRadix(sysUserLevelInsuranceDto.getMaternityRadix());
        // 生育险个人缴纳比例
        salary.setMaternityPersonalProportion(sysUserLevelInsuranceDto.getMaternityPersonalProportion());
        // 生育险个人缴纳金额
        salary.setMaternityPersonalAmount(sysUserLevelInsuranceDto.getMaternityRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMaternityPersonalProportion() / 100)));
        // 生育险公司缴纳比例
        salary.setMaternityCompanyProportion(sysUserLevelInsuranceDto.getMaternityCompanyProportion());
        // 生育险公司缴纳金额
        salary.setMaternityCompanyAmount(sysUserLevelInsuranceDto.getMaternityRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMaternityCompanyProportion() / 100)));
        // 工伤险基数
        salary.setIndustrialInjuryRadix(sysUserLevelInsuranceDto.getIndustrialInjuryRadix());
        // 工伤险个人缴纳比例
        salary.setIndustrialInjuryPersonalProportion(sysUserLevelInsuranceDto.getIndustrialInjuryPersonalProportion());
        // 工伤险个人缴纳金额
        salary.setIndustrialInjuryPersonalAmount(sysUserLevelInsuranceDto.getIndustrialInjuryRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getIndustrialInjuryPersonalProportion() / 100)));
        // 工伤险公司缴纳比例
        salary.setIndustrialInjuryCompanyProportion(sysUserLevelInsuranceDto.getIndustrialInjuryCompanyProportion());
        // 工伤险公司缴纳金额
        salary.setIndustrialInjuryCompanyAmount(sysUserLevelInsuranceDto.getIndustrialInjuryRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getIndustrialInjuryCompanyProportion() / 100)));
        // 返回
        return salary;
    }
 
    // 获取用户级别其他
    private Salary getUserLevelOther(Long userId, String targetYearMonth, Salary salary) {
        // 用户级别其他查询类
        SysUserLevelOtherQueryCriteria sysUserLevelOtherQueryCriteria = new SysUserLevelOtherQueryCriteria();
        sysUserLevelOtherQueryCriteria.setUserId(userId);
        sysUserLevelOtherQueryCriteria.setTargetYearMonth(targetYearMonth);
        // 查询指定年月用户级别其他
        SysUserLevelOtherDto sysUserLevelOtherDto = sysUserLevelOtherService.queryTargetYearMonth(sysUserLevelOtherQueryCriteria);
        // 个人业绩比例
        salary.setPersonalPerformanceProportion(sysUserLevelOtherDto.getPersonalPerformanceProportion());
        // 返回
        return salary;
    }
 
    // 获取工作天数
    private Salary getWorkDays(String targetYearMonth, Salary salary) {
        // 工资查询类
        SalaryQueryCriteria salaryQueryCriteria = new SalaryQueryCriteria();
        salaryQueryCriteria.setTargetYearMonth(targetYearMonth);
        // 查询某月工作天数
        Integer workDays = queryWorkDays(salaryQueryCriteria);
        // 全勤小时数
        salary.setFullDays(Double.valueOf(workDays * 8));
        // 应完成绩效工时
        salary.setNeedCompletedAchievementsHours(Double.valueOf(workDays * 8));
        // 定时器
        while (true) {
            // 判断个人业绩比例是否为空
            if (salary.getPersonalPerformanceProportion() != null) {
                // 应完成业绩工时
                salary.setNeedCompletedPerformanceHours(Double.valueOf(workDays * 8 * (1 + salary.getPersonalPerformanceProportion() / 100)));
                // 清空定时器
                break;
            }
        }
        // 返回
        return salary;
    }
 
    // 获取出勤小时数
    private Salary getAttendanceHours(Long userId, String targetYearMonth, Salary salary) {
        // 考勤查询类
        AttendanceQueryCriteria attendanceQueryCriteria = new AttendanceQueryCriteria();
        attendanceQueryCriteria.setUserId(userId);
        attendanceQueryCriteria.setTargetYearMonth(targetYearMonth);
        // 查询某月出勤小时数
        Double attendanceHours = attendanceService.queryAttendanceHours(attendanceQueryCriteria);
        // 定时器
        while (true) {
            // 判断预计基础工资、全勤小时数是否为空
            if (salary.getBasicWage() != null && salary.getFullDays() != null) {
                // 缺勤小时数
                salary.setAbsenceDays(salary.getFullDays() > attendanceHours ? Double.valueOf(salary.getFullDays() - attendanceHours) : 0);
                // 小时工资
                BigDecimal hourWage = salary.getBasicWage().divide(new BigDecimal(salary.getFullDays()), 2);
                // 缺勤小时数
                BigDecimal absenceDays = new BigDecimal(salary.getAbsenceDays());
                // 实际基础工资
                salary.setActualBasicWage(salary.getBasicWage().subtract(hourWage.multiply(absenceDays)));
                // 清空定时器
                break;
            }
        }
        // 返回
        return salary;
    }
 
    // 获取达标工时
    private Salary getComplianceWork(Long userId, String targetYearMonth, Salary salary) {
        // 任务日报查询类
        ProjectPerformanceDetailQueryCriteria taskDaliyQueryCriteria = new ProjectPerformanceDetailQueryCriteria();
        taskDaliyQueryCriteria.setUserId(userId);
        taskDaliyQueryCriteria.setYearMonth(Timestamp.valueOf(targetYearMonth + " 00:00:00"));
        // 查询某月已达标工时
        Double complianceWork = projectPerformanceDetailService.queryComplianceWork(taskDaliyQueryCriteria);
        // 实达标团队工时
        salary.setActualComplianceTeamHours(complianceWork);
        // 返回
        return salary;
    }
 
    // 获取已完成工时
    private Salary getCompletedWork(Long userId, String targetYearMonth, Salary salary) {
        // 任务日报查询类
        TaskDaliyQueryCriteria taskDaliyQueryCriteria = new TaskDaliyQueryCriteria();
        taskDaliyQueryCriteria.setUserId(userId);
        taskDaliyQueryCriteria.setTargetYearMonth(targetYearMonth);
        // 查询某月已完成工时
        Double completedWork = taskDaliyService.queryCompletedWork(taskDaliyQueryCriteria, false);
        // 已完成绩效工时
        salary.setAlreadyCompletedAchievementsHours(completedWork);
        // 已完成业绩工时
        salary.setAlreadyCompletedPerformanceHours(completedWork);
        // 已完成团队工时
        salary.setAlreadyCompletedTeamHours(completedWork);
 
        Double otherWage = taskDaliyService.queryOtherWage(taskDaliyQueryCriteria);
        salary.setPieceRate(BigDecimal.valueOf(otherWage));
        // 绩效定时器
        while (true) {
            // 判断预计绩效工资、应完成绩效工时是否为空
            if (salary.getAchievementsWage() != null && salary.getNeedCompletedAchievementsHours() != null) {
                // 实际绩效工资
                salary.setActualAchievementsWage(salary.getAchievementsWage().multiply(new BigDecimal(completedWork / salary.getNeedCompletedAchievementsHours())));
                // 清空绩效定时器
                break;
            }
        }
        // 业绩定时器
        while (true) {
            // 判断预计业绩工资、应完成业绩工时是否为空
            if (salary.getPerformanceWage() != null && salary.getNeedCompletedPerformanceHours() != null) {
                // 实际业绩工资
                salary.setActualPerformanceWage(completedWork >= salary.getNeedCompletedPerformanceHours() ? salary.getPerformanceWage() : new BigDecimal(0));
                // 清空业绩定时器
                break;
            }
        }
        // 团队定时器
        while (true) {
            // 判断预计团队工资、实达标团队工时是否为空
            if (salary.getTeamWage() != null && salary.getActualComplianceTeamHours() != null) {
                // 实际团队工资
                salary.setActualTeamWage(salary.getActualComplianceTeamHours() != 0 ? salary.getTeamWage().multiply(new BigDecimal(salary.getActualComplianceTeamHours() / completedWork)) : new BigDecimal(0));
                // 清空团队定时器
                break;
            }
        }
        // 统计定时器
        while (true) {
            // 判断 实际基础工资、实际绩效工资、实际业绩工资、实际团队工资、公积金个人缴纳金额、养老险个人缴纳金额、
            // 医疗险个人缴纳金额、失业险个人缴纳金额、生育险个人缴纳金额、工伤险个人缴纳金额 是否为空
            if (
                    salary.getActualBasicWage() != null &&
                    salary.getActualAchievementsWage() != null &&
                    salary.getActualPerformanceWage() != null &&
                    salary.getActualTeamWage() != null &&
                    salary.getAccumulationPersonalAmount() != null &&
                    salary.getPensionPersonalAmount() != null &&
                    salary.getMedicalPersonalAmount() != null &&
                    salary.getUnemploymentPersonalAmount() != null &&
                    salary.getMaternityPersonalAmount() != null &&
                    salary.getIndustrialInjuryPersonalAmount() != null
            ) {
                // 税前金额
                salary.setPreTaxAmount(salary.getActualBasicWage().add(salary.getActualAchievementsWage()).add(salary.getActualPerformanceWage()).add(salary.getActualTeamWage()));
                // 查询个人所得税列表
                List<SysConfigIndividualIncomeTaxDto> sysConfigIndividualIncomeTaxDtoList = sysConfigIndividualIncomeTaxService.queryAll(new SysConfigIndividualIncomeTaxQueryCriteria());
                // 税额
                BigDecimal taxAmount = new BigDecimal(0);
                // 计税金额
                BigDecimal taxableAmount = salary.getPreTaxAmount().subtract(salary.getAccumulationPersonalAmount()).subtract(salary.getPensionPersonalAmount())
                        .subtract(salary.getMedicalPersonalAmount()).subtract(salary.getUnemploymentPersonalAmount()).subtract(salary.getMaternityPersonalAmount())
                        .subtract(salary.getIndustrialInjuryPersonalAmount());
                // 循环个人所得税列表
                for (int i = 0; i < sysConfigIndividualIncomeTaxDtoList.size(); i ++) {
                    // 判断计税金额所在个人所得税范围
                    if (sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountEnd() != null &&
                            taxableAmount.compareTo(sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountEnd()) == 1) {
                        // 税额
                        taxAmount.add(
                                (sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountEnd().subtract(sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountStart()))
                                        .multiply(new BigDecimal(sysConfigIndividualIncomeTaxDtoList.get(i).getTaxRate()).divide(new BigDecimal(100), 2))
                        );
                    } else if (
                            (taxableAmount.compareTo(sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountStart()) == 1 ||
                                    taxableAmount.compareTo(sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountStart()) == 0) &&
                                    (sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountEnd() == null ||
                                            taxableAmount.compareTo(sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountEnd()) == -1 ||
                                            taxableAmount.compareTo(sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountEnd()) == 0)
                    ) {
                        // 税额
                        taxAmount.add(
                                (taxableAmount.subtract(sysConfigIndividualIncomeTaxDtoList.get(i).getGearAmountStart()))
                                        .multiply(new BigDecimal(sysConfigIndividualIncomeTaxDtoList.get(i).getTaxRate()).divide(new BigDecimal(100), 2))
                        );
                    }
                    // 判断循环是否结束
                    if (i == sysConfigIndividualIncomeTaxDtoList.size() - 1) {
                        // 个人所得税
                        salary.setIndividualIncomeTax(taxAmount);
                        // 税后金额
                        salary.setAfterTaxAmount(taxableAmount.subtract(taxAmount).compareTo(new BigDecimal(0)) == -1 ? new BigDecimal(0) : taxableAmount.subtract(taxAmount));
                    }
                }
                // 清空统计定时器
                break;
            }
        }
        // 返回
        return salary;
    }
    // ==================== 以上为定时生成工资记录主要逻辑 ====================
}