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.exception.CoolException;
|
import com.zy.asrs.entity.*;
|
import com.zy.asrs.entity.result.OrderResult;
|
import com.zy.asrs.service.*;
|
import com.zy.common.utils.HttpHandler;
|
import com.zy.system.entity.Config;
|
import com.zy.system.service.ConfigService;
|
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.Service;
|
|
import java.util.*;
|
|
@Service
|
@Slf4j
|
public class ERPReportScheduler extends AbstractHandler<String> {
|
|
@Value("${erp.address.URL}")
|
private String URL;
|
@Value("${erp.address.outReportAddress}")
|
private String outReportAddress;
|
@Value("${erp.address.inReportAddress}")
|
private String inReportAddress;
|
@Autowired
|
private OrderDetlService orderDetlService;
|
@Autowired
|
private ApiLogService apiLogService;
|
@Autowired
|
private WrkMastService wrkMastService;
|
@Autowired
|
private ConfigService configService;
|
@Autowired
|
private WrkDetlService wrkDetlService;
|
@Autowired
|
private OrderService orderService;
|
@Autowired
|
private DocTypeService docTypeService;
|
|
|
/**
|
* 单个任务上报erp
|
*/
|
@Scheduled(cron = "0/10 * * * * ? ")
|
private synchronized void execute() {
|
//查找所有任务档任务状态为40ERP上报中的任务
|
List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 40));
|
|
//是否需要上报ERP
|
Config config = configService.selectOne(new EntityWrapper<Config>().eq("code", "newErpReport"));
|
if(!Cools.isEmpty(config)&&config.getValue().equals("Y")&&!Cools.isEmpty(wrkMasts)){
|
|
for(WrkMast wrkMast:wrkMasts){
|
List<WrkDetl> wrkDetls = wrkDetlService.selectByWrkNo(wrkMast.getWrkNo());
|
if(wrkDetls!=null&&wrkDetls.size()>0){
|
List<LinkedHashMap<String,Object>> datas=new ArrayList<>();
|
for(WrkDetl wrkDetl:wrkDetls){
|
OrderDetl orderDetl= orderDetlService.selectItem(wrkDetl.getOrderNo(),wrkDetl.getMatnr(),wrkDetl.getBatch());
|
if(orderDetl==null){
|
log.error("orderNo={},matnr={},batch={},没有查询到订单明细",wrkDetl.getOrderNo(),wrkDetl.getMatnr(),wrkDetl.getBatch());
|
continue;
|
}
|
Order order= orderService.selectByNo(wrkDetl.getOrderNo());
|
if(order==null){
|
log.error("orderNo={},matnr={},batch={},没有查询到订单",wrkDetl.getOrderNo(),wrkDetl.getMatnr(),wrkDetl.getBatch());
|
continue;
|
}
|
DocType docType = docTypeService.selectById(order.getDocType());//单据编号
|
LinkedHashMap<String,Object> map=new LinkedHashMap<>();
|
map.put("orgNo",orderDetl.getManu());//组织编码
|
map.put("docNo",orderDetl.getOrderNo());
|
map.put("docType",docType.getMemo());
|
map.put("docSeqNo",orderDetl.getModel());
|
map.put("itemNo",orderDetl.getMatnr());
|
map.put("qty",wrkDetl.getAnfme());
|
map.put("unitNo",orderDetl.getUnit());
|
map.put("warehouseNo","02201");
|
map.put("cellNo",orderDetl.getBrand());
|
map.put("combinationLotNo",String.valueOf(orderDetl.getBatch()));
|
map.put("barcode",wrkDetl.getMatnr());
|
datas.add(map);
|
}
|
String path= "";
|
String work= "";
|
if(wrkMast.getIoType()<100){
|
path=inReportAddress;
|
work="入库";
|
}else{
|
path=outReportAddress;
|
work="出库";
|
}
|
path=outReportAddress;
|
HashMap<String,Object> map=new HashMap<>();
|
map.put("data",datas);
|
//上报ERP
|
String response = "";
|
boolean success = false;
|
try {
|
response = new HttpHandler.Builder()
|
.setUri(URL)
|
.setPath(path)
|
.setJson(JSON.toJSONString(map))
|
.build()
|
.doPost();
|
JSONObject jsonObject = JSON.parseObject(response);
|
if (jsonObject.getInteger("status") == 200) {
|
if(wrkMast.getIoType()<100){
|
wrkMast.setWrkSts(10L);//入库转历史档
|
}else{
|
wrkMast.setWrkSts(18L);//出库转历史档
|
}
|
wrkMastService.updateById(wrkMast);
|
} else {
|
log.error("任务号={},上报失败",wrkMast.getWrkNo());
|
}
|
} catch (Exception e) {
|
log.error("fail", e);
|
} finally {
|
try {
|
// 保存接口日志
|
apiLogService.save(
|
"上报"+work+"任务结果给ERP",
|
URL + path,
|
null,
|
"127.0.0.1",
|
map.toString(),
|
response,
|
success
|
);
|
} catch (Exception e) {
|
log.error("", e);
|
}
|
}
|
}else {
|
log.error("任务号:{},没有任务明细",wrkMast.getWrkNo());
|
}
|
}
|
}
|
|
}
|
}
|