|  |  | 
 |  |  | package com.zy.core; | 
 |  |  |  | 
 |  |  | import com.core.common.SpringUtils; | 
 |  |  | import com.zy.asrs.entity.WrkMast; | 
 |  |  | import com.zy.asrs.service.WrkMastService; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  |  | 
 |  |  | import java.lang.reflect.Array; | 
 |  |  | import java.util.Arrays; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * news stories for zoneyung | 
 |  |  | 
 |  |  | @Slf4j | 
 |  |  | public class News { | 
 |  |  |  | 
 |  |  |     interface NewsSupport<T> { boolean execute(T t); } | 
 |  |  |     public static void main(String[] args) { | 
 |  |  |         News.info("info{}", 1); | 
 |  |  |         News.warn("warn{}", 2); | 
 |  |  |         News.error("error{}", 3); | 
 |  |  |         System.out.println(News.print()); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     interface NewsSupport<T> { | 
 |  |  |         boolean execute(T t); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private static final NewsQueue<NewsDomain> NEWS_QUEUE = new NewsQueue<>(NewsDomain.class, 1024); | 
 |  |  |  | 
 |  |  | 
 |  |  |         private int head; | 
 |  |  |         private int tail; | 
 |  |  |  | 
 |  |  |         { this.head = 0; this.tail = 0; } | 
 |  |  |         { | 
 |  |  |             this.head = 0; | 
 |  |  |             this.tail = 0; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public NewsQueue(Class<T> cls, int capacity) { | 
 |  |  |             this.cls = cls; | 
 |  |  |             arr = (T[]) Array.newInstance(cls, capacity); | 
 |  |  |             this.arr = (T[]) Array.newInstance(cls, capacity); | 
 |  |  |             this.capacity = capacity; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public synchronized boolean offer(T t) { | 
 |  |  |             if (this.tail == this.capacity) { | 
 |  |  |                 this.peek(); | 
 |  |  |             } | 
 |  |  |             this.reform(); | 
 |  |  |             this.arr[this.tail] = t; | 
 |  |  |             this.tail++; | 
 |  |  |             return true; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public synchronized boolean put(T t) { | 
 |  |  |             if (this.tail == this.capacity) { | 
 |  |  |                 return false; | 
 |  |  |             } else { | 
 |  |  |                 for (int i = this.head; i < this.tail; i++) { | 
 |  |  |                     this.arr[i-this.head] = this.arr[i]; | 
 |  |  |                 } | 
 |  |  |                 this.tail -= this.head; | 
 |  |  |                 this.head = 0; | 
 |  |  |                 this.reform(); | 
 |  |  |             } | 
 |  |  |             this.arr[this.tail] = t; | 
 |  |  |             this.tail ++; | 
 |  |  |             this.tail++; | 
 |  |  |             return true; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  | 
 |  |  |                 return null; | 
 |  |  |             } | 
 |  |  |             T t = this.arr[this.head]; | 
 |  |  |             this.head ++; | 
 |  |  |             this.head++; | 
 |  |  |             this.reform(); | 
 |  |  |             return t; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         private void reform() { | 
 |  |  |             for (int i = this.head; i < this.tail; i++) { | 
 |  |  |                 this.arr[i - this.head] = this.arr[i]; | 
 |  |  |             } | 
 |  |  |             this.tail -= this.head; | 
 |  |  |             this.head = 0; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         public synchronized int size() { | 
 |  |  | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void info(String format) { | 
 |  |  |         offer(NewsLevel.INFO, format, null); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void info(String format, Object argument) { | 
 |  |  |         offer(NewsLevel.INFO, format, new Object[]{argument}); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void info(String format, Object... arguments) { | 
 |  |  |         log.info(format, arguments); | 
 |  |  |         offer(NewsLevel.INFO, format, arguments); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void warn(String format, Object... arguments) { | 
 |  |  |         log.warn(format, arguments); | 
 |  |  |         offer(NewsLevel.WARN, format, arguments); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void error(String format, Object... arguments) { | 
 |  |  |         log.error(format, arguments); | 
 |  |  |         offer(NewsLevel.ERROR, format, arguments); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void taskInfo(Integer wrkNo, String format, Object... arguments) { | 
 |  |  |         info(format, arguments); | 
 |  |  |         offerTask(wrkNo, format, arguments); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void taskWarn(Integer wrkNo, String format, Object... arguments) { | 
 |  |  |         warn(format, arguments); | 
 |  |  |         offerTask(wrkNo, format, arguments); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static void taskError(Integer wrkNo, String format, Object... arguments) { | 
 |  |  |         error(format, arguments); | 
 |  |  |         offerTask(wrkNo, format, arguments); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static String printStr() { | 
 |  |  |         StringBuilder sb = new StringBuilder("["); | 
 |  |  |         List<NewsDomain> domains = NEWS_QUEUE.data(); | 
 |  |  |         for (int i = 0; i < domains.size(); i++) { | 
 |  |  |             NewsDomain domain = domains.get(i); | 
 |  |  |             sb.append("{"); | 
 |  |  |             sb.append("\"l\":").append(domain.level.idx).append(","); | 
 |  |  |             sb.append("\"v\":\"").append(domain.content).append("\"").append(","); | 
 |  |  |             sb.append("\"t\":\"").append(domain.date).append("\""); | 
 |  |  |             sb.append("}"); | 
 |  |  |             if (i < domains.size() - 1) { | 
 |  |  |                 sb.append(","); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         sb.append("]"); | 
 |  |  |         return sb.toString(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static List<Map<String, Object>> print() { | 
 |  |  |         List<Map<String, Object>> res = new ArrayList<>(); | 
 |  |  |         for (NewsDomain datum : NEWS_QUEUE.data()) { | 
 |  |  |             Map<String, Object> map = new HashMap<>(); | 
 |  |  |             map.put("l", datum.level.idx); | 
 |  |  |             map.put("v", datum.content); | 
 |  |  |             map.put("t", datum.date); | 
 |  |  |             res.add(map); | 
 |  |  |         } | 
 |  |  |         return res; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private static boolean offerTask(Integer wrkNo, String msg, Object[] args) { | 
 |  |  |         WrkMastService wrkMastService = SpringUtils.getBean(WrkMastService.class); | 
 |  |  |         if (wrkMastService == null) { | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         WrkMast wrkMast = wrkMastService.selectByWorkNo(wrkNo); | 
 |  |  |         if (wrkMast == null) { | 
 |  |  |             return false; | 
 |  |  |         } | 
 |  |  |         String systemMsg = replace(msg, args); | 
 |  |  |         if (!systemMsg.equals(wrkMast.getSystemMsg())) { | 
 |  |  |             wrkMast.setSystemMsg(systemMsg); | 
 |  |  |             wrkMast.setModiTime(new Date()); | 
 |  |  |             wrkMastService.updateById(wrkMast); | 
 |  |  |         } | 
 |  |  |         return true; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private static boolean offer(NewsLevel level, String msg, Object[] args) { | 
 |  |  |         return NEWS_QUEUE.offer(new NewsDomain(level, msg, new Date())); | 
 |  |  |         return NEWS_QUEUE.offer(new NewsDomain(level, replace(msg, args), (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()))); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static String replace(String str, Object[] objs) { | 
 |  |  |         if (null == objs || objs.length == 0 || null == str || "".equals(str.trim())) { | 
 |  |  |             return str; | 
 |  |  |         } else { | 
 |  |  |             StringBuilder sb = new StringBuilder(str); | 
 |  |  |             for (Object obj : objs) { | 
 |  |  |                 int idx = sb.indexOf("{}"); | 
 |  |  |                 if (idx == -1) { | 
 |  |  |                     break; | 
 |  |  |                 } | 
 |  |  |                 sb.replace(idx, idx + 2, String.valueOf(obj)); | 
 |  |  |             } | 
 |  |  |             return sb.toString(); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     static class NewsDomain { | 
 |  |  |         public NewsLevel level; | 
 |  |  |         public String content; | 
 |  |  |         public Date date; | 
 |  |  |         public String date; | 
 |  |  |  | 
 |  |  |         public NewsDomain(NewsLevel level, String content, Date date) { | 
 |  |  |         public NewsDomain(NewsLevel level, String content, String date) { | 
 |  |  |             this.level = level; | 
 |  |  |             this.content = content; | 
 |  |  |             this.date = date; | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     enum NewsLevel { | 
 |  |  |         INFO, | 
 |  |  |         WARN, | 
 |  |  |         ERROR, | 
 |  |  |         INFO(1), | 
 |  |  |         WARN(2), | 
 |  |  |         ERROR(3), | 
 |  |  |         ; | 
 |  |  |         public int idx; | 
 |  |  |  | 
 |  |  |         NewsLevel(int idx) { | 
 |  |  |             this.idx = idx; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |