| | |
| | | /* |
| | | * 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. |
| | | */ |
| | | * 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.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | 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 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.system.domain.User; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.modules.treatment.domain.UserInsurance; |
| | |
| | | import me.zhengjie.modules.treatment.service.dto.UserLevelWageDto; |
| | | import me.zhengjie.modules.treatment.service.dto.UserLevelWageQueryCriteria; |
| | | import me.zhengjie.modules.treatment.service.mapstruct.UserLevelWageMapper; |
| | | import me.zhengjie.utils.FileUtil; |
| | | import me.zhengjie.utils.PageUtil; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import me.zhengjie.utils.*; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | | * @description 服务实现 |
| | | * @author 姚增春 |
| | | * @date 2023-05-07 |
| | | **/ |
| | | * @author 姚增春 |
| | | * @website https://eladmin.vip |
| | | * @description 服务实现 |
| | | * @date 2023-05-07 |
| | | **/ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class UserLevelWageServiceImpl implements UserLevelWageService { |
| | |
| | | private final CmnInsuranceRepository cmnInsuranceRepository; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(UserLevelWageQueryCriteria criteria, Pageable pageable){ |
| | | Page<UserLevelWage> page = userLevelWageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | public Map<String, Object> queryAll(UserLevelWageQueryCriteria criteria, Pageable pageable) { |
| | | Page<UserLevelWage> page = userLevelWageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable); |
| | | // 将实体类转换为DTO(重写) |
| | | Function<UserLevelWage, UserLevelWageDto> func = new Function<UserLevelWage, UserLevelWageDto>() { |
| | | @Override |
| | | public UserLevelWageDto apply(UserLevelWage userLevelWage) { |
| | | // 将实体类转换成DTO |
| | | UserLevelWageDto userLevelWageDto = userLevelWageMapper.toDto(userLevelWage); |
| | | UserLevelWageDto userLevelWageDto = userLevelWageMapper.toDto(userLevelWage); |
| | | User user = userRepository.findById(userLevelWageDto.getUserId()).orElseGet(User::new); |
| | | ValidationUtil.isNull(userLevelWageDto.getId(),"User","id",userLevelWageDto.getUserId()); |
| | | ValidationUtil.isNull(userLevelWageDto.getId(), "User", "id", userLevelWageDto.getUserId()); |
| | | userLevelWageDto.setNickName(user.getNickName()); |
| | | return userLevelWageDto; |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public List<UserLevelWageDto> queryAll(UserLevelWageQueryCriteria criteria){ |
| | | return userLevelWageMapper.toDto(userLevelWageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); |
| | | public List<UserLevelWageDto> queryAll(UserLevelWageQueryCriteria criteria) { |
| | | return userLevelWageMapper.toDto(userLevelWageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder))); |
| | | } |
| | | |
| | | @Override |
| | | public UserLevelWageDto queryTargetYearMonth(UserLevelWageQueryCriteria criteria) { |
| | | // 国际时间转中国时间 |
| | | String targetYearMonth = criteria.getTargetYearMonth(); |
| | | // 获取某年某月最后一天 |
| | | String yearMonth = targetYearMonth.substring(0,7); |
| | | // 根据用户ID、指定年月查询 |
| | | UserLevelWage userLevelWage = userLevelWageRepository.findByUserIdAndYearMonth(criteria.getUserId(), yearMonth); |
| | | // 判断查询结果 |
| | | if (userLevelWage == null) { |
| | | // throw new BadRequestException("没有获取到当前员工的级别信息!"); |
| | | throw new RuntimeException("没有获取到当前员工的级别信息!"); |
| | | } |
| | | // 返回 |
| | | return userLevelWageMapper.toDto(userLevelWage); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public UserLevelWageDto findById(Long id) { |
| | | UserLevelWage userLevelWage = userLevelWageRepository.findById(id).orElseGet(UserLevelWage::new); |
| | | ValidationUtil.isNull(userLevelWage.getId(),"UserLevelWage","id",id); |
| | | ValidationUtil.isNull(userLevelWage.getId(), "UserLevelWage", "id", id); |
| | | return userLevelWageMapper.toDto(userLevelWage); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public UserLevelWageDto create(UserLevelWage resources) { |
| | | resources.setPerformanceCoefficient(calculatePerformanceCoefficient(resources)); |
| | | resources.setPerformanceCoefficient(calculatePerformanceCoefficient(resources)); |
| | | return userLevelWageMapper.toDto(userLevelWageRepository.save(resources)); |
| | | } |
| | | |
| | | private BigDecimal calculatePerformanceCoefficient(UserLevelWage resources) { |
| | | BigDecimal total = resources.getBasicWage().add(resources.getRankWage()).add(resources.getPerformanceWage()); |
| | | Optional<UserInsurance> userInsuranceOpt = userInsuranceRepository.findByUserIdAndYearMonth(resources.getUserId(), resources.getYearMonth()); |
| | | if (userInsuranceOpt.isPresent()) { |
| | | UserInsurance userInsurance = userInsuranceOpt.get(); |
| | | total = total.add(userInsurance.getAccumulationFund().multiply(BigDecimal.valueOf(userInsurance.getAccumulationFundCompany() / 100))) |
| | | .add(userInsurance.getBirthInsurance().multiply(BigDecimal.valueOf(userInsurance.getBirthInsuranceCompany() / 100))) |
| | | .add(userInsurance.getEmploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getEmploymentInsuranceCompany() / 100))) |
| | | .add(userInsurance.getMedicalInsurance().multiply(BigDecimal.valueOf(userInsurance.getMedicalInsuranceCompany() / 100))) |
| | | .add(userInsurance.getRetirementInsurance().multiply(BigDecimal.valueOf(userInsurance.getRetirementInsuranceCompany() / 100))) |
| | | .add(userInsurance.getUnemploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getUnemploymentInsuranceCompany() / 100))); |
| | | } |
| | | return total.divide(BigDecimal.valueOf(11250), 2, RoundingMode.UP); |
| | | BigDecimal total = resources.getBasicWage().add(resources.getRankWage()).add(resources.getPerformanceWage()); |
| | | Optional<UserInsurance> userInsuranceOpt = userInsuranceRepository.findByUserIdAndYearMonth(resources.getUserId(), resources.getYearMonth()); |
| | | if (userInsuranceOpt.isPresent()) { |
| | | UserInsurance userInsurance = userInsuranceOpt.get(); |
| | | total = total.add(userInsurance.getAccumulationFund().multiply(BigDecimal.valueOf(userInsurance.getAccumulationFundCompany() / 100))) |
| | | .add(userInsurance.getBirthInsurance().multiply(BigDecimal.valueOf(userInsurance.getBirthInsuranceCompany() / 100))) |
| | | .add(userInsurance.getEmploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getEmploymentInsuranceCompany() / 100))) |
| | | .add(userInsurance.getMedicalInsurance().multiply(BigDecimal.valueOf(userInsurance.getMedicalInsuranceCompany() / 100))) |
| | | .add(userInsurance.getRetirementInsurance().multiply(BigDecimal.valueOf(userInsurance.getRetirementInsuranceCompany() / 100))) |
| | | .add(userInsurance.getUnemploymentInsurance().multiply(BigDecimal.valueOf(userInsurance.getUnemploymentInsuranceCompany() / 100))); |
| | | } |
| | | return total.divide(BigDecimal.valueOf(11250), 2, RoundingMode.UP); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void upload(List<UserLevelWage> resources) { |
| | | for (UserLevelWage userLevelWage : resources) { |
| | | UserLevelWage userLevelWageDB = userLevelWageRepository.findByUserIdAndYearMonth(userLevelWage.getUserId(), userLevelWage.getYearMonth()); |
| | | if (userLevelWageDB == null) { |
| | | userLevelWageRepository.save(userLevelWage); |
| | | } else { |
| | | userLevelWageDB.copy(userLevelWage); |
| | | userLevelWageRepository.save(userLevelWageDB); |
| | | } |
| | | } |
| | | for (UserLevelWage userLevelWage : resources) { |
| | | UserLevelWage userLevelWageDB = userLevelWageRepository.findByUserIdAndYearMonth(userLevelWage.getUserId(), userLevelWage.getYearMonth()); |
| | | if (userLevelWageDB == null) { |
| | | userLevelWageRepository.save(userLevelWage); |
| | | } else { |
| | | userLevelWageDB.copy(userLevelWage); |
| | | userLevelWageRepository.save(userLevelWageDB); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(UserLevelWage resources) { |
| | | UserLevelWage userLevelWage = userLevelWageRepository.findById(resources.getId()).orElseGet(UserLevelWage::new); |
| | | ValidationUtil.isNull( userLevelWage.getId(),"UserLevelWage","id",resources.getId()); |
| | | ValidationUtil.isNull(userLevelWage.getId(), "UserLevelWage", "id", resources.getId()); |
| | | userLevelWage.copy(resources); |
| | | userLevelWage.setPerformanceCoefficient(calculatePerformanceCoefficient(userLevelWage)); |
| | | userLevelWageRepository.save(userLevelWage); |
| | |
| | | @Override |
| | | public void flushAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | UserLevelWage userLevelWage = userLevelWageRepository.findById(id).orElseGet(UserLevelWage::new); |
| | | userLevelWage.setPerformanceCoefficient(calculatePerformanceCoefficient(userLevelWage)); |
| | | userLevelWageRepository.save(userLevelWage); |
| | | UserLevelWage userLevelWage = userLevelWageRepository.findById(id).orElseGet(UserLevelWage::new); |
| | | userLevelWage.setPerformanceCoefficient(calculatePerformanceCoefficient(userLevelWage)); |
| | | userLevelWageRepository.save(userLevelWage); |
| | | } |
| | | } |
| | | |
| | |
| | | public void download(List<UserLevelWageDto> all, HttpServletResponse response) throws IOException { |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (UserLevelWageDto userLevelWage : all) { |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | Map<String, Object> map = new LinkedHashMap<>(); |
| | | map.put("用户ID", userLevelWage.getUserId()); |
| | | map.put("年月", userLevelWage.getYearMonth()); |
| | | map.put("级别", userLevelWage.getLevel()); |