package com.ningdatech.carapi.car;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.ningdatech.carapi.AppTests;
import com.ningdatech.carapi.car.vehicle.service.IVehicleBaseInfoService;

import lombok.RequiredArgsConstructor;

/**
 * 车辆报表
 *
 * @author CMM
 * @since 2023/06/25 16:41
 */
@RequiredArgsConstructor
public class RptTest extends AppTests {

    private final IVehicleBaseInfoService vehicleBaseInfoService;
    @Test
    public void test() {

        // 假设有以下车辆的上线时间和下线时间
        Map<String, LocalDate[]> vehicles = new HashMap<>();
        vehicles.put("车辆1", new LocalDate[]{LocalDate.of(2021, 1, 1), LocalDate.of(2021, 1, 10)}); // 上线时间为1月1日，下线时间为1月10日
        vehicles.put("车辆2", new LocalDate[]{LocalDate.of(2021, 1, 5), LocalDate.of(2021, 1, 15)}); // 上线时间为1月5日，下线时间为1月15日
        vehicles.put("车辆3", new LocalDate[]{LocalDate.of(2021, 1, 10), LocalDate.of(2021, 1, 20)}); // 上线时间为1月10日，下线时间为1月20日

        // 统计每辆车当月某个时间段不上线的天数
        LocalDate startDate = LocalDate.of(2021, 1, 1); // 统计开始日期
        LocalDate endDate = LocalDate.of(2021, 1, 31); // 统计结束日期
        Map<String, Long> offlineDays = new HashMap<>();
        for (Map.Entry<String, LocalDate[]> entry : vehicles.entrySet()) {
            LocalDate[] dates = entry.getValue();
            LocalDate onlineDate = dates[0];
            LocalDate offlineDate = dates[1];
            long days = ChronoUnit.DAYS.between(startDate, endDate) + 1; // 当月总天数
            if (onlineDate.isAfter(endDate) || offlineDate.isBefore(startDate)) { // 车辆在统计时间段外上下线
                offlineDays.put(entry.getKey(), days);
            } else {
                if (onlineDate.isBefore(startDate)) {
                    onlineDate = startDate;
                }
                if (offlineDate.isAfter(endDate)) {
                    offlineDate = endDate;
                }
                long offline = ChronoUnit.DAYS.between(onlineDate, offlineDate) + 1; // 不上线天数
                offlineDays.put(entry.getKey(), days - offline);
            }
        }

        // 按不上线天数进行排序
        List<Map.Entry<String, Long>> list = new ArrayList<>(offlineDays.entrySet());
        list.sort(Map.Entry.comparingByValue());

        // 输出结果
        for (Map.Entry<String, Long> entry : list) {
            System.out.println(entry.getKey() + ": " + entry.getValue() + "天");
        }

    }

    @Test
    public void test2() {
        
        //DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        //dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        //String date = dateFormat.format(new Date());
        //System.out.println(date);


        String method = "GET";
        String url = "https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020230427000001/irs-res-bill/report/pdfUrl?appCode=A330000100000202105002530";
        String appScret = "BCDSGS_4ab4235d26a9a357170a39f3a13fd68c";
        String appKey = "BCDSGA_d874c8e46b541eb4e8aac6510fd3351b";

        Map<String, String> header = HmacAuthUtil.generateHeader(url, method, appKey, appScret);
        //请求头
        HttpHeaders headers = new HttpHeaders();

        for(Map.Entry<String, String> entry : header.entrySet()){
            headers.add(entry.getKey(), entry.getValue());
        }

        System.out.println(headers);
        //封装请求头
        HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> forEntity = restTemplate.exchange(url, HttpMethod.GET,formEntity, String.class);

        System.out.println(JSON.toJSONString(forEntity));
        
    }
}
