package com.aidex.quartz.task;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.analysis.*;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.aidex.common.constant.Constants;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.common.utils.StringUtils;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.domain.SysAlarm;
import com.aidex.system.domain.SysConfig;
import com.aidex.system.domain.SysMiniappDailyVisit;
import com.aidex.system.domain.SysShip;
import com.aidex.system.service.SysAlarmService;
import com.aidex.system.service.SysMiniappDailyVisitService;
import com.aidex.system.service.SysShipService;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.aidex.common.enums.AlarmType.*;

/**
 * @author ：ChenSir
 * @date ：Created in 2024/3/17 20:07
 * @description：获取小程序统计数据
 */
@Component("GetMinDataByDate")
public class GetMinDataByDate {

	protected static final Logger logger = LoggerFactory.getLogger(GetMinDataByDate.class);

	@Resource
	private WxMaService wxMaService;

	@Autowired
	private SysMiniappDailyVisitService miniappDailyVisitService;

	/**
	 * 刷新GPS的token
	 * @return token
	 */
	public void getDataService(){
		logger.info("获取小程序统计数据---------开始");
		try{
			// 获取本年度数据
			/*Date beginOfWeek = DateUtil.beginOfYear(new Date());
			Date endOfWeek = DateUtil.endOfWeek(new Date(), true);
			DateRange range = DateUtil.range(beginOfWeek, endOfWeek, DateField.DAY_OF_WEEK);
			List<DateTime> dateTimes = Lists.newArrayList(range.iterator());
			for (DateTime mydate : dateTimes){

			}*/

			List<WxMaVisitTrend> dailyVisitTrend = wxMaService.getAnalysisService().getDailyVisitTrend(DateUtil.yesterday(), DateUtil.yesterday());
			logger.info("获取用户访问小程序数据日趋势---[统计数据]----{}", dailyVisitTrend);
			for (WxMaVisitTrend item : dailyVisitTrend){
				SysMiniappDailyVisit sysMiniappDailyVisit = new SysMiniappDailyVisit();
				BeanUtils.copyProperties(item, sysMiniappDailyVisit);
				String date = DateUtil.format(DateUtil.parse(item.getRefDate(), "yyyyMMdd"), "yyyy-MM-dd");
				sysMiniappDailyVisit.setRefDate(DateUtil.parseDate(date));
				sysMiniappDailyVisit.setStayTimeUv(String.valueOf(item.getStayTimeUv()));
				sysMiniappDailyVisit.setStayTimeSession(String.valueOf(item.getStayTimeSession()));
				sysMiniappDailyVisit.setVisitDepth(String.valueOf(item.getVisitDepth()));
				if (miniappDailyVisitService.get(sysMiniappDailyVisit) == null){
					miniappDailyVisitService.save(sysMiniappDailyVisit);
				}
			}

		}catch (Exception e){
			logger.error(e.getLocalizedMessage());
		}
		logger.info("获取小程序统计数据----------结束");
	}


}
