hyingbo
6 天以前 e935889261ef38c8eaef31e54cbfc466d63d2ef4
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
package org.jeecg.modules.mdc.service.impl;
 
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.mdc.entity.MdcEquipmentPunch;
import org.jeecg.modules.mdc.entity.MdcEquipmentPunchRate;
import org.jeecg.modules.mdc.mapper.MdcEquipmentPunchMapper;
import org.jeecg.modules.mdc.mapper.MdcEquipmentPunchRateMapper;
import org.jeecg.modules.mdc.service.IMdcEquipmentPunchRateService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.UUID;
 
/**
 * @Description: mdc_equipment_punch
 * @Author: jeecg-boot
 * @Date:   2025-06-09
 * @Version: V1.0
 */
@Service
public class MdcEquipmentPunchRateServiceImpl extends ServiceImpl<MdcEquipmentPunchRateMapper, MdcEquipmentPunchRate> implements IMdcEquipmentPunchRateService {
    @Resource
    private MdcEquipmentPunchRateMapper mdcEquipmentPunchRateMapper;
 
    @Resource
    private MdcEquipmentPunchMapper mdcEquipmentPunchMapper;
    @Override
    public void savePunchRates(String targetDate, List<MdcEquipmentPunch> punchRecords) {
 
        int morningIn = mdcEquipmentPunchMapper.countMorningShiftIn(targetDate);
        int eveningIn = mdcEquipmentPunchMapper.countEveningShiftIn(targetDate);
        int morningOut = mdcEquipmentPunchMapper.countMorningShiftOut(targetDate);
        int eveningOut = mdcEquipmentPunchMapper.countEveningShiftOut(targetDate);
        for (MdcEquipmentPunch punch : punchRecords) {
            MdcEquipmentPunchRate rate = new MdcEquipmentPunchRate();
 
            rate.setId(UUID.randomUUID().toString()); // 生成唯一ID
            rate.setEquipmentId(punch.getEquipmentId());
            rate.setTheDate(targetDate);
            rate.setShiftSchedule(punch.getShiftSchedule());
 
 
 
 
            // 设置设备数量
            rate.setMornShiftInNum(morningIn);
            rate.setMornShiftOutNum(eveningIn);
            rate.setEvenShiftInNum(morningOut);
            rate.setEvenShiftOutNum(eveningOut);
 
 
 
            // 获取总设备数
            int totalDevices = mdcEquipmentPunchMapper.getTotalDeviceCount();
            if (totalDevices == 0) return;
 
 
            rate.setDeviceCountNum(totalDevices);
 
            // 计算打卡率(保留两位小数)
            rate.setMornShiftInRate(calculateRate(morningIn, totalDevices));
            rate.setMornShiftOutRate(calculateRate(eveningIn, totalDevices));
            rate.setEvenShiftInRate(calculateRate(morningOut, totalDevices));
            rate.setEvenShiftOutRate(calculateRate(eveningOut, totalDevices));
 
            this.save(rate);
        }
    }
    private BigDecimal calculateRate(int actual, int total) {
        if (total == 0) return BigDecimal.ZERO;
        return new BigDecimal(actual)
                .divide(new BigDecimal(total), 4, RoundingMode.DOWN)
                .multiply(BigDecimal.valueOf(100))
                .setScale(2, RoundingMode.DOWN); // 保留两位小数,不进行四舍五入
    }
 
}