郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.Scheduls.uploadUser;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gk.hotwork.Domain.UserInfo;
import com.gk.hotwork.Domain.Utils.Base64Encrypt;
import com.gk.hotwork.Domain.Utils.HttpUtils;
import com.gk.hotwork.Service.UserService;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
 
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
//@Configuration
//@EnableScheduling
//@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true")
public class uploadUserTask {
    private Logger logger = LogManager.getLogger(uploadUserTask.class);
 
    @Value("${nanowebUrl}")
    private String nanoweb;
    @Value("${nanowebEnable}")
    private Boolean nanowebEnable;
    @Autowired
    private UserService userService;
 
    @Scheduled(cron = "0/30 * * * * ?") //每隔30秒执行一次
    public void uploadUser() throws UnsupportedEncodingException {
        if (nanowebEnable){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<UserInfo> users = userService.selectNotUpload();
            if (users.size() > 0){
                String respnse = null;
                JSONArray jsonArray = new JSONArray();
                for (UserInfo userInfo : users) {
                    JSONObject jsonParam = new JSONObject();
                    jsonParam.put("name", userInfo.getRealname());
                    jsonParam.put("password", new String(Base64Encrypt.decode(users.get(0).getPassword()),"utf-8"));
                    jsonParam.put("tel", userInfo.getUsername());
                    jsonParam.put("idno", userInfo.getIdcard());
                    jsonParam.put("company", userInfo.getCompany());
                    jsonParam.put("department", userInfo.getDepartment());
                    jsonParam.put("email", userInfo.getEmail());
                    jsonArray.add(jsonParam);
                }
                String param = jsonArray.toJSONString();
                try {
                    respnse = HttpUtils.sendPost(nanoweb, param);
                    JSONObject jsonResult = JSONObject.parseObject(respnse);
                    String code = jsonResult.getString("code");
                    if (("success").equals(code)) {
                        logger.error("在时间为" + sdf.format(new Date()) + "上传成功" );
                        for (UserInfo userInfo : users) {
                            userInfo.setIsupload((byte)1);
                            userService.updateById(userInfo);
                        }
                    } else {
                        logger.error("在时间为" + sdf.format(new Date()) + "上传失败" + code);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
 
}