package com.zy.asrs.task.handler; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.Cools; import com.zy.asrs.entity.BasDevp; import com.zy.asrs.entity.Mat; import com.zy.asrs.entity.WrkDetl; import com.zy.asrs.entity.WrkMast; import com.zy.asrs.service.*; import com.zy.asrs.task.core.ReturnT; import com.zy.common.constant.OpenApiAddressConstant; import com.zy.common.utils.HttpHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author pang.jiabao * @description 冠鸿龙南定时任务具体实现 * @createDate 2024/7/10 9:28 */ @Slf4j @Service public class GhlnHandler { @Resource private WrkMastService wrkMastService; @Resource private BasDevpService basDevpService; @Resource private WrkDetlService wrkDetlService; @Autowired private ApiLogService apiLogService; @Autowired private LoginAuthHandler loginAuthHandler; @Resource private MatService matService; /** * 调用金蝶同步物料接口,增量同步每小时产生的新物料 */ public void syncMat() { //登录金蝶 ReturnT start = loginAuthHandler.start(); //条件和需要获得的结果拼接 JSONObject jsonObject = new JSONObject(); jsonObject.put("FormId", "BD_MATERIAL"); jsonObject.put("OrderString",""); jsonObject.put("TopRowCount",0); jsonObject.put("Limit", 3000); jsonObject.put("StartRow", "0"); jsonObject.put("SubSystemId", ""); // 获取当前时间 LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 获取当前时间的前一小时整点时间 LocalDateTime oneHourAgo = currentDateTime.minusHours(1).withMinute(0).withSecond(0).withNano(0); jsonObject.put("FilterString", "FCreateDate <= '2022-08-23 13:37:00'"); // jsonObject.put("FilterString", "FCreateDate >= '"+oneHourAgo.format(formatter)+"' and FUseOrgId .FNumber='10004'"); // jsonObject.put("FilterString","FNumber = '0101010003'"); jsonObject.put("FieldKeys", "FNumber,FName,FCreateDate,FTypeID"); JSONObject jsonObject1 = new JSONObject(); jsonObject1.put("data", jsonObject); String add = jsonObject1.toJSONString(); String response = ""; boolean success = false; String url = "http://192.168.16.7"; String address = "K3CLOUD/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc"; try { //获取Cookie值 HashMap headers = new HashMap<>(); headers.put("Cookie", start.getContent()); response = new HttpHandler.Builder() .setHeaders(headers) .setUri(url) .setPath(address) .setJson(add) .build() .doPost(); String[] tempstr; if (!"[]".equals(response)) { String[] returnstr = response.split("],"); int count = 0; log.info("--------调用金蝶同步物料信息开始--------"); for (int i = 0; i < returnstr.length; i++) { Mat mat = new Mat(); tempstr = returnstr[i].split(","); if (i == 0) { mat.setMatnr(tempstr[0].substring(2)); //物料编码 } else { mat.setMatnr(tempstr[0].substring(1)); //物料编码 } mat.setMaktx(tempstr[1]); // 物料名称 mat.setTagId(Long.valueOf(tempstr[3].substring(0,1))); // 物料分类 mat.setCreateTime(new Date());//商品创建时间 mat.setUpdateTime(new Date()); mat.setStatus(1); try { matService.insert(mat); count++; }catch (Exception e){ log.error("金蝶同步物料异常:{}", mat); } } log.info("----调用金蝶同步物料信息结果:总{},成{}--------",returnstr.length, count); success = true; } } catch (Exception e) { log.error("fail", e); } finally { try { // 保存接口日志 apiLogService.save( "请求金蝶同步物料信息", url + address, null, "127.0.0.1", add, response, success ); } catch (Exception e) { log.error("", e); } } } /** * 到达出库口,呼叫agv */ public void OutboundCallAGV() { BasDevp basDevp = basDevpService.selectOne(new EntityWrapper().eq("dev_no", 305)); if (basDevp.getWrkNo() > 6000 && basDevp.getWrkNo() <= 9000 && basDevp.getLoading().equals("Y") && basDevp.getAutoing().equals("Y") &&basDevp.getOutEnable().equals("Y")) { // 出库工作档 WrkMast wrkMast = wrkMastService.selectById(basDevp.getWrkNo()); if(wrkMast.getWhsType() == null) { WrkDetl wrkDetl = wrkDetlService.selectOne(new EntityWrapper().eq("wrk_no", basDevp.getWrkNo())); // 调用结果 boolean success = false; // 构造请求头 Map headers = new HashMap<>(); headers.put("Content-Type","application/json;charset=UTF-8"); // 构造请求体 JSONObject jsonObject = new JSONObject(); jsonObject.put("matnr", wrkDetl.getMatnr()); // 物料编码 jsonObject.put("model", wrkDetl.getModel()); // 型号 jsonObject.put("batch", wrkDetl.getBatch()); // 批次 jsonObject.put("specs", wrkDetl.getSpecs()); // 膜类型 jsonObject.put("manuDate", wrkDetl.getManuDate()); // 生产日期 jsonObject.put("length", wrkDetl.getLength()+""); // 长 jsonObject.put("weight", wrkDetl.getWeight()+""); // 宽 jsonObject.put("volume", wrkDetl.getVolume()+""); // 面积 String body = jsonObject.toString(); String response = ""; try { response = new HttpHandler.Builder() .setUri(OpenApiAddressConstant.AGV_IP) .setPath(OpenApiAddressConstant.CALL_AGV_URL) .setHeaders(headers) .setJson(body) .build() .doPost(); if (!Cools.isEmpty(response)) { JSONObject jsonObject1 = JSONObject.parseObject(response); if ((Integer) jsonObject1.get("code") == 200){ success = true; wrkMast.setWhsType(1); wrkMastService.updateById(wrkMast); } } else { log.error("出库呼叫agv接口异常接口失败!!!url:{};request:{};response:{}", OpenApiAddressConstant.AGV_IP + OpenApiAddressConstant.CALL_AGV_URL, body, response); } } catch (Exception e) { log.error("出库呼叫agv接口异常:{}", e.getMessage()); } finally { try { // 保存接口日志 apiLogService.save( "出库请求agv", OpenApiAddressConstant.AGV_IP + OpenApiAddressConstant.CALL_AGV_URL, null, "127.0.0.1", jsonObject.toJSONString(), response, success ); } catch (Exception e) { log.error("出库呼叫agv接口日志异常", e); } } } } } }