| | |
| | | /* |
| | | * 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.project.service.impl; |
| | | |
| | | import me.zhengjie.exception.BadRequestException; |
| | | import me.zhengjie.modules.attendance.domain.Calculate; |
| | | import me.zhengjie.modules.attendance.service.AttendanceService; |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.project.domain.Project; |
| | | import me.zhengjie.modules.project.domain.ProjectCost; |
| | | import me.zhengjie.modules.project.repository.ProjectRepository; |
| | | import me.zhengjie.modules.system.domain.SysConfigHoliday; |
| | | import me.zhengjie.modules.system.domain.SysUserLevelInsurance; |
| | | import me.zhengjie.modules.system.domain.SysUserLevelWage; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository; |
| | | import me.zhengjie.modules.system.repository.SysUserLevelInsuranceRepository; |
| | | import me.zhengjie.modules.system.repository.SysUserLevelWageRepository; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.modules.system.service.UserService; |
| | | import me.zhengjie.modules.system.service.dto.UserDto; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import me.zhengjie.utils.FileUtil; |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.modules.project.repository.ProjectCostRepository; |
| | | import me.zhengjie.modules.project.service.ProjectCostService; |
| | | import me.zhengjie.modules.project.service.dto.ProjectCostDto; |
| | | import me.zhengjie.modules.project.service.dto.ProjectCostQueryCriteria; |
| | | import me.zhengjie.modules.project.service.mapstruct.ProjectCostMapper; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import me.zhengjie.utils.PageUtil; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.text.*; |
| | | import java.time.*; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | | * @description 服务实现 |
| | | * @author HuaZhe |
| | | * @date 2022-12-27 |
| | | **/ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class ProjectCostServiceImpl implements ProjectCostService { |
| | | |
| | | private final ProjectCostRepository projectCostRepository; |
| | | private final ProjectCostMapper projectCostMapper; |
| | | private final ProjectRepository projectRepository; |
| | | private final UserRepository userRepository; |
| | | private final SysUserLevelWageRepository sysUserLevelWageRepository; |
| | | private final SysUserLevelInsuranceRepository sysUserLevelInsuranceRepository; |
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository; |
| | | private final UserService userService; |
| | | private final AttendanceService attendanceService; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(ProjectCostQueryCriteria criteria, Pageable pageable){ |
| | | Page<ProjectCost> page = projectCostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | // 将实体类转换为DTO(重写) |
| | | Function<ProjectCost, ProjectCostDto> func = new Function<ProjectCost, ProjectCostDto>() { |
| | | @Override |
| | | public ProjectCostDto apply(ProjectCost projectCost) { |
| | | // 将实体类转换成DTO |
| | | ProjectCostDto projectCostDto = projectCostMapper.toDto(projectCost); |
| | | // 根据项目ID查询项目信息 |
| | | Project project = projectRepository.findById(projectCostDto.getProjectId()).orElseGet(Project::new); |
| | | ValidationUtil.isNull(projectCostDto.getId(),"Project","id",projectCostDto.getProjectId()); |
| | | // 项目名称 |
| | | projectCostDto.setProjectName(project.getName()); |
| | | User user = userRepository.findById(projectCostDto.getUserId()).orElseGet(User::new); |
| | | ValidationUtil.isNull(projectCostDto.getId(),"User","id",projectCostDto.getUserId()); |
| | | projectCostDto.setUserName(user.getNickName()); |
| | | return projectCostDto; |
| | | } |
| | | }; |
| | | return PageUtil.toPage(page.map(func)); |
| | | } |
| | | |
| | | @Override |
| | | public List<ProjectCostDto> queryAll(ProjectCostQueryCriteria criteria){ |
| | | return projectCostMapper.toDto(projectCostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public ProjectCostDto findById(Long id) { |
| | | ProjectCost projectCost = projectCostRepository.findById(id).orElseGet(ProjectCost::new); |
| | | ValidationUtil.isNull(projectCost.getId(),"ProjectCost","id",id); |
| | | return projectCostMapper.toDto(projectCost); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public ProjectCostDto create(ProjectCost resources) { |
| | | return projectCostMapper.toDto(projectCostRepository.save(resources)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(ProjectCost resources) { |
| | | ProjectCost projectCost = projectCostRepository.findById(resources.getId()).orElseGet(ProjectCost::new); |
| | | ValidationUtil.isNull( projectCost.getId(),"ProjectCost","id",resources.getId()); |
| | | projectCost.copy(resources); |
| | | projectCostRepository.save(projectCost); |
| | | } |
| | | |
| | | @Override |
| | | public void deleteAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | projectCostRepository.deleteById(id); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public ProjectCost getCost(ProjectCostQueryCriteria criteria) throws ParseException { |
| | | ProjectCost projectCost = new ProjectCost(); |
| | | // 工资 |
| | | BigDecimal wageCost = BigDecimal.valueOf(0); |
| | | // 保险 |
| | | BigDecimal insurancesCost = BigDecimal.valueOf(0); |
| | | Date date = new Date(); |
| | | Date dateExit = new Date(); |
| | | // 工作天数 |
| | | Calculate calculate=new Calculate(); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | Date enterProjectTimeOne = simpleDateFormat.parse(criteria.getEnterProjectTime()); |
| | | Date exitProjectTimeOne = simpleDateFormat.parse(criteria.getExitProjectTime()); |
| | | |
| | | Set<String> availableZoneIds = ZoneOffset.getAvailableZoneIds(); |
| | | LocalDateTime enterProject = LocalDateTime.ofInstant(enterProjectTimeOne.toInstant(),ZoneOffset.UTC); |
| | | LocalDateTime exitProject = LocalDateTime.ofInstant(exitProjectTimeOne.toInstant(),ZoneOffset.UTC); |
| | | calculate.setStart(enterProject); |
| | | calculate.setEnd(exitProject); |
| | | |
| | | // 通过id获取用户级别工资 |
| | | List<SysUserLevelWage> sysUserLevelWages = sysUserLevelWageRepository.findByUserId(criteria.getUserId()); |
| | | // 通过id获取用户级别保险 |
| | | List<SysUserLevelInsurance> sysUserLevelInsurances = sysUserLevelInsuranceRepository.findByUserId(criteria.getUserId()); |
| | | // 通过id获取用户信息 |
| | | UserDto user = userService.findById(criteria.getUserId()); |
| | | // 判断是否有用户级别工资 |
| | | if(sysUserLevelWages.size() <= 0){ |
| | | throw new BadRequestException("此用户暂无级别工资"); |
| | | } |
| | | // 判断是否有用户级别保险 |
| | | if(sysUserLevelInsurances.size() <= 0){ |
| | | throw new BadRequestException("此用户暂无级别保险"); |
| | | } |
| | | for (SysUserLevelInsurance sysUserLevelInsurance : sysUserLevelInsurances) { |
| | | Date enterProjectTime = simpleDateFormat.parse(criteria.getEnterProjectTime()); |
| | | Date exitProjectTime = simpleDateFormat.parse(criteria.getExitProjectTime()); |
| | | if (exitProjectTime.before(sysUserLevelInsurance.getStartDate())) { |
| | | // nothing to do |
| | | } else if (sysUserLevelInsurance.getEndDate() !=null && enterProjectTime.after(sysUserLevelInsurance.getEndDate())) { |
| | | // nothing to do |
| | | } else { |
| | | if (enterProjectTime.before(sysUserLevelInsurance.getStartDate())) { |
| | | enterProjectTime = sysUserLevelInsurance.getStartDate(); |
| | | } |
| | | if (sysUserLevelInsurance.getEndDate() !=null && exitProjectTime.after(sysUserLevelInsurance.getEndDate())) { |
| | | exitProjectTime = sysUserLevelInsurance.getEndDate(); |
| | | } |
| | | int workDays = 0; |
| | | // Integer a= attendanceService.calculateDays(exitProjectTime); |
| | | for(long x = 0; x<= calculateDays(calculate);x++){ |
| | | // 增加天数 |
| | | String res = simpleDateFormat.format(enterProjectTime.getTime() + (x * 24 * 60 * 60 * 1000)); |
| | | String resExit = simpleDateFormat.format(exitProjectTime.getTime() + (x * 24 * 60 * 60 * 1000)); |
| | | try { |
| | | date = simpleDateFormat.parse(res); |
| | | dateExit = simpleDateFormat.parse(resExit); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(enterProjectTimeOne.equals(date)){ |
| | | int workHours = 0; |
| | | LocalTime parse = LocalTime.parse(res.substring(11)); |
| | | // 判断是否是工作日 |
| | | workHours=checkSun(res,date,workHours); |
| | | if(workHours > 0){ |
| | | // 是工作日与用户排班下的工作开始时间,工作结束时间,午休结束时间进行比较 |
| | | if(parse.isBefore(user.getSysRostering().getWorkStartTime()) || parse.equals(user.getSysRostering().getWorkStartTime())){ |
| | | workHours = 8; |
| | | // insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchStartTime())){ |
| | | workHours = user.getSysRostering().getLunchStartTime().getHour() - parse.getHour() +5; |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | continue; |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchEndTime()) || parse.equals(user.getSysRostering().getLunchEndTime())){ |
| | | workHours = 5; |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | continue; |
| | | } else if (parse.isBefore(user.getSysRostering().getWorkEndTime())){ |
| | | workHours = user.getSysRostering().getWorkEndTime().getHour() - parse.getHour(); |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | continue; |
| | | } else { |
| | | // nothing to do |
| | | workHours = 0; |
| | | } |
| | | // 计算小时的花费 |
| | | continue; |
| | | } else{ |
| | | // 不是工作日 nothing to do |
| | | continue; |
| | | } |
| | | } |
| | | if(x+1 == (exitProjectTime.getTime() - enterProjectTime.getTime())/(1000 * 60 * 60 * 24)){ |
| | | int workHours = 0; |
| | | LocalTime parse = LocalTime.parse(res.substring(11)); |
| | | // 判断是否是工作日 |
| | | workHours=checkSun(res,dateExit,workHours); |
| | | if(workHours > 0){ |
| | | // 是工作日与用户排班下的工作开始时间,工作结束时间,午休结束时间进行比较 |
| | | if(parse.isBefore(user.getSysRostering().getWorkStartTime()) || parse.equals(user.getSysRostering().getWorkStartTime())){ |
| | | // 不工作 nothing to do |
| | | workHours = 0; |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchStartTime())){ |
| | | workHours = parse.getHour() - user.getSysRostering().getWorkStartTime().getHour(); |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | continue; |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchEndTime()) || parse.equals(user.getSysRostering().getLunchStartTime())){ |
| | | workHours = 3; |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | continue; |
| | | } else if (parse.isBefore(user.getSysRostering().getWorkEndTime())){ |
| | | workHours = parse.getHour() - user.getSysRostering().getLunchEndTime().getHour() + 3; |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | continue; |
| | | } else if (parse.isAfter(user.getSysRostering().getWorkEndTime()) || parse.equals(user.getSysRostering().getWorkEndTime())){ |
| | | workHours = 8; |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcuHour(sysUserLevelInsurance,workHours)); |
| | | continue; |
| | | } |
| | | // 计算小时的花费 |
| | | continue; |
| | | } else{ |
| | | // 不是工作日 nothing to do |
| | | continue; |
| | | } |
| | | } |
| | | workDays=calculateDays(calculate)-1; |
| | | } |
| | | insurancesCost = insurancesCost.add(costInsurancesCalcu(sysUserLevelInsurance, workDays)); |
| | | } |
| | | } |
| | | for (SysUserLevelWage sysUserLevelWage : sysUserLevelWages) { |
| | | Date enterProjectTime = simpleDateFormat.parse(criteria.getEnterProjectTime()); |
| | | Date exitProjectTime = simpleDateFormat.parse(criteria.getExitProjectTime()); |
| | | if (exitProjectTime.before(sysUserLevelWage.getStartDate())) { |
| | | // nothing to do |
| | | } else if (sysUserLevelWage.getEndDate() !=null && enterProjectTime.after(sysUserLevelWage.getEndDate())) { |
| | | // nothing to do |
| | | } else { |
| | | if (enterProjectTime.before(sysUserLevelWage.getStartDate())) { |
| | | enterProjectTime = sysUserLevelWage.getStartDate(); |
| | | } |
| | | if ( sysUserLevelWage.getEndDate() != null && exitProjectTime.after(sysUserLevelWage.getEndDate())) { |
| | | exitProjectTime = sysUserLevelWage.getEndDate(); |
| | | } |
| | | int workDays = 0; |
| | | // 项目总用时 项目的出去时间减去项目的进入时间 |
| | | for ( long x = 0;x <= calculateDays(calculate); x++ ) { |
| | | // 增加天数 |
| | | String res = simpleDateFormat.format(enterProjectTime.getTime() + (x * 24 * 60 * 60 * 1000)); |
| | | String resExit = simpleDateFormat.format(exitProjectTime.getTime() + (x * 24 * 60 * 60 * 1000)); |
| | | try { |
| | | date = simpleDateFormat.parse(res); |
| | | dateExit = simpleDateFormat.parse(resExit); |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if(enterProjectTimeOne.equals(date)){ |
| | | int workHours = 0; |
| | | LocalTime parse = LocalTime.parse(res.substring(11)); |
| | | // 判断是否是工作日 |
| | | workHours=checkSun(res,date,workHours); |
| | | if(workHours > 0){ |
| | | // 是工作日与用户排班下的工作开始时间,工作结束时间,午休结束时间进行比较 |
| | | if(parse.isBefore(user.getSysRostering().getWorkStartTime()) || parse.equals(user.getSysRostering().getWorkStartTime())){ |
| | | workHours = 8; |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | continue; |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchStartTime())){ |
| | | workHours = user.getSysRostering().getLunchStartTime().getHour() - parse.getHour() +5; |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | continue; |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchEndTime()) || parse.equals(user.getSysRostering().getLunchEndTime())){ |
| | | workHours = 5; |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | continue; |
| | | } else if (parse.isBefore(user.getSysRostering().getWorkEndTime())){ |
| | | workHours = user.getSysRostering().getWorkEndTime().getHour() - parse.getHour(); |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | continue; |
| | | } else { |
| | | workHours = 0; |
| | | } |
| | | // 计算小时的花费 |
| | | continue; |
| | | } else{ |
| | | // 不是工作日 nothing to do |
| | | continue; |
| | | } |
| | | } |
| | | if(x+1 == (exitProjectTime.getTime() - enterProjectTime.getTime())/(1000 * 60 * 60 * 24)){ |
| | | int workHours = 0; |
| | | LocalTime parse = LocalTime.parse(resExit.substring(11)); |
| | | // 判断是否是工作日 |
| | | workHours=checkSun(res,dateExit,workHours); |
| | | if(workHours > 0){ |
| | | // 是工作日与用户排班下的工作开始时间,工作结束时间,午休结束时间进行比较 |
| | | if(parse.isBefore(user.getSysRostering().getWorkStartTime()) || parse.equals(user.getSysRostering().getWorkStartTime())){ |
| | | // 不工作 |
| | | workHours = 0; |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchStartTime())){ |
| | | // 上午工作 |
| | | workHours = parse.getHour() - user.getSysRostering().getWorkStartTime().getHour(); |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | } else if (parse.isBefore(user.getSysRostering().getLunchEndTime()) || parse.equals(user.getSysRostering().getLunchStartTime()) || parse.equals(user.getSysRostering().getLunchEndTime())){ |
| | | // 工作一上午 |
| | | workHours = 3; |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | } else if (parse.isBefore(user.getSysRostering().getWorkEndTime())){ |
| | | // 下午工作 |
| | | workHours = parse.getHour() - user.getSysRostering().getLunchEndTime().getHour() + 3; |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | } else if (parse.isAfter(user.getSysRostering().getWorkEndTime()) || parse.equals(user.getSysRostering().getWorkEndTime())){ |
| | | // 工作一天 |
| | | workHours = 8; |
| | | wageCost = wageCost.add(costWageCalcuHour(sysUserLevelWage,workHours)); |
| | | } |
| | | // 计算小时的花费 |
| | | continue; |
| | | } else{ |
| | | // 不是工作日 nothing to do |
| | | continue; |
| | | } |
| | | } |
| | | workDays=calculateDays(calculate)-1; |
| | | } |
| | | wageCost = wageCost.add(costWageCalcu( sysUserLevelWage, workDays)); |
| | | } |
| | | } |
| | | projectCost.setProjectCost(wageCost.add(insurancesCost)); |
| | | return projectCost; |
| | | } |
| | | |
| | | @Override |
| | | public void download(List<ProjectCostDto> all, HttpServletResponse response) throws IOException { |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (ProjectCostDto projectCost : all) { |
| | | String projectName = projectCost.getProjectId() != null ? projectRepository.findById(projectCost.getProjectId()).orElseGet(Project::new).getName() : ""; |
| | | String user = projectCost.getUserId() != null ? userRepository.findById(projectCost.getUserId()).orElseGet(User::new).getNickName() : ""; |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | map.put("项目", projectName); |
| | | map.put("用户", user); |
| | | map.put("进入项目时间", projectCost.getEnterProjectTime()); |
| | | map.put("退出项目时间", projectCost.getExitProjectTime()); |
| | | map.put("项目成本", projectCost.getProjectCost()); |
| | | map.put("备注", projectCost.getRemarks()); |
| | | map.put("创建者", projectCost.getCreateBy()); |
| | | map.put("创建时间", projectCost.getCreateTime()); |
| | | map.put("更新者", projectCost.getUpdateBy()); |
| | | map.put("更新时间", projectCost.getUpdateTime()); |
| | | list.add(map); |
| | | } |
| | | FileUtil.downloadExcel(list, response); |
| | | } |
| | | |
| | | // 获取工作天数 |
| | | public int checkSun(String res, Date date, int workDays) { |
| | | // 创建Calendar类实例 |
| | | Calendar instance = Calendar.getInstance(); |
| | | instance.setTime(date); |
| | | SysConfigHoliday sysConfigHoliday = sysConfigHolidayRepository.findByHolidayDate(res); |
| | | // 判断周六周日 |
| | | if(instance.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY || instance.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){ |
| | | // 判断周六周日是否为工作日 是工作日工作天数加一天 |
| | | if(sysConfigHoliday != null){ |
| | | if(sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_WEEKDAY) || sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_HOLIDAYS)){ |
| | | workDays++; |
| | | } |
| | | } |
| | | } else { |
| | | // 判断周一到周五是否为节假日 不是节假日工作天数加一天 |
| | | if(sysConfigHoliday == null){ |
| | | workDays++; |
| | | } else { |
| | | if(!sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_LEGAL_HOLIDAYS)){ |
| | | workDays++; |
| | | } |
| | | } |
| | | } |
| | | return workDays; |
| | | } |
| | | |
| | | // 计算工资成本 |
| | | public BigDecimal costWageCalcu(SysUserLevelWage sysUserLevelWage,int workDays){ |
| | | BigDecimal average = BigDecimal.valueOf(21.75); |
| | | BigDecimal wage = sysUserLevelWage.getLevelWage().divide(average,2,BigDecimal.ROUND_CEILING); |
| | | return wage.multiply(BigDecimal.valueOf(workDays)); |
| | | } |
| | | |
| | | // 计算小时工资成本 |
| | | public BigDecimal costWageCalcuHour(SysUserLevelWage sysUserLevelWage,int workDays){ |
| | | // 每个月的平均工时 |
| | | BigDecimal average = BigDecimal.valueOf(21.75); |
| | | // 每天的工时 |
| | | BigDecimal averHour = BigDecimal.valueOf(8); |
| | | BigDecimal wage = (sysUserLevelWage.getLevelWage().divide(average,2,BigDecimal.ROUND_CEILING)).divide(averHour,2,BigDecimal.ROUND_CEILING); |
| | | // System.out.println("wage:" +wage.multiply(BigDecimal.valueOf(workDays))); |
| | | return wage.multiply(BigDecimal.valueOf(workDays)); |
| | | } |
| | | // 计算小时保险成本 |
| | | public BigDecimal costInsurancesCalcuHour(SysUserLevelInsurance sysUserLevelInsurance,int workDays){ |
| | | BigDecimal average = BigDecimal.valueOf(21.75); |
| | | BigDecimal avarHour = BigDecimal.valueOf(8); |
| | | BigDecimal accumulationRadix = (sysUserLevelInsurance.getAccumulationRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getAccumulationCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING)).divide(avarHour,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal pensionRadix = (sysUserLevelInsurance.getPensionRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getPensionCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING)).divide(avarHour,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal medicalRadix = (sysUserLevelInsurance.getMedicalRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getMedicalCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING)).divide(avarHour,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal unemploymentRadix = (sysUserLevelInsurance.getUnemploymentRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getUnemploymentCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING)).divide(avarHour,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal maternityRadix = (sysUserLevelInsurance.getMaternityRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getMaternityCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING)).divide(avarHour,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal industrialInjuryRadix = (sysUserLevelInsurance.getIndustrialInjuryRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getIndustrialInjuryCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING)).divide(avarHour,2,BigDecimal.ROUND_CEILING); |
| | | // System.out.println("calcuhour:" + accumulationRadix.add(pensionRadix.add(medicalRadix.add(unemploymentRadix.add(maternityRadix.add(industrialInjuryRadix))))).multiply(BigDecimal.valueOf(workDays))); |
| | | return accumulationRadix.add(pensionRadix.add(medicalRadix.add(unemploymentRadix.add(maternityRadix.add(industrialInjuryRadix))))).multiply(BigDecimal.valueOf(workDays)); |
| | | } |
| | | |
| | | // 计算保险成本 |
| | | public BigDecimal costInsurancesCalcu(SysUserLevelInsurance sysUserLevelInsurance,int workDays){ |
| | | BigDecimal average = BigDecimal.valueOf(21.75); |
| | | BigDecimal accumulationRadix = sysUserLevelInsurance.getAccumulationRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getAccumulationCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal pensionRadix = sysUserLevelInsurance.getPensionRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getPensionCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal medicalRadix = sysUserLevelInsurance.getMedicalRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getMedicalCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal unemploymentRadix = sysUserLevelInsurance.getUnemploymentRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getUnemploymentCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal maternityRadix = sysUserLevelInsurance.getMaternityRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getMaternityCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING); |
| | | BigDecimal industrialInjuryRadix = sysUserLevelInsurance.getIndustrialInjuryRadix().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getIndustrialInjuryCompanyProportion()/100)).divide(average,2,BigDecimal.ROUND_CEILING); |
| | | return accumulationRadix.add(pensionRadix.add(medicalRadix.add(unemploymentRadix.add(maternityRadix.add(industrialInjuryRadix))))).multiply(BigDecimal.valueOf(workDays)); |
| | | } |
| | | |
| | | public Integer calculateDays(Calculate calculate) { |
| | | List<String> holiday = new ArrayList<>(), isWork = new ArrayList<>(); |
| | | //查询节假日表 |
| | | List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findAll(); |
| | | for(SysConfigHoliday sysConfigHoliday : sysConfigHolidayList){ |
| | | if(sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_WEEKDAY)){ |
| | | //工作日 |
| | | isWork.add(sysConfigHoliday.getHolidayDate().toString()); |
| | | }else{ |
| | | holiday.add(sysConfigHoliday.getHolidayDate().toString()); |
| | | } |
| | | } |
| | | Long chronoUnit = ChronoUnit.DAYS.between(calculate.getStart().toLocalDate(), calculate.getEnd().toLocalDate()); |
| | | Integer count = 0; |
| | | for (Integer i = 0; i <= chronoUnit; i++) { |
| | | LocalDate date = calculate.getStart().toLocalDate().plusDays(i); |
| | | Boolean isWeekend = date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY; |
| | | if ((!isWeekend && !holiday.contains(date.toString())) || (isWeekend && isWork.contains(date.toString()))) { |
| | | count++; |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | /*
|
| | | * 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.project.service.impl;
|
| | |
|
| | | import lombok.RequiredArgsConstructor;
|
| | | import me.zhengjie.exception.BadRequestException;
|
| | | import me.zhengjie.modules.constant.Constant;
|
| | | import me.zhengjie.modules.project.domain.Project;
|
| | | import me.zhengjie.modules.project.domain.ProjectCost;
|
| | | import me.zhengjie.modules.project.repository.ProjectCostRepository;
|
| | | import me.zhengjie.modules.project.repository.ProjectRepository;
|
| | | import me.zhengjie.modules.project.service.ProjectCostService;
|
| | | import me.zhengjie.modules.project.service.dto.ProjectCostDto;
|
| | | import me.zhengjie.modules.project.service.dto.ProjectCostQueryCriteria;
|
| | | import me.zhengjie.modules.project.service.mapstruct.ProjectCostMapper;
|
| | | import me.zhengjie.modules.system.domain.SysConfigHoliday;
|
| | | import me.zhengjie.modules.system.domain.User;
|
| | | 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.UserService;
|
| | | import me.zhengjie.modules.system.service.dto.RoleSmallDto;
|
| | | import me.zhengjie.modules.system.service.dto.SysRosteringDto;
|
| | | import me.zhengjie.modules.system.service.dto.UserDto;
|
| | | import me.zhengjie.modules.treatment.domain.UserInsurance;
|
| | | import me.zhengjie.modules.treatment.domain.UserLevelWage;
|
| | | import me.zhengjie.modules.treatment.repository.UserInsuranceRepository;
|
| | | import me.zhengjie.modules.treatment.repository.UserLevelWageRepository;
|
| | | import me.zhengjie.utils.*;
|
| | | import me.zhengjie.utils.enums.DataScopeEnum;
|
| | | 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.text.ParseException;
|
| | | import java.time.DayOfWeek;
|
| | | import java.time.LocalDate;
|
| | | import java.time.LocalDateTime;
|
| | | import java.time.ZoneId;
|
| | | import java.time.temporal.ChronoUnit;
|
| | | import java.util.*;
|
| | | import java.util.function.Function;
|
| | | import java.util.stream.Collectors;
|
| | | import java.util.stream.Stream;
|
| | |
|
| | | /**
|
| | | * @website https://eladmin.vip
|
| | | * @description 服务实现
|
| | | * @author HuaZhe
|
| | | * @date 2022-12-27
|
| | | **/
|
| | | @Service
|
| | | @RequiredArgsConstructor
|
| | | public class ProjectCostServiceImpl implements ProjectCostService {
|
| | |
|
| | | private final ProjectCostRepository projectCostRepository;
|
| | | private final ProjectCostMapper projectCostMapper;
|
| | | private final ProjectRepository projectRepository;
|
| | | private final UserRepository userRepository;
|
| | | private final UserLevelWageRepository sysUserLevelWageRepository;
|
| | | private final UserInsuranceRepository sysUserLevelInsuranceRepository;
|
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository;
|
| | | private final UserService userService;
|
| | | private final RoleService roleService;
|
| | |
|
| | | @Override
|
| | | public Map<String,Object> queryAll(ProjectCostQueryCriteria criteria, Pageable pageable) {
|
| | | // 获取当前用户ID
|
| | | Long userId = SecurityUtils.getCurrentUserId();
|
| | | // 查询用户角色
|
| | | List<RoleSmallDto> roleSet = roleService.findByUsersId(userId);
|
| | | if (roleSet.stream().anyMatch(role -> DataScopeEnum.find(role.getDataScope()) == DataScopeEnum.THIS_LEVEL)) {
|
| | | // 根据用户ID查询负责的项目ID
|
| | | List<Long> projectIdList = projectRepository.findIdByLiableUserId(userId);
|
| | | // 根据用户ID查询参与的项目ID
|
| | | List<Long> taskUserProjectIdList = projectCostRepository.findProjectIdByUserId(userId);
|
| | | // 检索条件
|
| | | List<Long> totalProjectIdList = Stream.of(taskUserProjectIdList, projectIdList).flatMap(Collection::stream).distinct().collect(Collectors.toList());
|
| | | if (totalProjectIdList.size() == 0) {
|
| | | return PageUtil.toPage(Collections.emptyList(), 0);
|
| | | }
|
| | | criteria.setProjectIdList(totalProjectIdList);
|
| | | }
|
| | |
|
| | | Page<ProjectCost> page = projectCostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
| | | // 将实体类转换为DTO(重写)
|
| | | Function<ProjectCost, ProjectCostDto> func = new Function<ProjectCost, ProjectCostDto>() {
|
| | | @Override
|
| | | public ProjectCostDto apply(ProjectCost projectCost) {
|
| | | // 将实体类转换成DTO
|
| | | ProjectCostDto projectCostDto = projectCostMapper.toDto(projectCost);
|
| | | // 根据项目ID查询项目信息
|
| | | Project project = projectRepository.findById(projectCostDto.getProjectId()).orElseGet(Project::new);
|
| | | ValidationUtil.isNull(projectCostDto.getId(),"Project","id",projectCostDto.getProjectId());
|
| | | // 项目名称
|
| | | projectCostDto.setProjectName(project.getName());
|
| | | User user = userRepository.findById(projectCostDto.getUserId()).orElseGet(User::new);
|
| | | ValidationUtil.isNull(projectCostDto.getId(),"User","id",projectCostDto.getUserId());
|
| | | projectCostDto.setUserName(user.getNickName());
|
| | | return projectCostDto;
|
| | | }
|
| | | };
|
| | | return PageUtil.toPage(page.map(func));
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<ProjectCostDto> queryAll(ProjectCostQueryCriteria criteria){
|
| | | return projectCostMapper.toDto(projectCostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional
|
| | | public ProjectCostDto findById(Long id) {
|
| | | ProjectCost projectCost = projectCostRepository.findById(id).orElseGet(ProjectCost::new);
|
| | | ValidationUtil.isNull(projectCost.getId(),"ProjectCost","id",id);
|
| | | return projectCostMapper.toDto(projectCost);
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public ProjectCostDto create(ProjectCost resources) {
|
| | | Project project = projectRepository.findByIdUpdate(resources.getProjectId());
|
| | | if(project.getApproveStatus().equals(Constant.APPROVE_STATUS_ACKNOWLEDGED)){
|
| | | throw new BadRequestException("项目已承认无法新增项目成本");
|
| | | }
|
| | | return projectCostMapper.toDto(projectCostRepository.save(resources));
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public void update(ProjectCost resources) {
|
| | | Project project = projectRepository.findByIdUpdate(resources.getProjectId());
|
| | | if(project.getApproveStatus().equals(Constant.APPROVE_STATUS_ACKNOWLEDGED)){
|
| | | throw new BadRequestException("项目已承认无法修改项目成本");
|
| | | }
|
| | | ProjectCost projectCost = projectCostRepository.findById(resources.getId()).orElseGet(ProjectCost::new);
|
| | | ValidationUtil.isNull( projectCost.getId(),"ProjectCost","id",resources.getId());
|
| | | projectCost.copy(resources);
|
| | | projectCostRepository.save(projectCost);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void deleteAll(Long[] ids) {
|
| | | for (Long id : ids) {
|
| | | ProjectCost projectCost = projectCostRepository.findById(id).orElseGet(ProjectCost::new);
|
| | | Project project = projectRepository.findByIdUpdate(projectCost.getProjectId());
|
| | | if(project.getApproveStatus().equals(Constant.APPROVE_STATUS_ACKNOWLEDGED)){
|
| | | throw new BadRequestException("项目已承认无法删除项目成本");
|
| | | }
|
| | | projectCostRepository.deleteById(id);
|
| | | }
|
| | | }
|
| | |
|
| | | // @Override
|
| | | // public BigDecimal calcUserCostByDate(Long userId, Timestamp enterProjectTime, Timestamp exitProjectTime, Map<String, Double> costHoursMap) {
|
| | | // // 工资
|
| | | // BigDecimal wageCost = BigDecimal.valueOf(0);
|
| | | // // 工作天数
|
| | | // LocalDateTime enterProject = enterProjectTime.toLocalDateTime();
|
| | | // LocalDateTime exitProject = exitProjectTime.toLocalDateTime();
|
| | | //
|
| | | //// // 通过id获取用户级别工资
|
| | | //// List<UserLevelWage> sysUserLevelWages = sysUserLevelWageRepository.findUserLevelWageByUserId(userId)
|
| | | //// .stream().filter(sysUserLevelWage -> !((sysUserLevelWage.getYearMonth().getEndDate() != null && sysUserLevelWage.getEndDate().before(enterProjectTime)) || sysUserLevelWage.getStartDate().after(exitProjectTime)))
|
| | | //// .collect(Collectors.toList());
|
| | | //// // 通过id获取用户级别保险
|
| | | //// List<SysUserLevelInsurance> sysUserLevelInsurances = sysUserLevelInsuranceRepository.findByUserId(userId)
|
| | | //// .stream().filter(sysUserLevelInsurance -> !((sysUserLevelInsurance.getEndDate() != null && sysUserLevelInsurance.getEndDate().before(enterProjectTime)) || sysUserLevelInsurance.getStartDate().after(exitProjectTime)))
|
| | | //// .collect(Collectors.toList());
|
| | | //
|
| | | // List<UserLevelWage> userLevelWages = sysUserLevelWageRepository.findUserLevelWageByUserId(userId);
|
| | | // UserInsurance userInsurance = sysUserLevelInsuranceRepository.findbyUserIdAndDate(userId, enterProject.format(DateTimeFormatter.ofPattern("yyyy-MM")));
|
| | | //
|
| | | // // 判断是否有用户级别工资
|
| | | // if(userLevelWages.size() == 0){
|
| | | // throw new BadRequestException("此用户暂无级别工资");
|
| | | // }
|
| | | // UserLevelWage userLevelWage = userLevelWages.get(0);
|
| | | // // 判断是否有用户级别保险
|
| | | // if(userInsurance == null){
|
| | | // throw new BadRequestException("此用户暂无级别保险");
|
| | | // }
|
| | | //
|
| | | //// Map<Long, Double> sysUserLevelWageHoursMap = new HashMap<Long, Double>();
|
| | | //// for (SysUserLevelWage sysUserLevelWage : sysUserLevelWages) {
|
| | | //// sysUserLevelWageHoursMap.put(sysUserLevelWage.getId(), 0.0);
|
| | | //// }
|
| | | ////
|
| | | //// Map<Long, Double> sysUserLevelInsuranceHoursMap = new HashMap<Long, Double>();
|
| | | //// for (SysUserLevelInsurance sysUserLevelInsurance : sysUserLevelInsurances) {
|
| | | //// sysUserLevelInsuranceHoursMap.put(sysUserLevelInsurance.getId(), 0.0);
|
| | | //// }
|
| | | //
|
| | | //// for (LocalDate currentLocalDate = enterProject.toLocalDate();
|
| | | //// currentLocalDate.isBefore(exitProject.toLocalDate()) || currentLocalDate.isEqual(exitProject.toLocalDate());
|
| | | //// currentLocalDate = currentLocalDate.plusDays(1)) {
|
| | | ////
|
| | | //// String dateStr = currentLocalDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
| | | //// if (!costHoursMap.containsKey(dateStr)) {
|
| | | //// continue;
|
| | | //// }
|
| | | ////
|
| | | //// Date currentDate = Date.from(currentLocalDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
|
| | | ////
|
| | | //// Double costHours = costHoursMap.get(dateStr);
|
| | | ////
|
| | | //// for (SysUserLevelWage sysUserLevelWage : sysUserLevelWages) {
|
| | | //// if (sysUserLevelWage.getEndDate() == null || sysUserLevelWage.getEndDate().after(currentDate) || sysUserLevelWage.getEndDate().equals(currentDate)) {
|
| | | //// sysUserLevelWageHoursMap.put(sysUserLevelWage.getId(), costHours + sysUserLevelWageHoursMap.get(sysUserLevelWage.getId()));
|
| | | //// break;
|
| | | //// }
|
| | | //// }
|
| | | ////
|
| | | //// for (SysUserLevelInsurance sysUserLevelInsurance : sysUserLevelInsurances) {
|
| | | //// if (sysUserLevelInsurance.getEndDate() == null || sysUserLevelInsurance.getEndDate().after(currentDate) || sysUserLevelInsurance.getEndDate().equals(currentDate)) {
|
| | | //// sysUserLevelInsuranceHoursMap.put(sysUserLevelInsurance.getId(), costHours + sysUserLevelInsuranceHoursMap.get(sysUserLevelInsurance.getId()));
|
| | | //// break;
|
| | | //// }
|
| | | //// }
|
| | | //// }
|
| | | //
|
| | | // for (Entry<Long, Double> entry : sysUserLevelWageHoursMap.entrySet()) {
|
| | | // SysUserLevelWage sysLevel = sysUserLevelWageRepository.findById(entry.getKey()).orElseGet(SysUserLevelWage::new);
|
| | | // wageCost = wageCost.add(sysLevel.getLevelWage().multiply(BigDecimal.valueOf(entry.getValue())).divide(BigDecimal.valueOf(160), 2, BigDecimal.ROUND_CEILING));
|
| | | // }
|
| | | //
|
| | | // for (Entry<Long, Double> entry : sysUserLevelInsuranceHoursMap.entrySet()) {
|
| | | // SysUserLevelInsurance sysLevel = sysUserLevelInsuranceRepository.findById(entry.getKey()).orElseGet(SysUserLevelInsurance::new);
|
| | | //
|
| | | // BigDecimal accumulationRadix = sysLevel.getAccumulationRadix().multiply(BigDecimal.valueOf(sysLevel.getAccumulationCompanyProportion() / 100));
|
| | | // BigDecimal pensionRadix = sysLevel.getPensionRadix().multiply(BigDecimal.valueOf(sysLevel.getPensionCompanyProportion() / 100));
|
| | | // BigDecimal medicalRadix = sysLevel.getMedicalRadix().multiply(BigDecimal.valueOf(sysLevel.getMedicalCompanyProportion() / 100));
|
| | | // BigDecimal unemploymentRadix = sysLevel.getUnemploymentRadix().multiply(BigDecimal.valueOf(sysLevel.getUnemploymentCompanyProportion() / 100));
|
| | | // BigDecimal maternityRadix = sysLevel.getMaternityRadix().multiply(BigDecimal.valueOf(sysLevel.getMaternityCompanyProportion() / 100));
|
| | | // BigDecimal industrialInjuryRadix = sysLevel.getIndustrialInjuryRadix().multiply(BigDecimal.valueOf(sysLevel.getIndustrialInjuryCompanyProportion() / 100));
|
| | | //
|
| | | // wageCost = wageCost.add(accumulationRadix.add(pensionRadix).add(medicalRadix).add(unemploymentRadix).add(maternityRadix).add(industrialInjuryRadix).multiply(BigDecimal.valueOf(entry.getValue())).divide(BigDecimal.valueOf(160), 2, BigDecimal.ROUND_CEILING));
|
| | | // }
|
| | | //
|
| | | // return wageCost;
|
| | | // }
|
| | |
|
| | | @Override
|
| | | public BigDecimal calcUserCostByHours(Long userId, Double timeTotalCost) {
|
| | | // 工资
|
| | | BigDecimal wageCost = BigDecimal.valueOf(0);
|
| | |
|
| | | // 通过id获取用户级别工资
|
| | | List<UserLevelWage> sysUserLevelWages = sysUserLevelWageRepository.findUserLevelWageByUserId(userId);
|
| | | if (sysUserLevelWages.size() == 0) {
|
| | | return BigDecimal.ZERO;
|
| | | }
|
| | | // 通过id获取用户级别保险
|
| | | List<UserInsurance> sysUserLevelInsurances = sysUserLevelInsuranceRepository.findByUserId(userId);
|
| | | if (sysUserLevelInsurances.size() == 0) {
|
| | | return BigDecimal.ZERO;
|
| | | }
|
| | |
|
| | | // 工资
|
| | | UserLevelWage sysUserLevelWage = sysUserLevelWages.get(sysUserLevelWages.size() - 1);
|
| | | wageCost = wageCost.add(sysUserLevelWage.getBasicWage().add(sysUserLevelWage.getRankWage()).add(sysUserLevelWage.getPerformanceWage()).multiply(BigDecimal.valueOf(timeTotalCost)).divide(BigDecimal.valueOf(160), 2, BigDecimal.ROUND_CEILING));
|
| | |
|
| | | // 五险一金
|
| | | UserInsurance sysUserLevelInsurance = sysUserLevelInsurances.get(sysUserLevelInsurances.size() - 1);
|
| | | BigDecimal accumulationRadix = sysUserLevelInsurance.getAccumulationFund().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getAccumulationFundCompany() / 100));
|
| | | BigDecimal pensionRadix = sysUserLevelInsurance.getRetirementInsurance().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getRetirementInsuranceCompany() / 100));
|
| | | BigDecimal medicalRadix = sysUserLevelInsurance.getMedicalInsurance().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getMedicalInsuranceCompany() / 100));
|
| | | BigDecimal unemploymentRadix = sysUserLevelInsurance.getUnemploymentInsurance().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getUnemploymentInsuranceCompany() / 100));
|
| | | BigDecimal maternityRadix = sysUserLevelInsurance.getBirthInsurance().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getBirthInsuranceCompany() / 100));
|
| | | BigDecimal industrialInjuryRadix = sysUserLevelInsurance.getEmploymentInsurance().multiply(BigDecimal.valueOf(sysUserLevelInsurance.getEmploymentInsuranceCompany() / 100));
|
| | |
|
| | | wageCost = wageCost.add(accumulationRadix.add(pensionRadix).add(medicalRadix).add(unemploymentRadix).add(maternityRadix).add(industrialInjuryRadix).multiply(BigDecimal.valueOf(timeTotalCost)).divide(BigDecimal.valueOf(160), 2, BigDecimal.ROUND_CEILING));
|
| | |
|
| | | return wageCost;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Double getHours(ProjectCostQueryCriteria criteria) throws ParseException {
|
| | | // 工时
|
| | | Double costHours = 0.0;
|
| | |
|
| | | // 工作天数
|
| | | LocalDateTime enterProject = criteria.getEnterProjectTime().toLocalDateTime();
|
| | | LocalDateTime exitProject = criteria.getExitProjectTime().toLocalDateTime();
|
| | |
|
| | | // 通过id获取用户信息
|
| | | UserDto user = userService.findById(criteria.getUserId());
|
| | |
|
| | | List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findByHolidayDateBetween(criteria.getEnterProjectTime(), criteria.getExitProjectTime());
|
| | |
|
| | | for (LocalDate currentLocalDate = enterProject.toLocalDate();
|
| | | currentLocalDate.isBefore(exitProject.toLocalDate()) || currentLocalDate.isEqual(exitProject.toLocalDate());
|
| | | currentLocalDate = currentLocalDate.plusDays(1)) {
|
| | | String dateType = "";
|
| | | for (SysConfigHoliday sysConfigHoliday : sysConfigHolidayList) {
|
| | | if (currentLocalDate.isEqual(sysConfigHoliday.getHolidayDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())) {
|
| | | dateType = sysConfigHoliday.getDateType();
|
| | | break;
|
| | | }
|
| | | }
|
| | | if ("".equals(dateType)) {
|
| | | dateType = currentLocalDate.getDayOfWeek() == DayOfWeek.SATURDAY || currentLocalDate.getDayOfWeek() == DayOfWeek.SUNDAY ? Constant.DATE_TYPE_WEEKEND : Constant.DATE_TYPE_WEEKDAY;
|
| | | }
|
| | | if (Constant.DATE_TYPE_WEEKEND.equals(dateType) || Constant.DATE_TYPE_LEGAL_HOLIDAYS.equals(dateType) || Constant.DATE_TYPE_HOLIDAYS.equals(dateType)) {
|
| | | continue;
|
| | | }
|
| | |
|
| | | SysRosteringDto sysRostering = user.getSysRostering();
|
| | | if (currentLocalDate.isEqual(exitProject.toLocalDate())) {
|
| | | if (exitProject.toLocalTime().compareTo(sysRostering.getWorkStartTime()) > 0 && exitProject.toLocalTime().compareTo(sysRostering.getLunchEndTime()) <= 0) {
|
| | | if (exitProject.toLocalTime().compareTo(sysRostering.getLunchStartTime()) > 0) {
|
| | | exitProject = exitProject.with(sysRostering.getLunchStartTime());
|
| | | }
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getWorkStartTime().until(exitProject.toLocalTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (exitProject.toLocalTime().compareTo(sysRostering.getLunchEndTime()) > 0 && exitProject.toLocalTime().compareTo(sysRostering.getWorkEndTime()) < 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES) + sysRostering.getLunchEndTime().until(exitProject.toLocalTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (exitProject.toLocalTime().compareTo(sysRostering.getWorkEndTime()) >= 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | }
|
| | | } else if (currentLocalDate.isEqual(enterProject.toLocalDate())) {
|
| | | if (enterProject.toLocalTime().compareTo(sysRostering.getWorkStartTime()) <= 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (enterProject.toLocalTime().compareTo(sysRostering.getWorkStartTime()) > 0 && enterProject.toLocalTime().compareTo(sysRostering.getLunchStartTime()) <= 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + enterProject.toLocalTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (enterProject.toLocalTime().compareTo(sysRostering.getLunchStartTime()) > 0 && enterProject.toLocalTime().compareTo(sysRostering.getWorkEndTime()) < 0) {
|
| | | if (enterProject.toLocalTime().compareTo(sysRostering.getLunchEndTime()) < 0) {
|
| | | enterProject = enterProject.with(sysRostering.getLunchEndTime());
|
| | | }
|
| | | costHours = costHours + BigDecimal.valueOf(enterProject.toLocalTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | }
|
| | | } else {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | }
|
| | | }
|
| | | return costHours;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public ProjectCost getCost(ProjectCostQueryCriteria criteria) throws ParseException {
|
| | | ProjectCost projectCost = new ProjectCost();
|
| | | // 工资
|
| | | BigDecimal wageCost = BigDecimal.valueOf(0);
|
| | | // 工时
|
| | | Double costHours = 0.0;
|
| | |
|
| | | // 工作天数
|
| | | LocalDateTime enterProject = criteria.getEnterProjectTime().toLocalDateTime();
|
| | | LocalDateTime exitProject = criteria.getExitProjectTime().toLocalDateTime();
|
| | |
|
| | | Date startDate = criteria.getEnterProjectTime();
|
| | | Date endDate = criteria.getExitProjectTime();
|
| | |
|
| | | // 通过id获取用户信息
|
| | | UserDto user = userService.findById(criteria.getUserId());
|
| | | // // 通过id获取用户级别工资
|
| | | // Optional<SysUserLevelWage> sysUserLevelWageOpl = sysUserLevelWageRepository.findByUserId(criteria.getUserId())
|
| | | // .stream().filter(sysUserLevelWage -> !((sysUserLevelWage.getEndDate() != null && sysUserLevelWage.getEndDate().before(startDate)) || sysUserLevelWage.getStartDate().after(endDate)))
|
| | | // .max((o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));
|
| | | // // 通过id获取用户级别保险
|
| | | // Optional<SysUserLevelInsurance> sysUserLevelInsuranceOpl = sysUserLevelInsuranceRepository.findByUserId(criteria.getUserId())
|
| | | // .stream().filter(sysUserLevelInsurance -> !((sysUserLevelInsurance.getEndDate() != null && sysUserLevelInsurance.getEndDate().before(startDate)) || sysUserLevelInsurance.getStartDate().after(endDate)))
|
| | | // .max((o1, o2) -> o1.getStartDate().compareTo(o2.getStartDate()));
|
| | |
|
| | | List<UserLevelWage> userLevelWageList = sysUserLevelWageRepository.findUserLevelWageByUserId(criteria.getUserId());
|
| | | List<UserInsurance> userInsurances = sysUserLevelInsuranceRepository.findByUserId(criteria.getUserId());
|
| | |
|
| | |
|
| | | // 判断是否有用户级别工资
|
| | | if (userLevelWageList.size() == 0) {
|
| | | throw new BadRequestException("此用户暂无级别工资");
|
| | | }
|
| | | // 判断是否有用户级别保险
|
| | | if (userInsurances.size() == 0) {
|
| | | throw new BadRequestException("此用户暂无级别保险");
|
| | | }
|
| | |
|
| | | if (criteria.getInvolvementHours() == null) {
|
| | | // 获取假期数据
|
| | | List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findByHolidayDateBetween(criteria.getEnterProjectTime(), criteria.getExitProjectTime());
|
| | |
|
| | | for (LocalDate currentLocalDate = enterProject.toLocalDate();
|
| | | currentLocalDate.isBefore(exitProject.toLocalDate()) || currentLocalDate.isEqual(exitProject.toLocalDate());
|
| | | currentLocalDate = currentLocalDate.plusDays(1)) {
|
| | | String dateType = "";
|
| | | for (SysConfigHoliday sysConfigHoliday : sysConfigHolidayList) {
|
| | | if (currentLocalDate.isEqual(sysConfigHoliday.getHolidayDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate())) {
|
| | | dateType = sysConfigHoliday.getDateType();
|
| | | break;
|
| | | }
|
| | | }
|
| | | if ("".equals(dateType)) {
|
| | | dateType = currentLocalDate.getDayOfWeek() == DayOfWeek.SATURDAY || currentLocalDate.getDayOfWeek() == DayOfWeek.SUNDAY ? Constant.DATE_TYPE_WEEKEND : Constant.DATE_TYPE_WEEKDAY;
|
| | | }
|
| | | if (Constant.DATE_TYPE_WEEKEND.equals(dateType) || Constant.DATE_TYPE_LEGAL_HOLIDAYS.equals(dateType) || Constant.DATE_TYPE_HOLIDAYS.equals(dateType)) {
|
| | | continue;
|
| | | }
|
| | |
|
| | | SysRosteringDto sysRostering = user.getSysRostering();
|
| | | if (currentLocalDate.isEqual(exitProject.toLocalDate())) {
|
| | | if (exitProject.toLocalTime().compareTo(sysRostering.getWorkStartTime()) > 0 && exitProject.toLocalTime().compareTo(sysRostering.getLunchEndTime()) <= 0) {
|
| | | if (exitProject.toLocalTime().compareTo(sysRostering.getLunchStartTime()) > 0) {
|
| | | exitProject = exitProject.with(sysRostering.getLunchStartTime());
|
| | | }
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getWorkStartTime().until(exitProject.toLocalTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (exitProject.toLocalTime().compareTo(sysRostering.getLunchEndTime()) > 0 && exitProject.toLocalTime().compareTo(sysRostering.getWorkEndTime()) < 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES) + sysRostering.getLunchEndTime().until(exitProject.toLocalTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (exitProject.toLocalTime().compareTo(sysRostering.getWorkEndTime()) >= 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | }
|
| | | } else if (currentLocalDate.isEqual(enterProject.toLocalDate())) {
|
| | | if (enterProject.toLocalTime().compareTo(sysRostering.getWorkStartTime()) <= 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (enterProject.toLocalTime().compareTo(sysRostering.getWorkStartTime()) > 0 && enterProject.toLocalTime().compareTo(sysRostering.getLunchStartTime()) <= 0) {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + enterProject.toLocalTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | } else if (enterProject.toLocalTime().compareTo(sysRostering.getLunchStartTime()) > 0 && enterProject.toLocalTime().compareTo(sysRostering.getWorkEndTime()) < 0) {
|
| | | if (enterProject.toLocalTime().compareTo(sysRostering.getLunchEndTime()) < 0) {
|
| | | enterProject = enterProject.with(sysRostering.getLunchEndTime());
|
| | | }
|
| | | costHours = costHours + BigDecimal.valueOf(enterProject.toLocalTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | }
|
| | | } else {
|
| | | costHours = costHours + BigDecimal.valueOf(sysRostering.getLunchEndTime().until(sysRostering.getWorkEndTime(), ChronoUnit.MINUTES) + sysRostering.getWorkStartTime().until(sysRostering.getLunchStartTime(), ChronoUnit.MINUTES)).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_CEILING).doubleValue();
|
| | | }
|
| | | }
|
| | | } else {
|
| | | costHours = criteria.getInvolvementHours();
|
| | | }
|
| | |
|
| | | // 参与工时
|
| | | projectCost.setInvolvementHours(BigDecimal.valueOf(costHours));
|
| | |
|
| | | // 用户级别工资成本
|
| | | wageCost = wageCost.add(userLevelWageList.get(0).getBasicWage())
|
| | | .add(userLevelWageList.get(0).getRankWage())
|
| | | .add(userLevelWageList.get(0).getPerformanceWage())
|
| | | .multiply(BigDecimal.valueOf(costHours))
|
| | | .divide(BigDecimal.valueOf(160), 2, BigDecimal.ROUND_CEILING);
|
| | |
|
| | | // 用户级别保险成本
|
| | | UserInsurance sysLevelInsurance = userInsurances.stream().max(Comparator.comparing(UserInsurance::getYearMonth)).orElseGet(UserInsurance::new);
|
| | |
|
| | |
|
| | | BigDecimal accumulationRadix = sysLevelInsurance.getAccumulationFund().multiply(BigDecimal.valueOf(sysLevelInsurance.getAccumulationFundCompany() / 100));
|
| | | BigDecimal pensionRadix = sysLevelInsurance.getRetirementInsurance().multiply(BigDecimal.valueOf(sysLevelInsurance.getRetirementInsuranceCompany() / 100));
|
| | | BigDecimal medicalRadix = sysLevelInsurance.getMedicalInsurance().multiply(BigDecimal.valueOf(sysLevelInsurance.getMedicalInsuranceCompany() / 100));
|
| | | BigDecimal unemploymentRadix = sysLevelInsurance.getUnemploymentInsurance().multiply(BigDecimal.valueOf(sysLevelInsurance.getUnemploymentInsuranceCompany() / 100));
|
| | | BigDecimal maternityRadix = sysLevelInsurance.getBirthInsurance().multiply(BigDecimal.valueOf(sysLevelInsurance.getBirthInsuranceCompany() / 100));
|
| | | BigDecimal industrialInjuryRadix = sysLevelInsurance.getEmploymentInsurance().multiply(BigDecimal.valueOf(sysLevelInsurance.getEmploymentInsuranceCompany() / 100));
|
| | |
|
| | | wageCost = wageCost.add(accumulationRadix.add(pensionRadix).add(medicalRadix).add(unemploymentRadix).add(maternityRadix).add(industrialInjuryRadix).multiply(BigDecimal.valueOf(costHours)).divide(BigDecimal.valueOf(160), 2, BigDecimal.ROUND_CEILING));
|
| | |
|
| | | projectCost.setProjectCost(wageCost);
|
| | | return projectCost;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void download(List<ProjectCostDto> all, HttpServletResponse response) throws IOException {
|
| | | List<Map<String, Object>> list = new ArrayList<>();
|
| | | for (ProjectCostDto projectCost : all) {
|
| | | String projectName = projectCost.getProjectId() != null ? projectRepository.findById(projectCost.getProjectId()).orElseGet(Project::new).getName() : "";
|
| | | String user = projectCost.getUserId() != null ? userRepository.findById(projectCost.getUserId()).orElseGet(User::new).getNickName() : "";
|
| | | Map<String,Object> map = new LinkedHashMap<>();
|
| | | map.put("项目", projectName);
|
| | | map.put("用户", user);
|
| | | map.put("进入项目时间", projectCost.getEnterProjectTime());
|
| | | map.put("退出项目时间", projectCost.getExitProjectTime());
|
| | | map.put("参与时间", projectCost.getInvolvementHours());
|
| | | map.put("项目成本", projectCost.getProjectCost());
|
| | | map.put("备注", projectCost.getRemarks());
|
| | | map.put("创建者", projectCost.getCreateBy());
|
| | | map.put("创建时间", projectCost.getCreateTime());
|
| | | map.put("更新者", projectCost.getUpdateBy());
|
| | | map.put("更新时间", projectCost.getUpdateTime());
|
| | | list.add(map);
|
| | | }
|
| | | FileUtil.downloadExcel(list, response);
|
| | | }
|
| | | } |