package com.zcloud.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.modules.job.task.ITask;
import com.zcloud.util.personnelPositioning.OpenAPI;
import com.zcloud.util.personnelPositioning.sendAPI;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.*;

@Component("userPositionInfoTask")
public class UserPositionInfoTask implements ITask {

    @Autowired
    private StringRedisTemplate redisTemplate;

    protected static String host = "https://xfusion.test.qxwz.com/mercatorapi";
    protected static String sik = "d502a502b90ab8d36a572e8b345a58c0";
    protected static String sis = "c059bfc19996bf2f30d6cf6cf995590477747eea71555320817bc3cf4166144b";


     // protected static String host = "http://172.16.11.2/mercatorapi";
     // protected static String sik = "b82eb77604d29fb993222f5b286ae77d";
     // protected static String sis = "c1b6e98e370e93151ea91826e457c46c02df2e687cfcbda1ea7fba653a7da1b4";

    //
    @Override
    public void run(String params) {
        String apiName = OpenAPI.FINDS_TRACK_STAFF_LIST;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNo","1");
        paramMap.put("pageSize","9999");
        JSONObject redisValueJson = sendAPI.send(apiName, paramMap);
        JSONObject dataJson = JSONObject.parseObject(redisValueJson.getString("data"));
        JSONArray data = dataJson.getJSONArray("data");
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < data.size(); i++) {
            JSONObject item = data.getJSONObject(i);
            JSONObject terminal = JSONObject.parseObject(item.getString("terminal"));
            if(ObjectUtils.allNotNull(terminal)){
                JSONObject redisJson = new JSONObject();
                String terminalNo = terminal.getString("terminalNo");
                String userName = item.getString("staffName");
                redisJson.put(terminalNo,userName);
                jsonArray.add(redisJson);
            }
        }
        redisTemplate.opsForValue().set("userPositionInfo",jsonArray.toJSONString());
    }
}
