3 天以前 b852254d90e7d1955db31db154193ec8ee84d8b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cn.iocoder.yudao.module.hrm.service.workhour;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.hrm.controller.admin.workhour.vo.HrmEmployeeWorkHourReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.workhour.vo.HrmWorkHourSummaryPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.workhour.vo.HrmWorkHourSummaryRespVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.workhour.HrmWorkHourSummaryDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmAttendanceRecordMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.workhour.HrmWorkHourSummaryMapper;
import cn.iocoder.yudao.module.hrm.service.employee.HrmEmployeeService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.math.BigDecimal;
import java.time.YearMonth;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
/**
 * 月度工时汇总 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class HrmWorkHourSummaryServiceImpl implements HrmWorkHourSummaryService {
 
    @Resource
    private HrmWorkHourSummaryMapper workHourSummaryMapper;
    @Resource
    private HrmAttendanceRecordMapper attendanceRecordMapper;
    @Resource
    private HrmEmployeeService employeeService;
    @Resource
    private DeptApi deptApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void generateMonthly(String period) {
        YearMonth yearMonth = YearMonth.parse(period);
        HrmEmployeeWorkHourReqVO reqVO = new HrmEmployeeWorkHourReqVO();
        reqVO.setStartDate(yearMonth.atDay(1));
        reqVO.setEndDate(yearMonth.atEndOfMonth());
        for (Map<String, Object> row : attendanceRecordMapper.selectWorkHourSummary(reqVO)) {
            Long employeeId = toLong(row.get("user_id"));
            if (employeeId == null) {
                continue;
            }
            HrmEmployeeDO employee = employeeService.getEmployee(employeeId);
            HrmWorkHourSummaryDO summary = workHourSummaryMapper.selectByEmployeeIdAndPeriod(employeeId, period);
            if (summary == null) {
                summary = new HrmWorkHourSummaryDO();
                summary.setEmployeeId(employeeId);
                summary.setPeriod(period);
            }
            summary.setDeptId(employee != null ? employee.getDeptId() : null);
            summary.setWorkDays(toInteger(row.get("work_days")));
            summary.setWorkHours(toDecimal(row.get("work_hours")));
            summary.setOvertimeHours(toDecimal(row.get("overtime_hours")));
            if (summary.getId() == null) {
                workHourSummaryMapper.insert(summary);
            } else {
                workHourSummaryMapper.updateById(summary);
            }
        }
    }
 
    @Override
    public PageResult<HrmWorkHourSummaryRespVO> getWorkHourSummaryPage(HrmWorkHourSummaryPageReqVO pageReqVO) {
        PageResult<HrmWorkHourSummaryDO> pageResult = workHourSummaryMapper.selectPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return new PageResult<>(Collections.emptyList(), pageResult.getTotal());
        }
        List<HrmWorkHourSummaryRespVO> list = pageResult.getList().stream()
                .map(this::convert)
                .toList();
        return new PageResult<>(list, pageResult.getTotal());
    }
 
    private HrmWorkHourSummaryRespVO convert(HrmWorkHourSummaryDO summary) {
        HrmWorkHourSummaryRespVO respVO = new HrmWorkHourSummaryRespVO();
        respVO.setId(summary.getId());
        respVO.setEmployeeId(summary.getEmployeeId());
        respVO.setDeptId(summary.getDeptId());
        respVO.setPeriod(summary.getPeriod());
        respVO.setWorkDays(summary.getWorkDays());
        respVO.setWorkHours(summary.getWorkHours());
        respVO.setOvertimeHours(summary.getOvertimeHours());
        respVO.setRemark(summary.getRemark());
        respVO.setCreateTime(summary.getCreateTime());
        HrmEmployeeDO employee = employeeService.getEmployee(summary.getEmployeeId());
        if (employee != null) {
            respVO.setEmployeeNo(employee.getEmployeeNo());
            respVO.setEmployeeName(employee.getName());
        }
        if (summary.getDeptId() != null) {
            DeptRespDTO dept = deptApi.getDept(summary.getDeptId());
            if (dept != null) {
                respVO.setDeptName(dept.getName());
            }
        }
        return respVO;
    }
 
    private Long toLong(Object value) {
        return value instanceof Number ? ((Number) value).longValue() : null;
    }
 
    private Integer toInteger(Object value) {
        return value instanceof Number ? ((Number) value).intValue() : 0;
    }
 
    private BigDecimal toDecimal(Object value) {
        return value instanceof Number ? new BigDecimal(value.toString()) : BigDecimal.ZERO;
    }
 
}