| package com.zy.asrs.task.handler; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.alibaba.fastjson.JSONObject; | 
| import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
| import com.core.common.Cools; | 
| import com.core.common.DateUtils; | 
| import com.core.common.SpringUtils; | 
| import com.core.exception.CoolException; | 
| import com.zy.asrs.entity.*; | 
| import com.zy.asrs.entity.param.ArmPrecomputeParam; | 
| import com.zy.asrs.service.ApiLogService; | 
| import com.zy.asrs.service.BasArmRulesService; | 
| import com.zy.asrs.task.AbstractHandler; | 
| import com.zy.asrs.task.core.ReturnT; | 
| import com.zy.common.constant.MesConstant; | 
| import com.zy.common.model.MesPakinParam; | 
| import com.zy.common.model.MesPakoutParam; | 
| import com.zy.common.model.StartupDto; | 
| import com.zy.common.utils.HttpHandler; | 
| import lombok.extern.slf4j.Slf4j; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.beans.factory.annotation.Value; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.util.Date; | 
| import java.util.List; | 
|   | 
| /** | 
|  * Created by vincent on 2020/7/4 | 
|  */ | 
| @Slf4j | 
| @Service | 
| @Transactional | 
| public class ArmRulesHandler extends AbstractHandler<String> { | 
|     @Autowired | 
|     private ApiLogService apiLogService; | 
|     @Autowired | 
|     private BasArmRulesService basArmRulesService; | 
|   | 
|     @Value("${arm.address.URL}") | 
|     //端口 | 
|     private String URL; | 
|   | 
|     @Value("${arm.address.QuantityOfPalletizing}") | 
|     //审核地址 | 
|     private String QuantityOfPalletizing; | 
|   | 
|     public ReturnT<String> start(ArmPrecomputeParam armPrecomputeParam) { | 
|         try{ | 
|             String response = ""; | 
|             boolean success = false; | 
|             try { | 
|                 response = new HttpHandler.Builder() | 
|                         .setUri(URL) | 
|                         .setPath(QuantityOfPalletizing) | 
|                         .setJson(JSON.toJSONString(armPrecomputeParam.getMatList().get(0))) | 
|                         .build() | 
|                         .doPost(); | 
|                 JSONObject jsonObject = JSON.parseObject(response); | 
|                 if (jsonObject.getInteger("code").equals(200)) { | 
|                     JSONObject data = JSON.parseObject(jsonObject.getString("data")); | 
|   | 
|                     success = true; | 
|                     ArmPrecomputeParam jsonObjectObject = new ArmPrecomputeParam( | 
|                             data.getDouble("material_length"), | 
|                             data.getDouble("material_width"), | 
|                             data.getDouble("material_height"), | 
|                             data.getDouble("material_weight"), | 
|                             data.getDouble("material_number") | 
|                     ); | 
|                     for (ArmPrecomputeParam.CombMat combMat : jsonObjectObject.getMatList()){ | 
|                         basArmRulesService.updateStatus(combMat); | 
|                     } | 
|                 } else { | 
|                     log.error("获取码垛数量!!!url:{};request:{};response:{}", URL+QuantityOfPalletizing, JSON.toJSONString(armPrecomputeParam), response); | 
|                     throw new CoolException("获取码垛数量失败"); | 
|                 } | 
|             } catch (Exception e) { | 
|                 try{ | 
|                     log.error("fail==>获取码垛数量:"+e.getLocalizedMessage()); | 
|                 } catch (Exception e1){ | 
|   | 
|                 } | 
| //                return FAIL.setMsg(e.getLocalizedMessage()); | 
|                 return FAIL; | 
| //                log.error("fail", e); | 
| //                return FAIL.setMsg(e.getMessage()); | 
|             } finally { | 
|                 try { | 
|   | 
|                     if (success){ | 
|                         // 保存接口日志 | 
|                         apiLogService.save( | 
|                                 "获取码垛数量", | 
|                                 URL +"/"+ QuantityOfPalletizing, | 
|                                 null, | 
|                                 "127.0.0.1", | 
|                                 JSON.toJSONString(armPrecomputeParam), | 
|                                 response, | 
|                                 success | 
|                         ); | 
|                     } else { | 
|                         beforeBodyWriteCallApiLogSave( | 
|                                 "获取码垛数量", | 
|                                 URL +"/"+ QuantityOfPalletizing, | 
|                                 null, | 
|                                 "127.0.0.1", | 
|                                 JSON.toJSONString(armPrecomputeParam), | 
|                                 response, | 
|                                 success | 
|                         ); | 
|                     } | 
|                 } catch (Exception e) { log.error("", e); } | 
|             } | 
|         }catch (Exception e){ | 
|   | 
|         } | 
|         return SUCCESS; | 
|     } | 
|     public void beforeBodyWriteCallApiLogSave(String name, String url, String appkey, String ip, String request, String response, boolean success) { | 
|         ApiLog apiLog = apiLogService.selectOne(new EntityWrapper<ApiLog>() | 
|                 .eq("namespace", name) | 
|                 .eq("request", request) | 
|                 .eq("response", response) | 
|                 .eq("result", success? 1:0) | 
|                 .orderBy("create_time", false) | 
|         ); | 
|   | 
|         if (!Cools.isEmpty(apiLog)){ | 
|             long parseLong = Long.parseLong(apiLog.getTimestamp()); | 
|             if (new Date().getTime()-parseLong<5*1000*60){ | 
|                 return; | 
|             } | 
|         } | 
|         // 保存接口日志 | 
|         apiLogService.save( | 
|                 name, | 
|                 url, | 
|                 appkey, | 
|                 ip, | 
|                 request, | 
|                 response, | 
|                 success | 
|         ); | 
|     } | 
| } |