111
tjx
4 天以前 9d0ce1368fc71660a8b9873af8f275e7689b7394
src/test/java/com/gs/dingtalk/DeviceReceivingApplicationTests.java
@@ -1,6 +1,9 @@
package com.gs.dingtalk;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gs.dingtalk.config.URLEncoder;
import com.gs.dingtalk.entity.QwStaff;
import com.gs.dingtalk.mapper.QwStaffMapper;
import com.gs.dingtalk.service.SendDingtalkService;
import com.gs.dingtalk.service.SimpleExample;
import com.gs.dingtalk.service.VwCjScSjTsBbService;
@@ -24,6 +27,9 @@
    @Autowired
    private WorkWXService workWXService;
    @Autowired
    private QwStaffMapper qwStaffMapper;
    /**
     * 测试导出生产数据并发送钉钉消息
@@ -56,15 +62,41 @@
    void testGetCheckinDataByQwStaff() {
        System.out.println("=== 开始测试通过QW_STAFF表获取打卡数据 ===");
        // 企业微信打卡接口限制:
        // 1. 获取记录时间跨度不超过30天
        // 2. 用户列表不超过100个。若用户超过100个,请分批获取(已在Service层实现自动分批)
        // 3. 接口返回最多3000条打卡数据
        // 4. 标准打卡时间只对于固定排班和自定义排班两种类型有效
        // 5. 接口调用频率限制为600次/分钟(已在Service层实现批次间延迟)
        try {
            long currentTime = System.currentTimeMillis() / 1000;
            long oneDaySeconds = 86400;
            long thirtyDaysSeconds = 30 * oneDaySeconds;
            long endTime = (currentTime / oneDaySeconds) * oneDaySeconds - 1;
            long startTime = endTime - oneDaySeconds + 1;
            // 验证时间跨度不超过30天
            long timeSpan = endTime - startTime;
            if (timeSpan > thirtyDaysSeconds) {
                System.out.println("✗ 时间跨度超过30天限制: " + (timeSpan / oneDaySeconds) + "天");
                return;
            }
            System.out.println("  - 开始时间: " + new java.util.Date(startTime * 1000));
            System.out.println("  - 结束时间: " + new java.util.Date(endTime * 1000));
            System.out.println("  - 时间跨度: " + (timeSpan / oneDaySeconds) + "天 (限制: ≤30天)");
            // 获取用户总数
            long totalUsers = qwStaffMapper.selectCount(new LambdaQueryWrapper<QwStaff>()
                    .isNotNull(QwStaff::getAccount)
                    .ne(QwStaff::getAccount, ""));
            System.out.println("  - QW_STAFF表用户总数: " + totalUsers);
            if (totalUsers > 100) {
                int batchCount = (int) ((totalUsers + 99) / 100);
                System.out.println("  - 将自动分批处理: " + batchCount + "批 (每批≤100用户)");
            }
            List<WorkWXService.CheckinData> checkinDataList = workWXService.getCheckinDataByQwStaff(startTime, endTime);
@@ -81,6 +113,54 @@
                });
            } else {
                System.out.println("✗ 获取的打卡数据为空(可能QW_STAFF表无数据或时间范围内无打卡记录)");
            }
        } catch (IOException e) {
            System.out.println("✗ 获取打卡数据失败: " + e.getMessage());
            e.printStackTrace();
        }
        System.out.println("=== 测试结束 ===");
    }
    @Test
    void testGetCheckinDataById() {
        System.out.println("=== 开始测试通过QW_STAFF表获取打卡数据 ===");
        QwStaff qwStaff = qwStaffMapper.selectById(3);
        if (qwStaff == null || qwStaff.getAccount() == null || qwStaff.getAccount().isEmpty()) {
            System.out.println("✗ 未找到ID为3的员工或员工account为空");
            return;
        }
        System.out.println("  - 员工姓名: " + qwStaff.getName());
        System.out.println("  - 员工账号: " + qwStaff.getAccount());
        try {
            long currentTime = System.currentTimeMillis() / 1000;
            long oneDaySeconds = 86400;
            long endTime = (currentTime / oneDaySeconds) * oneDaySeconds - 1;
            long startTime = endTime - oneDaySeconds + 1;
            System.out.println("  - 开始时间: " + new java.util.Date(startTime * 1000));
            System.out.println("  - 结束时间: " + new java.util.Date(endTime * 1000));
            List<String> useridList = new java.util.ArrayList<>();
            useridList.add(qwStaff.getAccount());
            List<WorkWXService.CheckinData> checkinDataList = workWXService.getCheckinData(startTime, endTime, useridList);
            if (checkinDataList != null && !checkinDataList.isEmpty()) {
                System.out.println("✓ 成功获取打卡数据");
                System.out.println("  - 打卡记录总数: " + checkinDataList.size());
                checkinDataList.forEach(data -> {
                    System.out.println("    * 打卡时间: " + new java.util.Date(data.getCheckinTime() * 1000) +
                            ", 打卡类型: " + data.getCheckinType() +
                            ", 异常类型: " + data.getExceptionType() +
                            ", 地点: " + data.getLocationDetail());
                });
            } else {
                System.out.println("✗ 该员工在时间范围内无打卡记录");
            }
        } catch (IOException e) {
            System.out.println("✗ 获取打卡数据失败: " + e.getMessage());
@@ -118,24 +198,16 @@
    }
    @Test
    void testGetUserDetail() {
        System.out.println("=== 开始测试获取用户详情 ===");
    void testSyncUsersToQwStaff() {
        System.out.println("=== 开始测试同步企业微信用户到QW_STAFF表 ===");
        try {
            String userid = "ShenJuanYue";
            WorkWXService.WorkWXUserDetail userDetail = workWXService.getUserDetail(userid);
            int insertCount = workWXService.syncUsersToQwStaff();
            if (userDetail != null && userDetail.getErrcode() == 0) {
                System.out.println("✓ 成功获取用户详情");
                System.out.println("  - userid: " + userDetail.getUserid());
                System.out.println("  - 姓名: " + userDetail.getName());
                System.out.println("  - 手机号: " + userDetail.getMobile());
                System.out.println("  - 职位: " + userDetail.getPosition());
            } else {
                System.out.println("✗ 获取用户详情失败");
            }
            System.out.println("✓ 同步完成");
            System.out.println("  - 新增用户数: " + insertCount);
        } catch (IOException e) {
            System.out.println("✗ 获取用户详情失败: " + e.getMessage());
            System.out.println("✗ 同步用户失败: " + e.getMessage());
            e.printStackTrace();
        }