後端工具集

零、判斷ES索引是否存在

    /**
     * 判斷索引是否存在
     * @param indexName
     * @return
     */
    public boolean isExistsIndex(String indexName){
        IndicesExistsResponse response =client.admin().indices().exists(
                        new IndicesExistsRequest().indices(new String[]{indexName})).actionGet();
        return response.isExists();
    }

一、只啓動一次方法

    /**
     * 重新創建索引
     * 補救措施 如果索引誤刪 自動創建新的索引
     */
    @PostConstruct
    public void init(){
        if(!isExistsIndex("index")){ elasticsearchTemplate.putMapping(User.class); }
    }
@Service
public class IndexService implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        if(!isExistsIndex("index")){ elasticsearchTemplate.putMapping(User.class); }
    }
}

二、時間計算工具類


    /**
     * 分鐘轉毫秒
     * @param timingTime
     * @return 返回毫秒
     */
    public static Long getMinutesToMilliseconds(long timingTime){
        return timingTime * 60000;
    }

    /**
     * 計算下一個時間
     * @param workDate 周
     * @param time 小時分鐘
     * @return 返回下一個時間戳
     */
    public static Long getNextTime(String workDate, String time){
        //判斷當前第幾周
        List<String> list = Arrays.asList(workDate.split(","));
        String week = getWeek();
        //只勾選當前時間與當前時間之前
        if(list.size() > 1){
            // 存在當前是之後
            String theNext = getTheNext(list, week);
            if(theNext != null){
                return dateToStamp(nextDate(),time);
            }
            // 存在當前是之前
            String thePrevious = getThePrevious(list, week);
            if(thePrevious != null){
                String oneWeek = list.get(0);
                return dateToStamp(weekDate(oneWeek),time);
            }
        }
        //只勾選當前時間
        if(list.size() == 1 && list.contains(week)){
            return dateToStamp(weekSevenDate(),time);
        }
        return 0L;
    }

    /**
     * 獲取上一個元素
     * @param list list數組
     * @param week 當前元素
     * @return  返回上一個元素 如果沒有返回null
     */
    public static String getThePrevious(List<String> list,String week){
        String thePrevious = null;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            String n = list.get(i);
            if(n.equals(week)){
                if(i-1 >= 0){
                    thePrevious = list.get(i-1);
                }
            }
        }
        return thePrevious;
    }


    /**
     * 獲取下一個元素
     * @param list list數組
     * @param week  當前元素
     * @return 返回下一個元素 如果沒有返回null
     */
    public static String getTheNext(List<String> list,String week){
        String theNext = null;
        int size = list.size();
        for (int i = 0; i < size; i++) {
            String n = list.get(i);
            if(n.equals(week)){
                if(size > i+1){
                    theNext = list.get(i+1);
                }
            }
        }
        return theNext;
    }

    /**
     * 判斷當前時間是周幾
     * @return 返回當前時間是周幾
     */
    public static String getWeek() {
        String week = "";
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1) {
            week = "7";
        } else if (weekday == 2) {
            week = "1";
        } else if (weekday == 3) {
            week = "2";
        } else if (weekday == 4) {
            week = "3";
        } else if (weekday == 5) {
            week = "4";
        } else if (weekday == 6) {
            week = "5";
        } else if (weekday == 7) {
            week = "6";
        }
        return week;
    }

    /**
     * 將時間轉換爲時間戳
     * @param date 年月日
     * @param time 小時分鐘
     * @return  返回時間戳
     */
    public static Long dateToStamp(String date, String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long endTime = 0L;
        try {
            endTime = sdf.parse(date.concat(" ").concat(time)).getTime();
        } catch (Exception e) {
            log.error("日期轉時間戳異常:{}",e.getMessage());
        }
        return endTime;
    }

    /**
     * 當前時間加一天
     * @return 返回年月日
     */
    public static String nextDate(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 當前時間加幾天
     * @param week
     * @return 返回年月日
     */
    public static String weekDate(String week){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_YEAR,CalculationDaysUtils.getDays(getWeek(),week));
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 當前時間加 7天
     * @return 返回 年月日
     */
    public static String weekSevenDate(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        calendar.add(Calendar.DAY_OF_YEAR, 7);
        Date date = calendar.getTime();
        return sdf.format(date);
    }

    /**
     * 判斷時間是否在時間段內
     *
     * @param nowTime 現在時間
     * @param beginTime 開始時間
     * @param endTime 結束時間
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判斷這周與下週間隔時間
     *
     * @param weekOne 這週週幾
     * @param weekTwo  下週周幾
     * @return  間隔天數
     */
    public static Integer getDays(String weekOne,String weekTwo){
        Map<String, Integer> head = new HashMap<>(53);
        head.put("12",6);
        head.put("13",5);
        head.put("14",4);
        head.put("15",3);
        head.put("16",2);
        head.put("17",1);
        head.put("21",6);
        head.put("23",6);
        head.put("24",5);
        head.put("25",4);
        head.put("26",3);
        head.put("27",2);
        head.put("31",5);
        head.put("32",6);
        head.put("34",6);
        head.put("35",5);
        head.put("36",4);
        head.put("37",3);
        head.put("41",4);
        head.put("42",5);
        head.put("43",6);
        head.put("45",6);
        head.put("46",5);
        head.put("47",4);
        head.put("51",3);
        head.put("52",4);
        head.put("53",5);
        head.put("54",6);
        head.put("56",6);
        head.put("57",5);
        head.put("61",2);
        head.put("62",3);
        head.put("63",4);
        head.put("64",5);
        head.put("65",6);
        head.put("67",6);
        head.put("71",1);
        head.put("72",2);
        head.put("73",3);
        head.put("74",4);
        head.put("75",5);
        head.put("76",6);
        return head.get(weekOne.concat(weekTwo));
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章