package com.emr.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.emr.entity.emrLog.Emr_Log;
import com.emr.service.emrLog.LogService;
import com.emr.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/**
 * 清除系统日志任务
 * @author Mr Du
 *
 */

@Component
public class MethodLogQuartz {
    @Value("${log.days}")
    private Integer LOGDAYS;

    @Autowired
    private LogService logServiceImpl;
    /** 
     * cron表达式：* * * * * *（共6位，使用空格隔开，具体如下） 
     * cron表达式：*(秒0-59) *(分 钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT) 
     */ 
    @Scheduled(cron="0 0 0 * * ?")
    public void clearLog() {
        Date date = DateUtils.getDate(DateUtils.getDate(), -LOGDAYS);
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = logServiceImpl.delLogsByDate(fmt.format(date));
        if(count != 0){
            Emr_Log log=new Emr_Log();
            log.setIp("");
            log.setLogTitle("删除");
            log.setLogContent("定时删除日志："+count+"条");
            log.setCreater("系统自动任务");
            log.setRemark("");
            logServiceImpl.autoInsert(log);
        }
    }
}
