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.PushMonitorRules;
import com.dsk.monitor.domain.vo.ConditionVo;
import com.dsk.monitor.domain.vo.PushMonitorDynamicPageVo;
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.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;

/**
 * @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());

        String aa = DateUtil.now();


        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) {
                QueryWrapper monitorInfoWrapper = new QueryWrapper();
                // 风险类型
//                monitorInfoWrapper.in("risk_type", detail.getRiskType());
                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(getDimension(detail.getDimension()));
                vo.setUserId(detail.getUserId());
                pageVo.setCondition(vo);;
                Integer count = monitorService.companyDynamicPage2(pageVo);
                if (count > 0 ) {
                    SendMessageUtil.sendMessage(detail.getPhones(), count.toString());
                }
            }
        }
    }


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

}
