yujian
2020-01-03 c9cbdfa41d645d42eeaa7e06d550d4ef8ac328e7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package com.yeshi.fanli.util;
 
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.servlet.http.HttpServletRequest;
 
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.yeshi.utils.taobao.TbImgUtil;
 
import com.yeshi.fanli.entity.admin.GoodsClassAdmin;
import com.yeshi.fanli.entity.admin.HotSearchAdmin;
import com.yeshi.fanli.entity.admin.RecommendBannerAdmin;
import com.yeshi.fanli.entity.admin.RecommendSpecialAdmin;
import com.yeshi.fanli.entity.common.AdminUser;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
public class Utils {
 
    private static final String ANDROID = "ANDROID";
    private static final String IOS = "IOS";
    private static final String WEB = "WEB";
 
    private static final Map<String, String> map = new HashMap<String, String>();
 
    static {
        map.put("1", ANDROID);
        map.put("2", IOS);
        map.put("3", WEB);
    }
 
    public static Map<String, String> getMap() {
        return map;
    }
 
    public static boolean isEmailUrlRight(String email, String sign, long time) {
        if (StringUtil.Md5(email + time + "WEIJU2016xxx").equalsIgnoreCase(sign)
                && Math.abs(System.currentTimeMillis() - time) < 1000 * 60 * 60 * 2) {
            return true;
        }
        return false;
    }
 
    public static String getUrlPageTitle(String url) {
        try {
            Document doc = Jsoup.connect(url).timeout(1000 * 60).get();
            return doc.title();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static AdminUser getAdminUser(HttpServletRequest request) {
        AdminUser adminUser = (AdminUser) request.getSession().getAttribute("ADMIN_USERINFO");
        return adminUser;
    }
 
    public static boolean signIsRight(String devicename, String imei, String sign) {
        return sign.equalsIgnoreCase(StringUtil.Md5(devicename + imei + "WEIju2016888xx3"));
    }
 
    public static Map<String, Object> sort(Map<String, Integer> map) {
        // 通过ArrayList构�?函数把map.entrySet()转换成list
        List<Map.Entry<String, Object>> list = new ArrayList<Map.Entry<String, Object>>((Collection) map.entrySet());
        // 通过比较器实现比较排�?
        Collections.sort(list, new Comparator<Map.Entry<String, Object>>() {
            public int compare(Map.Entry<String, Object> mapping1, Map.Entry<String, Object> mapping2) {
                return mapping1.getKey().compareTo(mapping2.getKey());
            }
        });
 
        Map<String, Object> newMap = new TreeMap<String, Object>();
        for (Entry<String, Object> entry : list) {
            newMap.put(entry.getKey(), entry.getValue());
        }
        return newMap;
    }
 
 
    @SuppressWarnings("unchecked")
    public static Map orderBy(Map map) {
        if (map.size() == 0) {
            return map;
        }
 
        Collection values = map.values();
        Object obj = null;
        for (Object object : values) {
            obj = object;
            break;
        }
        if (obj instanceof RecommendBannerAdmin) {
 
            // 通过ArrayList构�?函数把map.entrySet()转换成list
            List<Map.Entry<Long, RecommendBannerAdmin>> list = new ArrayList<Map.Entry<Long, RecommendBannerAdmin>>(
                    (Collection) map.entrySet());
            // 通过比较器实现比较排�?
            Collections.sort(list, new Comparator<Map.Entry<Long, RecommendBannerAdmin>>() {
                public int compare(Map.Entry<Long, RecommendBannerAdmin> mapping1,
                        Map.Entry<Long, RecommendBannerAdmin> mapping2) {
                    int orderby1 = mapping1.getValue().getRecommendBanner().getOrderby();
                    int orderby2 = mapping2.getValue().getRecommendBanner().getOrderby();
                    return orderby1 - orderby2;
                }
            });
            LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
            for (Entry<Long, RecommendBannerAdmin> entry : list) {
                newMap.put(entry.getKey(), entry.getValue());
            }
            return newMap;
        } else if (obj instanceof RecommendSpecialAdmin) {
            List<Map.Entry<Long, RecommendSpecialAdmin>> list = new ArrayList<Map.Entry<Long, RecommendSpecialAdmin>>(
                    (Collection) map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Long, RecommendSpecialAdmin>>() {
                public int compare(Map.Entry<Long, RecommendSpecialAdmin> mapping1,
                        Map.Entry<Long, RecommendSpecialAdmin> mapping2) {
                    int orderby1 = mapping1.getValue().getRecommendSpecial().getOrderby();
                    int orderby2 = mapping2.getValue().getRecommendSpecial().getOrderby();
                    return orderby1 - orderby2;
                }
            });
            LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
            for (Entry<Long, RecommendSpecialAdmin> entry : list) {
                newMap.put(entry.getKey(), entry.getValue());
            }
            return newMap;
        } else if (obj instanceof HotSearchAdmin) {
            List<Map.Entry<Long, HotSearchAdmin>> list = new ArrayList<Map.Entry<Long, HotSearchAdmin>>(
                    (Collection) map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Long, HotSearchAdmin>>() {
                public int compare(Map.Entry<Long, HotSearchAdmin> mapping1, Map.Entry<Long, HotSearchAdmin> mapping2) {
                    int orderby1 = mapping1.getValue().getHotSearch().getOrderby();
                    int orderby2 = mapping2.getValue().getHotSearch().getOrderby();
                    return orderby1 - orderby2;
                }
            });
            LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
            for (Entry<Long, HotSearchAdmin> entry : list) {
                newMap.put(entry.getKey(), entry.getValue());
            }
            return newMap;
        } else if (obj instanceof GoodsClassAdmin) {
            List<Map.Entry<Long, GoodsClassAdmin>> list = new ArrayList<Map.Entry<Long, GoodsClassAdmin>>(
                    (Collection) map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Long, GoodsClassAdmin>>() {
                public int compare(Map.Entry<Long, GoodsClassAdmin> mapping1,
                        Map.Entry<Long, GoodsClassAdmin> mapping2) {
                    int orderby1 = mapping1.getValue().getGoodsClass().getOrderby();
                    int orderby2 = mapping2.getValue().getGoodsClass().getOrderby();
                    return orderby1 - orderby2;
                }
            });
            LinkedHashMap<Long, Object> newMap = new LinkedHashMap<Long, Object>();
            for (Entry<Long, GoodsClassAdmin> entry : list) {
                newMap.put(entry.getKey(), entry.getValue());
            }
            return newMap;
        }
        return map;
    }
 
    public static List<String> getDateMonthList(Date beginDate, Date endDate) {
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
 
        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(beginDate);
 
        int month = beginCalendar.get(Calendar.MONTH) - endCalendar.get(Calendar.MONTH);
        List<String> list = new ArrayList<String>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        for (int ii = month; ii < 0; ii++) {
            Calendar ca = Calendar.getInstance();
            ca.set(Calendar.DAY_OF_MONTH, 1);
            ca.set(Calendar.MONTH, (ca.get(Calendar.MONTH)) + ii + 1);
            String format = dateFormat.format(ca.getTime());
            list.add(format);
        }
        return list;
    }
 
    public static List<String> getDateMonthList(int month, Date date) {
        List<String> list = new ArrayList<String>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Calendar ca;
        for (int ii = -month; ii < 0; ii++) {
            ca = Calendar.getInstance();
            ca.setTime(date);
            ca.set(Calendar.DAY_OF_MONTH, 1);
            ca.set(Calendar.MONTH, (ca.get(Calendar.MONTH)) + ii + 1);
            String format = dateFormat.format(ca.getTime());
            list.add(format);
        }
        return list;
    }
 
    public static int countMonths(String date1, String date2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
 
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
 
        int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
 
        // �?��日期若小月结束日�?
        if (year < 0) {
            year = -year;
            return year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
        }
 
        return year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH);
    }
 
    public static int countDay(String date1, String date2) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
 
        c1.setTime(sdf.parse(date1));
        c2.setTime(sdf.parse(date2));
        int day = ((Long) ((c2.getTimeInMillis() - c1.getTimeInMillis()) / 86400000)).intValue();
        return day;
    }
 
