package com.xhpc.pp.server;

import cn.hutool.core.util.StrUtil;
import com.xhpc.common.enums.StationDeviceEnum;
import com.xhpc.pp.domain.XhpcDeviceMessage;
import com.xhpc.pp.mapper.XhpcDeviceMessageMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;
import static com.xhpc.evcs.dto.ConnectorStatusInfo.OFF_LINE;
import static com.xhpc.pp.config.EarlierBeanConf.getLocalIPAndPort;
import static com.xhpc.pp.logic.RegisterLogic.DISCONNECTED;

@Component
public class HBCheckTask {

    @Resource
    XhpcDeviceMessageMapper deviceMessageMapper;

    //每5分钟执行一次
    @Scheduled(cron = "0 0/10 * * * ?")
    protected void run() {

        String svcSrvKey = "svcSrvGuns:".concat(getLocalIPAndPort());
        Set<String> cacheGunkeySet = REDIS.getCacheSet(svcSrvKey);
        Long now = Calendar.getInstance().getTimeInMillis();
        for (String gunkey : cacheGunkeySet) {
            Map<String, Object> cacheGun = REDIS.getCacheMap(gunkey);
            Long hbtime = (Long) cacheGun.get("hbtime");
            hbtime = hbtime == null ? 0 : hbtime;
            if ((now - hbtime) > 75000) {
                String pileId = StrUtil.sub(gunkey.substring(gunkey.lastIndexOf(":") + 1), 0, -2);
                if (DISCONNECTED.equals(cacheGun.get("status"))) {
                    cacheGun.put("statusInt", OFF_LINE);
                    cacheGun.put("status", DISCONNECTED);
                    REDIS.setCacheMap(gunkey, cacheGun);
                    XhpcDeviceMessage deviceMessage = new XhpcDeviceMessage();
                    deviceMessage.setType(StationDeviceEnum.PILE.getCode());
                    //String pileId = StrUtil.sub(gunkey.substring(gunkey.lastIndexOf(":") + 1), 0, -2);
                    deviceMessage.setSerialNumber(pileId);
                    deviceMessage.setStatus(0);
                    deviceMessage.setRemark("充电桩心跳掉线");
                    deviceMessageMapper.insertByDomain(deviceMessage);
                    try{
                        String pkey = "pile:".concat(pileId);
                        Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
                        String stationId = cachePile.get("stationId").toString();
                       // System.out.println("================gunkey===="+gunkey+"======pileId========"+pileId+"========stationId==="+stationId);
                        if(1==Integer.parseInt(gunkey.substring(gunkey.length() - 1))){
                            deviceMessageMapper.insertChargingPileStatus(gunkey.substring(gunkey.lastIndexOf(":") + 1),pileId,stationId,"A枪");
                        }else{
                            deviceMessageMapper.insertChargingPileStatus(gunkey.substring(gunkey.lastIndexOf(":") + 1),pileId,stationId,"B枪");
                        }

                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }
            }
        }
    }

    public static void main(String[] args) {

        long time = new Date().getTime();

        String st = "gun:8083600040000102";

        String substring = st.substring(st.length() - 1);


        System.out.println("substring   "+substring);


    }


}
