package me.zhengjie.modules.quartz.task;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Component
public class ZkDataPushTask {

    private final String key = "DMERPYT!@#$QWER2021+{:>";

    public void run() {
        log.info("pushAttendanceData 执行开始");
        pushAttendanceData();
        log.info("pushAttendanceData 执行结束");
    }

    private void pushAttendanceData(){
        String yktAttendanceApi = "http://192.168.1.25:8000/api/thirdparty/v2/user/getAttendance";
        String erpAttendanceApi = "http://10.22.2.65:16000/attendance-api/device/identify/callback";
        Map<String,Object> newToken = SecurityUtils.getToken(key);
        JSONObject param = new JSONObject();
        param.put("timestamp",newToken.get("timestamp"));
        param.put("nonce",newToken.get("nonce"));
        String res = HttpRequest.get(yktAttendanceApi)
                .header("XYTACCESSTOKEN",newToken.get("token").toString() )
                .body(param.toJSONString()).execute().body();
        if(StringUtils.isNotEmpty(res)) {
            log.info("res:"+res);
            JSONArray attendanceArray = JSONArray.parseArray(res);
            for(int i = 0;i<attendanceArray.size();i++){
                JSONObject attendance = attendanceArray.getJSONObject(i);
                //组装返回数据
                JSONObject ertAttendanceJSON = new JSONObject();
                ertAttendanceJSON.put("deviceKey",attendance.getString("roomDevice"));
                ertAttendanceJSON.put("personId",attendance.getString("userNumber"));
                ertAttendanceJSON.put("time",attendance.getString("attendanceTime"));
                ertAttendanceJSON.put("type","face_0");
                HttpRequest.get(erpAttendanceApi).body(ertAttendanceJSON.toJSONString()).execute().body();
            }
        } else {
            log.info("res is null.");
        }
    }
}
