|
|
@@ -0,0 +1,281 @@
|
|
|
+package com.yinjie.heating.webcore.servlet;
|
|
|
+
|
|
|
+import com.dySweetFishPlugin.tool.lang.DateUtil;
|
|
|
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
|
|
+import com.sweetfish.net.http.*;
|
|
|
+import com.sweetfish.util.AnyValue;
|
|
|
+import com.sweetfish.util.AutoLoad;
|
|
|
+import com.yinjie.heating.common.api.HeatingDocService;
|
|
|
+import com.yinjie.heating.common.base.ERPAdminHttpServlet;
|
|
|
+import com.yinjie.heating.common.entity.ceb.CEBInvokeHeader;
|
|
|
+import com.yinjie.heating.common.entity.ceb.CEBRequestItem;
|
|
|
+import com.yinjie.heating.common.entity.ceb.CEBResponseItem;
|
|
|
+import com.yinjie.heating.common.entity.heating.HeatingApp;
|
|
|
+import com.yinjie.heating.common.tool.CommonUtils;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.nio.charset.Charset;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by jlutt on 2019-05-31.
|
|
|
+ *
|
|
|
+ * @author jlutt
|
|
|
+ */
|
|
|
+@AutoLoad(false)
|
|
|
+@WebServlet(name = "提供给三方的api-当前主要给光大", value = {"/invoke/*"}, comment = "应用API中心")
|
|
|
+public class ThirdInvokeServlet extends ERPAdminHttpServlet {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ HeatingDocService gasDocService;
|
|
|
+
|
|
|
+
|
|
|
+ private XmlMapper xmlMapper;
|
|
|
+
|
|
|
+ //光大appId,写死算了,没有这个id获取不到解密key,不解密就获取不到他传过来的appId.....
|
|
|
+ private static final String CEB_APP_ID = "CEB20240909";
|
|
|
+ private static final String CEB_SM4_KEY = "a63e5f89caed5d17b62553d0725d47da";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void init(HttpContext context, AnyValue config) {
|
|
|
+ super.init(context, config);
|
|
|
+ xmlMapper = new XmlMapper();
|
|
|
+ }
|
|
|
+
|
|
|
+ @HttpMapping(auth = false, url = "/invoke/cebcall", methods = {"POST"}, comment = "光大银行接口")
|
|
|
+ public void cebCall(HttpRequest request, HttpResponse response) {
|
|
|
+// logger.info(request);
|
|
|
+ CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
|
|
|
+ String reqStr = request.getBody(Charset.forName("GBK"));
|
|
|
+ logger.info("收到光大调用消息:{}", reqStr);
|
|
|
+// String reqStr = request.getBody(StandardCharsets.UTF_8); //测试
|
|
|
+ try {
|
|
|
+ Map<String, Object> respBody = new HashMap<>();
|
|
|
+ HeatingApp gasApp = gasDocService.getRedisGasApp(CEB_APP_ID);
|
|
|
+ boolean errorFlag = false;
|
|
|
+ if (gasApp == null) {
|
|
|
+ //本地配置错误,但是也无法解密了
|
|
|
+ logger.error("未找到应用APPID");
|
|
|
+ return "未找到应用APPID";
|
|
|
+ }
|
|
|
+ if (gasApp.getStatus() > 0) {
|
|
|
+ respBody.put("ErrorCode", "DEF0006");
|
|
|
+ respBody.put("ErrorInfo", "应用APPID已禁用,请联系服务开发商");
|
|
|
+ errorFlag = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //解密
|
|
|
+// String decodeStr = SM4Utils.decryptEcb(gasApp.getAppPublicKey(), reqStr, SM4Utils.ENCODING);
|
|
|
+ //明文
|
|
|
+ String decodeStr = reqStr;
|
|
|
+ CEBRequestItem requestItem = xmlMapper.readValue(decodeStr, CEBRequestItem.class);
|
|
|
+
|
|
|
+ //todo 业务代码
|
|
|
+ if (!errorFlag) {
|
|
|
+ Map<String, Object> bodyMap = requestItem.getBody();
|
|
|
+
|
|
|
+ switch (requestItem.getInvokeHeader().getfTranCode()) {
|
|
|
+ case "BJ01":
|
|
|
+ respBody = queryBillsToPay(bodyMap, gasApp);
|
|
|
+ break;
|
|
|
+ case "BJ02":
|
|
|
+ respBody = payBill(bodyMap, gasApp);
|
|
|
+ break;
|
|
|
+ case "BJ05":
|
|
|
+ respBody = reqInvoice(bodyMap, gasApp);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CEBResponseItem responseItem = new CEBResponseItem();
|
|
|
+ if (respBody.containsKey("ErrorCode")) {
|
|
|
+ //有误
|
|
|
+ responseItem.setInvokeHeader(CEBInvokeHeader.error(requestItem));
|
|
|
+ } else {
|
|
|
+ responseItem.setInvokeHeader(CEBInvokeHeader.success(requestItem));
|
|
|
+ }
|
|
|
+ responseItem.setBody(respBody);
|
|
|
+ String respStr = responseItem.toXml(xmlMapper);
|
|
|
+
|
|
|
+ //加密
|
|
|
+// return SM4Utils.encryptEcb(gasApp.getAppPublicKey(), respStr, SM4Utils.ENCODING);
|
|
|
+ //明文
|
|
|
+ return respStr;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ //数据格式都错了,随便返回吧
|
|
|
+ logger.error("请求格式有误:{}", e.getMessage(), e);
|
|
|
+ return "请求格式有误";
|
|
|
+ }
|
|
|
+ }, getExecutor()).whenComplete((v, e) -> {
|
|
|
+ if (e != null) {
|
|
|
+ logger.error(e.getMessage(), e);
|
|
|
+ response.finish("网络通信错误,请稍后重试".getBytes(Charset.forName("GBK")));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ response.finish(v.getBytes(Charset.forName("GBK")));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ //查询待缴费
|
|
|
+ private Map<String, Object> queryBillsToPay(Map<String, Object> reqBody, HeatingApp gasApp) {
|
|
|
+ Map<String, Object> respBody = new HashMap<>();
|
|
|
+ if (!reqBody.containsKey("billKey")) {
|
|
|
+ respBody.put("ErrorCode", "DEF0010");
|
|
|
+ respBody.put("ErrorInfo", "用户不存在");
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ String billKey = reqBody.get("billKey").toString();
|
|
|
+ QueryOrderResult orderResult = gasFeeService.apiQueryFeeInfo(billKey);
|
|
|
+ if (!orderResult.getErrCode().equals("00000")) {
|
|
|
+ //将我们的错误代码翻译成光大的
|
|
|
+ switch (orderResult.getErrCode()) {
|
|
|
+ case "10005":
|
|
|
+ respBody.put("ErrorCode", "DEF0002");
|
|
|
+ respBody.put("ErrorInfo", "用户未欠费");
|
|
|
+ break;
|
|
|
+ case "10001":
|
|
|
+ respBody.put("ErrorCode", "DEF0005");
|
|
|
+ respBody.put("ErrorInfo", "超过缴费时间");
|
|
|
+ break;
|
|
|
+ case "10002":
|
|
|
+ respBody.put("ErrorCode", "DEF0010");
|
|
|
+ respBody.put("ErrorInfo", "用户不存在");
|
|
|
+ break;
|
|
|
+ case "10003":
|
|
|
+ case "10004":
|
|
|
+ respBody.put("ErrorCode", "DEF0012");
|
|
|
+ respBody.put("ErrorInfo", "该用户不支持网上缴费,请联系公共事业单位");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return respBody;
|
|
|
+ } else {
|
|
|
+ respBody.put("companyNo", CommonUtils.generateSerialCode());
|
|
|
+ respBody.put("billKey", billKey);
|
|
|
+ respBody.put("totalNum", "1");
|
|
|
+
|
|
|
+ Map<String, Object> frame = new HashMap<>();
|
|
|
+ frame.put("contractNo", billKey);
|
|
|
+ frame.put("customerName", orderResult.getCustomerName());
|
|
|
+ frame.put("balance", CommonUtils.money(orderResult.getCustomerBalance()));
|
|
|
+ frame.put("payAmount", CommonUtils.money(orderResult.getTotalOweAmt()));
|
|
|
+
|
|
|
+ respBody.put("Frame", frame);
|
|
|
+ }
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ //缴费销账
|
|
|
+ private Map<String, Object> payBill(Map<String, Object> reqBody, HeatingApp gasApp) {
|
|
|
+ Map<String, Object> respBody = new HashMap<>();
|
|
|
+ if (!reqBody.containsKey("billKey")) {
|
|
|
+ respBody.put("ErrorCode", "DEF0010");
|
|
|
+ respBody.put("ErrorInfo", "用户不存在");
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!reqBody.containsKey("payAmount")) {
|
|
|
+ respBody.put("ErrorCode", "DEF0013");
|
|
|
+ respBody.put("ErrorInfo", "金额不符合规则");
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ String billKey = reqBody.get("billKey").toString();
|
|
|
+ String payDate = reqBody.get("payDate").toString();
|
|
|
+ String bankBillNo = reqBody.get("bankBillNo").toString();
|
|
|
+ String payAmount = reqBody.get("payAmount").toString();
|
|
|
+
|
|
|
+ BigDecimal chargeMoney = new BigDecimal(payAmount);
|
|
|
+ if (chargeMoney.compareTo(BigDecimal.ZERO) < 1) {
|
|
|
+ respBody.put("ErrorCode", "DEF0013");
|
|
|
+ respBody.put("ErrorInfo", "金额不符合规则");
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ //光大要求缴费金额必须等于欠费金额
|
|
|
+ QueryOrderResult orderResult = gasFeeService.apiQueryFeeInfo(billKey);
|
|
|
+ if (!orderResult.getErrCode().equals("00000")) {
|
|
|
+ //将我们的错误代码翻译成光大的
|
|
|
+ switch (orderResult.getErrCode()) {
|
|
|
+ case "10005":
|
|
|
+ respBody.put("ErrorCode", "DEF0002");
|
|
|
+ respBody.put("ErrorInfo", "用户未欠费");
|
|
|
+ break;
|
|
|
+ case "10001":
|
|
|
+ respBody.put("ErrorCode", "DEF0005");
|
|
|
+ respBody.put("ErrorInfo", "超过缴费时间");
|
|
|
+ break;
|
|
|
+ case "10002":
|
|
|
+ respBody.put("ErrorCode", "DEF0010");
|
|
|
+ respBody.put("ErrorInfo", "用户不存在");
|
|
|
+ break;
|
|
|
+ case "10003":
|
|
|
+ case "10004":
|
|
|
+ respBody.put("ErrorCode", "DEF0012");
|
|
|
+ respBody.put("ErrorInfo", "该用户不支持网上缴费,请联系公共事业单位");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+ BigDecimal debtMoney = CommonUtils.money(orderResult.getTotalOweAmt());
|
|
|
+
|
|
|
+ if (chargeMoney.compareTo(debtMoney) != 0) {
|
|
|
+ respBody.put("ErrorCode", "DEF0013");
|
|
|
+ respBody.put("ErrorInfo", "金额不符合规则");
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ GasPeriodCharge customerCharge = new GasPeriodCharge();
|
|
|
+
|
|
|
+ customerCharge.setCustomerId(billKey);
|
|
|
+ customerCharge.setChargeMoney(chargeMoney);
|
|
|
+ customerCharge.setBankSerial(bankBillNo);
|
|
|
+ customerCharge.setBankDate(DateUtil.asDate(payDate, "yyyyMMdd"));
|
|
|
+ customerCharge.setBankDateLong(customerCharge.getBankDate().getTime());
|
|
|
+ //todo 不知道写啥
|
|
|
+// customerCharge.setPayMode(chargeOrder.getPayMode());
|
|
|
+ customerCharge.setCustomerCount(1);
|
|
|
+
|
|
|
+ ChargeOrderResult chargeResult = gasFeeService.apiChargeFee(customerCharge, gasApp);
|
|
|
+ if (!chargeResult.getErrCode().equals("00000")) {
|
|
|
+ switch (chargeResult.getErrCode()) {
|
|
|
+ case "00003":
|
|
|
+ respBody.put("ErrorCode", "DEF0001");
|
|
|
+ respBody.put("ErrorInfo", "无相应记录");
|
|
|
+ break;
|
|
|
+ case "10001":
|
|
|
+ respBody.put("ErrorCode", "DEF0005");
|
|
|
+ respBody.put("ErrorInfo", "超过缴费时间");
|
|
|
+ break;
|
|
|
+ case "10002":
|
|
|
+ respBody.put("ErrorCode", "DEF0010");
|
|
|
+ respBody.put("ErrorInfo", "用户不存在");
|
|
|
+ break;
|
|
|
+ case "00002":
|
|
|
+ respBody.put("ErrorCode", "DEF0006");
|
|
|
+ respBody.put("ErrorInfo", "业务状态异常");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return respBody;
|
|
|
+ } else {
|
|
|
+ respBody.put("companyNo", CommonUtils.generateSerialCode());
|
|
|
+ respBody.put("billKey", billKey);
|
|
|
+ respBody.put("payAmount", payAmount);
|
|
|
+ }
|
|
|
+ return respBody;
|
|
|
+ }
|
|
|
+
|
|
|
+ //申请开票
|
|
|
+ private Map<String, Object> reqInvoice(Map<String, Object> reqBody, HeatingApp gasApp) {
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|