let test_data = {
    "info": {
        "id": "202509231006",
        "name": "铁力路站~友谊路站测试巡查任务",
        "time": "2025-09-23 10:06:57",
        "timestamp": 1758593217.160917,
        "type": 0
    },
    "line": {
        "code": "03",
        "interval": "铁力路~友谊路",
        "interval_code": "03001",
        "name": "地铁3号线",
        "status": 1
    },
    "risk": {
        "content": "发现1个吊车",
        "id": "20250923100600003",
        "level": 2,
        "mark": {
        "left": [
            1062,
            1102
        ],
        "right": [
            815,
            926
        ]
        },
        "photo": {
        "origin": "https://subway-guiyang.oss-cn-shanghai.aliyuncs.com/Chengjian/202509231006/S202509231006-1.jpg",
        "result": "https://subway-guiyang.oss-cn-shanghai.aliyuncs.com/Chengjian/202509231006/R202509231006-1.jpg"
        },
        "time": "2025-09-23 10:07:19",
        "timestamp": 1758593239.038757,
        "type": 1011,
        "video": {
        "origin": "https://subway-guiyang.oss-cn-shanghai.aliyuncs.com/Chengjian/S202509231006.mp4",
        "result": "https://subway-guiyang.oss-cn-shanghai.aliyuncs.com/Chengjian/R202509231006.mp4"
        },
        "wgs84": {
        "x": 31.40948546,
        "y": 121.459669216064
        }
    }
}

// 目标类别
// {
// "Crane": "吊车",
// "Digger": "挖掘机",
// "Mound": "堆土",
// "Steel": "施工钢材",
// "TowerCrane": "塔吊",
// "Bulldozer": "推土车",
// "PileFoundation": "打桩机",
// "Tankcar": "罐车",
// "Grouting": "灌浆车",
// "Roller": "压路车",
// "Construction": "疑似施工机具",
// "Gantry": "龙门架",
// "Ladder": "登高车"
// }
function main() {
    let data = payload.get()?.body?.toObject()
    // 判断data的数据结构是否和test_data匹配
    if (data && data.info && data.risk && data.line) {
        const { name, time: findTime,id:taskId } = data.info || {}
        const { interval_code:stationId } = data.line || {}
        const { wgs84, photo,level,type } = data.risk || {}
        const extra = JSON.stringify(data)
        if (name && findTime && wgs84 && wgs84.lng && wgs84.lat && taskId && stationId) {
            const id = ex.suid().base58()
            let fileId
            if (photo?.origin) {
                const img = photo.result
                // 存储图片
                fileId = ex.suid().base58()
                const fileRes = SQL.exec('system_sql', `INSERT INTO sys_files (file_id, file_path,file_name) VALUES (?, ?,?)`, fileId, img, '标注图片.png')
                if (!fileRes) {
                    return errMsg(500, '图片存储失败')
                }
            }
            // 插入事件表
            const res = SQL.exec('system_sql', `INSERT INTO dr_event (name, find_time, longitude, latitude,extra,proprietor_id,
                 proprietor_name,
                 station_id,
                 task_id,
                 id 
                 ${fileId ? ',file_img' : ''}
                 ${level||level==0 ? ',abnormal_level' : ''}
                 ${type ? ',aim_type' : ''}
                ) VALUES (?, ?, ?, ?,?,?,?,?,?,? ${fileId ? ',?' : ''} ${level||level==0 ? ',?' : ''} ${type ? ',?' : ''})`, 
                 name, findTime, wgs84.lng, wgs84.lat, extra, '1', '系统推送',
                 stationId, taskId,
                 id, 
                 fileId ? fileId : null,
                 level||level==0 ? level : null,
                 type ? type : null
                )
            if (res) {
                return okMsg(true)
            }
        }
        // 记录缺失字段
        const miss = []
        if (!name) miss.push('name')
        if (!findTime) miss.push('time')
        if (!wgs84) miss.push('wgs84')
        if (!wgs84.lat) miss.push('wgs84.lat')
        if (!wgs84.lng) miss.push('wgs84.lng')
        if (!taskId) miss.push('taskId')
        if (!stationId) miss.push('stationId')
        return errMsg(400, `缺少字段: ${miss.join(', ')}`)
    }
    return errMsg(400, "数据结构错误")
}