Commit 146a9b48 authored by 曹军's avatar 曹军

上传定时任务

parent 6a647741
package com.bme.access.upload;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
DruidDataSourceAutoConfigure.class ,
HibernateJpaAutoConfiguration.class})
@SpringBootApplication()
@MapperScan("com.bme.access.upload.module.dao")
public class Application {
@ConfigurationProperties(prefix = "spring.datasource.original")
@Bean
public DruidDataSource dataSource() {
return new DruidDataSource();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
......
......@@ -10,15 +10,15 @@ import lombok.NoArgsConstructor;
public class InFactoryTransportVehicleInfoUrl {
//车辆识 别代码 (VIN)
private String vin;
//环保登记编
//发动机号
private String numberPlate;
//注册登记日期
private String registrationDate;
//环保登记编码
private String environmentCode;
//生产日期
private String dtime;
//车辆品牌型号
//车辆类型
private String vehicleType;
//排放标准
private String vehicleEmissions;
......@@ -28,4 +28,22 @@ public class InFactoryTransportVehicleInfoUrl {
private String onBoardList;
// 行驶证
private String drivingLicense;
// 发动机铭牌(图片URL地址)
private String engineImage;
// 燃料类型
private String fuelType;
// 车辆所有(单位)
private String owener;
// 重型柴油车辆排放阶段查询照片URL地址
private String vehicleLicenseVerificationImg;
// 车辆照片
private String vehicleImage;
// 机动车环保网查询照片URL地址
private String vinVerificationImg;
// 生产日期
private String productionDate;
// 在线状态
private String onlineStatus;
// 车辆品牌型号
private String model;
}
\ No newline at end of file
package com.bme.access.upload.module.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bme.access.upload.common.HttpUtils;
import com.bme.access.upload.module.dao.InFactoryTransportVehicleInfoMapper;
import com.bme.access.upload.module.module.InFactoryTransportVehicleInfoUrl;
import com.bme.access.upload.module.module.upload.InFactoryTransportVehicleInfo;
import com.bme.access.upload.module.utils.BeanConverter;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
......@@ -14,19 +18,30 @@ import java.util.List;
@Service
@Slf4j
public class InFactoryTransportVehicleInfoService {
@Autowired
private BeanConverter beanConverter;
@Autowired
private InFactoryTransportVehicleInfoMapper inFactoryTransportVehicleInfoMapper;
@Autowired
private LoginService loginService;
public void getAndUploadInFactoryTransportVehicleInfo() {
private final static String IN_FACTORY_URL = "https://dctapi.soszyzg.com/dct/new/addTransPorVehicle";
public void getAndUploadInFactoryTransportVehicleInfo() throws Exception {
List<InFactoryTransportVehicleInfoUrl> inFactoryTransportVehicleInfoUrls = inFactoryTransportVehicleInfoMapper.selectInFactoryTransportVehicleInfo1();
List<InFactoryTransportVehicleInfo> inFactoryTransportVehicleInfos = beanConverter.convertInFactoryTransportVehicleInfo(inFactoryTransportVehicleInfoUrls);
if (!CollectionUtils.isEmpty(inFactoryTransportVehicleInfos)) {
try {
loginService.login();
} catch (Exception e) {
log.error("更新token失败:{}", e.getMessage());
}
}
for (InFactoryTransportVehicleInfo inFactoryTransportVehicleInfo : inFactoryTransportVehicleInfos) {
// log.info("上传场内运输车辆: {}", inFactoryTransportVehicleInfo);
String responseCode = null;
log.info("场内车辆上传结果: {}", responseCode);
String bodyJson = JSONObject.toJSONString(inFactoryTransportVehicleInfo);
HttpResponse httpResponse = HttpUtils.sendPost(IN_FACTORY_URL, null, null, bodyJson);
log.info("场内车辆上传结果: {}", JSON.toJSONString(httpResponse));
Thread.sleep(50);
}
log.info("上传场内车辆{}条", inFactoryTransportVehicleInfoUrls.size());
}
......
package com.bme.access.upload.module.utils;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.util.StringUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
/**
* description:
* <p></p>
*
* @author huanghao
* @since 2021/5/6 19:49
* Copyright: 2021, BME (Shanghai) Co., Ltd. All Rights Reserved.
*/
public class Base64Utils {
/**
* 根据图片链接转为base64数据
*
* @param imageUrl
* @return
* @throws Exception
*/
public static String getBase64ByUrl(String imageUrl) {
// new一个URL对象
URL url = null;
try {
url = new URL(imageUrl);
// 打开链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方式为"GET"
conn.setRequestMethod("GET");
// 超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
// 通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
// 得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = readInputStream(inStream);
return Base64.getEncoder().encodeToString(data);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据图片链接转为base64数据
*
* @param imageUrl
* @return
* @throws Exception
*/
public static byte[] getByteImgByUrl(String imageUrl) {
if (StringUtils.isEmpty(imageUrl)) {
return null;
}
// new一个URL对象
URL url = null;
try {
url = new URL(imageUrl);
// 打开链接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置请求方式为"GET"
conn.setRequestMethod("GET");
// 超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
// 通过输入流获取图片数据
InputStream inStream = conn.getInputStream();
// 得到图片的二进制数据,以二进制封装得到数据,具有通用性
return readInputStream(inStream);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 创建一个Buffer字符串
byte[] buffer = new byte[1024];
// 每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
// 使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
// 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
// 关闭输入流
inStream.close();
// 把outStream里的数据写入内存
return outStream.toByteArray();
}
/**
* base64转文件
* @param base64sString
* @param filePath "E:/out.jpg" "/home/test.pdf"
*/
public static void base64StringToFile(String base64sString,String filePath){
BufferedInputStream bin = null;
FileOutputStream fout = null;
BufferedOutputStream bout = null;
try {
//将base64编码的字符串解码成字节数组
byte[] bytes = Base64.getDecoder().decode(base64sString);
//apache公司的API
//byte[] bytes = Base64.decodeBase64(base64sString);
//创建一个将bytes作为其缓冲区的ByteArrayInputStream对象
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
//创建从底层输入流中读取数据的缓冲输入流对象
bin = new BufferedInputStream(bais);
//指定输出文件
//File file = new File("/home/test.pdf");
File file = new File(filePath);
//创建到指定文件的输出流
fout = new FileOutputStream(file);
//为文件输出流对接缓冲输出流对象
bout = new BufferedOutputStream(fout);
byte[] buffers = new byte[1024];
int len = bin.read(buffers);
while (len != -1) {
bout.write(buffers,0,len);
len = bin.read(buffers);
}
//刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
bout.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bin.close();
fout.close();
bout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//转换
public static byte[] imageThumbnailsToBase64(String imageUrl) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();;
try {
Thumbnails.of(new URL(imageUrl)).toOutputStream(bos);
// Thumbnails.of(imageUrl).size(600, 800).toOutputStream(bos);
byte[] bytes = bos.toByteArray();
return bytes;
} catch (Exception e) {
System.out.println("Thumbnails Upload failed:" + e);
}
return null;
}
public static void main(String[] args) {
byte[] bytes = getByteImgByUrl("https://q1.itc.cn/q_70/images03/20240430/59fe8596e4cb4824a6f27315328a3559.jpeg");
System.out.println(bytes);
}
}
\ No newline at end of file
......@@ -3,17 +3,60 @@ package com.bme.access.upload.module.utils;
import com.bme.access.upload.module.module.InFactoryTransportVehicleInfoUrl;
import com.bme.access.upload.module.module.upload.InFactoryTransportVehicleInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Objects;
@Component
@Slf4j
public class BeanConverter {
private final String owner = "四川罡宸不锈钢有限责任公司";
public List<InFactoryTransportVehicleInfo> convertInFactoryTransportVehicleInfo(List<InFactoryTransportVehicleInfoUrl> inFactoryTransportVehicleInfoUrls) {
List<InFactoryTransportVehicleInfo> list = Lists.newArrayList();
for (InFactoryTransportVehicleInfoUrl item : inFactoryTransportVehicleInfoUrls) {
InFactoryTransportVehicleInfo info = new InFactoryTransportVehicleInfo();
info.setCarNo(item.getNumberPlate());
info.setVehicleListImg(Base64Utils.getBase64ByUrl(item.getOnBoardList()));
info.setDrivingLicenseImg(Base64Utils.getBase64ByUrl(item.getDrivingLicense()));
info.setSensorCatalyticConverterImg(null);
info.setCarImg(Base64Utils.getBase64ByUrl(item.getVehicleImage()));
if (item.getVehicleEmissions() == null) {
info.setEmissionStandard("X");
} else {
if (item.getVehicleEmissions().equals("99")) {
info.setEmissionStandard("D");
} else {
info.setEmissionStandard(item.getVehicleEmissions());
}
}
info.setFuelType(item.getFuelType()); // TODO
info.setHbCarNo(item.getEnvironmentCode());
if (Objects.nonNull(item.getOnlineStatus())) {
// 联网状态 0—未联网,1—已联网
info.setInternetStatus(item.getOnlineStatus());
} else {
info.setInternetStatus("0");
}
if (StringUtils.isBlank(item.getOwener())) {
info.setOwner(owner);
} else {
info.setOwner(item.getOwener());
}
return null;
info.setProductionDate(item.getProductionDate());
info.setRegistrationDate(item.getRegistrationDate());
info.setVin(item.getVin());
info.setNetData(1);
info.setVehModel(item.getModel());
info.setVehBrand(item.getVehicleType());
list.add(info);
}
return list;
}
}
\ No newline at end of file
......@@ -25,13 +25,21 @@
Registration_date registrationDate,
VIN vin,
Engine_number engineNumber,
Environment_Code environmentCode,
vehicle_type vehicleType,
Vehicle_Emissions vehicleEmissions,
On_board_list onBoardList,
Driving_license drivingLicense,
DTime dtime,
vehicle_type vehicleType
Engine_image engineImage,
Fuel_Type fuelType,
vehicle_type vehicleType,
Owener owener,
VehicleLicense_Verification_Img vehicleLicenseVerificationImg,
Vehicle_Image vehicleImage,
VIN_Verification_Img vinVerificationImg,
production_date productionDate,
online_status onlineStatus,
model,
Environment_Code environmentCode
from InFactory_Transport_Vehicle
</select>
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bme.upload.module.dao.NoneRoadMovementInfoMapper">
<select id="selectNoneRoadMovementInfo"
resultType="com.bme.upload.module.entity.origin.NoneRoadMovementInfoUrl">
select
Environment_Code environmentCode,
Mechanical_Environment mechanicalEnvironment,
Mechanical_model mechanicalModel,
Production_date productionDate,
vehicle_type vehicleType,
engine_num engineNum,
engine_vendor engineVendor,
engine_model engineModel,
engine_nameplate engineNameplate,
car_nameplate carNameplate,
vehicle_lable vehicleLable,
Vehicle_Emissions vehicleEmissions,
cphm,
Engine_Image engineImage,
DTime dtime
from Non_Road_Movement_Info where DTime > #{uploadTime}
</select>
<select id="selectNoneRoadMovementInfo1"
resultType="com.bme.upload.module.entity.origin.NoneRoadMovementInfoUrl">
select
Environment_Code environmentCode,
Mechanical_Environment mechanicalEnvironment,
Mechanical_model mechanicalModel,
Production_date productionDate,
vehicle_type vehicleType,
engine_num engineNum,
engine_vendor engineVendor,
engine_model engineModel,
engine_nameplate engineNameplate,
car_nameplate carNameplate,
vehicle_lable vehicleLable,
Vehicle_Emissions vehicleEmissions,
cphm,
Engine_Image engineImage,
DTime dtime
from Non_Road_Movement_Info
</select>
<select id="selectNoneRoadMovementInfoCount" resultType="java.lang.Integer">
select count(1) from Non_Road_Movement_Info
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment