lyh
7 小时以前 7126eab629d31beb5164b576a44b865a6a00f07c
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
package com.lxzn.activiti;
 
import com.lxzn.base.service.IDncPassLogService;
import com.lxzn.framework.domain.base.DncPassLog;
import com.lxzn.framework.domain.base.SysLogMessageDto;
import com.lxzn.framework.domain.base.SysLogTypeObjectDto;
import com.lxzn.framework.utils.FileClient;
import com.lxzn.framework.utils.SyslogClient;
import com.lxzn.framework.utils.date.DateUtil;
import com.lxzn.framework.utils.file.FileUtil;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
 
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestActiviti {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProcessEngine processEngine;
 
    @Autowired
    private IDncPassLogService dncPassLogService;
 
    @Test
    public void fileClientTxtOrNc() {
        dncPassLogService.fileClientTxtOrNc();
    }
 
 
    /**
     * 流程定义的部署
     * activiti表有哪些?
     * act_re_deployment  部署信息
     * act_re_procdef     流程定义的一些信息
     * act_ge_bytearray   流程定义的bpmn文件及png文件
     */
    @Test
    public void testDeployment() {
        //ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("processes/assign_nc_to_device.bpmn")  //添加bpmn资源
                .name("指派NC文档到设备")
                .deploy();
        System.out.println(deployment.getName());
        System.out.println(deployment.getId());
    }
 
    /**
     * 启动流程实例:
     *     前提是先已经完成流程定义的部署工作
     *
     *  背后影响的表:
     *  ACT_HI_ACTINST     已完成的活动信息
     *  ACT_HI_DETAIL      历史详情
     *  ACT_HI_IDENTITYLINK   参与者信息
     *  ACT_HI_PROCINST   流程实例
     *  ACT_HI_TASKINST   任务实例
     *  ACT_HI_VARINST   历史变量
     *  ACT_RU_IDENTITYLINK  执行时 参与者信息
     *  ACT_RU_EXECUTION     执行表
     *  ACT_RU_TASK  当前任务
     *  ACT_RU_VARIABLE  执行时 参数信息
     */
    @Test
    public void testStartInstance() {
        //3.创建流程实例  流程定义的key需要知道 holiday
        String applyUser = "1255172650880737281";
        //String[] approveUsers = {"1254773336467689474", "1254966905669160962"};
        String approveUsers = "1254773336467689474, 1254966905669160962";
        String businessKey = "111111";
        String key = "assign_nc_to_device";
        Map<String, Object> map = new HashMap<>();
 
        map.put("apply_user", applyUser);
        map.put("approve_users", approveUsers);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(key, businessKey, map);
 
        //4.输出实例的相关信息
        System.out.println("流程部署ID"+processInstance.getDeploymentId());//null
        System.out.println("流程定义ID"+processInstance.getProcessDefinitionId());//holiday:1:4
        System.out.println("流程实例ID"+processInstance.getId());//2501
        System.out.println("活动ID"+processInstance.getActivityId());//null
    }
 
    /**
     * 从ACT_RU_TASK表中获取当前用户的任务列表
     * 从ACT_RU_TASK表中获取taskId的任务
     */
    @Test
    public void testCompleteTask() {
        //获取用户的任务列表
        //List<Task> taskList = taskService.createTaskQuery().taskAssignee("1255172650880737281").list();
        Task task = taskService.createTaskQuery().taskId("7509").singleResult();
        if(task != null) {
            //4.任务列表的展示
            System.out.println("流程实例ID:"+task.getProcessInstanceId());
            System.out.println("任务ID:"+task.getId());  //5002
            System.out.println("任务负责人:"+task.getAssignee());
            System.out.println("任务名称:"+task.getName());
            taskService.complete(task.getId());
        }
    }
 
    @Test
    public void testCandidateUser() {
        String key = "assign_nc_to_device";
        String candidate_user = "1254773336467689474";
//        List<Task> taskList = taskService.createTaskQuery().processDefinitionKey(key).taskCandidateUser(candidate_user)//设置候选用户
//                .list();
//        taskList.forEach(System.out::println);
        Task task = taskService.createTaskQuery().processDefinitionKey(key)
                .taskCandidateUser(candidate_user).singleResult();
        if(task != null) {
            taskService.claim(task.getId(), candidate_user);
            System.out.println("拾取任务成功");
            taskService.complete(task.getId());
            System.out.println("完成任务成功");
        }
    }
 
    @Value("${securedoc.serverIp}")
    private String serverIp;
    @Value("${securedoc.serverPort}")
    private String serverPort;
    @Value("${securedoc.username}")
    private String usernameService;
    @Value("${securedoc.pwd}")
    private String pwdService;
    @Value("${securedoc.addressToken}")
    private String addressToken;
    @Value("${securedoc.addressUploadFile}")
    private String addressUploadFile;
    @Value("${securedoc.localFilePath}")
    private String localFilePathC;
    @Value("${securedoc.servicePath}")
    private String servicePathS;
    @Value("${securedoc.copyFilePath}")
    private String newFilePathC;
    @Value("${securedoc.logIp}")
    private String logIp;
    @Value("${securedoc.logPort}")
    private String logPort;
 
    @Test
    public void fileClientTest() {
        /*SysLogMessageDto message = new SysLogMessageDto();
        message.setSystemName("DNC");
        message.setDatetime(DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_SMALL));
        message.setLocation("成发工业园");
        message.setTypes("info");
        message.setAbstract1("abstract");
        message.setSourceAddress("");
       */
        String host = "127.0.0.1";
        if (StringUtils.isNotBlank(serverIp)) {
            host = serverIp;
        }
 
        String port = "8299";
        if (StringUtils.isNotBlank(serverPort)) {
            port = serverPort;
        }
        String username = "admin";
        if (StringUtils.isNotBlank(usernameService)) {
            username = usernameService;
        }
        String pwd = "123";
        if (StringUtils.isNotBlank(pwdService)) {
            pwd = pwdService;
        }
        //文件本地地址,根据地址找到文件,将文件解析成文件流
        String localFilePath = "E:\\test\\a\\";
        if (StringUtils.isNotBlank(localFilePathC)) {
            localFilePath = localFilePathC;
        }
        //文件上传目的地址,将文件上传到该地址
        String servicePath = "E:\\test\\b\\";
        if (StringUtils.isNotBlank(servicePathS)) {
            servicePath = servicePathS;
        }
        //第一步,获取token
        String token = null;
        try {
            token = FileClient.getToken(host,port,username,pwd,addressToken);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
 
        //若获取token成功,再进行上传文件接口调用
        if (!token.equals("")){
            try {
                File f3 = new File(localFilePath);
                File[] files = f3.listFiles();
                for (File fi : files){
                    if (fi.isFile()){
                        SysLogTypeObjectDto objectName = new SysLogTypeObjectDto();
                        objectName.setDateTime(DateUtil.format(DateUtil.getNow(),DateUtil.STR_DATE_TIME_SMALL));
                        objectName.setFileName(fi.getName());
                        objectName.setFileSize(FileUtil.changeFileFormatKb(String.valueOf(new File(localFilePath).length())));
                        objectName.setSourceAddress(localFilePath);
                        //顺序号
                        DncPassLog passLog = dncPassLogService.getById("num0001");
                        if (passLog == null) {
                            passLog = new DncPassLog();
                            passLog.setId("num0001");
                            passLog.setSequenceNumber(1);
                            objectName.setFileNum(Integer.toString(1));;
                            dncPassLogService.save(passLog);
                        } else {
                            Integer number = passLog.getSequenceNumber()+1;
                            dncPassLogService.removeById("num0001");
                            objectName.setFileNum(Integer.toString(number));
 
                            passLog = new DncPassLog();
                            passLog.setId("num0001");
                            passLog.setSequenceNumber(number);
                            dncPassLogService.save(passLog);
                        }
 
                        InetAddress address = null;
                        try {
                            address = InetAddress.getLocalHost();
                            String ip = address.getHostAddress();
                            objectName.setSourceAddress(ip);
                        } catch (UnknownHostException e) {
                            objectName.setSourceAddress("127.0.0.1");
                        }
                        objectName.setDestination(host);
                        objectName.setResult("失败");
                        //获取某个文件下的所有文件
                        String loFilePath = localFilePath + "\\" + fi.getName();
                        String servicePathName =servicePath + "\\" + fi.getName();
                        String b  = FileClient.uploadFile(host,port,token,fi.getName(),servicePathName,loFilePath,addressUploadFile);
                        //文件备份删除
                        if (b == null) {
                            try {
                                objectName.setResult("失败");
                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
                            }catch (Exception e) {
                                return;
                            }
                        }
                        else if ( b.equals("成功")) {
                            //备份数据
                            boolean fCopy = FileUtil.copyNcFile(loFilePath,newFilePathC + "/" + DateUtil.format(DateUtil.getNow(),
                                    DateUtil.STR_YEARMONTHDAY) + "/" +fi.getName());
                            if (fCopy) {
                                FileUtil.deleteNcFile(loFilePath);
                            }
                            try {
                                objectName.setResult("成功");
                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
                            }catch (Exception e) {
                                return;
                            }
                        } else {
                            try {
                                objectName.setResult("失败");
                                SyslogClient.sendClient(logIp,Integer.valueOf(logPort),objectName.toString());
                            }catch (Exception e) {
                                return;
                            }
                        }
                    }
                }
 
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }
 
 
}