    public static String imgSize(String jsonStr) {
        JSONObject json = JSONObject.fromObject(jsonStr);
        String picUrl = (String) json.opt("pictUrl");
        if (picUrl.contains("alicdn") || picUrl.contains("tbcdn")) {
            picUrl = TbImgUtil.getTBSize320Img(picUrl);
        }
        json.element("pictUrl", picUrl);
        return json.toString();
    }
 
    public static String imgListSize(String jsonStr) {
        JSONArray json = JSONArray.fromObject(jsonStr);
        for (Object object : json) {
            String picUrl = (String) ((JSONObject) object).opt("pictUrl");
            if (picUrl.contains("alicdn") || picUrl.contains("tbcdn")) {
                picUrl = TbImgUtil.getTBSize320Img(picUrl);
            }
            ((JSONObject) object).element("pictUrl", picUrl);
        }
        return json.toString();
    }
 
    public static Map<String, String> parseURL(String url) {
 
        String URL_REGEX = "(((http|https)://)|(www\\.))[a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6}(:[0-9]{1,4})?(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
        Pattern p = Pattern.compile(URL_REGEX);
        Matcher matcher = p.matcher(url);
        boolean b = matcher.matches();
        Map<String, String> map = new HashMap<String, String>();
        if (!b) {
            return map;
        }
        if (!url.contains("?")) {
            return map;
        }
 
        String params = url.substring(url.indexOf("?") + 1);
        if (params == null || "".equals(params)) {
            return map;
        }
 
        String[] paramArr = params.split("&");
 
        for (String arr : paramArr) {
            String[] kv = arr.split("=");
            if (kv.length == 1) {
                kv[1] = "";
            }
            map.put(kv[0], kv[1]);
        }
 
        return map;
    }
 
    public static double random(double mix) {
        return (Math.random() * 20) + mix;
    }
 
    public static String getStarString(String content, int begin, int end) {
 
        if (begin >= content.length() || begin < 0) {
            return content;
        }
        if (end >= content.length() || end < 0) {
            return content;
        }
        if (begin >= end) {
            return content;
        }
        String starStr = "";
        for (int i = begin; i < end; i++) {
            starStr = starStr + "*";
        }
        return content.substring(0, begin) + starStr + content.substring(end, content.length());
 
    }
 
    public static boolean isNum(String content) {
        if (content == null) {
            return false;
        }
        String regex = "\\d+";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(content);
        return matcher.matches();
    }
 
    public static boolean isOrderNum(String order) {
        if (order == null) {
            return false;
        }
        String regex = "\\d{16,}";
        Pattern compile = Pattern.compile(regex);
        Matcher matcher = compile.matcher(order);
        return matcher.matches();
    }
 
    public static boolean isUserOrder(String existOrder, String lostOrder) {
        if (existOrder.length() < 16 || lostOrder.length() < 16) {
            return false;
        }
        String existNum = existOrder.substring(existOrder.length() - 6);
        String lostOrderNum = lostOrder.substring(lostOrder.length() - 6);
 
        if (existNum.equals(lostOrderNum)) {
            return true;
        }
 
        return false;
    }
 
}