|  |  | 
 |  |  | import com.zy.asrs.service.*; | 
 |  |  | import com.zy.asrs.task.AbstractHandler; | 
 |  |  | import com.zy.asrs.task.core.ReturnT; | 
 |  |  | import com.zy.asrs.utils.OrderInAndOutUtil; | 
 |  |  | import com.zy.erp.kingdee.entity.param.InFormIdParam; | 
 |  |  | import com.zy.erp.kingdee.enums.KingDeeUtilType; | 
 |  |  | import com.zy.common.utils.HttpHandler; | 
 |  |  | import com.zy.erp.kingdee.utils.KingDeeUtil; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  |  | 
 |  |  | import java.text.SimpleDateFormat; | 
 |  |  | import java.time.LocalDateTime; | 
 |  |  | import java.time.format.DateTimeFormatter; | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  |  | 
 |  |  | @Service | 
 |  |  | @Slf4j | 
 |  |  | 
 |  |  |         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | 
 |  |  |         //登录金蝶 | 
 |  |  |         ReturnT<String> start = loginAuthenticationHandler.start(); | 
 |  |  |         for(int i=0;i<docTypeService.selectCount(new EntityWrapper<>())+2;i++) { | 
 |  |  |         for(KingDeeUtilType kingDeeUtilType : KingDeeUtilType.values()) { | 
 |  |  |             InFormIdParam inFormIdParam = new InFormIdParam(); | 
 |  |  |             KingDeeUtilType kingDeeUtilType = KingDeeUtilType.get((short)i); | 
 |  |  |             if (Cools.isEmpty(kingDeeUtilType)){ | 
 |  |  |                 continue; | 
 |  |  |             } | 
 |  |  |             DocType docType = docTypeService.selectOne(new EntityWrapper<DocType>().eq("doc_name", kingDeeUtilType.desc)); | 
 |  |  |             if (Cools.isEmpty(docType) && !kingDeeUtilType.formId.equals("BD_MATERIAL")){ | 
 |  |  |                 docType = docTypeService.selectOrAdd(kingDeeUtilType.desc, kingDeeUtilType.pakIn.equals(1)); | 
 |  |  |             } | 
 |  |  |             inFormIdParam.setFormId(kingDeeUtilType.formId); | 
 |  |  |             inFormIdParam.setLimit(100); | 
 |  |  |             inFormIdParam.setStartRow("0"); | 
 |  |  |             Date time=new Date(); | 
 |  |  |             String FDocumentStatus=null; | 
 |  |  |  | 
 |  |  |             //条件和需要获得的结果拼接 | 
 |  |  | 
 |  |  |             jsonObject.put("Limit", inFormIdParam.getLimit()); | 
 |  |  |             jsonObject.put("StartRow", inFormIdParam.getStartRow()); | 
 |  |  |             //物料档案查询 | 
 |  |  |             if(inFormIdParam.getFormId().equals("BD_MATERIAL")){ | 
 |  |  |                 Tag tag = tagService.selectById(20); | 
 |  |  |                 //最新时间 | 
 |  |  |                 time=tag.getCreateTime(); | 
 |  |  |             if(kingDeeUtilType.formId.equals("BD_MATERIAL")){ | 
 |  |  |                 Tag tag = tagService.selectByName("全部", 1); | 
 |  |  |                 //物料状态 FUseOrgId .FNumber | 
 |  |  |                 FDocumentStatus="C"; | 
 |  |  |                 jsonObject.put("FilterString", "FCreateDate>'"+sdf.format(time)+"' and FDocumentStatus='"+FDocumentStatus+"' and FUseOrgId .FNumber='201'"); | 
 |  |  |                 jsonObject.put("FilterString", "FCreateDate>'"+sdf.format(tag.getUpdateTime())+"' and FDocumentStatus='"+FDocumentStatus+"' and FUseOrgId .FNumber='100'"); | 
 |  |  |                 jsonObject.put("FieldKeys", "FName,FNumber,FCreateDate,FSpecification,FDocumentStatus"); | 
 |  |  |                 // FName : 物料名称 ; | 
 |  |  |                 // FNumber :物料编号; | 
 |  |  | 
 |  |  |                 //获取最新时间 | 
 |  |  |                 //单据状态 | 
 |  |  |                 FDocumentStatus="B"; | 
 |  |  |                 jsonObject.put("FilterString", "FCreateDate>'"+sdf.format(docType.getCreateTime())+"' and FStockId.Fname='立库'"+" and FDocumentStatus='"+FDocumentStatus+"'"); | 
 |  |  |                 jsonObject.put("FilterString", "FCreateDate>'"+sdf.format(docType.getCreateTime())+"'"+" and FStockId.Fname='立库'"+" and FDocumentStatus='"+FDocumentStatus+"'"); | 
 |  |  |                 jsonObject.put("FieldKeys", "FBillNo,FCreateDate,FMaterialId.FNumber,FMaterialId.FName,FMaterialId.FSpecification,"+kingDeeUtilType.anfme+",FDocumentStatus"); | 
 |  |  |                 // FBillNo :单据编号; | 
 |  |  |                 // FCreateDate :单据创建时间; | 
 |  |  | 
 |  |  |                 JSONArray data = JSON.parseArray(response); | 
 |  |  |                 if(data.size()<=0)  continue; | 
 |  |  |                 if (data.getJSONArray(0).get(0).toString().length()>=20) continue; | 
 |  |  |                 if (i == 0) { | 
 |  |  |                 if (kingDeeUtilType.formId.equals("BD_MATERIAL")) {//物料档案 | 
 |  |  |                     Tag tag = tagService.selectByName("全部", 1); | 
 |  |  |                     for (int j = 0; j < data.size(); j++) { | 
 |  |  |                         JSONArray jsonArray = data.getJSONArray(j); | 
 |  |  |                         //对美国时间进行转换 | 
 |  |  |                         Date jsonDate = KingDeeUtil.KingDeeDate(jsonArray.get(2).toString()); | 
 |  |  |                         //生成商品档案 | 
 |  |  |                         //商品编号 | 
 |  |  |                         Mat mat = matService.selectByMatnr(jsonArray.get(1).toString()); | 
 |  |  |                         if (mat == null) { | 
 |  |  |                             mat = new Mat(); | 
 |  |  |                             Tag tag = new Tag(); | 
 |  |  |                             mat.setMatnr(jsonArray.get(1).toString()); | 
 |  |  |                             mat.setMaktx(jsonArray.get(0).toString()); | 
 |  |  |                             mat.setSpecs(jsonArray.get(3).toString());//规格 | 
 |  |  |                             mat.setTagId(20L); | 
 |  |  |                             //对美国时间进行转换 | 
 |  |  |                             DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | 
 |  |  |                             DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; | 
 |  |  |                             LocalDateTime dateTime = LocalDateTime.parse(jsonArray.get(2).toString(), formatter); | 
 |  |  |                             String format = dateFormat.format(dateTime); | 
 |  |  |                             Date date = sdf.parse(format); | 
 |  |  |                             mat.setCreateTime(date);//商品创建时间 | 
 |  |  |                             mat.setTagId(tag.getId()); | 
 |  |  |                             mat.setCreateTime(jsonDate);//商品创建时间 | 
 |  |  |                             mat.setUpdateTime(new Date()); | 
 |  |  |                             mat.setStatus(1); | 
 |  |  |                             //最新抓取商品时间 | 
 |  |  |                             tag.setCreateTime(date); | 
 |  |  |                             tagService.update(tag, new EntityWrapper<Tag>().eq("id", 20)); | 
 |  |  |                             if (!matService.insert(mat)) { | 
 |  |  |                                 throw new CoolException("服务器内部错误,请联系管理员"); | 
 |  |  | //                                throw new CoolException("服务器内部错误,请联系管理员"); | 
 |  |  |                                 callApiLogSaveMat(mat,kingDeeUtilType, "同步新物料出错!!!"+mat, false); | 
 |  |  |                                 log.info("同步新物料出错!!!参数:[{}]、[{}]",mat,jsonArray); | 
 |  |  |                             } else { | 
 |  |  |                                 log.info("同步新物料[商品编号:{}]", mat.getMatnr()); | 
 |  |  |                                 callApiLogSaveMat(mat,kingDeeUtilType, "同步新物料!", true); | 
 |  |  | //                                log.info("同步新物料[商品编号:{}]", mat.getMatnr()); | 
 |  |  |                             } | 
 |  |  |                         }else { | 
 |  |  |                             if (!jsonDate.equals(tag.getUpdateTime())){ | 
 |  |  |                                 callApiLogSaveMat(mat,kingDeeUtilType, "商品编号重复:"+mat.getMatnr(), false); | 
 |  |  |                                 log.info("同步新物料[商品编号:{}],商品编号重复!!!", mat.getMatnr()); | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                         tag.setUpdateTime(jsonDate); | 
 |  |  |                     } | 
 |  |  |                     tagService.updateById(tag); | 
 |  |  |                 } else { | 
 |  |  |                     for (int j = 0; j < data.size(); j++) { | 
 |  |  |                         JSONArray jsonArray = data.getJSONArray(j); | 
 |  |  |                         Order order = orderService.selectByNo(jsonArray.get(0).toString()); | 
 |  |  |                         //最新抓取时间 | 
 |  |  |                         Date jsonDate = KingDeeUtil.KingDeeDate(jsonArray.get(1).toString()); | 
 |  |  |                         docType.setCreateTime(jsonDate); | 
 |  |  |                         Order order = OrderInAndOutUtil.selectByNo(kingDeeUtilType.pakIn == 1, jsonArray.get(0).toString()); | 
 |  |  | //                        Order order = orderService.selectByNo(jsonArray.get(0).toString()); | 
 |  |  |                         if (Cools.isEmpty(order)) { | 
 |  |  |                             Date now = new Date(); | 
 |  |  |                             // 单据主档 | 
 |  |  | 
 |  |  |                                     now,    // 修改时间 | 
 |  |  |                                     null    // 备注 | 
 |  |  |                             ); | 
 |  |  |                             //对美国时间进行转换 | 
 |  |  |                             DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | 
 |  |  |                             DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; | 
 |  |  |                             LocalDateTime dateTime = LocalDateTime.parse(jsonArray.get(1).toString(), formatter); | 
 |  |  |                             String format = dateFormat.format(dateTime); | 
 |  |  |                             Date date = sdf.parse(format); | 
 |  |  |                             //最新抓取时间 | 
 |  |  |                             docType.setCreateTime(date); | 
 |  |  |                             docTypeService.updateById(docType); | 
 |  |  |                             if (!orderService.insert(order)) { | 
 |  |  |                                 throw new CoolException("生成单据主档失败,请联系管理员"); | 
 |  |  |                             } | 
 |  |  |                              OrderInAndOutUtil.insertOrder(kingDeeUtilType.pakIn == 1, order); | 
 |  |  |  | 
 |  |  | //                            if (!orderService.insert(order)) { | 
 |  |  | //                                if (!jsonDate.equals(docType.getCreateTime())){ | 
 |  |  | //                                    callApiLogSaveOrder(order,kingDeeUtilType, "生成单据主档失败,请联系管理员"+order, false); | 
 |  |  | //                                } | 
 |  |  | //                                continue; | 
 |  |  | ////                                throw new CoolException("生成单据主档失败,请联系管理员"); | 
 |  |  | //                            } | 
 |  |  |                         } | 
 |  |  |                         //物料编码 | 
 |  |  |                         Mat mat = matService.selectByMatnr(jsonArray.get(2).toString()); | 
 |  |  |                         if (Cools.isEmpty(mat)) { | 
 |  |  |                             throw new CoolException(jsonArray.get(2).toString() + "编号商品检索失败,请先添加商品"); | 
 |  |  |                             if (!jsonDate.equals(docType.getCreateTime())){ | 
 |  |  |                                 callApiLogSaveOrder(order,kingDeeUtilType, "编号商品检索失败,请先添加商品"+jsonArray.get(2).toString(), false); | 
 |  |  |                             } | 
 |  |  |                             continue; | 
 |  |  |                         } | 
 |  |  |                         OrderDetl orderDetl1 = orderDetlService.selectOne(new EntityWrapper<OrderDetl>().eq("order_no", jsonArray.get(0).toString()).eq("matnr", jsonArray.get(2).toString())); | 
 |  |  |                         if (!Cools.isEmpty(orderDetl1)) { | 
 |  |  | //                        OrderDetl orderDetl1 = orderDetlService.selectOne(new EntityWrapper<OrderDetl>().eq("order_no", jsonArray.get(0).toString()).eq("matnr", jsonArray.get(2).toString())); | 
 |  |  | //                        if (!Cools.isEmpty(orderDetl1)) { | 
 |  |  | //                            continue; | 
 |  |  | //                        } | 
 |  |  |                         Order order1 = OrderInAndOutUtil.selectByNo(kingDeeUtilType.pakIn == 1, jsonArray.get(0).toString()); | 
 |  |  |                         List<OrderDetl> orderDetls = OrderInAndOutUtil.selectByOrderId(kingDeeUtilType.pakIn == 1, order1.getId()); | 
 |  |  |                         boolean sign = false; | 
 |  |  |                         for (OrderDetl orderDetl : orderDetls){ | 
 |  |  |                             if (orderDetl.getMatnr().equals( jsonArray.get(2).toString())){ | 
 |  |  |                                 sign = true; | 
 |  |  |                                 break; | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                         if (sign){ | 
 |  |  |                             continue; | 
 |  |  |                         } | 
 |  |  |                         OrderDetl orderDetl = new OrderDetl(); | 
 |  |  | 
 |  |  |                         orderDetl.setUpdateTime(new Date()); | 
 |  |  |                         orderDetl.setStatus(1); | 
 |  |  |                         orderDetl.setQty(0.0D); | 
 |  |  |                         DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | 
 |  |  |                         DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; | 
 |  |  |                         LocalDateTime dateTime = LocalDateTime.parse(jsonArray.get(1).toString(), formatter); | 
 |  |  |                         String format = dateFormat.format(dateTime); | 
 |  |  |                         Date date = sdf.parse(format); | 
 |  |  |                         //最新抓取时间 | 
 |  |  |                         docType.setCreateTime(date); | 
 |  |  |                         if (!orderDetlService.insert(orderDetl)) { | 
 |  |  |                             throw new CoolException("生成单据明细失败,请联系管理员"); | 
 |  |  |                         } | 
 |  |  | //                        if (!orderDetlService.insert(orderDetl)) { | 
 |  |  | //                            callApiLogSaveOrder(order,kingDeeUtilType, "生成单据明细失败,请联系管理员!订单号:"+order.getOrderNo(), false); | 
 |  |  | ////                            throw new CoolException("生成单据明细失败,请联系管理员"); | 
 |  |  | //                        } | 
 |  |  |                         OrderInAndOutUtil.insertOrderDetl(kingDeeUtilType.pakIn == 1,order,orderDetl); | 
 |  |  |                     } | 
 |  |  |                     docTypeService.updateById(docType); | 
 |  |  |                 } | 
 |  |  |             } catch (Exception e) { | 
 |  |  |                 log.error("fail", e); | 
 |  |  | //            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); | 
 |  |  |                 return FAIL.setMsg(e.getMessage()); | 
 |  |  |             } finally { | 
 |  |  |                 try { | 
 |  |  |                     // 保存接口日志 | 
 |  |  |                     apiLogService.save( | 
 |  |  |                             "轮询数据", | 
 |  |  |                             URL + inaddress, | 
 |  |  |                             null, | 
 |  |  |                             "127.0.0.1", | 
 |  |  |                             add, | 
 |  |  |                             response, | 
 |  |  |                             success | 
 |  |  |                     ); | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     log.error("", e); | 
 |  |  |                 } | 
 |  |  | //                try { | 
 |  |  | //                    // 保存接口日志 | 
 |  |  | //                    apiLogService.save( | 
 |  |  | //                            "轮询数据", | 
 |  |  | //                            URL + inaddress, | 
 |  |  | //                            null, | 
 |  |  | //                            "127.0.0.1", | 
 |  |  | //                            add, | 
 |  |  | //                            response, | 
 |  |  | //                            success | 
 |  |  | //                    ); | 
 |  |  | //                } catch (Exception e) { | 
 |  |  | //                    log.error("", e); | 
 |  |  | //                } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return SUCCESS; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void callApiLogSaveMat(Mat mat, KingDeeUtilType kingDeeUtilType, String response, Boolean bool) { | 
 |  |  |         apiLogService.save("获取"+kingDeeUtilType.desc, kingDeeUtilType.formId, "null", URL, | 
 |  |  |                 "物料编号:" + mat.getMatnr() + "、物料名称:" + mat.getName() + "、类型:" + mat.getTagId$() + "、状态:" + mat.getStatus$(), | 
 |  |  |                 response, bool); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void callApiLogSaveOrder(Order order, KingDeeUtilType kingDeeUtilType, String response, Boolean bool) { | 
 |  |  |         apiLogService.save("获取"+kingDeeUtilType.desc, kingDeeUtilType.formId, "null", URL, | 
 |  |  |                 "单据编号:" + order.getId() +  "、状态:" + order.getStatus$(), | 
 |  |  |                 response, bool); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |