package com.yihu.wlyy.job;

import com.google.zxing.datamatrix.encoder.SymbolShapeHint;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.repository.patient.PatientDao;
import com.yihu.wlyy.service.common.account.PatientService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.util.SendNews;
import com.yihu.wlyy.util.SystemConf;
import com.yihu.wlyy.web.BaseController;
import com.yihu.wlyy.web.WeixinBaseController;
import com.yihu.wlyy.web.common.account.WechatController;
import com.yihu.wlyy.wechat.process.WeiXinEventProcess;
import com.yihu.wlyy.wechat.util.WeiXinMessageReplyUtils;
import org.json.JSONObject;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Reece on 2017/5/18.
 */
public class SignAgainJob extends WeixinBaseController implements Job {
    @Autowired
    private  PatientDao patientDao;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            System.out.println("sign again job start");
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String trigger = SystemConf.getInstance().getSystemProperties().getProperty("sign_again_job_end");
            Date nowDate = new Date();
            Date endTime = sdf.parse(trigger);
            Boolean flag = nowDate.before(endTime);
            System.out.println(flag);
//            如果当期时间在截止时间之前，发送图文，否则不发送
            if (nowDate.before(endTime)){
                String accessToken = getAccessToken();
                List<String> openIds = patientDao.findOpenids();
                System.out.println("accessToken ---> "+ accessToken);
                System.out.println("openIds --> "+openIds.size());
                SendNews.sendNews(accessToken,openIds);
            }
            System.out.println("sign again job end");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("sign again job failed");
        }
    }
}
