package com.zy.common.service;
|
|
import com.alibaba.fastjson.JSON;
|
import com.core.common.Cools;
|
import com.core.common.DateUtils;
|
import com.zy.asrs.entity.DocLog;
|
import com.zy.asrs.entity.MatCode;
|
import com.zy.asrs.entity.RequestLog;
|
import com.zy.asrs.service.DocLogService;
|
import com.zy.asrs.service.MatCodeService;
|
import com.zy.asrs.service.RequestLogService;
|
import com.zy.common.model.BillDto;
|
import com.zy.common.model.UploadBill;
|
import com.zy.common.model.UploadBillDetail;
|
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 java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
/**
|
* Created by vincent on 2021/3/23
|
*/
|
@Slf4j
|
@Service("erpService")
|
public class ErpService {
|
|
@Value("${erp.url}")
|
private String url;
|
|
public static final String UPLOAD_BILL = "cM/basis/uploadBill";
|
|
@Autowired
|
private MatCodeService matCodeService;
|
@Autowired
|
private RequestLogService requestLogService;
|
@Autowired
|
private DocLogService docLogService;
|
|
/**
|
* 单据上报
|
*/
|
public Boolean uploadBill(List<BillDto> dtos, Integer docId, String docNumber){
|
try {
|
Date now = new Date();
|
if (Cools.isEmpty(dtos)) {
|
return false;
|
}
|
|
// 日志
|
List<DocLog> docLogs = new ArrayList<>();
|
|
UploadBill uploadBill = new UploadBill();
|
uploadBill.setNumber(docNumber);
|
uploadBill.setBillDate(DateUtils.convert(new Date(), DateUtils.yyyyMMdd_F));
|
uploadBill.setBTypeID("SHHT");
|
uploadBill.setKTypeID("宏挺仓库");
|
uploadBill.setVchType(docId);
|
if (docId == 9) {
|
uploadBill.setDifAtype(22);
|
} else if (docId == 14) {
|
uploadBill.setDifAtype(23);
|
}
|
uploadBill.setSummary("采购单 - " + DateUtils.convert(new Date(), "yyyy-MM-dd HH:mm"));
|
List<UploadBillDetail> detail = new ArrayList<>();
|
uploadBill.setDetail(detail);
|
for (BillDto dto : dtos) {
|
MatCode mat = matCodeService.selectById(dto.getMatnr());
|
UploadBillDetail detl = new UploadBillDetail();
|
detl.setUserCode(dto.getMatnr());
|
detl.setQty(dto.getQty());
|
detl.setPrice(0);
|
detl.setUnit(mat==null?"暂无":"箱");
|
detail.add(detl);
|
|
|
// 日志
|
docLogs.add(new DocLog(
|
docNumber, // 单据编号
|
docId.longValue(), // 单据类型
|
null, // 单据名称
|
dto.getMatnr(), // 商品编号
|
dto.getQty(), // 上报数量
|
1, // 结果
|
now, // 上报时间
|
null // 备注
|
));
|
}
|
log.warn(JSON.toJSONString(uploadBill));
|
String response = new HttpHandler.Builder()
|
.setUri(url)
|
.setPath(UPLOAD_BILL)
|
.setJson(JSON.toJSONString(uploadBill))
|
.build()
|
.doPost();
|
try {
|
|
for (DocLog docLog : docLogs) {
|
docLogService.insert(docLog);
|
}
|
|
// 日志记录
|
RequestLog logInfo = new RequestLog();
|
logInfo.setName("单据上传,单据类型" + docId);
|
logInfo.setRequest(JSON.toJSONString(uploadBill)); // 入参
|
logInfo.setResponse(response); // 出参
|
logInfo.setCreateTime(new Date());
|
requestLogService.insert(logInfo);
|
} catch (Exception e) {
|
log.error(e.getMessage());
|
}
|
|
if (!Cools.isEmpty(response)) {
|
log.warn(response);
|
Result result = JSON.parseObject(response, Result.class);
|
if (result.getCode() != 1) {
|
return false;
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
return false;
|
}
|
return Boolean.TRUE;
|
}
|
|
|
}
|