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.exception.CoolException; import com.zy.api.entity.OrderItemsParam; import com.zy.api.entity.OutOrderParams; import com.zy.api.entity.PubOrderParams; import com.zy.api.entity.ReportOrderParam; import com.zy.api.entity.ReportOrderParams; import com.zy.api.entity.StockAdjustParams; import com.zy.api.entity.StockUpOrderParams; import com.zy.api.entity.dto.XSR; import com.zy.api.service.KopenApiService; import com.zy.asrs.entity.*; import com.zy.asrs.enums.OrderTypeEnum; import com.zy.asrs.service.*; import com.zy.asrs.service.impl.CheckOrderDetlServiceImpl; import com.zy.asrs.task.AbstractHandler; import com.zy.asrs.task.core.ReturnT; import com.zy.asrs.utils.OrderInAndOutUtil; import com.zy.asrs.utils.Utils; import com.zy.common.constant.MesConstant; import com.zy.common.model.DetlDto; import com.zy.common.model.MesPakinParam; import com.zy.common.model.MesPakoutParam; import com.zy.common.utils.HttpHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; /** * Created by vincent on 2020/7/7 */ @Slf4j @Service public class OrderSyncHandler extends AbstractHandler { private final CheckOrderDetlServiceImpl checkOrderDetlService; @Autowired private JdbcTemplate jdbcTemplate; @Autowired private OrderService orderService; @Autowired private OrderDetlService orderDetlService; @Autowired private ApiLogService apiLogService; @Autowired private DocTypeService docTypeService; @Autowired private OrderPakinService orderPakinService; @Autowired private OrderDetlPakinService orderDetlPakinService; @Autowired private KopenApiService kopenApiService; @Autowired private MatService matService; OrderSyncHandler(CheckOrderDetlServiceImpl checkOrderDetlService) { this.checkOrderDetlService = checkOrderDetlService; } @Transactional public ReturnT startOrderIssuedOnceMore(Order order) { DocType docType = docTypeService.selectById(order.getDocType()); if (null == docType) { return SUCCESS; } Order order1 = OrderInAndOutUtil.selectByNo(order.getPakinPakoutStatus$(), order.getOrderNo()); if (!Cools.isEmpty(order1)) { return FAIL.setMsg("请等待...订单执行档案正在转历史档案!!!"); } List orderDetls = OrderInAndOutUtil.selectByOrderId(order.getPakinPakoutStatus$(), order.getId()); if (!Cools.isEmpty(orderDetls) || !orderDetls.isEmpty()) { return FAIL.setMsg("请等待...订单执行明细档案正在转历史档案!!!"); } // 单据明细档 List orderDetlList = orderDetlService.selectByOrderId(order.getId()); for (OrderDetl orderDetl : orderDetlList) { if (orderDetl.getQty().equals(0D)) { continue; } orderDetl.setWorkQty(orderDetl.getQty()); orderDetl.setAnfme(orderDetl.getAnfme() - orderDetl.getQty()); orderDetl.setQty(0D); orderDetlService.updateById(orderDetl); } // 修改订单状态 99.再次作业 ===>> 1.待作业 if (!orderService.updateSettle(order.getId(), 1L, null)) { throw new CoolException("服务器内部错误,请联系管理员"); } return SUCCESS; } @Transactional(rollbackFor = Exception.class) public ReturnT startOrderIssued(Order order) { DocType docType = docTypeService.selectById(order.getDocType()); if (null == docType) { return SUCCESS; } List orderDetls = orderDetlService.selectByOrderId(order.getId()); OrderInAndOutUtil.insertOrder(order.getPakinPakoutStatus$(), order); // 单据明细档 List list = new ArrayList<>(); List orderDetlList = new ArrayList<>(); for (OrderDetl detail : orderDetls) { DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(), detail.getBrand(), detail.getStandby1(), detail.getStandby2(), detail.getStandby3(), detail.getBoxType1(), detail.getBoxType2(), detail.getBoxType3(), detail.getAnfme(), detail.getThreeCode()); if (DetlDto.has(list, dto)) { DetlDto detlDto = DetlDto.find(list, dto.getMatnr(), dto.getBatch(), dto.getBrand(), dto.getStandby1(), dto.getStandby2(), dto.getStandby3(), dto.getBoxType1(), dto.getBoxType2(), dto.getBoxType3()); assert detlDto != null; detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme()); OrderDetl orderDetl = OrderDetl.find(orderDetlList, dto.getMatnr(), dto.getBatch(), dto.getBrand(), dto.getStandby1(), dto.getStandby2(), dto.getStandby3(), dto.getBoxType1(), dto.getBoxType2(), dto.getBoxType3()); assert orderDetl != null; orderDetl.setAnfme(orderDetl.getAnfme() + detail.getAnfme()); } else { list.add(dto); orderDetlList.add(detail); } } for (OrderDetl orderDetl : orderDetlList) { orderDetl.setWorkQty(0D); OrderInAndOutUtil.insertOrderDetl(order.getPakinPakoutStatus$(), order, orderDetl); // 小松:如果下发备货派工单,则生成备货出库+备货入库 if (order.getDocType().equals(OrderTypeEnum.TRANSFER.type.longValue())) { try { ifTransfetInsertStockIn(order, orderDetl); } catch (Exception e) { throw new CoolException(e.getMessage()); } } } // 修改订单状态 1.未作业 ===>> 2.作业中 if (!orderService.updateSettle(order.getId(), 2L, null)) { throw new CoolException("服务器内部错误,请联系管理员"); } return SUCCESS; } @Transactional(rollbackFor = Exception.class) public void ifTransfetInsertStockIn(Order order, OrderDetl orderDetl) { OrderPakin orderPakin = new OrderPakin(); BeanUtils.copyProperties(order, orderPakin); orderPakin.setDocType(OrderTypeEnum.STOCK_IN.type.longValue()); if (!orderPakinService.insert(orderPakin)) { throw new CoolException("插入备货入库单失败"); } OrderDetlPakin orderDetlPakin = new OrderDetlPakin(); BeanUtils.copyProperties(orderDetl, orderDetlPakin); orderDetlPakin.setOrderId(orderPakin.getId()); if (!orderDetlPakinService.insert(orderDetlPakin)) { throw new CoolException("插入备货入库单失败"); } } @Transactional public ReturnT startOrderInAndOutUtil(Order order) { // 小松:轮训到备货派工单是查询备货入库单是否完成 if (order.getDocType().equals(OrderTypeEnum.STOCK_IN.type.longValue())) { return SUCCESS; } if (order.getDocType().equals(OrderTypeEnum.TRANSFER.type.longValue())) { OrderPakin orderPakin = orderPakinService.selectByNo(order.getOrderNo()); if (null != orderPakin) { if (!orderPakin.getSettle().equals(4L)) { return SUCCESS; } } } DocType docType = docTypeService.selectById(order.getDocType()); if (null == docType) { return SUCCESS; } Order orderSou = orderService.selectByNo(order.getOrderNo()); if (!orderSou.getSettle().equals(2L)) { return FAIL.setMsg("订单状态不正确,请联系管理员"); } List orderDetlList = orderDetlService.selectByOrderId(orderSou.getId()); List orderDetls = OrderInAndOutUtil.selectByOrderId(order.getPakinPakoutStatus$(), order.getId()); for (OrderDetl orderDetlSou : orderDetlList) { for (OrderDetl orderDetl : orderDetls) { if (orderDetl.getQty().equals(0D)) { continue; } if (orderDetlSou.beSimilar(orderDetl)) { double v = orderDetlSou.getAnfme() - orderDetlSou.getQty(); if (v < orderDetl.getQty() || orderDetl.getQty().equals(v)) { orderDetlSou.setQty(orderDetlSou.getAnfme()); orderDetl.setQty(orderDetl.getQty() - v); break; } else { orderDetlSou.setQty(orderDetlSou.getQty() + orderDetl.getQty()); orderDetl.setQty(0D); } } } } for (OrderDetl orderDetlSou : orderDetlList) { orderDetlService.updateById(orderDetlSou); } // 修改订单状态 2.作业中 ===>> 4.待上报 if (!orderService.updateSettle(orderSou.getId(), 4L, null)) { throw new CoolException("服务器内部错误,请联系管理员"); } // 修改订单状态 4.完成 ===>> 6.结束 OrderInAndOutUtil.updateOrder(order.getPakinPakoutStatus$(), order.getId(), 6L, null); return SUCCESS; } /** * 单据上报 * * @param order * @return com.zy.asrs.task.core.ReturnT * @author Ryan * @date 2025/12/15 16:14 */ @Transactional public ReturnT startOrderReport(Order order) { DocType docType = docTypeService.selectById(order.getDocType()); if (null == docType) { return SUCCESS; } long settle = 6L; List orderDetls = orderDetlService.selectByOrderId(order.getId()); if (Integer.valueOf(docType.getDocId() + "").equals(OrderTypeEnum.PICKING.type)) { // 上架派工单完成 ReportOrderParam orderParam = new ReportOrderParam(); orderParam.setCompany_id(order.getCstmrName()) .setWms_id(order.getId() + "") .setOrder_no(order.getOrderNo()) .setType(order.getDocType() + "") .setInv_no(order.getNumber()) .setPm_tktid(order.getItemName()) .setUpdate_time(Utils.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss")) .setTarget_location(""); List reportOrderParams = new ArrayList<>(); orderDetls.forEach(detl -> { Mat material = matService.selectByMatnr(detl.getMatnr()); if (Objects.isNull(material)) { throw new CoolException("物料不存在"); } ReportOrderParams itemParam = new ReportOrderParams(); // BS Code itemParam.setTotal_serial(detl.getThreeCode()) // 上报时,默认产品类型是null .setPro_type(material.getProType()) // 上报时,默认产品编码 .setPro_komcode(detl.getMatnr()) // 上报时,默认产品id是供应商编码 .setPro_id(detl.getSuppCode()) // 上报时,默认来源位置是null .setLocation_no(null) // 上报时,默认目标位置是null .setLocation_type(null) // 上报时,默认上报时间是更新时间 .setSj_date(order.getUpdateBy().toString()) // 上报时,默认上报人是更新人 .setSj_emp(order.getUpdateBy$()) // 上报时,默认上报数量是订单数量 .setPick_qty(detl.getQty().intValue()) // 上报时,默认损坏数量是0 .setDamage_qty(0) // 上报时,默认差异数量是0 .setDiff_qty(0); reportOrderParams.add(itemParam); }); orderParam.setDetails(reportOrderParams); XSR response = null; boolean success = false; try { List orderParams = new ArrayList<>(); orderParams.add(orderParam); response = kopenApiService.getInDispatchResult(orderParams); // if (true) { // success = true; // // 修改订单状态 4.完成 ===>> 6.已上报 // if (!orderService.updateSettle(order.getId(), settle, null)) { // throw new CoolException("服务器内部错误,请联系管理员"); // } // } else { // log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + // MesConstant.PAKIN_URL, JSON.toJSONString(pakinParam), response); // throw new CoolException("上报mes系统失败"); // } } catch (Exception e) { log.error("fail", e); // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return FAIL.setMsg(e.getMessage()); } finally { try { // 保存接口日志 apiLogService.save( "上架派工单完成上报", MesConstant.URL + MesConstant.IN_DISPATCH_RESULT, null, "127.0.0.1", JSON.toJSONString(orderParam), JSON.toJSONString(response), success); } catch (Exception e) { log.error("", e); } } } else if (Integer.valueOf(docType.getDocId() + "").equals(OrderTypeEnum.TRANSFER.type)) { // 备货派工单(出库) ReportOrderParam orderParam = new ReportOrderParam(); orderParam.setCompany_id(order.getCstmrName()) .setWms_id(order.getId() + "") .setOrder_no(order.getOrderNo()) .setType(order.getDocType() + "") .setInv_no(order.getNumber()) .setPm_tktid(order.getItemName()) .setUpdate_time(Utils.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss")) .setTarget_location(""); List reportOrderParams = new ArrayList<>(); orderDetls.forEach(detl -> { Mat material = matService.selectByMatnr(detl.getMatnr()); if (Objects.isNull(material)) { throw new CoolException("物料不存在"); } ReportOrderParams itemParam = new ReportOrderParams(); // BS Code itemParam.setTotal_serial(detl.getThreeCode()) // 上报时,默认产品类型是null .setPro_type(material.getProType()) // 上报时,默认产品编码 .setPro_komcode(detl.getMatnr()) // 上报时,默认产品id是供应商编码 .setPro_id(detl.getSuppCode()) // 上报时,默认来源位置是null .setLocation_no(null) // 上报时,默认目标位置是null .setLocation_type(null) // 上报时,默认上报时间是更新时间 .setSj_date(order.getUpdateBy().toString()) // 上报时,默认上报人是更新人 .setSj_emp(order.getUpdateBy$()) // 上报时,默认上报数量是订单数量 .setPick_qty(detl.getQty().intValue()) // 上报时,默认损坏数量是0 .setDamage_qty(0) // 上报时,默认差异数量是0 .setDiff_qty(0); reportOrderParams.add(itemParam); }); orderParam.setDetails(reportOrderParams); XSR response = null; boolean success = false; try { // response = new HttpHandler.Builder() // .setUri(MesConstant.URL) // .setPath(MesConstant.PAKOUT_URL) // .setJson(JSON.toJSONString(pakoutParam)) // .build() // .doPost(); // JSONObject jsonObject = JSON.parseObject(response); // if (jsonObject.getInteger("code").equals(200)) { if (true) { success = true; // 修改订单状态 4.完成 ===>> 6.已上报 if (!orderService.updateSettle(order.getId(), settle, null)) { throw new CoolException("服务器内部错误,请联系管理员"); } } else { log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.OUT_DISPATCH_RESULT, JSON.toJSONString(orderParam), JSON.toJSONString(response)); throw new CoolException("上报KOPEN系统失败"); } } catch (Exception e) { log.error("fail", e); return FAIL.setMsg(e.getMessage()); } finally { try { // 保存接口日志 apiLogService.save( "备货派工单完成上报", MesConstant.URL + MesConstant.OUT_DISPATCH_RESULT, null, "127.0.0.1", JSON.toJSONString(orderParam), JSON.toJSONString(response), success); } catch (Exception e) { log.error("", e); } } } else if (Integer.valueOf(docType.getDocId() + "").equals(OrderTypeEnum.STOCK.type)) { // 备货单反馈 StockUpOrderParams stockUpOrderParams = new StockUpOrderParams(); stockUpOrderParams.setCompany_id(order.getCstmrName()) .setWms_id(order.getId() + "") .setDispatch_no(order.getNumber()); OutOrderParams pubOrderParams = new OutOrderParams(); pubOrderParams.setPick_no(order.getOrderNo()) .setOrder_no(order.getShipCode()) .setCus_id(order.getCstmrName()) .setCus_address(order.getTel()); List reportOrderParams = new ArrayList<>(); orderDetls.forEach(detl -> { Mat material = matService.selectByMatnr(detl.getMatnr()); if (Objects.isNull(material)) { throw new CoolException("物料不存在"); } OrderItemsParam itemParam = new OrderItemsParam(); // 上报时,默认产品类型是null itemParam.setPro_type(material.getProType()); // 上报时,默认产品编码 itemParam.setPro_komcode(material.getMatnr()); // 上报时,默认产品id是供应商编码 itemParam.setPro_id(detl.getSuppCode()); // 上报时,默认上报时间是更新时间 itemParam.setPick_qty(detl.getQty().intValue()); reportOrderParams.add(itemParam); }); pubOrderParams.setPartList(reportOrderParams); List pubOrderParamsList = new ArrayList<>(); pubOrderParamsList.add(pubOrderParams); // 设置派工单明细 stockUpOrderParams.setDetails(pubOrderParamsList); stockUpOrderParams.setUpdate_time(order.getUpdateBy$()); XSR response = null; boolean success = false; try { // response = new HttpHandler.Builder() // .setUri(MesConstant.URL) // .setPath(MesConstant.PAKOUT_URL) // .setJson(JSON.toJSONString(pakoutParam)) // .build() // .doPost(); // JSONObject jsonObject = JSON.parseObject(response); // if (jsonObject.getInteger("code").equals(200)) { if (true) { success = true; // 修改订单状态 4.完成 ===>> 6.已上报 if (!orderService.updateSettle(order.getId(), settle, null)) { throw new CoolException("服务器内部错误,请联系管理员"); } } else { log.error("请求接口失败!!!url:{};request:{};response:{}", MesConstant.URL + MesConstant.OUT_DETAILS_RESULT, JSON.toJSONString(stockUpOrderParams), JSON.toJSONString(response)); throw new CoolException("上报KOPEN系统失败"); } } catch (Exception e) { log.error("fail", e); return FAIL.setMsg(e.getMessage()); } finally { try { // 保存接口日志 apiLogService.save( "备货单完成上报", MesConstant.URL + MesConstant.OUT_DETAILS_RESULT, null, "127.0.0.1", JSON.toJSONString(stockUpOrderParams), JSON.toJSONString(response), success); } catch (Exception e) { log.error("", e); } } } return SUCCESS; } /** * 损溢单完结上报 * * @param order * @return */ @Transactional(rollbackFor = Exception.class) public ReturnT startCheckOrderReport(CheckOrder order) { List adjustParams = new ArrayList<>(); StockAdjustParams adjustParam = new StockAdjustParams(); adjustParam.setCompany_id(order.getCstmrName()) .setWms_id(order.getId() + "") .setType(0 + "") .setCompany_id(order.getCstmrName()); adjustParam.setBsby_no(order.getNumber()) .setResason("") .setReason_name(""); List detls = checkOrderDetlService .selectList(new EntityWrapper().eq("order_id", order.getId())); if (Objects.isNull(detls) || detls.isEmpty()) { throw new CoolException("损溢单详情不存在"); } List items = new ArrayList<>(); detls.forEach(detl -> { Mat material = matService.selectByMatnr(detl.getMatnr()); if (Objects.isNull(material)) { throw new CoolException("物料不存在"); } OrderItemsParam itemParam = new OrderItemsParam(); itemParam.setLocation_no(detl.getZpallet()) .setPro_komcode(material.getMatnr()) .setPro_id(detl.getSuppCode()) .setBsby_qty(detl.getDiffQty().intValue()); items.add(itemParam); }); adjustParam.setDetails(items); adjustParams.add(adjustParam); return SUCCESS; // TODO Auto-generated method stub } }