package com.zy.asrs.task.kingdee.handler;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
import com.core.common.Cools;
|
import com.core.common.DateUtils;
|
import com.core.common.SnowflakeIdWorker;
|
import com.core.exception.CoolException;
|
import com.zy.asrs.entity.*;
|
import com.zy.asrs.entity.kingdee.*;
|
import com.zy.asrs.service.*;
|
import com.zy.asrs.task.AbstractHandler;
|
import com.zy.asrs.task.core.ReturnT;
|
import com.zy.common.utils.HttpHandler;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
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;
|
|
@Service
|
@Slf4j
|
public class InboundOrderHandler extends AbstractHandler<String> {
|
@Value("${erp.address.URL}")
|
//端口
|
private String URL;
|
|
@Value("${erp.address.inaddress}")
|
//单据,商品档案地址
|
private String inaddress;
|
|
@Autowired
|
private ApiLogService apiLogService;
|
|
@Autowired
|
private LoginAuthenticationHandler loginAuthenticationHandler;
|
|
@Autowired
|
private MatService matService;
|
|
@Autowired
|
private TagService tagService;
|
|
@Autowired
|
private DocTypeService docTypeService;
|
|
@Autowired
|
private OrderService orderService;
|
|
@Autowired
|
private OrderDetlService orderDetlService;
|
|
@Autowired
|
private SnowflakeIdWorker snowflakeIdWorker;
|
|
@Transactional
|
public synchronized ReturnT<String> start() {
|
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
//登录金蝶
|
ReturnT<String> start = loginAuthenticationHandler.start();
|
for(int i=0;i<14;i++) {
|
InFormIdParam inFormIdParam = new InFormIdParam();
|
//对应订单类型ID
|
int docName=0;
|
//数量字段
|
String anfme =null;
|
switch (i){
|
//物料档案
|
case 0 : inFormIdParam.setFormId("BD_MATERIAL"); break;
|
//采购入库
|
case 1 : inFormIdParam.setFormId("STK_InStock");docName=13;anfme ="FRealQty"; break;
|
//采购退料
|
case 2 : inFormIdParam.setFormId("PUR_MRB");docName=21;anfme ="FRMREALQTY"; break;
|
//生产领料
|
case 3 : inFormIdParam.setFormId("PRD_PickMtrl");docName=22;anfme ="FActualQty"; break;
|
//生产退料
|
case 4 : inFormIdParam.setFormId("PRD_ReturnMtrl");docName=24;anfme ="FQty"; break;
|
//生产补料
|
case 5 : inFormIdParam.setFormId("PRD_FeedMtrl");docName=25;anfme ="FActualQty"; break;
|
//直接调拨单
|
case 6 : inFormIdParam.setFormId("STK_TransferDirect");docName=26;anfme ="FQty"; break;
|
//盘盈单
|
case 7 : inFormIdParam.setFormId("STK_StockCountGain");docName=28;anfme ="FGainQty"; break;
|
//盘亏单
|
case 8 : inFormIdParam.setFormId("STK_StockCountLoss");docName=29;anfme ="FLossQty"; break;
|
//其他出库
|
case 9 : inFormIdParam.setFormId("STK_MisDelivery");docName=30;anfme ="FQty"; break;
|
//简单生产领料
|
case 10 : inFormIdParam.setFormId("SP_PickMtrl");docName=31;anfme ="FActualQty"; break;
|
//简单生产退料
|
case 11 : inFormIdParam.setFormId("SP_ReturnMtrl");docName=32;anfme ="FQty"; break;
|
//销售出库
|
case 12 : inFormIdParam.setFormId("SAL_OUTSTOCK");docName=14;anfme ="FRealQty"; break;
|
//销售退货
|
case 13 : inFormIdParam.setFormId("SAL_RETURNSTOCK");docName=33;anfme ="FRealQty"; break;
|
}
|
inFormIdParam.setLimit(100);
|
inFormIdParam.setStartRow("0");
|
Date time=new Date();
|
String FDocumentStatus=null;
|
|
//条件和需要获得的结果拼接
|
JSONObject jsonObject = new JSONObject();
|
jsonObject.put("FormId", inFormIdParam.getFormId());
|
jsonObject.put("Limit", inFormIdParam.getLimit());
|
jsonObject.put("StartRow", inFormIdParam.getStartRow());
|
//物料档案查询
|
if(i==0){
|
Tag tag = tagService.selectById(20);
|
//最新时间
|
time=tag.getCreateTime();
|
//物料状态 FUseOrgId .FNumber
|
FDocumentStatus="C";
|
jsonObject.put("FilterString", "FCreateDate>'"+sdf.format(time)+"' and FDocumentStatus='"+FDocumentStatus+"' and FUseOrgId .FNumber='201'");
|
jsonObject.put("FieldKeys", "FName,FNumber,FCreateDate,FSpecification,FDocumentStatus");
|
// FName : 物料名称 ;
|
// FNumber :物料编号;
|
// FCreateDate :物料创建时间;
|
// FSpecification :物料规格;
|
// FDocumentStatus :物料状态
|
}else{
|
//获取最新时间
|
DocType docType= docTypeService.selectById(docName);
|
//单据状态
|
FDocumentStatus="B";
|
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,"+anfme+",FDocumentStatus");
|
// FBillNo :单据编号;
|
// FCreateDate :单据创建时间;
|
// FMaterialId.FNumber : 物料编码;
|
// FMaterialName :物料名称;
|
// FMaterialId.FSpecification:物料规格
|
// anfme : 物料数量
|
// FDocumentStatus :单据状态;
|
|
}
|
JSONObject jsonObject1 = new JSONObject();
|
jsonObject1.put("data", jsonObject);
|
String add = jsonObject1.toJSONString();
|
//上报
|
String response = "";
|
boolean success = false;
|
try {
|
//获取Cookie值
|
HashMap<String, Object> headers = new HashMap<>();
|
headers.put("Cookie", start.getContent());
|
response = new HttpHandler.Builder()
|
.setHeaders(headers)
|
.setUri(URL)
|
.setPath(inaddress)
|
.setJson(add)
|
.build()
|
.doPost();
|
JSONArray data = JSON.parseArray(response);
|
if(data.size()>0) {
|
if (data.getJSONArray(0).get(0).toString().length()<20) {
|
if (i == 0) {
|
for (int j = 0; j < data.size(); j++) {
|
JSONArray jsonArray = data.getJSONArray(j);
|
//生成商品档案
|
//商品编号
|
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.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("服务器内部错误,请联系管理员");
|
} else {
|
log.info("同步新物料[商品编号:{}]", mat.getMatnr());
|
}
|
}
|
}
|
} else {
|
for (int j = 0; j < data.size(); j++) {
|
JSONArray jsonArray = data.getJSONArray(j);
|
Order order = orderService.selectByNo(jsonArray.get(0).toString());
|
if (Cools.isEmpty(order)) {
|
DocType docType = docTypeService.selectById(docName);
|
Date now = new Date();
|
// 单据主档
|
order = new Order(
|
String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空]
|
jsonArray.get(0).toString(), // 订单编号
|
DateUtils.convert(now), // 单据日期
|
docType.getDocId(), // 单据类型
|
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, // 状态
|
9527L, // 添加人员
|
now, // 添加时间
|
9527L, // 修改人员
|
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.update(docType, new EntityWrapper<DocType>().eq("doc_id", docName));
|
if (!orderService.insert(order)) {
|
throw new CoolException("生成单据主档失败,请联系管理员");
|
}
|
}
|
//物料编码
|
Mat mat = matService.selectByMatnr(jsonArray.get(2).toString());
|
if (Cools.isEmpty(mat)) {
|
throw new CoolException(jsonArray.get(2).toString() + "编号商品检索失败,请先添加商品");
|
}
|
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;
|
}
|
DocType docType = docTypeService.selectById(docName);
|
OrderDetl orderDetl = new OrderDetl();
|
orderDetl.sync(mat);
|
orderDetl.setBatch(null);
|
orderDetl.setAnfme(Double.parseDouble(jsonArray.get(5).toString()));
|
orderDetl.setOrderId(order.getId());
|
orderDetl.setOrderNo(order.getOrderNo());
|
orderDetl.setCreateBy(9527L);
|
orderDetl.setCreateTime(new Date());
|
orderDetl.setUpdateBy(9527L);
|
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("生成单据明细失败,请联系管理员");
|
}
|
}
|
}
|
}else {
|
}
|
}else {
|
}
|
} 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);
|
}
|
}
|
}
|
return SUCCESS;
|
}
|
|
}
|