package com.zy.asrs.task;
|
|
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.result.OrderResult;
|
import com.zy.asrs.service.*;
|
import com.zy.asrs.service.impl.DocTypeServiceImpl;
|
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.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Service;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.Date;
|
import java.util.HashMap;
|
|
@Service
|
@Slf4j
|
public class CollectOrdersScheduler extends AbstractHandler<String> {
|
@Value("${erp.address.URL}")
|
private String URL;
|
@Value("${erp.address.inaddress}")
|
private String inaddress;
|
|
@Autowired
|
private OrderService orderService;
|
@Autowired
|
private DocTypeService docTypeService;
|
@Autowired
|
private SnowflakeIdWorker snowflakeIdWorker;
|
@Autowired
|
private MatService matService;
|
@Autowired
|
private OrderDetlService orderDetlService;
|
@Autowired
|
private ApiLogService apiLogService;
|
|
/**
|
* 轮询获取入库订单
|
*/
|
@Scheduled(cron = "0/30 * * * * ? ")
|
private synchronized void execute() {
|
// 获取当前日期时间
|
LocalDateTime now = LocalDateTime.now();
|
|
// 减去一个月
|
LocalDateTime oneMonthAgo = now.minusMonths(1);
|
|
// 定义日期时间格式(HH表示24小时制)
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
// 格式化日期时间
|
String formattedDateTime = oneMonthAgo.format(formatter);
|
|
HashMap<String ,Object> hashMap=new HashMap<>();
|
|
HashMap<String, Object> map = new HashMap<>();
|
|
map.put("orgNo", "2");//组织编号
|
// map.put("docNo",0);
|
map.put("startTime", formattedDateTime);//开始时间
|
|
String format = now.format(formatter);
|
map.put("endTime", format);//结束时间
|
|
hashMap.put("condition",map);
|
|
//查询订单
|
String response = "";
|
boolean success = false;
|
try {
|
response = new HttpHandler.Builder()
|
.setUri(URL)
|
.setPath(inaddress)
|
.setJson(JSON.toJSONString(hashMap))
|
.build()
|
.doPost();
|
JSONObject jsonObject = JSON.parseObject(response);
|
if (jsonObject.getInteger("status") == 200) {
|
JSONArray response1 = JSON.parseArray(jsonObject.get("response").toString());
|
for (int j = 0; j < response1.size(); j++) {
|
OrderResult orderResult = response1.getObject(j, OrderResult.class);
|
Order order = orderService.selectByNo(orderResult.getDocNo());//单据编号
|
if (Cools.isEmpty(order)) {
|
Date now1 = new Date();
|
DocType docType = docTypeService.selectOne(new EntityWrapper<DocType>().eq("memo",orderResult.getDocType()));//单据编号
|
// 单据主档
|
order = new Order(
|
String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空]
|
orderResult.getDocNo(), // 订单编号
|
DateUtils.convert(now1), // 单据日期
|
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, // 添加人员
|
now1, // 添加时间
|
9527L, // 修改人员
|
now1, // 修改时间
|
null // 备注
|
);
|
if (!orderService.insert(order)) {
|
throw new CoolException("生成单据失败,请联系管理员");
|
}
|
}
|
//物料编码
|
Mat mat = matService.selectByMatnr(orderResult.getItemNo());
|
if (Cools.isEmpty(mat)) {
|
Mat mat1=new Mat();
|
mat1.setTagId(1L);
|
mat1.setMatnr(orderResult.getItemNo());
|
mat1.setMaktx(orderResult.getItemName());
|
mat1.setSpecs(orderResult.getItemSpec());
|
matService.insert(mat1);
|
// throw new CoolException(orderResult.getItemNo() + "编号商品检索失败,请先添加商品");
|
}
|
OrderDetl orderDetl1 = orderDetlService.selectOne(new EntityWrapper<OrderDetl>()
|
.eq("order_no", orderResult.getDocNo())
|
.eq("matnr", orderResult.getItemNo())
|
.eq("batch", orderResult.getDocLotNo()));//批号
|
if(Cools.isEmpty(orderResult.getDocLotNo())){
|
orderDetl1 = orderDetlService.selectOne(new EntityWrapper<OrderDetl>()
|
.eq("order_no", orderResult.getDocNo())
|
.eq("matnr", orderResult.getItemNo()));
|
}
|
|
if (!Cools.isEmpty(orderDetl1)) {
|
continue;
|
}
|
OrderDetl orderDetl = new OrderDetl();
|
orderDetl.setMatnr(orderResult.getItemNo());
|
orderDetl.setMaktx(orderResult.getItemName());
|
orderDetl.setSpecs(orderResult.getItemSpec());
|
orderDetl.setBatch(orderResult.getDocLotNo());
|
orderDetl.setAnfme(orderResult.getApplyQty());
|
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);
|
|
//保存erp传过来的字段
|
orderDetl.setManu((orderResult.getOrgNo()));//组织编码
|
orderDetl.setModel(orderResult.getDocSeqNo());//项次--行号
|
orderDetl.setColor(orderResult.getDocWarehouseNo());//任务仓库
|
orderDetl.setBrand(orderResult.getDocCellNo());//任务储位
|
orderDetl.setUnit(orderResult.getUnitNo());//单位编号
|
orderDetl.setSku(orderResult.getUnitName());//单位名称
|
orderDetl.setItemNum(orderResult.getSupplierNo());//供应商编码
|
orderDetl.setOrigin(orderResult.getSupplierName());//供应商名称
|
|
if (!orderDetlService.insert(orderDetl)) {
|
throw new CoolException("生成单据明细失败,请联系管理员");
|
}
|
}
|
} else {
|
}
|
} catch (Exception e) {
|
log.error("fail", e);
|
} finally {
|
try {
|
// 保存接口日志
|
apiLogService.save(
|
"轮询数据",
|
URL + inaddress,
|
null,
|
"127.0.0.1",
|
map.toString(),
|
response,
|
success
|
);
|
} catch (Exception e) {
|
log.error("", e);
|
}
|
}
|
}
|
|
}
|