ludc
2024-09-09 88c5cf35a5ea870378d7964086ed2c09ddc299c8
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
package com.vci.web.service.impl;
 
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.starter.web.pagemodel.SessionInfo;
import com.vci.starter.web.util.WebThreadLocalUtil;
import com.vci.bo.VciLogObjectBO;
import com.vci.web.service.WebLogServiceI;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 日志服务
 * @author weidy
 * 
 */
@Service
public class WebLogServiceImpl implements WebLogServiceI {
    
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
    
    private LogThread logThread = null;
 
    /**
     * 平台客户端代理类
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    @Override
    public void addOperateLog(VciLogObjectBO logObject) {
        //VCIInvocationInfo vcii = WebThreadLocalUtil.getCurrentUserInvocationInfoInThread().get();
        SessionInfo si = WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
        if(si!=null){
            logObject.setUserId(si.getUserId());
            logObject.setUserName(si.getUserName());
            logObject.setUserOid(si.getUserOid());
            logObject.setIp(si.getIp());
            initThread(logObject,si);
        }else{
            //说明还没登录,不记录
        }
    }
    
    private void initThread(VciLogObjectBO logObject,SessionInfo si){
        if(logThread == null){
            logThread = new LogThread(platformClientUtil);
            logThread.isOnService=true;
            logThread.add(logObject,si);
            logThread.start();
        }else{
            logThread.add(logObject,si);
        }
    }
}
 
class LogThread extends Thread{
    public boolean isOnService = false;
    private PlatformClientUtil platformClientUtil = null;
    public LogThread(PlatformClientUtil platformClientUtil){
        this.platformClientUtil = platformClientUtil;
    }
 
    public List<VciLogObjectBO> allLogs = new ArrayList<VciLogObjectBO>();
 
    public Map<String,SessionInfo> log_userMap = new ConcurrentHashMap<String, SessionInfo>();
 
    
    public void add(VciLogObjectBO log,SessionInfo si){
        log.setOid(WebUtil.getPk());
        allLogs.add(log);
        log_userMap.put(log.getOid(),si);
    }
    @Override
    public void run() {
        while(isOnService){
            while(allLogs.size() >0){
                VciLogObjectBO log = allLogs.get(0);
                SessionInfo si = log_userMap.get(log.getOid());
                UserEntityInfo ue = new UserEntityInfo();
                ue.modules = log.getModelName();
                ue.userName = log.getUserId();
                WebThreadLocalUtil.setCurrentUserSessionInfoInThread(si);
                if(log.isDebug()){
                    log.setDescription(log.getDescription() + "," + log.getController() + "/" + log.getMethod());
                }
//                try {
//                    platformClientUtil.getLogonFactoryService().getFrameworkService().savelogGeneralOperation("操作完成",log.getDescription(), ue, "00000000000000000000000000000000000",log.getOperateName());
//                } catch (VCIError e) {
//                    System.out.println(e.error_code);
//                }
                allLogs.remove(log);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("关闭线程了");
    }
}