#!/bin/bash
# 钉钉工作日志查询脚本

# 配置（从 TOOLS.md 读取）
APP_KEY="dingklemniq8uqk5qbgx"
APP_SECRET="_8EHgyhvHRHRMx6fZbh9LNpQoxyYl3At0b-fXXlQiahwupbt9oY5P6Grj8IM9Dx8"
API_BASE="https://oapi.dingtalk.com"

# 获取 access_token
get_token() {
  curl -s -X POST 'https://api.dingtalk.com/v1.0/oauth2/accessToken' \
    -H 'Content-Type: application/json' \
    -d "{\"appKey\":\"$APP_KEY\",\"appSecret\":\"$APP_SECRET\"}" | \
    grep -o '"accessToken":"[^"]*"' | cut -d'"' -f4
}

# 查询日志
query_logs() {
  local userid=$1
  local start_time=$2  # 毫秒
  local end_time=$3    # 毫秒
  local size=${4:-10}
  local cursor=${5:-0}
  local token=$6

  curl -s "$API_BASE/topapi/report/list?access_token=$token" \
    -H 'Content-Type: application/json' \
    -d "{
      \"userid\": \"$userid\",
      \"start_time\": $start_time,
      \"end_time\": $end_time,
      \"cursor\": $cursor,
      \"size\": $size
    }"
}

# 解析毫秒时间戳为日期
ms_to_date() {
  local ms=$1
  date -d @$((ms / 1000)) "+%Y-%m-%d %H:%M:%S"
}

# 帮助信息
usage() {
  echo "钉钉工作日志查询"
  echo ""
  echo "用法: $0 <userid> <start_time_ms> <end_time_ms> [size] [cursor]"
  echo ""
  echo "示例:"
  echo "  $0 121922510028034588 1738329600000 1743292800000"
  echo "  $0 121922510028034588 1738329600000 1743292800000 20 0"
  echo ""
  echo "时间戳获取: date +%s000000 (mac/linux)"
}
