package org.jeecg.modules.okki.wechatgroup.job;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.okki.site.service.IOkCommonService;
import org.jeecg.modules.okki.wechatgroup.service.IOkkiShopWechatGroupMsgService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

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

/**
 * 发送提醒消息给建站客服
 *
 * @author Chen
 */
@Slf4j
public class ReminderCustomerWechatGroupMsgJob implements Job {

    @Resource
    private IOkkiShopWechatGroupMsgService okkiShopWechatGroupMsgService;

    @Resource
    private IOkCommonService okCommonService;

    private String parameter;

    public void setParameter(String parameter) {
        this.parameter = parameter;
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        long start = System.currentTimeMillis();
        log.info(String.format("开始发送 未回复客户消息给建站客服-普通定时任务 NoReplyWechatGroupMsgJob !  时间:" + DateUtils.now()));
//        int days = okCommonService.getCalculateDays();
        String nowDate = DateUtil.now();
//        log.info("nowDate:{}", nowDate);
        int days = 3;
        Map<String, Object> workDayMap = okkiShopWechatGroupMsgService.todayCheckWorkDay();
//        log.info("workDayMap:{}", workDayMap);
        JSONObject workDayObj = new JSONObject(workDayMap);
        //	是否需要上班，0为工作日，1为休息日
        int isNotWork = workDayObj.getInteger("isnotwork");
        if (isNotWork == 0) {
            Date date = DateUtils.str2Date(nowDate, DateUtils.datetimeFormat.get());
            boolean isWorkingHours = okCommonService.isWorkingHours(date);
//            log.info("isWorkingHours:{}", isWorkingHours);
            if (isWorkingHours) {
                okkiShopWechatGroupMsgService.getNoReplyCustomerMsgByHours(this.parameter, days, "workingHour");
            }
            boolean isAfterHours = okCommonService.isAfterHours(date);
//            log.info("isAfterHours:{}", isAfterHours);
            if (isAfterHours) {
                okkiShopWechatGroupMsgService.getNoReplyCustomerMsgByHours(this.parameter, days, "afterHour");
            }
        } else {
            okkiShopWechatGroupMsgService.getNoReplyCustomerMsgByHours(this.parameter, days, "restDay");
        }
        long end = System.currentTimeMillis();
        log.info("请求发送 未回复客户消息给建站客服-普通定时任务 NoReplyWechatGroupMsgJob !结束,耗时:{}s", (end - start) / 1000);
    }
}
