|  |  |  | 
|---|
|  |  |  | package com.zy.ints.task.scheduler; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.zy.asrs.service.MatService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
|---|
|  |  |  | import com.core.common.Cools; | 
|---|
|  |  |  | import com.core.common.DateUtils; | 
|---|
|  |  |  | import com.core.common.SnowflakeIdWorker; | 
|---|
|  |  |  | import com.zy.asrs.entity.*; | 
|---|
|  |  |  | import com.zy.asrs.service.*; | 
|---|
|  |  |  | import com.zy.ints.entity.ErpDetTb; | 
|---|
|  |  |  | import com.zy.ints.entity.ErpDetTbBak; | 
|---|
|  |  |  | import com.zy.ints.erp.ErpSqlServer; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.scheduling.annotation.Scheduled; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Value("${erp.enabled}") | 
|---|
|  |  |  | private Boolean erpEnabled; | 
|---|
|  |  |  | @Value("${erp.enabledErpDetTb}") | 
|---|
|  |  |  | private Boolean erpEnabledErpDetTb; | 
|---|
|  |  |  | @Value("${erp.useName.ip}") | 
|---|
|  |  |  | private String ip; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ErpSqlServer erpSqlServer; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MatService matService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private OrderService orderService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private OrderDetlService orderDetlService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SnowflakeIdWorker snowflakeIdWorker; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DocTypeService docTypeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ApiLogService apiLogService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取入出库订单信息 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Scheduled(cron = "${erp.refreshtime}") | 
|---|
|  |  |  | public void InOrOutOrder() { | 
|---|
|  |  |  | if (!erpEnabled) return; | 
|---|
|  |  |  | String sqlSelecterpDetTb = "select bill_no as billNo,prd_no as prdNo,iokindid,add_id as addId,qty,prd_mark as prdMark,wh,billdate,status,temp1,temp2,temp3 from erp_det_tb where 1=1"; | 
|---|
|  |  |  | String sqlUpDateerpDetTbOne = "update erp_det_tb set status=1 where prd_no="; | 
|---|
|  |  |  | String sqlUpDateerpDetTbTwo = "update erp_det_tb set status=2 where prd_no="; | 
|---|
|  |  |  | if (!erpEnabledErpDetTb) return; | 
|---|
|  |  |  | String sqlSelectErpDetTb = "select BIL_NO as billNo,prd_no as prdNo,ITM as itm,ADD_ID as addId2,QTY as qty,PRD_MARK as prdMark,WH as wh,STATUS as status,Temp1 as temp1,Temp2 as temp2,Temp3 as temp3 from ERP_DETTB where 1=1"; | 
|---|
|  |  |  | String sqlUpDateErpDetTbOne = "update ERP_DETTB set STATUS=1 where 1=1 and PRD_NO="; | 
|---|
|  |  |  | String sqlUpDateErpDetTbTwo = "update ERP_DETTB set STATUS=2 "; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | List<ErpDetTb> erpDetTbs = erpSqlServer.select(sqlSelecterpDetTb, ErpDetTb.class); | 
|---|
|  |  |  | if (erpDetTbs.size() > 0){ | 
|---|
|  |  |  | for (ErpDetTb erpDetTb:erpDetTbs){ | 
|---|
|  |  |  | List<ErpDetTb> erpDetTbs = erpSqlServer.select(sqlSelectErpDetTb, ErpDetTb.class); | 
|---|
|  |  |  | if (erpDetTbs.size() > 0) { | 
|---|
|  |  |  | for (ErpDetTb erpDetTb : erpDetTbs) { | 
|---|
|  |  |  | Date now = new Date(); | 
|---|
|  |  |  | Integer status = erpDetTb.getStatus(); | 
|---|
|  |  |  | if (status == 0){ | 
|---|
|  |  |  | if (status == 0) { | 
|---|
|  |  |  | Mat mat = matService.selectByMatnr(erpDetTb.getPrdNo()); | 
|---|
|  |  |  | if (Cools.isEmpty(mat)) { | 
|---|
|  |  |  | if(Cools.isEmpty(erpDetTb.getTemp1())){ | 
|---|
|  |  |  | erpSqlServer.update(sqlUpDateErpDetTbTwo + ",Temp1='品号信息在立库不存在,请先添加商品!!!'" + "where 1=1 " | 
|---|
|  |  |  | +" and PRD_NO=" + "'" + erpDetTb.getPrdNo() + "'" | 
|---|
|  |  |  | +" and ITM=" + "'" + erpDetTb.getItm() + "'" | 
|---|
|  |  |  | +" and BIL_NO=" + "'" + erpDetTb.getBillNo() + "'"); | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "品号信息在立库不存在,请先添加商品!!!", false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | Order order = orderService.selectByNo(erpDetTb.getBillNo()); | 
|---|
|  |  |  | if (Cools.isEmpty(order)) { | 
|---|
|  |  |  | order = new Order( | 
|---|
|  |  |  | String.valueOf(snowflakeIdWorker.nextId()),    // 编号[非空] | 
|---|
|  |  |  | erpDetTb.getBillNo(),    // 订单编号 | 
|---|
|  |  |  | DateUtils.convert(now),    // 单据日期 | 
|---|
|  |  |  | null,    // 单据类型 | 
|---|
|  |  |  | null,    // 项目编号 | 
|---|
|  |  |  | null,    // | 
|---|
|  |  |  | null,    // 调拨项目编号 | 
|---|
|  |  |  | null,    // 初始票据号 | 
|---|
|  |  |  | null,    // 票据号 | 
|---|
|  |  |  | null,    // 客户编号 | 
|---|
|  |  |  | null,    // 客户 | 
|---|
|  |  |  | null,    // 联系方式 | 
|---|
|  |  |  | null,    // 操作人员 | 
|---|
|  |  |  | null,    // 合计金额 | 
|---|
|  |  |  | null,    // 优惠率 | 
|---|
|  |  |  | null,    // 优惠金额 | 
|---|
|  |  |  | null,    // 销售或采购费用合计 | 
|---|
|  |  |  | null,    // 实付金额 | 
|---|
|  |  |  | null,    // 付款类型 | 
|---|
|  |  |  | null,    // 业务员 | 
|---|
|  |  |  | null,    // 结算天数 | 
|---|
|  |  |  | null,    // 邮费支付类型 | 
|---|
|  |  |  | null,    // 邮费 | 
|---|
|  |  |  | null,    // 付款时间 | 
|---|
|  |  |  | null,    // 发货时间 | 
|---|
|  |  |  | null,    // 物流名称 | 
|---|
|  |  |  | null,    // 物流单号 | 
|---|
|  |  |  | 1L,    // 订单状态 | 
|---|
|  |  |  | 1,    // 状态 | 
|---|
|  |  |  | 9999L,    // 添加人员 | 
|---|
|  |  |  | now,    // 添加时间 | 
|---|
|  |  |  | 9999L,    // 修改人员 | 
|---|
|  |  |  | now,    // 修改时间 | 
|---|
|  |  |  | null    // 备注 | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | String InAndOut = erpDetTb.getAddId2().equals(2) ? "(出库)" : "(入库)"; | 
|---|
|  |  |  | DocType docType = docTypeService.selectOrAdd("ERP订单"+InAndOut, erpDetTb.getAddId2().equals(1)); | 
|---|
|  |  |  | if (!Cools.isEmpty(docType)) { | 
|---|
|  |  |  | order.setDocType(docType.getDocId()); | 
|---|
|  |  |  | if (!orderService.insert(order)) { | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "订单主档添加失败!!", false); | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "订单主档添加成功!!", true); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Order order1 = orderService.selectByNo(erpDetTb.getBillNo()); | 
|---|
|  |  |  | if (!Cools.isEmpty(order1)) { | 
|---|
|  |  |  | //                                OrderDetl orderDetl = orderDetlService.selectItem(order1.getId(), erpDetTb.getBillNo(), erpDetTb.getPrdMark()); | 
|---|
|  |  |  | OrderDetl orderDetl = orderDetlService.selectOne(new EntityWrapper<OrderDetl>().eq("order_no",erpDetTb.getBillNo()).eq("matnr",erpDetTb.getPrdNo())); | 
|---|
|  |  |  | if (Cools.isEmpty(orderDetl)) { | 
|---|
|  |  |  | orderDetl = new OrderDetl(); | 
|---|
|  |  |  | orderDetl.setOrderId(order1.getId()); | 
|---|
|  |  |  | orderDetl.setOrderNo(erpDetTb.getBillNo()); | 
|---|
|  |  |  | orderDetl.setAnfme(erpDetTb.getQty()); | 
|---|
|  |  |  | orderDetl.setQty(0.0D); | 
|---|
|  |  |  | orderDetl.setMatnr(erpDetTb.getPrdNo()); | 
|---|
|  |  |  | orderDetl.setMaktx(mat.getMaktx()); | 
|---|
|  |  |  | orderDetl.setBatch(erpDetTb.getPrdMark()); | 
|---|
|  |  |  | orderDetl.setSpecs(mat.getSpecs()); | 
|---|
|  |  |  | orderDetl.setUnit(mat.getUnit()); | 
|---|
|  |  |  | orderDetl.setOrigin(erpDetTb.getItm().toString()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }else if (status == 1){ | 
|---|
|  |  |  | orderDetl.setStatus(1); | 
|---|
|  |  |  | orderDetl.setCreateBy(9999L); | 
|---|
|  |  |  | orderDetl.setCreateTime(now); | 
|---|
|  |  |  | orderDetl.setUpdateBy(9999L); | 
|---|
|  |  |  | orderDetl.setUpdateTime(now); | 
|---|
|  |  |  | orderDetl.setMemo(erpDetTb.getWh()); | 
|---|
|  |  |  | if (orderDetlService.insert(orderDetl)) { | 
|---|
|  |  |  | erpSqlServer.update(sqlUpDateErpDetTbOne + "'" + erpDetTb.getPrdNo() + "'"); | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "接收ERP下发订单接收成功!!", true); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (Cools.isEmpty(erpDetTb.getTemp2())){ | 
|---|
|  |  |  | erpSqlServer.update(sqlUpDateErpDetTbTwo + ",temp2='添加订单明细档失败!!!'" + "where 1=1 " | 
|---|
|  |  |  | +" and PRD_NO=" + "'" + erpDetTb.getPrdNo() + "'" | 
|---|
|  |  |  | +" and ITM=" + "'" + erpDetTb.getItm() + "'" | 
|---|
|  |  |  | +" and BIL_NO=" + "'" + erpDetTb.getBillNo() + "'"); | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "ERP下发订单接收失败!!", false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else if (!erpDetTb.getItm().toString().equals(orderDetl.getOrigin())){ | 
|---|
|  |  |  | orderDetl.setAnfme(orderDetl.getAnfme()+erpDetTb.getQty()); | 
|---|
|  |  |  | EntityWrapper<OrderDetl> wrapper = new EntityWrapper<>(); | 
|---|
|  |  |  | wrapper.eq("id", orderDetl.getId()); | 
|---|
|  |  |  | if (orderDetlService.update(orderDetl,wrapper)) { | 
|---|
|  |  |  | erpSqlServer.update(sqlUpDateErpDetTbOne + "'" + erpDetTb.getPrdNo() + "'"); | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "接收ERP下发订单接收成功!!", true); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (Cools.isEmpty(erpDetTb.getTemp2())){ | 
|---|
|  |  |  | erpSqlServer.update(sqlUpDateErpDetTbTwo + ",temp2='更新订单明细档失败!!!'" + "where 1=1 " | 
|---|
|  |  |  | +" and PRD_NO=" + "'" + erpDetTb.getPrdNo() + "'" | 
|---|
|  |  |  | +" and ITM=" + "'" + erpDetTb.getItm() + "'" | 
|---|
|  |  |  | +" and BIL_NO=" + "'" + erpDetTb.getBillNo() + "'"); | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "ERP下发订单接收失败!!", false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | if (Cools.isEmpty(erpDetTb.getTemp3())){ | 
|---|
|  |  |  | erpSqlServer.update(sqlUpDateErpDetTbTwo + ",temp3='更新订单明细档失败!!!订单明细已存在!!'" + "where 1=1 " | 
|---|
|  |  |  | +" and PRD_NO=" + "'" + erpDetTb.getPrdNo() + "'" | 
|---|
|  |  |  | +" and ITM=" + "'" + erpDetTb.getItm() + "'" | 
|---|
|  |  |  | +" and BIL_NO=" + "'" + erpDetTb.getBillNo() + "'"); | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "ERP下发订单接收失败!!", false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else if (status == 1) { | 
|---|
|  |  |  | String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now); | 
|---|
|  |  |  | HashMap<String, Object> condition = new HashMap<>(); | 
|---|
|  |  |  | condition.put("BIL_NO", "'" + erpDetTb.getBillNo() + "'"); | 
|---|
|  |  |  | condition.put("PRD_NO", "'" + erpDetTb.getPrdNo() + "'"); | 
|---|
|  |  |  | condition.put("ITM", "'" + erpDetTb.getItm() + "'"); | 
|---|
|  |  |  | condition.put("ADD_ID", "'" + erpDetTb.getAddId2() + "'"); | 
|---|
|  |  |  | condition.put("QTY", "'" + erpDetTb.getQty() + "'"); | 
|---|
|  |  |  | condition.put("PRD_MARK", "'" + erpDetTb.getPrdMark() + "'"); | 
|---|
|  |  |  | condition.put("WH", "'" + erpDetTb.getWh() + "'"); | 
|---|
|  |  |  | condition.put("BIL_DATE", "'" + format + "'"); | 
|---|
|  |  |  | condition.put("STATUS", "'" + erpDetTb.getStatus() + "'"); | 
|---|
|  |  |  | condition.put("Temp1", "'" + erpDetTb.getTemp1() + "'"); | 
|---|
|  |  |  | condition.put("Temp2", "'" + erpDetTb.getTemp2() + "'"); | 
|---|
|  |  |  | condition.put("Temp3", "'" + erpDetTb.getTemp3() + "'"); | 
|---|
|  |  |  | erpSqlServer.insert(ErpDetTbBak.class, condition); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }else if (status == 2){ | 
|---|
|  |  |  | //                        callApiLogSave(erpDetTb, "ERP_DETTB_BAK", "ERP下发订单备份成功!!", true); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | HashMap<String, String> condition2 = new HashMap<>(); | 
|---|
|  |  |  | condition2.put("BIL_NO", "'" + erpDetTb.getBillNo() + "'"); | 
|---|
|  |  |  | condition2.put("PRD_NO", "'" + erpDetTb.getPrdNo() + "'"); | 
|---|
|  |  |  | condition2.put("ITM", "'" + erpDetTb.getItm() + "'"); | 
|---|
|  |  |  | //                        condition2.put("PRD_MARK", "'" + erpDetTb.getPrdMark() + "'"); | 
|---|
|  |  |  | erpSqlServer.delete(ErpDetTb.class, condition2); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                        callApiLogSave(erpDetTb, "ERP_DETTB", "ERP下发订单备份后删除成功!!", true); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (Cools.isEmpty(erpDetTb.getTemp1()) && Cools.isEmpty(erpDetTb.getTemp2()) && Cools.isEmpty(erpDetTb.getTemp3())){ | 
|---|
|  |  |  | erpSqlServer.update(sqlUpDateErpDetTbTwo + ",Temp1='接收ERP下发订单失败!!!状态异常!!!请ERP、立库双方协助解决。'" + "where 1=1 " | 
|---|
|  |  |  | +" and PRD_NO=" + "'" + erpDetTb.getPrdNo() + "'" | 
|---|
|  |  |  | +" and ITM=" + "'" + erpDetTb.getItm() + "'" | 
|---|
|  |  |  | +" and BIL_NO=" + "'" + erpDetTb.getBillNo() + "'"); | 
|---|
|  |  |  | callApiLogSave(erpDetTb, "ERP_DETTB", "接收ERP下发订单失败!!!状态异常!!!", false); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | Date date = new Date(); | 
|---|
|  |  |  | log.error(date + ": 表名:ERP_DETTB :" + e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void callApiLogSave(ErpDetTb erpDetTb, String tableName, String response, Boolean bool) { | 
|---|
|  |  |  | apiLogService.save("ERP下发订单信息", tableName, "null", ip, | 
|---|
|  |  |  | "订单号:" + erpDetTb.getBillNo() + "、品号:" + erpDetTb.getPrdNo() + "、货品特征:" + erpDetTb.getPrdMark() + "、状态:" + erpDetTb.getStatus$(), | 
|---|
|  |  |  | response, bool); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|