UserLogThread.java 3.38 KB
Newer Older
zhangxiaoyan committed
1 2
package util;

manxiaoqiang committed
3
import common.context.*;
zhangxiaoyan committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import common.model.UserLog;
import common.repository.UserLogRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;

/**
 * Created by zxy on 2017/12/15.
 */
public class UserLogThread
        extends Thread
{
    protected Logger logger = LoggerFactory.getLogger(UserLogThread.class);

    private UserLogRepository userLogRepository = AppUtils.getApplicationContext().getBean(UserLogRepository.class);
zhangxiaoyan committed
22
    //操作用户
zhangxiaoyan committed
23
    private String operaAccount;
zhangxiaoyan committed
24
    //操作用户名
zhangxiaoyan committed
25
    private String accountName;
zhangxiaoyan committed
26
    //操作对象的类型:OperateObjectTypeEnum
zhangxiaoyan committed
27
    private String operateObjectType;
zhangxiaoyan committed
28
    //客户管理和系统账号管理是操作对象的账号,以后再加别的模块再定
zhangxiaoyan committed
29
    private String operateObject;
zhangxiaoyan committed
30
    //操作类型:“创建账号”“编辑账号”“录入缴费信息”“录入回访信息”“处理过期提醒”等等,如果都定下来有哪些,可以做一个字典
zhangxiaoyan committed
31
    private String operateType;
zhangxiaoyan committed
32
    //为了获取ip及解析地点
zhangxiaoyan committed
33 34
    private HttpServletRequest request;

manxiaoqiang committed
35 36
    private Long role;

manxiaoqiang committed
37 38
    private String platfrom;

zhangxiaoyan committed
39 40 41 42 43
    /**
     * 传入内容为对象
     * UserLogThread userlog = new UserLogThread("zxy@reyun.com", "zxy", OperateObjectTypeEnum.CUSTOMER.getKey(), "app", "查看app", request);
     * userlog.start();
     */
manxiaoqiang committed
44 45 46 47 48 49 50 51 52 53 54 55
    public UserLogThread(String operaAccount, String accountName, String operateObjectType, String operateObject, String operateType, HttpServletRequest request, Long role, String platform)
    {
        this.operaAccount = operaAccount;
        this.accountName = accountName;
        this.operateObjectType = operateObjectType;
        this.operateObject = operateObject;
        this.operateType = operateType;
        this.request = request;
        this.role = role;
        this.platfrom = platform;
    }

manxiaoqiang committed
56
    public UserLogThread(String operaAccount, String accountName, String operateObjectType, String operateObject, String operateType, HttpServletRequest request, Long role)
zhangxiaoyan committed
57 58 59 60 61 62 63
    {
        this.operaAccount = operaAccount;
        this.accountName = accountName;
        this.operateObjectType = operateObjectType;
        this.operateObject = operateObject;
        this.operateType = operateType;
        this.request = request;
manxiaoqiang committed
64
        this.role = role;
zhangxiaoyan committed
65 66 67 68 69 70 71 72 73 74 75 76 77
    }


    @Override
    public void run()
    {
        try
        {
            UserLog audit = new UserLog();
            audit.setOperaAccount(this.operaAccount);
            audit.setAccountName(this.accountName);
            audit.setOperateObjectType(this.operateObjectType);
            audit.setOperateObject(this.operateObject);
zhangxiaoyan committed
78
            audit.setOperateTime(DateUtil.format(new Date(), DateUtil.C_TIME_PATTON_DEFAULT));
zhangxiaoyan committed
79
            audit.setOperateType(this.operateType);
manxiaoqiang committed
80
            audit.setRole(this.role);
manxiaoqiang committed
81
            audit.setPlatfrom(this.platfrom);
zhangxiaoyan committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

            if (this.request != null) {
                String ipAddr = IPAddrUtil.getIpAddrNew(request);
                audit.setIp(ipAddr);
                if(ValidateUtil.isValid(ipAddr)){
                    String locationFromIpAddr = IPAddrUtil.getLocationFromIpAddr(ipAddr);
                    audit.setLocation(locationFromIpAddr);
                }
            }


            userLogRepository.save(audit);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}