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");
            //2017-06-06 新签约和续签逻辑修改
//            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);

            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR)-1;
            int month = cal.get(Calendar.MONTH)+1;
            if(month >=7){
                //设置已过期
                String sql = "UPDATE wlyy_sign_family t SET t.status = -4,t.apply_unsign_date='"+ DateUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss")+"'  WHERE t.status>=0 AND t.sign_year='"+year+"'";
//                //数据迁移
//                String sql2 ="INSERT INTO wlyy_sign_family(" +
//                        "code,lw_code,type,patient,openid,name,idcard,ssc," +
//                        "mobile,emer_mobile,hospital,hospital_name,doctor," +
//                        "doctor_name,begin,end,images,group_code,status,reason," +
//                        "czrq,team_code,sign_type,apply_date,release_speak," +
//                        "doctor_health_name,doctor_health,family_code,patient_apply_date," +
//                        "expenses,expenses_status,sign_source,sign_doctor_code,sign_doctor_name," +
//                        "sign_doctor_level,patient_apply_unsgin_date,apply_unsign_date,expenses_type," +
//                        "sign_year,medical_insurance_num,agent_doctor_code,agent_doctor_name," +
//                        "agent_doctor_level,admin_team_code,expenses_time,server_type,server_type_name,special_population,special_population_name) " +
//                        "SELECT code,lw_code,type,patient,openid,name,idcard,ssc," +
//                        "mobile,emer_mobile,hospital,hospital_name,doctor,doctor_name," +
//                        "begin,end,images,group_code,status,reason,czrq,team_code," +
//                        "sign_type,apply_date,release_speak,doctor_health_name," +
//                        "doctor_health,family_code,patient_apply_date,expenses," +
//                        "expenses_status,sign_source,sign_doctor_code,sign_doctor_name," +
//                        "sign_doctor_level,patient_apply_unsgin_date,apply_unsign_date," +
//                        "expenses_type,sign_year,medical_insurance_num,agent_doctor_code," +
//                        "agent_doctor_name,agent_doctor_level,admin_team_code,expenses_time,server_type,server_type_name,special_population,special_population_name " +
//                        "FROM wlyy_sign_family_renew t WHERE t.is_valid =0 AND t.status =1 AND t.sign_year='"+(year+1)+"' AND t.expenses_status='1'";
//                //更改迁移状态
//                String sql3 = "UPDATE wlyy_sign_family_renew t SET t.is_valid =1 WHERE t.is_valid =0 AND t.status =1 AND t.sign_year='"+(year+1)+"' AND t.expenses_status='1'";
                jdbcTemplate.execute(sql);
//                jdbcTemplate.execute(sql2);
//                jdbcTemplate.execute(sql3);
            }

            System.out.println("sign end job end");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("sign end job failed");
        }
    }

}
