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 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(); } } } } }