summaryrefslogtreecommitdiff
path: root/src/main/java/com/mesasoft/cn/util/ServiceUtils.java
blob: 2e3b5423b9de0c4bef44f6ea7f4337d05f8cb6ac (plain)
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
package com.mesasoft.cn.util;

import com.mesasoft.cn.service.ICategoryService;
import com.mesasoft.cn.service.IFileService;
import com.mesasoft.cn.service.IUserService;
import com.zhazhapan.modules.constant.ValueConsts;
import com.zhazhapan.util.Checker;
import com.zhazhapan.util.ReflectUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;

/**
 * @author pantao
 * @since 2018/2/28
 */
@Component
public class ServiceUtils {

    private static IUserService userService;

    private static IFileService fileService;

    private static ICategoryService categoryService;

    private static Logger logger = Logger.getLogger(ServiceUtils.class);

    @Autowired
    public ServiceUtils(IUserService userService, IFileService fileService, ICategoryService categoryService) {
        ServiceUtils.userService = userService;
        ServiceUtils.fileService = fileService;
        ServiceUtils.categoryService = categoryService;
    }

    public static int getUserId(String usernameOrEmail) {
        return Checker.isEmpty(usernameOrEmail) ? ValueConsts.ZERO_INT : userService.getUserId(usernameOrEmail);
    }

    public static long getFileId(String fileName) {
        return Checker.isEmpty(fileName) ? ValueConsts.ZERO_INT : fileService.getFileId(fileName);
    }

    public static int getCategoryId(String categoryName) {
        return Checker.isEmpty(categoryName) ? ValueConsts.ZERO_INT : categoryService.getIdByName(categoryName);
    }

    public static Object invokeFileFilter(Object object, String methodName, String user, String file, String
            category, int offset) {
        try {
            return ReflectUtils.invokeMethodUseBasicType(object, methodName, new Object[]{getUserId(user), getFileId
                    (file), file, getCategoryId(category), offset});
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            logger.error(e.getMessage());
            return null;
        }
    }
}