package com.zcloud.modules.map.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.common.utils.R;
import com.zcloud.modules.map.service.UsersPositionService;
import com.zcloud.util.personnelPositioning.OpenAPI;
import com.zcloud.util.personnelPositioning.OpenAPIUtils;
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.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ExecutionException;

/**
 * 说明：用户服务接口实现类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Service
@Transactional //开启事物
public class UsersPositionServiceImpl implements UsersPositionService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取人员定位信息
     * @return
     */
    public R findRedisAll() {
        Set<String> keys = redisTemplate.keys("Kafka@".concat("*"));
        List<String> list = redisTemplate.opsForValue().multiGet(keys);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        return R.ok(map);
    }

    /**
     * 获取人员轨迹信息
     */
    public R getPersonnelTrack(Map<String, Object> paramMap) {
        String apiName = OpenAPI.FINDS_POINT_TRACE;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 按照指定格式解析字符串
        LocalDateTime startTimeLong = LocalDateTime.parse(paramMap.get("startTime").toString(), formatter);
        LocalDateTime endTimeTimeLong = LocalDateTime.parse(paramMap.get("endTime").toString(), formatter);
        paramMap.put("startTime",startTimeLong+"");
        paramMap.put("endTime",endTimeTimeLong+"");
        JSONObject redisValueJson = sendAPI.send(apiName, paramMap);
        JSONObject dataJson = JSONObject.parseObject(redisValueJson.getString("data"));
        Map<String, Object> map = new HashMap<>();
        map.put("data", dataJson);
        return R.ok(map);
    }

     public R getFenceList() {
        String apiName = OpenAPI.FINDS_FENCE_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"));
        Map<String, Object> map = new HashMap<>();
        map.put("list", dataJson);
        return R.ok(map);
     }


    public R getAlarmCount() {
        String apiName = OpenAPI.FINDS_ALARM_LIST;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNo","1");
        paramMap.put("pageSize","9999");
        // 获取当前时间戳
        // Instant currentInstant = Instant.now();
        // long currentTimestamp = currentInstant.getEpochSecond();
////
        // // 计算一周前的时间戳
        // Instant oneWeekAgoInstant = currentInstant.minus(1, ChronoUnit.DAYS);
        // long oneWeekAgoTimestamp = oneWeekAgoInstant.getEpochSecond();
        // paramMap.put("startTime",currentTimestamp+"");
        // paramMap.put("endTime",oneWeekAgoTimestamp+"");
        // 获取当前时间戳跟一周前的时间戳
        JSONObject redisValueJson = sendAPI.send(apiName, paramMap);
        JSONObject dataJson = JSONObject.parseObject(redisValueJson.getString("data"));
        JSONArray dataArray = dataJson.getJSONArray("data");
        Map<String, List<JSONObject>> map = new HashMap<>();
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject item = dataArray.getJSONObject(i);
            String alarmCode = item.getString("alarmCode");
            List<JSONObject> list = new ArrayList<>();
            if(map.containsKey(alarmCode)){
                list = map.get(alarmCode);
            }
            list.add(item);
            map.put(alarmCode, list);
        }
        Map<String,String> mapList = mapList();
        Map<String,Object> resultMap = new HashMap<>();
        for(String key:mapList.keySet()){
            if(map.containsKey(key)){
                resultMap.put(mapList.get(key),map.get(key).size());
            }else {
                resultMap.put(mapList.get(key),0);
            }
        }
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("data",resultMap);
        return R.ok(objectMap);
    }

    @Override
    public R getAlarmList() {
        String apiName = OpenAPI.FINDS_ALARM_LIST;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("pageNo","1");
        paramMap.put("pageSize","10");
        JSONObject redisValueJson = sendAPI.send(apiName, paramMap);
        JSONObject dataJson = JSONObject.parseObject(redisValueJson.getString("data"));
        JSONArray dataArray = dataJson.getJSONArray("data");
        Map<String, Object> map = new HashMap<>();
        map.put("data", dataArray);
        return R.ok(map);
    }


    private Map<String,String> mapList(){
        Map<String,String> map = new HashMap<>();
        map.put("CROSS_IN","闯入报警");
        map.put("CROSS_OUT","离开报警");
        map.put("RETENTION","滞留报警");
        map.put("CLUSTER","聚集报警");
        map.put("CROWDED","超员报警");
        map.put("LACKED","缺员报警");
        map.put("STATIC","静止报警");
        map.put("STAY","停留报警");
        map.put("OVER_SPEED","超速报警");
        map.put("CRASH","碰撞报警");
        map.put("LOW_BATTERY","低电量报警");
        map.put("SOS","SOS报警");
        return map;
    }
}

