作者 朱兆平

国内转运验放脚本

  1 +package com.sy.groovy
  2 +
  3 +import com.alibaba.fastjson.JSON
  4 +import com.alibaba.fastjson.JSONArray
  5 +import com.alibaba.fastjson.JSONObject
  6 +import com.sy.model.GatherInfo
  7 +import com.sy.model.LAND_BUSINEESTYPE_LIST_INFO
  8 +import com.sy.model.LandBusinessTypeList
  9 +import com.sy.response.ResultJson
  10 +import com.sy.service.impl.GatherInfoHandle;
  11 +import feign.Feign
  12 +import feign.Headers
  13 +import feign.Param;
  14 +import feign.Request
  15 +import feign.RequestLine;
  16 +import feign.hystrix.FallbackFactory;
  17 +import groovy.lang.Script;
  18 +import org.basis.enhance.groovy.entity.ExecuteParams;
  19 +import org.slf4j.Logger;
  20 +import org.slf4j.LoggerFactory
  21 +import org.springframework.context.ApplicationContext
  22 +
  23 +import java.util.stream.Collectors;
  24 +
  25 +/**
  26 + * 单证验放
  27 + * 放行验放
  28 + */
  29 +class BillDomTransportCheck extends Script implements ChannelCheckScript {
  30 +
  31 + private final Logger logger = LoggerFactory.getLogger(getClass());
  32 +
  33 + @Override
  34 + Boolean check(ExecuteParams executeParams) {
  35 + try {
  36 + String cookieUserName = "admin"
  37 + String cookieUserId = "1903e605-a2e1-404d-a1a6-a749752fb030"
  38 + String ServiceAdr = "http://192.168.1.63:10001"
  39 +
  40 + GatherInfo gatherInfo = (GatherInfo) executeParams.get("GatherInfo");
  41 + LandBusinessTypeList landBusinessTypeList = (LandBusinessTypeList) executeParams.get("ChanelFormInfo");
  42 + List<LAND_BUSINEESTYPE_LIST_INFO> listinfos = (List<LAND_BUSINEESTYPE_LIST_INFO>) executeParams.get("ChanelFormBillLists");
  43 +
  44 + // 调用方法
  45 + ApplicationContext context = getContext();
  46 + // 获取容器中的bean
  47 + GatherInfoHandle gatherInfoHandle = context.getBean(GatherInfoHandle.class);
  48 +
  49 + if (listinfos!=null && !listinfos.isEmpty()){
  50 + //数组过滤,只要单证,不要其他 板箱之类
  51 + List<LAND_BUSINEESTYPE_LIST_INFO> list_infos = listinfos.stream().filter({ listInfo ->
  52 + if ("B".equals(listInfo.getExt4())) {
  53 + return true;
  54 + } else {
  55 + return false;
  56 + }
  57 + }).collect(Collectors.toList());
  58 +
  59 + list_infos.forEach({bill->
  60 + // 创建 Feign Client
  61 + //https://nmms.zzcargo.com:8443/api/wlpt-nmms-manage/trans/dom?originFlightno=&originFlightdate=&originFlightdateEnd=&originMasterwaybill=ML66158691&agentName=&transType=dom&pageNum=1&pageSize=10
  62 + MyFeignClient myFeignClient = createFeignClient(MyFeignClient, ServiceAdr,cookieUserName,cookieUserId);
  63 +
  64 + // 调用第三方接口
  65 + String response = myFeignClient.callThirdPartyApi(bill.getAwba(),cookieUserName,cookieUserId);
  66 + logger.info("[TRANS-API-RESPONSE]-{}",response)
  67 +
  68 + // 处理响应结果...
  69 + //返回的ResultJson实体类
  70 + JSONObject jo = JSONObject.parseObject(response);
  71 + StringBuilder sb = new StringBuilder("");
  72 + if ("200".equals(jo.getString("code"))){
  73 + //获取返回列表 data->list
  74 + JSONObject rData = jo.getJSONObject("data")
  75 + if (rData!=null){
  76 + JSONArray rList = rData.getJSONArray("list")
  77 + if (rList!=null && !rList.isEmpty()){
  78 + rList.forEach({ item ->
  79 + JSONObject jsonObject = (JSONObject) item;
  80 + Integer status = jsonObject.getInteger("status")
  81 + String oBill = jsonObject.getString("originMasterwaybill")
  82 + logger.info("[TRANS-API-RESPONSE]-单证:{},状态:{}",oBill,status)
  83 +
  84 + if (status>0){
  85 +// return true
  86 + }else{
  87 + sb.append("转运单证[").append(bill.getAwba()).append("]核验失败,未审核通过")
  88 + gatherInfoHandle.sendBw(gatherInfo,false,sb.toString(),landBusinessTypeList,listinfos);
  89 + return false
  90 + }
  91 + })
  92 + }
  93 + }
  94 + }else{
  95 + logger.info("[TRANS-CHECK-FAILD]-转运单证{}核验失败",bill.getAwba())
  96 + sb.append("转运单证[").append(bill.getAwba()).append("]核验失败,API-FAILD")
  97 + gatherInfoHandle.sendBw(gatherInfo,false,sb.toString(),landBusinessTypeList,listinfos);
  98 + return false;
  99 + }
  100 + })
  101 + //这里返回验放成功,验放失败从上面返回
  102 + return true;
  103 + }else{
  104 + return false;
  105 + }
  106 +
  107 + } catch (Exception e) {
  108 + e.printStackTrace();
  109 + logger.error("[CACHE-PARSE-ERROR]:", e);
  110 + return false;
  111 + }
  112 + }
  113 +
  114 + @Override
  115 + Object run() {
  116 + return false;
  117 + }
  118 +
  119 +
  120 + // 获取spring容器
  121 + ApplicationContext getContext() {
  122 + // 获取spring IOC容器
  123 + ApplicationContext context = applicationContext;
  124 + return context;
  125 + }
  126 +
  127 + private <T> T createFeignClient(Class<T> clientClass, String url,String username, String userid) {
  128 + return Feign.builder()
  129 + .options(new Request.Options(5000, 5000))
  130 + .requestInterceptor({ requestTemplate ->
  131 + String cookieValue = "username=" + username + "; userid=" + userid;
  132 + requestTemplate.header("Cookie", cookieValue);
  133 + })
  134 + .target(clientClass, url);
  135 + }
  136 +
  137 +
  138 + interface MyFeignClient {
  139 + @RequestLine("GET /trans/dom?originMasterwaybill={originMasterwaybill}")
  140 + @Headers("Cookie: username={username}; userid={userid}")
  141 + String callThirdPartyApi(@Param("originMasterwaybill") String originMasterwaybill,
  142 + @Param("username") String username,
  143 + @Param("userid") String userid);
  144 + }
  145 +
  146 + class MyFallbackFactory implements FallbackFactory<MyFeignClient> {
  147 + @Override
  148 + public MyFeignClient create(Throwable throwable) {
  149 + return new MyFeignClient() {
  150 + @Override
  151 + public String callThirdPartyApi(String originMasterwaybill,String username,String userid) {
  152 + // 处理 fallback 逻辑...
  153 + ResultJson resultJson = new ResultJson("400","访问接口出错");
  154 + return JSON.toJSONString(resultJson);
  155 + }
  156 + };
  157 + }
  158 + }
  159 +}
  1 +import java.text.DecimalFormat;
  2 +
  3 +public class WeightTest {
  4 +
  5 + public static void main(String[] args) {
  6 + DecimalFormat df = new DecimalFormat("0.00");
  7 + double grossWt = 9060;
  8 + double inWt = 8330;
  9 + double goodsWt = 805;
  10 + grossWt = Double.parseDouble(String.format("%.1f", grossWt));
  11 + Double goodCheckResult = Double.parseDouble(df.format(Math.abs((grossWt-inWt-goodsWt)) / goodsWt));
  12 + Double goodCheckResult1 = Double.parseDouble(df.format(Math.abs((grossWt-inWt-goodsWt)) / goodsWt));
  13 +
  14 + System.out.println("goodCheckResult = " + goodCheckResult);
  15 + System.out.println("goodCheckResult1 = " + goodCheckResult1);
  16 + }
  17 +
  18 +}
