package com.malk.mcli;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.malk.Boot;
import com.malk.utils.UtilDateTime;
import com.malk.utils.UtilString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Unit 单元测试
 *
 * @RunWith(SpringRunner.class：若需要调用项目内方法，则需要添加，否则会报空指针错误。注解类
 * @SpringBootTest(classes = Application.class)：若需要调用项目内方法，则需要添加，否则会报空指针错误【Application为项目启动application类名称】。注解类
 * @Test: 测试注解，添加可执行测试方法，不支持入参。注解方法。注解方法
 * @ActiveProfiles("dev"})：一般用于注解测试类，加载指定环境配置，如dev、prod、test。多个环境配置@ActiveProfiles({"dev”})。注解类
 * @Rollback，回滚注解，配合事务注解@Transactional，来回滚事务。可以定义在测试类上，对整个类起作用，也可以对方法单独定义。默认值true，若junit需要写入设置为false
 */
@Slf4j
@ActiveProfiles({"dev"})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Boot.class)
public class McTest {

    @Test
    public void date() {

        long startTime = DateUtil.parse(20220404 + " 060000", "yyyyMMdd HHmmss").getTime() / 1000;
        long endTime = startTime + 24 * 60 * 60 - 1;

        log.info("1111, {}, {}", startTime, endTime);

        // NOTE: 必须使用 Long 类型, 或者用 L 修饰 long 类型, 否则就会java转为 int 类型, 导致溢出计算错误
        long time = 1649123903 * 1000L;
        log.info("222, {}, {}", DateUtil.date(time), DateUtil.format(DateUtil.date(time), "yyyyMMdd"));

        JSONObject eventJson = new JSONObject();
        eventJson.put("check_time", "1649123903");
        String date = DateUtil.format(DateUtil.date(Long.valueOf(eventJson.getString("check_time")).longValue() * 1000), "yyyyMMdd");

        log.info("xxx, {}", date);

        log.info("333, {}, {}", System.currentTimeMillis(), DateUtil.date(1649160925434L));


        log.info("444, {}", StringUtils.substring("20220311", 0, 4));
    }


    @Test
    public void math() {
        log.info("1111, {}, {}", String.format("%.2f", 1319.3214, 3213.2323));
        log.info("2222, {}", UtilString.stringFormatFixed(01319.3214));


        log.info("3333, {}", 3.31431f >= 3.31f);

        log.info("4444, {}", new BigDecimal(0.25d).add(new BigDecimal(0.751d)));
    }

    @Test
    public void dateTime() {

        String sTime = "2022-05-24 09:30:00";
        String eTime = "2022-05-24 18:30:00";


        LocalDateTime cTime = UtilDateTime.parseLocalDateTime(eTime.split(" ")[0] + " 18:00:00");
        LocalDateTime lTime = UtilDateTime.parseLocalDateTime(eTime);
        if (lTime.isAfter(cTime)) {
            long seconds = Duration.between(cTime, lTime).getSeconds();
            eTime = UtilDateTime.formatLocalDateTime(lTime.minusSeconds(seconds));
            sTime = UtilDateTime.formatLocalDateTime(UtilDateTime.parseLocalDateTime(sTime).minusSeconds(seconds));

            log.info("11111, {}, {}", sTime, eTime);
        }
        Map map = new HashMap();
        map.put("dateField_l9t7eqe9", "1669996800000");

        log.info("xxxx, {}", UtilDateTime.formatDate(new Date(Long.valueOf(String.valueOf(map.get("dateField_l9t7eqe9"))))));


//        String begin = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format();
//        String finish = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(leaveDo.getEndTime());
    }
}
