com.hzq.car.CarTest.java Source code

Java tutorial

Introduction

Here is the source code for com.hzq.car.CarTest.java

Source

package com.hzq.car;

import com.alibaba.fastjson.JSON;
import com.hzq.project.car.dao.SecondCarMapper;
import com.hzq.project.car.dao.entity.SecondCar;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * CarTest
 * Created by hzq on 16/11/6.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class CarTest {
    @Autowired
    SecondCarMapper secondCarMapper;

    private static final CloseableHttpClient client = HttpClients.custom().build();
    private static final String TOKEN_URL_TEST = "http://42.96.134.15:20009/oauth/AccessToken/token.json";
    private static final String TOKEN_URL_PROD = "https://open.souche.com/oauth/AccessToken/token.json";
    private static final String DATA_URL_TEST = "http://42.96.134.15:20009/api/car/queryCar.json";
    private static final String DATA_URL_PROD = "https://open.souche.com/api/car/queryCar.json";
    private static final boolean isProd = true;
    private static Logger logger = LoggerFactory.getLogger(CarTest.class);

    /**
     * ?token?
     */
    private String getToken() throws IOException {
        String url = TOKEN_URL_TEST;
        List<NameValuePair> params = new ArrayList<>();
        if (isProd) {
            url = TOKEN_URL_PROD;
            params.add(new BasicNameValuePair("username", "cx0576"));
            params.add(new BasicNameValuePair("password", "D9C67CBD875F94BFC0F4C16297AEC2AE"));
            params.add(new BasicNameValuePair("client_id", "cx0576"));
            params.add(new BasicNameValuePair("client_secret", "8114a135-8fc2-4b72-8b34-fd2fc210c722"));
            params.add(new BasicNameValuePair("grant_type", "password"));
        } else {
            params.add(new BasicNameValuePair("username", "cx0576"));
            params.add(new BasicNameValuePair("password", "cx0576"));
            params.add(new BasicNameValuePair("client_id", "cx0576"));
            params.add(new BasicNameValuePair("client_secret", "227b39f8-9cd4-4d4b-b74c-7ef41af2f238"));
            params.add(new BasicNameValuePair("grant_type", "password"));
        }
        String s = sendAndGetResponse(url, params);
        Map map = JSON.parseObject(s, Map.class);
        return (String) ((Map) map.get("data")).get("access_token");
    }

    @Test
    public void test02() throws IOException {
        String token = getToken();
        logger.warn("getToken: {}", token);
    }

    @Test
    public void test01() throws IOException, ParseException {

        //        String token = getToken();
        //        logger.warn("getToken: {}", token);
        String token = "932039219c9b5bf0ab65b9b50023ee56";
        String url = DATA_URL_TEST;
        if (isProd)
            url = DATA_URL_PROD;

        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = format.parse("2017-03-07"); //? 
        calendar.setTime(parse);
        //        calendar.add(Calendar.MONTH, -6);
        Date grabDate = calendar.getTime();

        //        Calendar NOW = Calendar.getInstance();
        //        NOW.add(Calendar.DATE, -2);

        while (grabDate.before(new Date())) {
            try {
                sendAndSave(token, grabDate, url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            calendar.add(Calendar.DATE, 1);
            grabDate = calendar.getTime();
        }

    }

    /**
     * ??post,json
     */
    private String sendAndGetResponse(String url, List<NameValuePair> params) throws IOException {
        HttpPost post = new HttpPost(url);
        post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
        StringBuilder messageBuilder = new StringBuilder("\n");
        messageBuilder.append(post.getMethod());
        messageBuilder.append("  ");
        messageBuilder.append(post.getURI());
        messageBuilder.append("  ");
        HttpEntity entity = post.getEntity();
        String body = IOUtils.toString(entity.getContent());
        List<NameValuePair> parse = URLEncodedUtils.parse(body, ContentType.get(entity).getCharset());
        parse.stream().forEach(pair -> {
            messageBuilder.append(pair.getName());
            messageBuilder.append(":");
            messageBuilder.append(pair.getValue());
            messageBuilder.append(" ");
        });
        logger.warn("send httpRequest: {}", messageBuilder.toString());
        CloseableHttpResponse response = client.execute(post);
        InputStream content = response.getEntity().getContent();
        String s = IOUtils.toString(content);
        response.close();
        logger.warn("get httpResponse: \n{}", s);
        return s;
    }

    /**
     * ?
     */
    private String formatIso8601Date(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(new SimpleTimeZone(0, "GMT"));
        return df.format(date);
    }

    /**
     * ?
     */
    private Integer getPages(String token, Date date, String url) throws IOException, ParseException {
        Map map = getData(token, date, url, 0);
        Map result = (Map) (((Map) map.get("data")).get("pager"));
        return (Integer) result.get("total_pages");
    }

    /**
     * ???
     */
    private Map getData(String token, Date date, String url, Integer pageIndex) throws ParseException, IOException {
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("access_token", token));
        params.add(new BasicNameValuePair("timestamp", formatIso8601Date(new Date())));
        params.add(new BasicNameValuePair("signNonce", new Random().nextInt(1000000) + ""));
        String format = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String str = format + " 00:00:00";
        String str2 = format + " 23:59:59";
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = fmt.parse(str);
        Date date2 = fmt.parse(str2);
        params.add(new BasicNameValuePair("startQueryTime", formatIso8601Date(date1)));
        params.add(new BasicNameValuePair("endQueryTime", formatIso8601Date(date2)));
        params.add(new BasicNameValuePair("pageIndex", pageIndex.toString()));
        params.add(new BasicNameValuePair("pageSize", "20"));
        params.add(new BasicNameValuePair("carDealerType", "all"));
        params.add(new BasicNameValuePair("province", ""));
        params.add(new BasicNameValuePair("city", "?"));
        String s = sendAndGetResponse(url, params);
        Map map = JSON.parseObject(s, Map.class);
        if (!"10000".equals(map.get("code")))
            throw new RuntimeException(
                    "?? " + url + "  " + date.toString() + " pageIndex" + pageIndex);
        return map;
    }

    private void sendAndSave(String token, Date date, String url) throws ParseException, IOException {
        Integer pages = getPages(token, date, url);
        for (int i = 0; i <= pages; i++) {
            Map map = getData(token, date, url, i);
            List<Map> o = (List<Map>) ((Map) ((Map) map.get("data")).get("pager")).get("items");

            o.stream().filter(secondCar -> "".equals(secondCar.get("status"))
                    && "".equals(secondCar.get("upShelf"))).forEach(data1 -> {
                        SecondCar car = new SecondCar();
                        car.setUserId(-1);
                        car.setMerchantId(0);
                        car.setIsMerchant(0);
                        //?
                        String brand = (String) data1.get("brand");
                        if (brand != null) {
                            Integer type = getCarType(brand);
                            car.setType(type);
                        }
                        //
                        String model = (String) data1.get("model");
                        car.setTitle(model);
                        // journey
                        String mileage = (String) data1.get("mileage");
                        BigDecimal journey = new BigDecimal(mileage).divide(new BigDecimal(10000), 2,
                                RoundingMode.HALF_UP);
                        car.setJourney(journey);

                        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        SimpleDateFormat fmt2 = new SimpleDateFormat("yyyy-MM-dd");
                        //
                        String firstLicensePlateDate = (String) data1.get("firstLicensePlateDate");
                        try {
                            firstLicensePlateDate = fmt2.format(fmt.parse(firstLicensePlateDate));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        car.setLicenseTime(firstLicensePlateDate);

                        //
                        car.setBuyTime(firstLicensePlateDate);

                        //
                        String insuranceExpiresDate = (String) data1.get("insuranceExpiresDate");
                        try {
                            if (insuranceExpiresDate != null && !"null".equals(insuranceExpiresDate)) {
                                insuranceExpiresDate = fmt2.format(fmt.parse(insuranceExpiresDate));
                            }
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        car.setInsuranceDeadtime(insuranceExpiresDate);
                        //??
                        String carDescribe = (String) data1.get("carDescribe");
                        car.setIntroduction(carDescribe);
                        //??? nature
                        Integer usage = 0;
                        String useType = (String) data1.get("useType");
                        if ("??".equals(useType))
                            usage = 1;
                        car.setNature(usage.toString());
                        //? exhaust
                        BigDecimal engineVolume = new BigDecimal((String) data1.get("engineVolume"));
                        car.setExhaust(engineVolume);
                        // carPicture
                        List<String> carPicture = (List<String>) data1.get("carPicture");
                        if (carPicture.size() > 0) {
                            car.setPictue(carPicture.get(0));
                            StringJoiner joiner = new StringJoiner(",");
                            carPicture.forEach(joiner::add);
                            car.setCarPic(joiner.toString());
                        }
                        // salePrice
                        Integer salePrice = Integer.parseInt((String) data1.get("salePrice"));
                        BigDecimal price = new BigDecimal(salePrice).divide(new BigDecimal(10000), 2,
                                RoundingMode.HALF_UP);
                        car.setPrice(price);
                        //?  firstLicensePlateDate
                        Integer year = Integer.parseInt(firstLicensePlateDate.substring(0, 4));
                        Integer toSet = 0;
                        int now = 2017 - year;
                        if (now <= 1)
                            toSet = 0;
                        if (now > 1 && now <= 3)
                            toSet = 1;
                        if (now > 3 && now <= 5)
                            toSet = 3;
                        if (now > 5 && now <= 8)
                            toSet = 4;
                        if (now > 8 && now <= 10)
                            toSet = 5;
                        if (now > 10)
                            toSet = 6;
                        car.setYear(toSet);
                        //
                        String color = (String) data1.get("color");
                        Integer resultColor = 15;
                        if (color != null) {
                            if (color.contains(""))
                                resultColor = 1;
                            if (color.contains(""))
                                resultColor = 2;
                            if (color.contains(""))
                                resultColor = 3;
                            if (color.contains("?"))
                                resultColor = 4;
                            if (color.contains(""))
                                resultColor = 5;
                            if (color.contains("?"))
                                resultColor = 6;
                            if (color.contains(""))
                                resultColor = 7;
                            if (color.contains(""))
                                resultColor = 8;
                            if (color.contains(""))
                                resultColor = 9;
                            if (color.contains(""))
                                resultColor = 10;
                            if (color.contains(""))
                                resultColor = 11;
                            if (color.contains(""))
                                resultColor = 12;
                            if (color.contains(""))
                                resultColor = 13;
                            if (color.contains(""))
                                resultColor = 14;
                        }
                        car.setColor(resultColor);
                        //?,?? contactPerson  phone
                        String contactPerson = (String) data1.get("contactPerson");
                        String phone = (String) data1.get("phone");
                        car.setConcactName(contactPerson);
                        car.setConcactPhone(phone);
                        //
                        Integer transferNumber = (Integer) data1.get("transferNumber");
                        car.setTimes(transferNumber);
                        //
                        try {
                            car.setCreatedAt(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                    .parse((String) data1.get("upShelfDate")));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        try {
                            secondCarMapper.insert(car);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    });

        }

    }

    @SuppressWarnings("all")
    private Integer getCarType(String type) {

        String carType = "  {\"\": 1,\n" + "    \"\": 2,\n" + "    \"?\": 3,\n"
                + "    \"\": 4,\n" + "    \"\": 90,\n" + "    \"\": 6,\n"
                + "    \"\": 7,\n" + "    \"?\": 8,\n" + "    \"?\": 88,\n"
                + "    \"??\": 10,\n" + "    \"??\": 11,\n" + "    \"\": 12,\n"
                + "    \"\": 87,\n" + "    \"\": 14,\n" + "    \"?\": 15,\n"
                + "    \"\": 96,\n" + "    \"\": 17,\n" + "    \"\": 18,\n"
                + "    \"\": 19,\n" + "    \"?\": 20,\n" + "    \"?\": 21,\n" + "    \"DS\": 22,\n"
                + "    \"?\": 23,\n" + "    \"\": 24,\n" + "    \"?\": 25,\n"
                + "    \"?\": 26,\n" + "    \"??\": 27,\n" + "    \"\": 28,\n"
                + "    \"\": 29,\n" + "    \"?\": 30,\n" + "    \"\": 31,\n"
                + "    \"\": 32,\n" + "    \"Jeep\": 33,\n" + "    \"\": 34,\n"
                + "    \"?\": 35,\n" + "    \"?\": 36,\n" + "    \"?\": 37,\n"
                + "    \"\": 38,\n" + "    \"\": 39,\n" + "    \"\": 40,\n"
                + "    \"\": 41,\n" + "    \",\": 42,\n" + "    \"?\": 43,\n"
                + "    \"\": 44,\n" + "    \"\": 45,\n" + "    \"\": 46,\n"
                + "    \"\": 47,\n" + "    \"?\": 48,\n" + "    \"\": 49,\n"
                + "    \"\": 50,\n" + "    \"MG\": 51,\n" + "    \"\": 52,\n"
                + "    \"?\": 53,\n" + "    \"\": 54,\n" + "    \"?\": 55,\n"
                + "    \"\": 56,\n" + "    \"\": 57,\n" + "    \"?\": 58,\n"
                + "    \"??\": 59,\n" + "    \"\": 60,\n" + "    \"\": 169,\n"
                + "    \"\": 62,\n" + "    \"smart\": 63,\n" + "    \"?\": 64,\n"
                + "    \"?\": 65,\n" + "    \"??\": 66,\n" + "    \"\": 67,\n"
                + "    \"?\": 68,\n" + "    \"\": 69,\n" + "    \"?\": 70,\n"
                + "    \"\": 189,\n" + "    \"?\": 72,\n" + "    \"\": 73,\n"
                + "    \"?\": 74,\n" + "    \"\": 75,\n" + "    \"?\": 76,\n"
                + "    \"\": 77,\n" + "    \"AC Schnitzer\": 78,\n" + "    \"ALPINA\": 79,\n"
                + "    \"Arash\": 80,\n" + "    \"ARCFOX\": 81,\n" + "    \" \": 82,\n"
                + "    \"?\": 83,\n" + "    \"\": 84,\n" + "    \"?\": 85,\n"
                + "    \"?\": 86,\n" + "    \"\": 89,\n" + "    \"\": 91,\n"
                + "    \"\": 92,\n" + "    \"Caterham\": 93,\n" + "    \"\": 94,\n"
                + "    \"?\": 95,\n" + "    \"\": 97,\n" + "    \"EV\": 98,\n"
                + "    \"Dacia\": 99,\n" + "    \"DMC\": 100,\n" + "    \"?\": 101,\n"
                + "    \"\": 102,\n" + "    \"\": 103,\n" + "    \"\": 104,\n"
                + "    \"\": 105,\n" + "    \"?\": 106,\n" + "    \"Faraday\": 107,\n"
                + "    \"Future\": 108,\n" + "    \"Fisker\": 109,\n" + "    \"\": 110,\n"
                + "    \"?\": 111,\n" + "    \"?\": 112,\n" + "    \"GLM\": 113,\n"
                + "    \"GMC\": 114,\n" + "    \"Gumpert\": 115,\n" + "    \"\": 116,\n"
                + "    \"Hennessey\": 117,\n" + "    \"\": 118,\n" + "    \"\": 119,\n"
                + "    \"\": 120,\n" + "    \"?\": 121,\n" + "    \"?\": 122,\n"
                + "    \"\": 123,\n" + "    \"?\": 124,\n" + "    \"?\": 125,\n"
                + "    \"?\": 126,\n" + "    \"??\": 127,\n" + "    \"?\": 128,\n"
                + "    \"?\": 129,\n" + "    \"??\": 130,\n" + "    \"?\": 131,\n"
                + "    \"\": 132,\n" + "    \"?\": 133,\n"
                + "    \"\": 134,\n" + "    \"\": 135,\n" + "    \"?\": 136,\n"
                + "    \"KTM\": 137,\n" + "    \"?\": 138,\n" + "    \"??\": 139,\n"
                + "    \"\": 140,\n" + "    \"\": 141,\n" + "    \"\": 142,\n"
                + "    \"LeSEE\": 143,\n" + "    \"LYNK&CO\": 144,\n" + "    \"?\": 145,\n"
                + "    \"?\": 146,\n" + "    \"\": 147,\n" + "    \"\": 148,\n"
                + "    \"\": 149,\n" + "    \"\": 150,\n" + "    \"\": 151,\n"
                + "    \"\": 152,\n" + "    \"\": 153,\n" + "    \"MINI\": 154,\n"
                + "    \"\": 155,\n" + "    \"\": 156,\n" + "    \"\": 157,\n"
                + "    \"nanoFLOWCELL\": 158,\n" + "    \"Noble\": 159,\n" + "    \"?\": 160,\n"
                + "    \"\": 161,\n" + "    \"\": 162,\n" + "    \"\": 163,\n"
                + "    \"?\": 164,\n" + "    \"Rezvani\": 165,\n" + "    \"Rimac\": 166,\n"
                + "    \"Rinspeed\": 167,\n" + "    \"\": 168,\n" + "    \"Scion\": 170,\n"
                + "    \"SPIRRA\": 171,\n" + "    \"SSC\": 172,\n" + "    \"SWM?\": 173,\n"
                + "    \"\": 174,\n" + "    \"\": 175,\n" + "    \"\": 176,\n"
                + "    \"\": 177,\n" + "    \"?\": 178,\n" + "    \"?\": 179,\n"
                + "    \"?\": 180,\n" + "    \"\": 181,\n" + "    \"WEY\": 182,\n"
                + "    \"?\": 183,\n" + "    \"?\": 184,\n" + "    \"?\": 185,\n"
                + "    \"\": 186,\n" + "    \"??\": 187,\n" + "    \"\": 188,\n"
                + "    \"\": 190,\n" + "    \"?\": 191,\n" + "    \"?\": 192,\n"
                + "    \"\": 193,\n" + "    \"Zenvo\": 194,\n" + "    \"\": 195,\n"
                + "    \"\": 196,\n" + "    \"\": 197\n" + "}\n";

        Map map = JSON.parseObject(carType, Map.class);
        Object o = map.get(type);
        Integer result = -1;
        if (o != null)
            result = (Integer) o;
        return result;
    }
}