package com.rnt.service;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.rnt.commo.enums.ErrorType;
import com.rnt.commo.enums.OrderTypeEnum;
import com.rnt.model.park.IrainPknoRelation;
import com.rnt.model.zf.Order;
import com.rnt.model.zf.OrderDetailPark;
import com.rnt.utils.HttpClientTutorial;
import com.rnt.utils.MD5Utils;
import com.rnt.vo.BizResult;
import org.beetl.sql.core.kit.StringKit;
/**
* 艾润费用查询service.
*
* Copyright: Copyright (c) 2017 zteits
*
* @ClassName: IRainQueryService.java
* @Description:
* @version: v1.0.0
* @author: wangfs
* @date: 2017年6月13日 上午9:25:31
* Modification History:
* Date Author Version Description
* ---------------------------------------------------------*
* 2017年6月13日 wangfs v1.0.0 创建
*/
public class IRainQueryService {
private static final Log logger = Log.getLog(IRainQueryService.class);
/**
* 调用艾润查询费用接口.
*
* @param carNum
* @param parkCode
* @return
*/
public String billQuery(String carNum, String parkCode) {
logger.info("开始调用查询费用接口,入参={carNum,parkCode}=" + "{" + carNum + "," + parkCode + "}");
//1.查询停车场关系映射表-获取艾润停车场查询费用编码 ztetis-park.irain_pkno_relation
StringBuffer sql = new StringBuffer("select a.irain_pkno1");
sql.append(" from irain_pkno_relation a");
sql.append(" where a.park_lotpkno = ?");
String rs = "";
IrainPknoRelation irainPknoRelation = new IrainPknoRelation().findFirst(sql.toString(), parkCode);
if (irainPknoRelation != null && StringKit.isNotBlank(irainPknoRelation.getIrainPkno1())) {
/**** 以下为模拟入参 实际入参 由app提供-------------------------------------*/
Prop prop = PropKit.use("a_little_config.txt");
Long time = System.currentTimeMillis();
String md5 = MD5Utils.enMD5(prop.get("irain.appid") + prop.get("irain.appsecret") + time);
Map params = new HashMap<>();
params.put("appid", prop.get("irain.appid"));
params.put("sign", md5);
params.put("timestamp", time);
params.put("vpl_number", carNum);
params.put("park_code", irainPknoRelation.getIrainPkno1());
try {
logger.info("irain 查询停车费用入参:" + JSONObject.toJSONString(params));
rs = HttpClientTutorial.httpPostRequest(prop.get("irain.url") + "/bill/Query", params);
logger.info("irain 查询停车费用返回:" + JSONObject.toJSONString(rs));
} catch (Exception e) {
logger.info("irain 查询停车费用出错:" + e);
}
} else {
logger.info("没有查询到艾润查询费用编码");
}
logger.info("结束调用查询费用接口,结果=" + JSONObject.toJSONString(rs));
return JSONObject.toJSON(rs) + "";
}
/***
* 支付完成,停车通过栏杆
*/
public BizResult passHandrail(String orderId) throws Exception {
BizResult bizResult = new BizResult<>();
logger.info(" 支付完成,通知抬杆 ----start--- req=" + orderId);
Order order = Order.dao.findFirst("SELECT * FROM td_b_order t where t.order_id = ?", orderId);
if (null == order) {
logger.info(" 支付完成,通知抬杆 订单不存在 orderId=" + orderId);
bizResult.setErrorMessage(ErrorType.ORDER_NO_EXISTS, "订单不存在");
return bizResult;
}
if (order.getSourceType().equals(OrderTypeEnum.ORDER_SOURCE_TYPE_IN)) {
/**
* 艾润通知
*/
bizResult = passIRail(order.getCarNumber(), order.getParkId(), order.getOrderTotalFee());
logger.info("艾润抬杆通知 返回为: bizResult=" + JSONObject.toJSONString(bizResult));
} else if (order.getSourceType().equals(OrderTypeEnum.ORDER_SOURCE_TYPE_OUT)) {
/**
* 青岛
*/
bizResult = passQD(order);
} else {
logger.info("支付完成,通知抬杆 未知的订单来源类型: orderSourceType=" + order.getSourceType());
bizResult.setErrorMessage(ErrorType.BIZ_ERROR, "未知的订单来源类型");
}
return bizResult;
}
/**
* 艾润抬杆设置
*/
private BizResult passIRail(String vpl_number, String park_code, BigDecimal amount) throws Exception {
BizResult bizResult = new BizResult<>();
//1.查询停车场关系映射表-获取艾润停车场查询费用编码 ztetis-park.irain_pkno_relation
StringBuffer sql = new StringBuffer("select a.irain_pkno1");
sql.append(" from irain_pkno_relation a");
sql.append(" where a.park_lotpkno = ?");
String rs = "";
IrainPknoRelation irainPknoRelation = new IrainPknoRelation().findFirst(sql.toString(), park_code);
if (irainPknoRelation != null && StringKit.isNotBlank(irainPknoRelation.getIrainPkno2())) {
/**** 以下为模拟入参 实际入参 由app提供-------------------------------------*/
Prop prop = PropKit.use("a_little_config.txt");
Long time = System.currentTimeMillis();
String md5 = MD5Utils.enMD5(prop.get("irain.appid") + prop.get("irain.appsecret") + time);
Map params = new HashMap<>();
params.put("appid", prop.get("irain.appid"));
params.put("sign", md5);
params.put("timestamp", time);
params.put("vpl_number", vpl_number);
//要用进出场上报的那个编码
params.put("park_code", irainPknoRelation.getIrainPkno2());
params.put("amount", amount.intValue());
logger.info("开始通知irain 支付已经完成:" + JSONObject.toJSONString(params));
rs = HttpClientTutorial.httpPostRequest(prop.get("irain.url") + "/pay/Issued", params);
logger.info("结束通知irain 支付已经完成::" + rs);
JSONObject result = JSONObject.parseObject(rs);
if ("OK".equals(result.getString("message"))) {
bizResult.setData("通知成功!");
} else {
bizResult.setErrorMessage(ErrorType.BIZ_ERROR, "通知irain 支付已经完成失败!");
}
return bizResult;
} else {
logger.info("没有查询到艾润进出场上报编码");
bizResult.setErrorMessage(ErrorType.BIZ_ERROR, "未查询到艾润进出场上报编码");
return bizResult;
}
}
/**
* 青岛抬杆设置
*/
private BizResult passQD(Order order) throws Exception {
BizResult bizResult = new BizResult<>();
String url = PropKit.get("qd.retrun_fee_url");
OrderDetailPark orderDetailPark = OrderDetailPark.dao.findFirst(
"SELECT * FROM td_b_order_detail_park t where t.order_id = ?", order.getOrderId());
Map params = new HashMap<>();
params.put("orderCode", orderDetailPark.getRecordId());
params.put("amount", order.getOrderTotalFee().intValue());
params.put("orderPay", order.getOrderPayedFee().intValue());
params.put("payType", 1);
logger.info("开始通知irain 支付已经完成:" + JSONObject.toJSONString(params));
String rs = HttpClientTutorial.httpPostRequest(url, JSONObject.toJSONString(params));
logger.info("结束通知irain 支付已经完成:" + rs);
JSONObject result = JSONObject.parseObject(rs);
if ("1".equals(result.getString("status"))) {
bizResult.setData("通知成功!");
} else {
bizResult.setErrorMessage(ErrorType.BIZ_ERROR, "通知青岛 支付已经完成失败!");
}
return bizResult;
}
public static void main(String[] args) {
Prop prop = PropKit.use("a_little_config.txt");
String rs = "";
Long time = System.currentTimeMillis();
String md5 = MD5Utils.enMD5(prop.get("irain.appid") + prop.get("irain.appsecret") + time);
Map params = new HashMap<>();
params.put("appid", prop.get("irain.appid"));
params.put("sign", md5);
params.put("timestamp", time);
params.put("vpl_number", "苏B1B566");
params.put("park_code", "734861a1e8656ffa51bdd90829941ca9");
try {
logger.info("irain 查询停车费用入参:" + JSONObject.toJSONString(params));
rs = HttpClientTutorial.httpPostRequest(prop.get("irain.url") + "/bill/Query", params);
logger.info("irain 查询停车费用返回:" + JSONObject.toJSONString(rs));
} catch (Exception e) {
logger.info("irain 查询停车费用出错:" + e);
}
}
}