package zy.cloud.wms.common.service;
|
|
import com.core.common.Cools;
|
import com.core.common.SnowflakeIdWorker;
|
import com.core.exception.CoolException;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import zy.cloud.wms.common.model.MatnrDto;
|
import zy.cloud.wms.common.model.OrderStoDto;
|
import zy.cloud.wms.common.utils.VersionUtils;
|
import zy.cloud.wms.manager.entity.*;
|
import zy.cloud.wms.manager.service.*;
|
|
import java.util.*;
|
|
/**
|
* Created by vincent on 2021/3/1
|
*/
|
@Slf4j
|
@Service("mainService")
|
public class MainService {
|
|
@Autowired
|
private MatService matService;
|
@Autowired
|
private LocDetlService locDetlService;
|
@Autowired
|
private PakoutService pakoutService;
|
@Autowired
|
private SnowflakeIdWorker snowflakeIdWorker;
|
@Autowired
|
private OrderService orderService;
|
|
@Transactional
|
public List<StoPreTab> stockOutPreview(OrderStoDto orderStoDto, Long hostId) {
|
if (Cools.isEmpty(orderStoDto) || Cools.isEmpty(orderStoDto.getDtos())) {
|
throw new CoolException("数据异常,请联系管理员");
|
}
|
List<StoPreTab> res = new ArrayList<>();
|
// 检查库存是否足够
|
// locDetlService.checkLocDetlCount(orderStoDto.getDtos());
|
|
for (MatnrDto matnrDto : orderStoDto.getDtos()) {
|
// 判断物料是否存在
|
Mat mat = matService.selectByMatnr(hostId, matnrDto.getMatnr());
|
if (null == mat) {
|
throw new CoolException(matnrDto.getMatnr() + "物料尚未更新。" + orderStoDto.getOrderNo() +"单据因此中断!");
|
}
|
|
Double sumAnfme = Optional.ofNullable(locDetlService.selectCountByMatnr(mat.getMatnr(), hostId)).orElse(0.0D);
|
double lack = 0.0D;
|
// 缺货
|
if (sumAnfme < matnrDto.getCount()) {
|
lack = matnrDto.getCount() - sumAnfme;
|
// 视图对象
|
StoPreTab tab = new StoPreTab();
|
tab.setTitle(mat.getMaktx() + "(" + mat.getMatnr() + ")");
|
tab.setMatnr(mat.getMatnr());
|
tab.setMaktx(mat.getMaktx());
|
tab.setAnfme(matnrDto.getCount());
|
tab.setLocNo("缺货");
|
tab.setTotal(lack);
|
tab.setReduce(lack);
|
tab.setRemQty(0.0D);
|
tab.setPrior(false);
|
tab.setPrior$("×");
|
tab.setType(0);
|
res.add(tab);
|
}
|
|
// 查询存有当前物料的货位
|
List<LocDetl> locDetls = locDetlService.findOfSort(hostId, mat.getMatnr());
|
double issued = Optional.of(matnrDto.getCount() - lack).orElse(0.0D) ;
|
double anfme = issued;
|
for (LocDetl locDetl : locDetls) {
|
if (issued > 0) {
|
// 视图对象
|
StoPreTab tab = new StoPreTab();
|
tab.setTitle(mat.getMaktx() + "(" + mat.getMatnr() + ")");
|
tab.setMatnr(mat.getMatnr());
|
tab.setMaktx(mat.getMaktx());
|
tab.setAnfme(matnrDto.getCount());
|
|
tab.setLocNo(locDetl.getLocNo());
|
tab.setNodeId(locDetl.getNodeId());
|
tab.setTotal(locDetl.getAnfme());
|
tab.setReduce(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
|
tab.setRemQty(tab.getTotal() - tab.getReduce());
|
tab.setPrior(locDetlService.isPrior(locDetl.getNodeId(), mat.getMatnr()));
|
tab.setPrior$(tab.getPrior()?"✔":"×");
|
tab.setType(1);
|
res.add(tab);
|
// 剩余待出数量递减
|
issued = issued - locDetl.getAnfme();
|
}
|
}
|
|
}
|
res.sort(new Comparator<StoPreTab>() {
|
@Override
|
public int compare(StoPreTab o1, StoPreTab o2) {
|
// return o1.getMatnr().length() - o2.getMatnr().length();
|
return (int) (o1.getAnfme() - o2.getAnfme());
|
}
|
});
|
return res;
|
}
|
|
@Transactional
|
public void stockOutProcess(OrderStoDto orderStoDto, Long hostId) {
|
if (Cools.isEmpty(orderStoDto) || Cools.isEmpty(orderStoDto.getDtos())) {
|
return;
|
}
|
Order order = orderService.selectByOrderNo(orderStoDto.getOrderNo(), hostId);
|
if (order == null) {
|
throw new CoolException(orderStoDto.getOrderNo() + "单据不存在");
|
}
|
Date now = new Date();
|
// 检查库存是否足够
|
locDetlService.checkLocDetlCount(orderStoDto.getDtos(), hostId);
|
|
for (MatnrDto matnrDto : orderStoDto.getDtos()) {
|
// 判断物料是否存在
|
Mat mat = matService.selectByMatnr(hostId, matnrDto.getMatnr());
|
if (null == mat) {
|
throw new CoolException(matnrDto.getMatnr() + "物料尚未更新。" + matnrDto.getCount() +"单据因此中断!");
|
}
|
// 查询存有当前物料的货位
|
List<LocDetl> locDetls = locDetlService.findOfSort(hostId, mat.getMatnr());
|
double issued = Optional.ofNullable(matnrDto.getCount()).orElse(0.0D) ;
|
for (LocDetl locDetl : locDetls) {
|
if (issued > 0) {
|
// 保存出库通知单
|
Pakout pakout = new Pakout();
|
pakout.setHostId(hostId);
|
pakout.setWrkSts(1L);
|
pakout.setAnfme(issued>=locDetl.getAnfme()?locDetl.getAnfme():issued);
|
pakout.setZpallet(locDetl.getZpallet());
|
pakout.setLocNo(locDetl.getLocNo());
|
pakout.setNodeId(locDetl.getNodeId());
|
pakout.setWrkNo(String.valueOf(snowflakeIdWorker.nextId()));
|
VersionUtils.setPakout(pakout, mat);
|
pakout.setDocId(order.getDocType()); // 单据类型
|
pakout.setDocNum(order.getOrderNo()); // 单据编号
|
pakout.setCreateTime(now);
|
pakout.setUpdateTime(now);
|
pakout.setStatus(1);
|
if (!pakoutService.insert(pakout)) {
|
throw new CoolException("保存出库通知单失败");
|
}
|
if (issued>=locDetl.getAnfme()) {
|
// // 删除库存明细
|
// if (!locDetlService.removeStock(locDetl.getNodeId(), mat.getMatnr())) {
|
// throw new CoolException("删除库存明细失败");
|
// }
|
} else {
|
// // 修改库存明细数量
|
// if (!locDetlService.reduceStock(locDetl.getNodeId(), mat.getMatnr(), issued)) {
|
// throw new CoolException("修改库存明细数量失败");
|
// }
|
}
|
// 剩余待出数量递减
|
issued = issued - locDetl.getAnfme();
|
}
|
}
|
// 修改单据状态
|
if (!orderService.updateSettle(orderStoDto.getOrderNo(), 2L, hostId)) {
|
throw new CoolException("修改单据状态失败");
|
}
|
}
|
}
|
|
}
|