/*
|
* Copyright 2019-2020 Zheng Jie
|
*
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* you may not use this file except in compliance with the License.
|
* You may obtain a copy of the License at
|
*
|
* http://www.apache.org/licenses/LICENSE-2.0
|
*
|
* Unless required by applicable law or agreed to in writing, software
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* See the License for the specific language governing permissions and
|
* limitations under the License.
|
*/
|
package me.zhengjie.modules.treatment.service.impl;
|
|
import java.io.IOException;
|
import java.util.ArrayList;
|
import java.util.LinkedHashMap;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Optional;
|
import java.util.function.Function;
|
import java.util.stream.Collectors;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import me.zhengjie.exception.BadRequestException;
|
import me.zhengjie.modules.system.domain.SysUserLevelInsurance;
|
import me.zhengjie.modules.system.service.dto.SysUserLevelInsuranceDto;
|
import me.zhengjie.modules.system.service.dto.SysUserLevelInsuranceQueryCriteria;
|
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 lombok.RequiredArgsConstructor;
|
import me.zhengjie.modules.system.domain.User;
|
import me.zhengjie.modules.system.repository.DeptRepository;
|
import me.zhengjie.modules.system.repository.UserRepository;
|
import me.zhengjie.modules.system.service.DeptService;
|
import me.zhengjie.modules.treatment.domain.CmnInsurance;
|
import me.zhengjie.modules.treatment.domain.UserInsurance;
|
import me.zhengjie.modules.treatment.repository.CmnInsuranceRepository;
|
import me.zhengjie.modules.treatment.repository.UserInsuranceRepository;
|
import me.zhengjie.modules.treatment.service.UserInsuranceService;
|
import me.zhengjie.modules.treatment.service.dto.UserInsuranceDto;
|
import me.zhengjie.modules.treatment.service.dto.UserInsuranceGenerateParam;
|
import me.zhengjie.modules.treatment.service.dto.UserInsuranceQueryCriteria;
|
import me.zhengjie.modules.treatment.service.mapstruct.UserInsuranceGenMapper;
|
import me.zhengjie.modules.treatment.service.mapstruct.UserInsuranceMapper;
|
|
/**
|
* @website https://eladmin.vip
|
* @description 服务实现
|
* @author 姚增春
|
* @date 2023-05-07
|
**/
|
@Service
|
@RequiredArgsConstructor
|
public class UserInsuranceServiceImpl implements UserInsuranceService {
|
|
private final UserInsuranceRepository userInsuranceRepository;
|
private final CmnInsuranceRepository cmnInsuranceRepository;
|
private final UserInsuranceMapper userInsuranceMapper;
|
private final UserInsuranceGenMapper userInsuranceGenMapper;
|
private final UserRepository userRepository;
|
private final DeptRepository deptRepository;
|
private final DeptService deptService;
|
|
@Override
|
public Map<String, Object> queryAll(UserInsuranceQueryCriteria criteria, Pageable pageable) {
|
Page<UserInsurance> page = userInsuranceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder), pageable);
|
// 将实体类转换为DTO(重写)
|
Function<UserInsurance, UserInsuranceDto> func = new Function<UserInsurance, UserInsuranceDto>() {
|
@Override
|
public UserInsuranceDto apply(UserInsurance t) {
|
// 将实体类转换成DTO
|
UserInsuranceDto userInsuranceDto = userInsuranceMapper.toDto(t);
|
// 根据用户ID查询用户信息
|
User user = userRepository.findById(userInsuranceDto.getUserId()).orElseGet(User::new);
|
ValidationUtil.isNull(user.getId(),"User","id",userInsuranceDto.getUserId());
|
// 用户昵称
|
userInsuranceDto.setNickName(user.getNickName());
|
// 返回处理好的DTO
|
return userInsuranceDto;
|
}
|
};
|
return PageUtil.toPage(page.map(func));
|
}
|
|
@Override
|
public List<UserInsuranceDto> queryAll(UserInsuranceQueryCriteria criteria){
|
return userInsuranceMapper.toDto(userInsuranceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
}
|
|
@Override
|
public UserInsuranceDto queryTargetYearMonth(UserInsuranceQueryCriteria criteria) {
|
// 国际时间转中国时间
|
String targetYearMonth = criteria.getTargetYearMonth();
|
// 获取某年某月最后一天
|
String yearMonth = targetYearMonth.substring(0,7);
|
// 根据用户ID、指定年月查询
|
Optional<UserInsurance> userInsuranceOpt = userInsuranceRepository.findByUserIdAndYearMonth(criteria.getUserId(), yearMonth);
|
// 判断查询结果
|
if (userInsuranceOpt.isPresent()) {
|
UserInsurance userInsurance = userInsuranceOpt.get();
|
// 返回
|
return userInsuranceMapper.toDto(userInsurance);
|
} else {
|
// throw new BadRequestException("没有获取到当前员工的级别信息!");
|
throw new RuntimeException("没有获取到当前员工的级别信息!");
|
}
|
}
|
|
@Override
|
@Transactional
|
public UserInsuranceDto findById(Long id) {
|
UserInsurance userInsurance = userInsuranceRepository.findById(id).orElseGet(UserInsurance::new);
|
ValidationUtil.isNull(userInsurance.getId(),"UserInsurance","id",id);
|
return userInsuranceMapper.toDto(userInsurance);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public UserInsuranceDto create(UserInsurance resources) {
|
return userInsuranceMapper.toDto(userInsuranceRepository.save(resources));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void update(UserInsurance resources) {
|
UserInsurance userInsurance = userInsuranceRepository.findById(resources.getId()).orElseGet(UserInsurance::new);
|
ValidationUtil.isNull( userInsurance.getId(),"UserInsurance","id",resources.getId());
|
userInsurance.copy(resources);
|
userInsuranceRepository.save(userInsurance);
|
}
|
|
@Override
|
public void deleteAll(Long[] ids) {
|
for (Long id : ids) {
|
userInsuranceRepository.deleteById(id);
|
}
|
}
|
|
@Override
|
public void download(List<UserInsuranceDto> all, HttpServletResponse response) throws IOException {
|
List<Map<String, Object>> list = new ArrayList<>();
|
for (UserInsuranceDto userInsurance : all) {
|
Map<String,Object> map = new LinkedHashMap<>();
|
map.put("用户ID", userInsurance.getUserId());
|
map.put("年月", userInsurance.getYearMonth());
|
map.put("公积金基数", userInsurance.getAccumulationFund());
|
map.put("公积金个人缴纳比例", userInsurance.getAccumulationFundPersonal());
|
map.put("公积金公司缴纳比例", userInsurance.getAccumulationFundCompany());
|
map.put("养老险基数", userInsurance.getRetirementInsurance());
|
map.put("养老险个人缴纳比例", userInsurance.getRetirementInsurancePersonal());
|
map.put("养老险公司缴纳比例", userInsurance.getRetirementInsuranceCompany());
|
map.put("医疗险基数", userInsurance.getMedicalInsurance());
|
map.put("医疗险个人缴纳比例", userInsurance.getMedicalInsurancePersonal());
|
map.put("医疗险公司缴纳比例", userInsurance.getMedicalInsuranceCompany());
|
map.put("失业险基数", userInsurance.getUnemploymentInsurance());
|
map.put("失业险个人缴纳比例", userInsurance.getUnemploymentInsurancePersonal());
|
map.put("失业险公司缴纳比例", userInsurance.getUnemploymentInsuranceCompany());
|
map.put("生育险基数", userInsurance.getBirthInsurance());
|
map.put("生育险个人缴纳比例", userInsurance.getBirthInsurancePersonal());
|
map.put("生育险公司缴纳比例", userInsurance.getBirthInsuranceCompany());
|
map.put("工伤险基数", userInsurance.getEmploymentInsurance());
|
map.put("工伤险个人缴纳比例", userInsurance.getEmploymentInsurancePersonal());
|
map.put("工伤险公司缴纳比例", userInsurance.getEmploymentInsuranceCompany());
|
list.add(map);
|
}
|
FileUtil.downloadExcel(list, response);
|
}
|
|
@Override
|
@Transactional
|
public void generate(UserInsuranceGenerateParam param) {
|
List<User> userList = null;
|
List<Long> deptIds = param.getNodes().stream().filter(node -> node.getLeaf() == false).map(node -> node.getId()).collect(Collectors.toList());
|
List<Long> deptIdAlls = deptService.getDeptChildren(deptRepository.findByIdIn(deptIds));
|
userList = userRepository.findByDeptIdInAndEnabled(deptIdAlls, true);
|
// if (param.getUserIds() != null && param.getUserIds().size() > 0) {
|
// userList = userRepository.findByIdIn(param.getUserIds());
|
// } else {
|
// userList = userRepository.findAllByEnabled(true);
|
// }
|
List<Long> userIds = param.getNodes().stream().filter(node -> node.getLeaf() == true).map(node -> node.getId()).collect(Collectors.toList());
|
userList.addAll(userRepository.findByIdIn(userIds));
|
userList.stream().forEach(user -> {
|
Optional<CmnInsurance> cmnInsuranceOpt = cmnInsuranceRepository.findByCompany(user.getCompany());
|
if (cmnInsuranceOpt.isPresent()) {
|
UserInsurance userInsuranceTarget = userInsuranceRepository.findByUserIdAndYearMonth(user.getId(), param.getTargetYearMonth()).orElseGet(UserInsurance::new);
|
UserInsurance userInsuranceSrc = userInsuranceGenMapper.cloneInsurance(cmnInsuranceOpt.get());
|
userInsuranceTarget.copy(userInsuranceSrc);
|
userInsuranceTarget.setUserId(user.getId());
|
userInsuranceTarget.setYearMonth(param.getTargetYearMonth());
|
userInsuranceRepository.save(userInsuranceTarget);
|
}
|
});
|
}
|
}
|