/*
 * 文 件 名:  AmapCongestTest
 * 版    权:
 * 描    述:  <描述>
 * 修 改 人:  learshaw
 * 修改时间:  2022/3/28
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.test;

import com.huashe.common.utils.DateUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.ems.EmsDevAdpApplication;
import com.ruoyi.ems.config.AcrelConfig;
import com.ruoyi.ems.core.Acrel3000Template;
import com.ruoyi.ems.model.acrel.AlarmEventLog;
import com.ruoyi.ems.model.acrel.Circuit;
import com.ruoyi.ems.model.acrel.ConsumeReport;
import com.ruoyi.ems.model.acrel.MeterKeyValue;
import com.ruoyi.ems.model.acrel.MeterUse;
import com.ruoyi.ems.model.acrel.Station;
import com.ruoyi.ems.model.acrel.Token;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 安科瑞 - 3000 接口测试
 * <功能详细描述>
 *
 * @author learshaw
 * @version [版本号, 2022/3/28]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EmsDevAdpApplication.class)
public class Acrel3000Test {

    @Resource
    private AcrelConfig config;

    @Resource
    private RedisService redisService;

    /**
     * 测试-获取权限
     */
    @Before
    public void testGetAuthToken() {
        Acrel3000Template template = new Acrel3000Template(config.getUrl());
        Token token = template.getAuthToken(config.getAuthLoginName(), config.getAuthPassword());
        Assert.assertNotNull(token);
        log.info("Token: {}\nexpireDate: {}", token.getToken(), token.getExpireDate());

        Date expireDate = DateUtils.stringToDate(token.getExpireDate(), "yyyy-MM-dd HH:mm:ss");
        Date currentDate = new Date();
        long expireSeconds = (expireDate.getTime() - currentDate.getTime()) / 1000;
        com.huashe.common.exception.Assert.isTrue(expireSeconds > 0, -1, "Token已过期或过期时间无效");
        redisService.setCacheObject("ACREL_AUTH_TOKEN", token.getToken(), expireSeconds, TimeUnit.SECONDS);
    }

    @Test
    public void testGetStationTreeList() {
        Acrel3000Template template = new Acrel3000Template(config.getUrl());
        List<Station> stationList = template.getStationTreeList("1", null);
        log.info("stationList: {}", stationList);
        Assert.assertNotNull(stationList);
    }

    @Test
    public void testGetMeterUseInfoList() {
        Acrel3000Template template = new Acrel3000Template(config.getUrl());
        List<MeterUse> meterUseList = template.getMeterUseInfoList("10100002");
        log.info("meterUseList: {}", meterUseList);
        Assert.assertNotNull(meterUseList);
    }

    @Test
    public void testGetCircuitInfoTree() {
        Acrel3000Template template = new Acrel3000Template(config.getUrl());
        List<Circuit> circuitList = template.getCircuitInfoTree("10100002");
        log.info("circuitList: {}", circuitList);
        Assert.assertNotNull(circuitList);
    }

    @Test
    public void testGetMeterParamValueByKey() {
        Acrel3000Template template = new Acrel3000Template(config.getUrl());
        List<MeterKeyValue> meterKeyValueList = template.getMeterParamValueByKey("10100002", "T102006");
        log.info("MeterKeyValueList: {}", meterKeyValueList);
        Assert.assertNotNull(meterKeyValueList);
    }

    @Test
    public void testGetNewConsumeReport() {
        Acrel3000Template template = new Acrel3000Template(config.getUrl());
        ConsumeReport consumeReport = template.getNewConsumeReport("101000020001", "min", "2025-10-16 00:00:00",
            "2025-10-17 00:00:00", "24", "EPI");
        log.info("consumeReport: {}", consumeReport);
        Assert.assertNotNull(consumeReport);
    }

    @Test
    public void testGetAlarmEventLogList() {
        Acrel3000Template template = new Acrel3000Template(config.getUrl());
        List<AlarmEventLog> list = template.getAlarmEventLogList("10100001001", "2025-07-01 00:00:00",
            "2025-07-20 00:00:00");
        log.info("eventLogList: {}", list);
        Assert.assertNotNull(list);
    }
}