111
tjx
16 小时以前 8e3309ef57424194ce9683175b59d3c9e8cb0b27
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.gs.dingtalk.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gs.dingtalk.entity.QwCheckinDayData;
import com.gs.dingtalk.mapper.QwCheckinDayDataMapper;
import com.gs.dingtalk.service.QwCheckinDayDataService;
import com.gs.dingtalk.service.WorkWXService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.util.Date;
import java.util.List;
 
/**
 * 企业微信打卡日报数据Service实现
 */
@Service
@RequiredArgsConstructor
public class QwCheckinDayDataServiceImpl extends ServiceImpl<QwCheckinDayDataMapper, QwCheckinDayData>
        implements QwCheckinDayDataService {
 
    private static final Logger log = LoggerFactory.getLogger(QwCheckinDayDataServiceImpl.class);
 
    private final WorkWXService workWXService;
 
    @Override
    public QwCheckinDayData convertToEntity(WorkWXService.CheckinDayData dayData) {
        if (dayData == null) {
            return null;
        }
 
        QwCheckinDayData entity = new QwCheckinDayData();
 
        // 基础信息
        WorkWXService.BaseInfo baseInfo = dayData.getBaseInfo();
        if (baseInfo != null) {
            entity.setReportDate(baseInfo.getDate());
            if (baseInfo.getDate() != null) {
                entity.setReportDatetime(new Date(baseInfo.getDate() * 1000));
            }
            entity.setAcctid(baseInfo.getAcctid());
            entity.setName(baseInfo.getName());
            entity.setNameEx(baseInfo.getNameEx());
            entity.setDepartsName(baseInfo.getDepartsName());
            entity.setRecordType(baseInfo.getRecordType());
            entity.setDayType(baseInfo.getDayType());
 
            // 规则信息
            WorkWXService.RuleInfo ruleInfo = baseInfo.getRuleInfo();
            if (ruleInfo != null) {
                entity.setGroupid(ruleInfo.getGroupid());
                entity.setGroupname(ruleInfo.getGroupname());
                entity.setScheduleid(ruleInfo.getScheduleid());
                entity.setSchedulename(ruleInfo.getSchedulename());
            }
        }
 
        // 汇总信息
        WorkWXService.SummaryInfo summaryInfo = dayData.getSummaryInfo();
        if (summaryInfo != null) {
            entity.setCheckinCount(summaryInfo.getCheckinCount());
            entity.setRegularWorkSec(summaryInfo.getRegularWorkSec());
            entity.setStandardWorkSec(summaryInfo.getStandardWorkSec());
            entity.setEarliestTime(summaryInfo.getEarliestTime());
            entity.setLastestTime(summaryInfo.getLastestTime());
        }
 
        // 异常信息 - 初始化默认值
        entity.setLateCount(0);
        entity.setLateDuration(0);
        entity.setEarlyLeaveCount(0);
        entity.setEarlyLeaveDuration(0);
        entity.setAbsentCount(0);
        entity.setAbsenteeismCount(0);
        entity.setAbsenteeismDuration(0);
        entity.setLocationExCount(0);
        entity.setDeviceExCount(0);
 
        // 解析异常信息
        List<WorkWXService.ExceptionInfo> exceptionInfos = dayData.getExceptionInfos();
        if (exceptionInfos != null && !exceptionInfos.isEmpty()) {
            for (WorkWXService.ExceptionInfo ex : exceptionInfos) {
                if (ex.getException() == null) continue;
                switch (ex.getException()) {
                    case 1: // 迟到
                        entity.setLateCount(ex.getCount() != null ? ex.getCount() : 0);
                        entity.setLateDuration(ex.getDuration() != null ? ex.getDuration() : 0);
                        break;
                    case 2: // 早退
                        entity.setEarlyLeaveCount(ex.getCount() != null ? ex.getCount() : 0);
                        entity.setEarlyLeaveDuration(ex.getDuration() != null ? ex.getDuration() : 0);
                        break;
                    case 3: // 缺卡
                        entity.setAbsentCount(ex.getCount() != null ? ex.getCount() : 0);
                        break;
                    case 4: // 旷工
                        entity.setAbsenteeismCount(ex.getCount() != null ? ex.getCount() : 0);
                        entity.setAbsenteeismDuration(ex.getDuration() != null ? ex.getDuration() : 0);
                        break;
                    case 5: // 地点异常
                        entity.setLocationExCount(ex.getCount() != null ? ex.getCount() : 0);
                        break;
                    case 6: // 设备异常
                        entity.setDeviceExCount(ex.getCount() != null ? ex.getCount() : 0);
                        break;
                }
            }
        }
 
        // 加班信息
        entity.setOtStatus(0);
        entity.setOtDuration(0);
        WorkWXService.OtInfo otInfo = dayData.getOtInfo();
        if (otInfo != null) {
            entity.setOtStatus(otInfo.getOtStatus() != null ? otInfo.getOtStatus() : 0);
            entity.setOtDuration(otInfo.getOtDuration() != null ? otInfo.getOtDuration() : 0);
        }
 
        entity.setCreateTime(new Date());
        return entity;
    }
 
    @Override
    public int saveDayDataBatch(List<WorkWXService.CheckinDayData> dayDataList) {
        if (dayDataList == null || dayDataList.isEmpty()) {
            return 0;
        }
 
        int insertCount = 0;
        for (WorkWXService.CheckinDayData dayData : dayDataList) {
            QwCheckinDayData entity = convertToEntity(dayData);
            if (entity == null || entity.getAcctid() == null || entity.getReportDate() == null) {
                continue;
            }
 
            // 检查是否已存在(按acctid + report_date去重)
            LambdaQueryWrapper<QwCheckinDayData> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(QwCheckinDayData::getAcctid, entity.getAcctid())
                    .eq(QwCheckinDayData::getReportDate, entity.getReportDate());
 
            QwCheckinDayData existing = this.getOne(wrapper);
            if (existing != null) {
                // 已存在则更新
                entity.setId(existing.getId());
                this.updateById(entity);
                log.debug("更新打卡日报数据: acctid={}, date={}", entity.getAcctid(), entity.getReportDatetime());
            } else {
                // 不存在则插入
                this.save(entity);
                insertCount++;
                log.debug("新增打卡日报数据: acctid={}, date={}", entity.getAcctid(), entity.getReportDatetime());
            }
        }
 
        log.info("打卡日报数据保存完成,新增: {}, 总处理: {}", insertCount, dayDataList.size());
        return insertCount;
    }
 
    @Override
    public int syncDayData(long date) throws IOException {
        List<WorkWXService.CheckinDayData> dayDataList = workWXService.getCheckinDayDataByQwStaff(date, date);
        return saveDayDataBatch(dayDataList);
    }
 
    @Override
    public int syncYesterdayDayData() throws IOException {
        long currentTime = System.currentTimeMillis() / 1000;
        long oneDaySeconds = 86400;
        long yesterdayStart = ((currentTime / oneDaySeconds) - 1) * oneDaySeconds;
 
        log.info("开始同步昨天({})的打卡日报数据", new Date(yesterdayStart * 1000));
        return syncDayData(yesterdayStart);
    }
}