@@ -25,7 +25,7 @@ public class Base64Test { @@ -25,7 +25,7 @@ public class Base64Test {
25 25
26 @Test 26 @Test
27 public void base64Decode(){ 27 public void base64Decode(){
28 - String testcode = "PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPEdBVEhFUl9JTkZPIHhtbG5zPSJodHRwOi8vcnBiLmN1c3RvbXMuZ292LmNuL2NvbW1vbi9Gcm9udEVuZFNjaGVtYXMvIgogICAgeG1sbnM6eHNpPSJodHRwOi8vd3d3LnczLm9yZy8yMDAxL1hNTFNjaGVtYS1pbnN0YW5jZSIKICAgIHhzaTpzY2hlbWFMb2NhdGlvbj0iaHR0cDovL3JwYi5jdXN0b21zLmdvdi5jbi9jb21tb24vRnJvbnRFbmRTY2hlbWFzLyI+CiAgICA8SV9FX0ZMQUc+RTwvSV9FX0ZMQUc+CiAgICA8QVJFQV9JRD40NjA0NjAwMDAwPC9BUkVBX0lEPgogICAgPENITkxfTk8+NDYwNDYwMDAyMDwvQ0hOTF9OTz4KICAgIDxHRVRIRVJfTU9ERT5BPC9HRVRIRVJfTU9ERT4KICAgIDxTRVNTSU9OX0lEPjIwMjMwODIzMTIxMzE3Mjg3MDAzPC9TRVNTSU9OX0lEPgogICAgPElDX0lORk8+CiAgICAgICA8SUNfSUQ+PC9JQ19JRD4KICAgICAgICA8SUNfTk8+PC9JQ19OTz4KICAgICAgICA8SUNfVFlQRT48L0lDX1RZUEU+CiAgICAgICAgPElDX0VYVEVOREVEX0NPTlRFTlQ+PC9JQ19FWFR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.getDecoder().decode(testcode); 29 byte[] base64decodedBytes = Base64.getDecoder().decode(testcode);
30 try { 30 try {
31 System.out.println("解密后的信息为:"+new String(base64decodedBytes, "utf-8")); 31 System.out.println("解密后的信息为:"+new String(base64decodedBytes, "utf-8"));
@@ -220,4 +220,14 @@ public class EnginTest { @@ -220,4 +220,14 @@ public class EnginTest {
220 log.error("脚本验放测试失败"); 220 log.error("脚本验放测试失败");
221 } 221 }
222 } 222 }
  223 +
  224 + @Test
  225 + public void enginTranseTest(){
  226 + // 构建参数
  227 + ExecuteParams executeParams = new ExecuteParams();
  228 + // 执行脚本中指定的方法 changeProduct
  229 + EngineExecutorResult executorResult = engineExecutor.execute(
  230 + "check", new ScriptQuery("bill-trans-check"), executeParams);
  231 + log.info("使用groovy脚本来验证过卡判定放行结果=========>>>>>>>>>>>执行结果:{}", executorResult);
  232 + }
223 } 233 }