package com.dsk.web.schedule;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dsk.common.constant.PushConstants;
import com.dsk.common.utils.DateUtils;
import com.dsk.system.domain.SysConfig;
import com.dsk.system.domain.SysPush;
import com.dsk.system.domain.SysStatutoryHoliday;
import com.dsk.system.mapper.SysStatutoryHolidayMapper;
import com.dsk.system.service.ISysConfigService;
import com.dsk.system.service.ISysPushService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

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

/**
 * 推送相关定时任务
 *
 * @Author lcl
 * @Data 2023/12/21 17:05
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class PushTask {

    @Autowired
    private ISysPushService pushService;

    @Autowired
    private ISysConfigService configService;

    @Resource
    private SysStatutoryHolidayMapper statutoryHolidayMapper;

    /**
     * 供应商不良推送(每分钟)
     */
//    @Scheduled(cron = "0 * * * * ? ")
    public void customerBadness() {
        //1.是否推送时间段（日期、时间）
        if (!isPush()) return;
        //2.推送数据（当前时段是否存在数据）

        //3.推送人员
        List<SysPush> list = pushService.list(Wrappers.<SysPush>lambdaQuery().eq(SysPush::getStatus, 0));
        if (CollectionUtils.isNotEmpty(list)) {
            list.parallelStream().forEach(item -> {
                Map<String, Object> param = new HashMap<>();
                param.put("name", item.getName());

            });
        }
    }

    //是否推送
    private boolean isPush() {
        //推送类型
        SysConfig type = configService.selectConfigByKey(PushConstants.CUSTOMEER_BADNESS_TYPE_KEY);
        if (ObjectUtils.isEmpty(type)) return false;
        if ("1".equals(type.getConfigValue()) && !isWorkingDay()) return false;
        //推送时段
        SysConfig frame = configService.selectConfigByKey(PushConstants.CUSTOMEER_BADNESS_FRAME_KEY);
        return !ObjectUtils.isEmpty(frame) && isPushTime(frame.getConfigValue());
    }

    //判断当前是否工作日
    private boolean isWorkingDay() {
        LocalDate date = LocalDate.now();
        SysStatutoryHoliday today = statutoryHolidayMapper.selectOne(Wrappers.<SysStatutoryHoliday>lambdaQuery()
                .eq(SysStatutoryHoliday::getDate, date));
        if (!ObjectUtils.isEmpty(today)) return today.getIsHoliday() == 0;
        return date.getDayOfWeek() != DayOfWeek.SATURDAY && date.getDayOfWeek() != DayOfWeek.SUNDAY;
    }

    //判断是否在当前时段推送
    private boolean isPushTime(String timeFrame) {
        String date = LocalDate.now().toString();
        String[] split = timeFrame.split("-");
        Date currentTime = new Date();
        return currentTime.after(DateUtils.parseDate(date + " " + split[0])) && currentTime.before(DateUtils.parseDate(date + " " + split[1]));
    }
}
