package com.yltd.sams.service.settings.general.impl; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.yltd.sams.entity.system.SysSchedule; import com.yltd.sams.service.settings.general.SystemTimeService; import com.yltd.sams.service.shell.ShellService; import com.yltd.sams.service.system.SysScheduleService; import lombok.extern.slf4j.Slf4j; /** * 系統(tǒng)設(shè)置 > 通用設(shè)置-系統(tǒng)時(shí)間 * @author zhangyixin * @date 2020/04/20 */ @Slf4j @Service public class SystemTimeServiceImpl implements SystemTimeService { //操作腳本的實(shí)現(xiàn)類 @Autowired private ShellService shellService; //定時(shí)任務(wù)的實(shí)現(xiàn)類 @Autowired private SysScheduleService sysScheduleService; /** * 修改系統(tǒng)的時(shí)區(qū)/日期和時(shí)間 * @param map * 修改時(shí)區(qū) * cp /usr/share/zoneinfo/$主時(shí)區(qū)/$次時(shí)區(qū) /etc/localtime 例如:在設(shè)置中國時(shí)區(qū)使用亞洲/上海(+8) cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime * 修改時(shí)間 * date -s "2016-06-18 16:10:00" */ @Override public String setSystemTime(Map<String, Object> map) throws Exception { log.info("==============<setSystemTime-selectIndex>=============="); //狀態(tài):自動同步時(shí)間:0自動同步時(shí)間,,1:手動同步時(shí)間 if("0".equals(map.get("state"))){ String timeZone =(String) map.get("timeZone"); return shellService.execShell("cp /usr/share/zoneinfo/Asia/"+timeZone+" "+"/etc/localtime"); }else if("1".equals(map.get("state"))){ String dateTime =(String) map.get("dateTime"); String setTime =(String) map.get("setTime"); //修改日期和時(shí)間 return shellService.execShell("date -s "+dateTime+" "+setTime ); } return null; } /** * 立即同步(NTP服務(wù)器設(shè)置) * * 同步時(shí)間: ntpdate 192.168.10.20 * */ @Override public String synchronizationNTP(Map<String, Object> map) throws Exception{ return shellService.execShell("ntpdate "+map.get("serverAddress")); } /** * 預(yù)約同步(NTP服務(wù)器設(shè)置) * * 預(yù)約同步時(shí)間: 0 12 * * * /usr/sbin/ntpdate 192.168.0.1 * */ @Override public String reservationNTP(Map<String, Object> map) throws Exception{ //設(shè)置定時(shí)任務(wù)-同步周期,,時(shí)間換算 :獲取表達(dá)式 String cornExpression =makeDailyTrigger(Integer.parseInt(map.get("synchronizationCycle").toString())); if(cornExpression!=null || cornExpression!=""){ //如果不為空,操作數(shù)據(jù)庫 SysSchedule sysSchedule = new SysSchedule(); sysSchedule.setName("NTP服務(wù)器設(shè)置同步周期");//任務(wù)名稱 sysSchedule.setClassName("com.yltd.sams.schedule.ReservationNTPStat");//定時(shí)任務(wù)類 sysSchedule.setMethodName("reservationNTP");//定時(shí)任務(wù)方法 //查詢定時(shí)任務(wù)相關(guān)數(shù)據(jù) SysSchedule selectSchedule = sysScheduleService.selectByParam(sysSchedule); sysSchedule.setCronExpression(cornExpression.toString());//cron表達(dá)式 sysSchedule.setDescription("每"+map.get("synchronizationCycle")+"分鐘同步一次");//描述信息 sysSchedule.setStatus((byte)0);//狀態(tài):0正常,,1暫停 try { //判斷是否為空,,為空則添加,不為空則更新數(shù)據(jù) if (selectSchedule!=null){ sysSchedule.setId(selectSchedule.getId()); sysScheduleService.updateEntity(sysSchedule); sysScheduleService.updateCron(selectSchedule.getId(),cornExpression.toString()); }else { sysScheduleService.addScheduleJob(sysSchedule); } } catch (Exception e) { e.printStackTrace(); } } return cornExpression; } /**定時(shí)任務(wù)- 定時(shí)任務(wù)-日期和表達(dá)式的轉(zhuǎn)化/驗(yàn)證分鐘 * @param minute */ public String makeDailyTrigger(int minute) { if (minute < 0 ||minute>44640) throw new IllegalArgumentException("Invalid minute (must be >= 0 and must be <= 65530)."); if(minute>=0 && minute <= 59){//分鐘 // 0 */1 * * * ? return "0 */"+minute +" * * * ?"; }else if(minute>59 && minute<=1440){//24小時(shí) int hours = (int) Math.floor(minute / 60); int fenZhong = minute % 60; return "0 " + fenZhong + " */" + hours + " * * ?"; }else{//30天 int day=0,hours=0; day = minute/(60*24); minute -= day*60*24; hours = minute/60; minute -= hours*60; return "0 " + minute + " " + hours + " */"+day+" * ?"; } } } |