package com.zy.asrs.utils; 
 | 
  
 | 
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.SpringUtils; 
 | 
import com.core.exception.CoolException; 
 | 
import com.zy.asrs.entity.ApiLog; 
 | 
import com.zy.asrs.service.ApiLogService; 
 | 
import com.zy.asrs.utils.core.AbstractHandler; 
 | 
import com.zy.asrs.utils.core.ReturnT; 
 | 
import com.zy.common.utils.HttpHandler; 
 | 
import com.zy.core.model.protocol.StaProtocol; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
  
 | 
import java.util.Date; 
 | 
import java.util.Map; 
 | 
  
 | 
@Slf4j 
 | 
public class PostMesDataUtils extends AbstractHandler<String> { 
 | 
  
 | 
    public ReturnT<String> postMesDataWcs(String URL, String erpPath, Object combParam){ 
 | 
        if(combParam != null){ 
 | 
            String response = ""; 
 | 
            boolean success = false; 
 | 
            try { 
 | 
                response = new HttpHandler.Builder() 
 | 
                        .setUri(URL) 
 | 
                        .setPath(erpPath) 
 | 
                        .setJson(JSON.toJSONString(combParam)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                JSONObject jsonObject = JSON.parseObject(response); 
 | 
                if (jsonObject.getInteger("code")==200) { 
 | 
                    success = true; 
 | 
                } else { 
 | 
                    log.error("任务完成信息上传请求接口失败!!!url:{};request:{};response:{}"+URL+erpPath, JSON.toJSONString(combParam), response); 
 | 
                    throw new CoolException("上报wcs系统失败"); 
 | 
                } 
 | 
            } catch (Exception e) { 
 | 
                log.error("fail:上报wcs系统失败==>", e); 
 | 
//                      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 
 | 
                return FAIL.setMsg(e.getMessage()); 
 | 
            } finally { 
 | 
                try { 
 | 
                    // 保存接口日志 
 | 
                    ApiLogService apiLogService = SpringUtils.getBean(ApiLogService.class); 
 | 
  
 | 
                    apiLogService.save( 
 | 
                            "上报wcs系统", 
 | 
                            URL + erpPath, 
 | 
                            null, 
 | 
                            URL, 
 | 
                            JSON.toJSONString(combParam), 
 | 
                            response, 
 | 
                            success 
 | 
                    ); 
 | 
                } catch (Exception e) { 
 | 
                    log.error("任务完成信息上传保存接口日志异常,异常信息:", e); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return SUCCESS; 
 | 
    } 
 | 
  
 | 
    public ReturnT<String> postMesData(String name, String URL, String mesPath, Object combParam){ 
 | 
//        if (true){ 
 | 
//            System.out.println("name:"+name+",URL:"+URL+",mesPath:"+mesPath+",combParam:"+combParam); 
 | 
//            return SUCCESS; 
 | 
//        } 
 | 
        if(combParam != null){ 
 | 
            String response = ""; 
 | 
            boolean success = false; 
 | 
            try { 
 | 
//                Map<String, Object> map = new HashMap<>() 
 | 
//                map.put("appkey","ea1f0459efc02a79f046f982767939ae"); 
 | 
                response = new HttpHandler.Builder() 
 | 
//                        .setHeaders(map) 
 | 
                        .setUri(URL) 
 | 
                        .setPath(mesPath) 
 | 
                        .setJson(JSON.toJSONString(combParam)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                System.out.println("response:"+response); 
 | 
                JSONObject jsonObject = JSON.parseObject(response); 
 | 
  
 | 
                if (jsonObject.getInteger("code").equals(200)) { 
 | 
                    success = true; 
 | 
                } else { 
 | 
                    log.error("请求接口失败!!!url:{};request:{};response:{}", URL+"/"+mesPath, JSON.toJSONString(combParam), response); 
 | 
                    throw new CoolException(name); 
 | 
                } 
 | 
            } catch (Exception e) { 
 | 
                log.error("请求接口失败!!!url:{};request:{};response:{}", URL+"/"+mesPath, JSON.toJSONString(combParam), response); 
 | 
//                      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 
 | 
                return FAIL.setMsg(e.getMessage()); 
 | 
            } finally { 
 | 
                try { 
 | 
                    if (success){ 
 | 
                        // 保存接口日志 
 | 
                        ApiLogService apiLogService = SpringUtils.getBean(ApiLogService.class); 
 | 
                        apiLogService.save( 
 | 
                                name, 
 | 
                                URL +"/"+ mesPath, 
 | 
                                null, 
 | 
                                "127.0.0.1", 
 | 
                                JSON.toJSONString(combParam), 
 | 
                                response, 
 | 
                                success 
 | 
                        ); 
 | 
                    } else { 
 | 
                        postMesDataCallApiLogSave(response,success,name, URL, mesPath, combParam); 
 | 
                    } 
 | 
                } catch (Exception e) { 
 | 
                    log.error("", e); } 
 | 
            } 
 | 
        } 
 | 
        return SUCCESS; 
 | 
    } 
 | 
    public ReturnT<String> postMesDataAgv(String name, String URL, String mesPath, Object combParam){ 
 | 
//        if (true){ 
 | 
//            System.out.println("name:"+name+",URL:"+URL+",mesPath:"+mesPath+",combParam:"+combParam); 
 | 
//            return SUCCESS; 
 | 
//        } 
 | 
        if(combParam != null){ 
 | 
            String response = ""; 
 | 
            boolean success = false; 
 | 
            try { 
 | 
//                Map<String, Object> map = new HashMap<>() 
 | 
//                map.put("appkey","ea1f0459efc02a79f046f982767939ae"); 
 | 
                response = new HttpHandler.Builder() 
 | 
//                        .setHeaders(map) 
 | 
                        .setUri(URL) 
 | 
                        .setPath(mesPath) 
 | 
                        .setJson(JSON.toJSONString(combParam)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                System.out.println("response:"+response); 
 | 
                JSONObject jsonObject = JSON.parseObject(response); 
 | 
  
 | 
                if (jsonObject.getInteger("code").equals(0)) { 
 | 
                    success = true; 
 | 
                } else { 
 | 
                    log.error("请求接口失败!!!url:{};request:{};response:{}", URL+"/"+mesPath, JSON.toJSONString(combParam), response); 
 | 
                    throw new CoolException(name); 
 | 
                } 
 | 
            } catch (Exception e) { 
 | 
                log.error("请求接口失败!!!url:{};request:{};response:{}", URL+"/"+mesPath, JSON.toJSONString(combParam), response); 
 | 
//                      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 
 | 
                return FAIL.setMsg(e.getMessage()); 
 | 
            } finally { 
 | 
                try { 
 | 
                    if (success){ 
 | 
                        // 保存接口日志 
 | 
                        ApiLogService apiLogService = SpringUtils.getBean(ApiLogService.class); 
 | 
                        apiLogService.save( 
 | 
                                name, 
 | 
                                URL +"/"+ mesPath, 
 | 
                                null, 
 | 
                                "127.0.0.1", 
 | 
                                JSON.toJSONString(combParam), 
 | 
                                response, 
 | 
                                success 
 | 
                        ); 
 | 
                    } else { 
 | 
                        postMesDataCallApiLogSave(response,success,name, URL, mesPath, combParam); 
 | 
                    } 
 | 
                } catch (Exception e) { 
 | 
                    log.error("", e); } 
 | 
            } 
 | 
        } 
 | 
        return SUCCESS; 
 | 
    } 
 | 
  
 | 
    public ReturnT<String> postMesData(String name, String URL, String mesPath, Object combParam,Map<String, Object> map){ 
 | 
//        if (true){ 
 | 
//            System.out.println("name:"+name+",URL:"+URL+",mesPath:"+mesPath+",combParam:"+combParam); 
 | 
//            return SUCCESS; 
 | 
//        } 
 | 
        if(combParam != null){ 
 | 
            String response = ""; 
 | 
            boolean success = false; 
 | 
            try { 
 | 
//                Map<String, Object> map = new HashMap<>(); 
 | 
//                map.put("appkey","ea1f0459efc02a79f046f982767939ae"); 
 | 
                response = new HttpHandler.Builder() 
 | 
                        .setHeaders(map) 
 | 
                        .setUri(URL) 
 | 
                        .setPath(mesPath) 
 | 
                        .setJson(JSON.toJSONString(combParam)) 
 | 
                        .build() 
 | 
                        .doPost(); 
 | 
                System.out.println("response:"+response); 
 | 
                JSONObject jsonObject = JSON.parseObject(response); 
 | 
  
 | 
                if (jsonObject.getInteger("code").equals(200)) { 
 | 
                    success = true; 
 | 
                } else { 
 | 
                    log.error("请求接口失败!!!url:{};request:{};response:{}", URL+"/"+mesPath, JSON.toJSONString(combParam), response); 
 | 
                    throw new CoolException(name); 
 | 
                } 
 | 
            } catch (Exception e) { 
 | 
                log.error("请求接口失败!!!url:{};request:{};response:{}", URL+"/"+mesPath, JSON.toJSONString(combParam), response); 
 | 
//                      TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 
 | 
                return FAIL.setMsg(e.getMessage()); 
 | 
            } finally { 
 | 
                try { 
 | 
                    // 保存接口日志 
 | 
                    ApiLogService apiLogService = SpringUtils.getBean(ApiLogService.class); 
 | 
                    apiLogService.save( 
 | 
                            name, 
 | 
                            URL +"/"+ mesPath, 
 | 
                            null, 
 | 
                            "127.0.0.1", 
 | 
                            JSON.toJSONString(combParam), 
 | 
                            response, 
 | 
                            success 
 | 
                    ); 
 | 
                } catch (Exception e) { 
 | 
                    log.error("", e); } 
 | 
            } 
 | 
        } 
 | 
        return SUCCESS; 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
  
 | 
  
 | 
  
 | 
    public void postMesDataCallApiLogSave(String response,boolean success,String name, String URL, String mesPath, Object combParam) { 
 | 
        ApiLogService apiLogService = SpringUtils.getBean(ApiLogService.class); 
 | 
  
 | 
        ApiLog apiLog = apiLogService.selectOne(new EntityWrapper<ApiLog>() 
 | 
                .eq("namespace", name) 
 | 
                .eq("response", response) 
 | 
                .eq("url", URL +"/"+ mesPath) 
 | 
                .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 +"/"+ mesPath, 
 | 
                null, 
 | 
                "127.0.0.1", 
 | 
                JSON.toJSONString(combParam), 
 | 
                response, 
 | 
                success 
 | 
        ); 
 | 
    } 
 | 
} 
 |