package com.yihu.wlyy.job;

import com.yihu.wlyy.service.app.followup.FollowUpService;
import com.yihu.wlyy.util.DateUtil;
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.util.Calendar;
import java.util.Date;

/**
 * Created by lyr-pc on 2017/3/9.
 */
public class SignEndJob implements Job {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {
            System.out.println("sign end job start");
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            Calendar today = Calendar.getInstance();
            today.add(Calendar.DATE, -1);
            String date = DateUtil.dateToStrShort(today.getTime());
            String start = date + " 00:00:00";
            String end = date + " 23:59:59";
            String sql = "update wlyy_sign_family set status = -4,apply_unsign_date =SYSDATE() where end >= ? and end <= ? and status > 0";
            String sqlT = "update wlyy_doctor_team set del = '0' where del = '1' and code in (select team_code from wlyy_sign_family where end >= ? and end <= ? and status > 0)";
            String sqlTm = "update wlyy_doctor_team_member set del = '0' where del = '1' and team in (select team_code from wlyy_sign_family where end >= ? and end <= ? and status > 0)";
            int sucessT = jdbcTemplate.update(sqlT, start, end);
            int sucessTm = jdbcTemplate.update(sqlTm, start, end);
            int sucess = jdbcTemplate.update(sql, start, end);
            System.out.println("sign end job end");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("sign end job failed");
        }
    }

}
