package com.dsk.monitor.job;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsk.common.tenant.helper.TenantHelper;
import com.dsk.common.utils.DateUtils;
import com.dsk.monitor.domain.dto.PushMonitorInfo;
import com.dsk.monitor.domain.dto.PushMonitorRules;
import com.dsk.monitor.domain.vo.ConditionVo;
import com.dsk.monitor.domain.vo.PushMonitorDynamicPageVo;
import com.dsk.monitor.domain.vo.PushSettingHolidayVo;
import com.dsk.monitor.mapper.PushMonitorInfoMapper;
import com.dsk.monitor.mapper.PushMonitorRulesMapper;
import com.dsk.monitor.service.MonitorService;
import com.dsk.monitor.util.SendMessageUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * @ClassName PushScheduling
 * @Description 发送短信
 * @Author Dgm
 * @Date 2023/12/9 13:51
 * @Version
 */
@Component
public class PushScheduling {

    @Resource
    private MonitorService monitorService;

    @Resource
    private PushMonitorInfoMapper monitorInfoMapper;

    @Resource
    private PushMonitorRulesMapper rulesMapper;

//    @Scheduled(cron = "0 0/2 * * * ?")
    @Scheduled(cron = "0 0 * * * ?")
    public void receiveModePhone() throws Exception {
        // 是否是周末
        boolean isWeekend = DateUtil.isWeekend(new Date());
        PushSettingHolidayVo holidayVo = new PushSettingHolidayVo();
        holidayVo.setTimestamp(System.currentTimeMillis());
        Map<String, Object> settingHolidayMap = monitorService.settingHoliday(holidayVo);
        Integer settingHolidayCode = MapUtils.getInteger(settingHolidayMap, "code", 300);
        Boolean settingHolidayData = MapUtils.getBoolean(settingHolidayMap, "data", null);
        if (200 == settingHolidayCode) {
            isWeekend = !settingHolidayData;
        }
        TenantHelper.setTenantId("1");
        // 获取当前整点时间
        Integer concurrentHour = DateUtils.getHour();
        QueryWrapper<PushMonitorRules> queryWrapper = new QueryWrapper();
        List<PushMonitorRules> detailList  = rulesMapper.selectList(queryWrapper);
        for (PushMonitorRules detail : detailList) {
            // 推送时段-起
            String timePeriodStart = detail.getTimePeriodStart();
            timePeriodStart = timePeriodStart.substring(0,2);
            if (timePeriodStart.contains("0")) {
                timePeriodStart = timePeriodStart.substring(1,2);
            }
            // 推送时段-止
            String timePeriodEnd = detail.getTimePeriodEnd();
            timePeriodEnd = timePeriodEnd.substring(0,2);
            if (timePeriodEnd.contains("0")) {
                timePeriodEnd = timePeriodEnd.substring(1,2);
            }
            Integer pushFrequency = detail.getPushFrequency();
            if (pushFrequency > 1) {
                if (isWeekend) {
                    continue;
                }
            }
            Integer timeStart = Integer.valueOf(timePeriodStart);
            Integer timeEnd = Integer.valueOf(timePeriodEnd);
            if (concurrentHour >= timeStart && concurrentHour <= timeEnd) {
                PushMonitorDynamicPageVo pageVo = new PushMonitorDynamicPageVo();
                pageVo.setUserId(detail.getUserId());
                ConditionVo vo = new ConditionVo();
                // 截止时间
                vo.setEndTime(DateUtils.getDate());
                vo.setBeginTime(DateUtils.format(DateUtils.addDays(new Date(),-1)));
                // 监控维度
                vo.setDimension(getParamList(detail.getDimension()));
                vo.setUserId(detail.getUserId());
                pageVo.setCondition(vo);;
                Map<String, Object> resMap = monitorService.companyDynamicPageScheduled(pageVo);
                Integer code = MapUtils.getInteger(resMap, "code", 300);
                Map data = MapUtils.getMap(resMap, "data", null);
                if (200 != code) {
                    continue;
                }
                Integer count = MapUtils.getInteger(data, "totalCount", 0);
                if (count > 0 ) {
                    SendMessageUtil.sendMessage(detail.getPhones(), count.toString());
                }
                if (count > 0) {
                    PushMonitorInfo pushMonitorInfo = new PushMonitorInfo();
                    pushMonitorInfo.setUserId(detail.getUserId());
                    pushMonitorInfo.setCount(count);
                    monitorInfoMapper.insert(pushMonitorInfo);
                }
            }
        }
    }

    public List<String> getParamList(String dimension) {
        List<String> getParamList = Arrays.asList(dimension.split(","));
        return getParamList;
    }

}
