package com.gic.haoban.manage.web.utils.eventtracking;

import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.DataApiUtils;
import com.gic.commons.util.DateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author guojx
 * @Date 2024/5/30 15:34
 */
public class DateExpandUtils {

    public static List<String> getDateList(String startDate, String endDate, Integer dateType) {
        //数据类型 1天 2近7天 3近30天 4周 5月
        List<String> list = new ArrayList<>();
        if (dateType == null) {
            dateType = 1;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            switch (dateType) {
                case 1:
                case 2:
                case 3:
                    calendar.setTime(dateType == 1 ? df.parse(startDate) : df.parse(endDate));
                    calendar.add(Calendar.DAY_OF_YEAR, -29);
                    list = DateUtil.getBetweenDates(df.format(calendar.getTime()), dateType == 1 ? startDate : endDate);
                    break;
                case 4:
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("startDate", startDate);
                    jsonObject.put("pageNum", 1);
                    jsonObject.put("pageSize", 100);
                    Map<String, Object> res = DataApiUtils.http(jsonObject.toJSONString(), "data_pub_date_week");
                    List<JSONObject> data = DataApiUtils.getPageList(res);
                    list = data.stream().map(e ->e.getString("weekYear")).collect(Collectors.toList());
                    break;
                case 5:
                    //月
                    SimpleDateFormat monthFormat = new SimpleDateFormat("yyyy-MM");
                    calendar.setTime(df.parse(startDate));
                    calendar.add(Calendar.MONTH, -11);
                    for (int i = 1; i <= 12; i++) {
                        list.add(monthFormat.format(calendar.getTime()));
                        calendar.add(Calendar.MONTH, 1);
                    }
                    break;
                default:
            }
        } catch (Exception e) {

        }
        return list;
    }
}
