DriverJob.java 5.16 KB
package com.ruoyi.job;

import com.ruoyi.driver.domain.Driver;
import com.ruoyi.driver.service.IDriverService;
import com.ruoyi.utils.ListUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该定时任务用户获取驾驶员信息
 *
 * @author 20412
 */
@Component("driverJob")
public class DriverJob implements InitializingBean {

    @Resource
    private RestTemplate restTemplate;

    @Autowired
    private IDriverService driverService;

    @Value("${api.url.getDriverInfo}")
    private String getDriverInfoUrl;

    @Value("${api.config.password}")
    private String password;

    @Value("${api.config.nonce}")
    private String nonce;

    private static IDriverService DRIVER_SERVICE;
    private static RestTemplate RESTTEMPLATE;

    private static String GET_DRIVER_INFO_URL;

    private static String PASSWORD;

    private static String NONCE;

    /**
     * 通过该定时任务获取驾驶员信息 并保存到数据库
     */
    public void getDriverInfo(String params) throws Exception {
        try {
            String format = String.format(GET_DRIVER_INFO_URL, params);
            Map<String, String> configMap = new HashMap<>(5);
            long timestamp = System.currentTimeMillis();
            configMap.put("timestamp", String.valueOf(timestamp));
            configMap.put("nonce", NONCE);
            configMap.put("password", PASSWORD);
            String sign = getSHA1(configMap);
            String url = format
                    + "?timestamp=" + timestamp
                    + "&nonce=" + NONCE
                    + "&password=" + PASSWORD
                    + "&sign=" + sign;
            //生成签名
            List<Driver> drivers = RESTTEMPLATE.exchange(
                    url,
                    HttpMethod.GET,
                    null,
                    new ParameterizedTypeReference<List<Driver>>() {
                    }).getBody();
            List<List<Driver>> splitList = ListUtils.splitList(drivers, 1000);
            System.out.println("开始更新数据-----");
            for (List<Driver> driverList : splitList) {
                DRIVER_SERVICE.insertDrivers(driverList);
            }
            System.out.println("数据更新完毕-----");
        } catch (Exception e) {
            System.out.println("执行失败:" + e.getMessage());
        }
        System.out.println("执行结束");
    }


    /**
     * 获取签名
     *
     * @param map
     * @return
     * @throws Exception
     */
    public static String getSHA1(Map<String, String> map) throws Exception {
        try {
            String[] array = new String[map.size()];
            map.values().toArray(array);
            StringBuffer sb = new StringBuffer();
            // 字符串排序
            Arrays.sort(array);
            for (int i = 0; i < array.length; i++) {
                sb.append(array[i]);
            }
            String str = sb.toString();
            // SHA1签名生成
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes());
            byte[] digest = md.digest();
            StringBuffer hexstr = new StringBuffer();
            String shaHex = "";
            for (int i = 0; i < digest.length; i++) {
                shaHex = Integer.toHexString(digest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexstr.append(0);
                }
                hexstr.append(shaHex);
            }
            return hexstr.toString();
        } catch (Exception e) {
            throw e;
        }


    }

//    public static void main(String[] args) throws Exception{
//        RestTemplate restTemplate1 = new RestTemplate();
//        String url = "http://101.95.136.206:9089/webservice/rest/person/company/%s";
//        String format = String.format(url,"99");
//        Map<String, String> configMap = new HashMap<>(5);
//        long timestamp = System.currentTimeMillis();
//        configMap.put("timestamp", String.valueOf(timestamp));
//        configMap.put("nonce", "NONCE");
//        configMap.put("password", "c4dd3d8cb9a82f6d6a625818618b28ca7bebb464");
//        String sign = getSHA1(configMap);
//        String httpUrl = format
//                + "?timestamp=" + timestamp
//                + "&nonce=" + "NONCE"
//                + "&password=" + "c4dd3d8cb9a82f6d6a625818618b28ca7bebb464"
//                + "&sign=" + sign;
//        Object forObject = restTemplate1.getForObject(httpUrl, Object.class);
//    }

    @Override
    public void afterPropertiesSet() throws Exception {
        GET_DRIVER_INFO_URL = getDriverInfoUrl;
        NONCE = nonce;
        PASSWORD = password;
        RESTTEMPLATE = restTemplate;
        DRIVER_SERVICE = driverService;
    }
}