package com.wms_admin.timer; import com.wms_admin.server.service.ProductService; import com.wms_admin.utils.RedisUtil; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.*; import static com.wms_admin.utils.MyUtils.isLastDayOfMonth; @Component public class Timer { @Resource private ProductService service; @Scheduled(cron="0 0 23 * * ?") //每天23点执行一次 public void execute() { List xList = new ArrayList(); List yList = new ArrayList(); SimpleDateFormat formatter = new SimpleDateFormat("M.d"); String time0 = formatter.format(new Date()); Integer dayCountData = service.TimerCountWeekDayData(); Map sevenDaysCount = null; try { sevenDaysCount = (Map) RedisUtil.get("SevenDaysCount"); }finally { if (sevenDaysCount != null) { xList = (List) sevenDaysCount.get("xList"); yList = (List) sevenDaysCount.get("yList"); if (xList.size() == 7 && yList.size() == 7) { xList.remove(0); yList.remove(0); } } xList.add(time0); yList.add(dayCountData); HashMap map = new HashMap<>(); map.put("xList", xList); map.put("yList", yList); RedisUtil.set("SevenDaysCount", map); } } //每个月第一天统计剩余库存 @Scheduled(cron="0 0 1 1 * ?") //每月初凌晨1点执行一次 public void monthlyExecute() { List pieDate = new ArrayList(); List pieNum = new ArrayList(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM"); String datetime = simpleDateFormat.format(new Date()); int beginningMonthNum = service.count(); Map beginningMonth = null; try { beginningMonth = (Map) RedisUtil.get("beginningMonth"); }finally { if (beginningMonth != null){ pieDate = (List) beginningMonth.get("datetime"); pieNum = (List) beginningMonth.get("num"); if (pieDate.size() == 4 && pieNum.size() == 4){ pieDate.remove(0); pieNum.remove(0); } } pieDate.add(datetime); pieNum.add(beginningMonthNum); Map map = new HashMap<>(); map.put("datetime", pieDate); map.put("num", pieNum); RedisUtil.set("beginningMonth", map); } } //每个月最后一天统计剩余库存 @Scheduled(cron = "0 0 23 28-31 * ?") //每月最后一天的28~31天都执行此代码 public void finallyExecute() { //将范围缩小到28~31后,每次调用此接口来判断当天是否是当月的最后一天 boolean flag = isLastDayOfMonth(new Date()); if (flag){ List pieDate = new ArrayList(); List pieNum = new ArrayList(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM"); String datetime = simpleDateFormat.format(new Date()); int endOfMonthNum = service.count(); Map endOfMonth = null; try { endOfMonth = (Map) RedisUtil.get("endOfMonth"); System.out.println(endOfMonth); }finally { if (endOfMonth != null){ pieDate = (List) endOfMonth.get("datetime"); pieNum = (List) endOfMonth.get("num"); if (pieDate.size() == 4 && pieNum.size() == 4){ pieDate.remove(0); pieNum.remove(0); } } pieDate.add(datetime); pieNum.add(endOfMonthNum); Map map = new HashMap<>(); map.put("datetime", pieDate); map.put("num", pieNum); RedisUtil.set("endOfMonth", map); } } } }