/* * 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.text.SimpleDateFormat; import java.time.YearMonth; 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 me.zhengjie.modules.system.domain.SysUserLevelWage; import me.zhengjie.modules.system.repository.SysUserLevelWageRepository; import me.zhengjie.modules.treatment.domain.UserLevelWage; import me.zhengjie.modules.treatment.repository.UserLevelWageRepository; 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 SysUserLevelWageRepository sysUserLevelWageRepository; // ==================== 以下为定时生成工资记录主要依赖 ==================== 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 queryAll(SalaryQueryCriteria criteria, Pageable pageable){ // 判断工资查询类工资年月是否为空 if (criteria.getSalaryYearMonth() != null && !"".equals(criteria.getSalaryYearMonth())) { // 工资年月:国际时间转中国时间 criteria.setSalaryYearMonth(DateUtil.internationalTimeToChinaTime(criteria.getSalaryYearMonth())); } // 获取登录用户角色等级 List 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 page = salaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @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 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 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 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 all, HttpServletResponse response) throws IOException { List> 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 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){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List user = userRepository.findByUserId(criteria.getUserId()); String stTime = sdf.format(user.get(0).getCreateTime()).substring(0,7); if (stTime.equals(criteria.getTargetYearMonth().substring(0,7))) { criteria.setTargetYearMonth(sdf.format(user.get(0).getCreateTime()).substring(0,10)); } // 国际时间转中国时间 String targetYearMonth = DateUtil.internationalTimeToChinaTime(criteria.getTargetYearMonth()); // 节日查询类 SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria(); sysConfigHolidayQueryCriteria.setHolidayDateLike(targetYearMonth.substring(0,7)); // 获取某月节日数据 List 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 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())).divide(new BigDecimal(100))); // 绩效工资比例 salary.setAchievementsWageProportion(sysUserLevelWageDto.getAchievementsWageProportion()); // 预计绩效工资 salary.setAchievementsWage(sysUserLevelWageDto.getLevelWage().multiply(new BigDecimal(sysUserLevelWageDto.getAchievementsWageProportion())).divide(new BigDecimal(100))); // 业绩工资比例 salary.setPerformanceWageProportion(sysUserLevelWageDto.getPerformanceWageProportion()); // 预计业绩工资 salary.setPerformanceWage(sysUserLevelWageDto.getLevelWage().multiply(new BigDecimal(sysUserLevelWageDto.getPerformanceWageProportion())).divide(new BigDecimal(100))); // 团队工资比例 salary.setTeamWageProportion(sysUserLevelWageDto.getTeamWageProportion()); // 预计团队工资 salary.setTeamWage(sysUserLevelWageDto.getLevelWage().multiply(new BigDecimal(sysUserLevelWageDto.getTeamWageProportion())).divide(new BigDecimal(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())).divide(new BigDecimal(100))); // 公积金公司缴纳比例 salary.setAccumulationCompanyProportion(sysUserLevelInsuranceDto.getAccumulationCompanyProportion()); // 公积金公司缴纳金额 salary.setAccumulationCompanyAmount(sysUserLevelInsuranceDto.getAccumulationRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getAccumulationCompanyProportion())).divide(new BigDecimal(100))); // 养老险基数 salary.setPensionRadix(sysUserLevelInsuranceDto.getPensionRadix()); // 养老险个人缴纳比例 salary.setPensionPersonalProportion(sysUserLevelInsuranceDto.getPensionPersonalProportion()); // 养老险个人缴纳金额 salary.setPensionPersonalAmount(sysUserLevelInsuranceDto.getPensionRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getPensionPersonalProportion())).divide(new BigDecimal(100))); // 养老险公司缴纳比例 salary.setPensionCompanyProportion(sysUserLevelInsuranceDto.getPensionCompanyProportion()); // 养老险公司缴纳金额 salary.setPensionCompanyAmount(sysUserLevelInsuranceDto.getPensionRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getPensionCompanyProportion())).divide(new BigDecimal(100))); // 医疗险基数 salary.setMedicalRadix(sysUserLevelInsuranceDto.getMedicalRadix()); // 医疗险个人缴纳比例 salary.setMedicalPersonalProportion(sysUserLevelInsuranceDto.getMedicalPersonalProportion()); // 医疗险个人缴纳金额 salary.setMedicalPersonalAmount(sysUserLevelInsuranceDto.getMedicalRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMedicalPersonalProportion())).divide(new BigDecimal(100))); // 医疗险公司缴纳比例 salary.setMedicalCompanyProportion(sysUserLevelInsuranceDto.getMedicalCompanyProportion()); // 医疗险公司缴纳金额 salary.setMedicalCompanyAmount(sysUserLevelInsuranceDto.getMedicalRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMedicalCompanyProportion())).divide(new BigDecimal(100))); // 失业险基数 salary.setUnemploymentRadix(sysUserLevelInsuranceDto.getUnemploymentRadix()); // 失业险个人缴纳比例 salary.setUnemploymentPersonalProportion(sysUserLevelInsuranceDto.getUnemploymentPersonalProportion()); // 失业险个人缴纳金额 salary.setUnemploymentPersonalAmount(sysUserLevelInsuranceDto.getUnemploymentRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getUnemploymentPersonalProportion())).divide(new BigDecimal(100))); // 失业险公司缴纳比例 salary.setUnemploymentCompanyProportion(sysUserLevelInsuranceDto.getUnemploymentCompanyProportion()); // 失业险公司缴纳金额 salary.setUnemploymentCompanyAmount(sysUserLevelInsuranceDto.getUnemploymentRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getUnemploymentCompanyProportion())).divide(new BigDecimal(100))); // 生育险基数 salary.setMaternityRadix(sysUserLevelInsuranceDto.getMaternityRadix()); // 生育险个人缴纳比例 salary.setMaternityPersonalProportion(sysUserLevelInsuranceDto.getMaternityPersonalProportion()); // 生育险个人缴纳金额 salary.setMaternityPersonalAmount(sysUserLevelInsuranceDto.getMaternityRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMaternityPersonalProportion())).divide(new BigDecimal(100))); // 生育险公司缴纳比例 salary.setMaternityCompanyProportion(sysUserLevelInsuranceDto.getMaternityCompanyProportion()); // 生育险公司缴纳金额 salary.setMaternityCompanyAmount(sysUserLevelInsuranceDto.getMaternityRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getMaternityCompanyProportion())).divide(new BigDecimal(100))); // 工伤险基数 salary.setIndustrialInjuryRadix(sysUserLevelInsuranceDto.getIndustrialInjuryRadix()); // 工伤险个人缴纳比例 salary.setIndustrialInjuryPersonalProportion(sysUserLevelInsuranceDto.getIndustrialInjuryPersonalProportion()); // 工伤险个人缴纳金额 salary.setIndustrialInjuryPersonalAmount(sysUserLevelInsuranceDto.getIndustrialInjuryRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getIndustrialInjuryPersonalProportion())).divide(new BigDecimal(100))); // 工伤险公司缴纳比例 salary.setIndustrialInjuryCompanyProportion(sysUserLevelInsuranceDto.getIndustrialInjuryCompanyProportion()); // 工伤险公司缴纳金额 salary.setIndustrialInjuryCompanyAmount(sysUserLevelInsuranceDto.getIndustrialInjuryRadix().multiply(new BigDecimal(sysUserLevelInsuranceDto.getIndustrialInjuryCompanyProportion())).divide(new BigDecimal(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.setUserId(salary.getUserId()); 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(salary.getBasicWage().multiply(absenceDays).divide(new BigDecimal(salary.getFullDays())))); // 清空定时器 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)).divide(new BigDecimal(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 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))) ); } 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))) ); } // 判断循环是否结束 if (i == sysConfigIndividualIncomeTaxDtoList.size() - 1) { // 个人所得税 // salary.setIndividualIncomeTax(taxAmount); salary.setIndividualIncomeTax(new BigDecimal(0)); // 税后金额 salary.setAfterTaxAmount(taxableAmount.subtract(taxAmount).compareTo(new BigDecimal(0)) == -1 ? new BigDecimal(0) : taxableAmount.subtract(taxAmount)); } } // 清空统计定时器 break; } } // 返回 return salary; } // ==================== 以上为定时生成工资记录主要逻辑 ==================== }