package com.java110.job.adapt.car;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.OutRestTemplate;
import com.java110.core.client.RestTemplate;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.machine.CarInoutDetailDto;
import com.java110.dto.parking.ParkingAreaDto;
import com.java110.dto.system.Business;
import com.java110.intf.community.IParkingAreaV1InnerServiceSMO;
import com.java110.job.adapt.DatabusAdaptImpl;
import com.java110.po.car.CarInoutDetailPo;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ImageUtils;
import com.java110.utils.util.StringUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpStatusCodeException;
import java.util.List;
/**
* 停车进出数据向 东莞市公安局社会停车场(道闸卡口)平台同步数据
*
* databus 配置
*
* 名称 车辆进出databus
* 业务类型 saveCarInoutDetail
* 适配器 sendInoutCarToDGGAJParkingAdapt
* 顺序 80
* 状态 在用
*
*
* mapping 中配置 DOGUAN_PAKRING_KEY 平台提供的key
*/
@Component(value = "sendInoutCarToDGGAJParkingAdapt")
public class SendInoutCarToDGGAJParkingAdapt extends DatabusAdaptImpl {
private static Logger logger = LoggerFactory.getLogger(SendInoutCarToDGGAJParkingAdapt.class);
@Autowired
private IParkingAreaV1InnerServiceSMO parkingAreaV1InnerServiceSMOImpl;
@Autowired
private RestTemplate outRestTemplate;
private static final String url = "http://59.39.179.74:9070/services/hole";
@Override
public void execute(Business business, List businesses) {
JSONObject data = business.getData();
//todo 获取到 参数信息
CarInoutDetailPo carInoutDetailPo = BeanConvertUtil.covertBean(data, CarInoutDetailPo.class);
//todo 获取到token
String token = initTrans(carInoutDetailPo);
//todo 上传进出场车辆信息
parkWriteInfoV2(carInoutDetailPo,token);
}
/**
* 写停车场车辆信息
* @param carInoutDetailPo
* @param token
*/
private void parkWriteInfoV2(CarInoutDetailPo carInoutDetailPo, String token) {
ParkingAreaDto parkingAreaDto = new ParkingAreaDto();
parkingAreaDto.setPaId(carInoutDetailPo.getPaId());
parkingAreaDto.setCommunityId(carInoutDetailPo.getCommunityId());
List parkingAreaDtos = parkingAreaV1InnerServiceSMOImpl.queryParkingAreas(parkingAreaDto);
if (parkingAreaDtos == null || parkingAreaDtos.size() < 1) {
throw new IllegalArgumentException("停车场未找到");
}
//进
String directType = "1";
if (CarInoutDetailDto.CAR_INOUT_OUT.equals(carInoutDetailPo.getCarInout())) {
directType = "2"; // todo 出
}
String photo = ImageUtils.getBase64ByImgUrl(carInoutDetailPo.getPhotoJpg());
JSONObject picBase64 = JSONObject.parseObject("{\"SubImageInfo\": [{\"FileFormat\":\"Jpeg\",\"Data\":\"无图片\"}]}");
if(!StringUtil.isEmpty(photo)){
picBase64.getJSONArray("SubImageInfo").getJSONObject(0).put("Data","data:image/jpeg;base64,"+photo);
}
String reqParam = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 441958003150000001\n" +
" \n" +
" "+directType+"\n" +
" 1\n" +
" \n" +
" 00\n" +
" \n" +
" 02\n" +
" \n" +
" "+ DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A) +"\n" +
" \n" +
" 9\n" +
" \n" +
" K33\n" +
" \n" +
" "+carInoutDetailPo.getCarNum()+"\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 1\n" +
" \n" +
" 99\n" +
" \n" +
" 0\n" +
" \n" +
" 8\n" +
" \n" +
" "+picBase64.toJSONString()+"\n" +
" -1\n" +
" \n" +
" 0\n" +
" \n" +
" "+ DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_A) +"\n" +
" \n" +
" "+token+"\n" +
" \n" +
" \n" +
"";
logger.debug("请求报文:{}",reqParam);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type","text/xml;charset=UTF-8");
HttpEntity> httpEntity = new HttpEntity(reqParam, httpHeaders);
try {
ResponseEntity responseEntity = outRestTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
logger.debug("返回报文,{}",responseEntity);
}catch (HttpStatusCodeException e){
logger.error("调用异常",e);
}catch (Exception e){
logger.error("调用异常",e);
}
}
/**
* 初始化连接
* @param carInoutDetailPo
* @return
*/
public String initTrans(CarInoutDetailPo carInoutDetailPo) {
String token = CommonCache.getValue("DGGAJ_Parking_token");
if(!StringUtil.isEmpty(token)){
return token;
}
ParkingAreaDto parkingAreaDto = new ParkingAreaDto();
parkingAreaDto.setPaId(carInoutDetailPo.getPaId());
parkingAreaDto.setCommunityId(carInoutDetailPo.getCommunityId());
List parkingAreaDtos = parkingAreaV1InnerServiceSMOImpl.queryParkingAreas(parkingAreaDto);
if (parkingAreaDtos == null || parkingAreaDtos.size() < 1) {
throw new IllegalArgumentException("停车场未找到");
}
String key = MappingCache.getValue("DOGUAN_PAKRING_KEY");
//进
String directType = "1";
if (CarInoutDetailDto.CAR_INOUT_OUT.equals(carInoutDetailPo.getCarInout())) {
directType = "2"; // todo 出
}
String paramIn = "\n" +
" \n" +
" \n" +
" \n" +
" \n" +
" 441958003150000001\n" +
" \n" +
" " + directType + "\n" +
" 1\n" +
" \n" +
" "+key+"\n" +
" \n" +
" \n" +
"";
logger.debug("请求报文:{}",paramIn);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type","text/xml;charset=UTF-8");
HttpEntity> httpEntity = new HttpEntity(paramIn, httpHeaders);
try {
ResponseEntity responseEntity = outRestTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
logger.debug("返回报文,{}",responseEntity);
String body = responseEntity.getBody();
if(!body.contains("token")){
return token;
}
token = body.substring(body.indexOf("<token>")+13,body.indexOf("</token>"));
CommonCache.setValue("DGGAJ_Parking_token",token,CommonCache.TOKEN_EXPIRE_TIME);
}catch (HttpStatusCodeException e){
logger.error("调用异常",e);
}catch (Exception e){
logger.error("调用异常",e);
}
return token;
}
}