| | |
| | | import com.core.common.R; |
| | | import com.core.common.SnowflakeIdWorker; |
| | | import com.core.exception.CoolException; |
| | | import com.zy.api.controller.params.HmesBackLocParams; |
| | | import com.zy.api.entity.dto.HmesDeviceFreezeStatusDto; |
| | | import com.zy.api.entity.dto.HmesStationFreezeStatusDto; |
| | | import com.zy.api.controller.params.ReceviceTaskParams; |
| | | import com.zy.api.service.HmesApiService; |
| | | import com.zy.api.service.WcsApiService; |
| | |
| | | import com.zy.asrs.service.impl.OrderDetlPakoutServiceImpl; |
| | | import com.zy.asrs.service.impl.OrderPakoutServiceImpl; |
| | | import com.zy.common.model.DetlDto; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class HmesApiServiceImpl implements HmesApiService { |
| | | |
| | | private static final Long HMES_USER_ID = 9995L; |
| | | |
| | | @Autowired |
| | | private BasDeviceService basDeviceService; |
| | |
| | | @Autowired |
| | | private LocDetlServiceImpl locDetlService; |
| | | |
| | | @Autowired |
| | | private WrkMastService wrkMastService; |
| | | |
| | | @Autowired |
| | | private WorkService workService; |
| | | |
| | | |
| | | /** |
| | | * 接收MES穿线任务 |
| | |
| | | // 如果单据不存在则添加;如果单据存在,作业中无法修改,反之则修改单据 |
| | | if (!Cools.isEmpty(order)) { |
| | | if (order.getSettle() > 1L) { |
| | | log.error(param.getOrderNo() + "正在出库,无法修改单据"); |
| | | throw new CoolException(param.getOrderNo() + "正在出库,无法修改单据"); |
| | | } |
| | | orderPakoutService.remove(order.getId()); |
| | | } |
| | | DocType docType = docTypeService.selectOrAdd(param.getBillType(), Boolean.FALSE); |
| | | Date now = new Date(); |
| | | |
| | | // 单据明细档 |
| | | List<DetlDto> list = new ArrayList<>(); |
| | | List<DetlDto> orderDetails = param.getMatList(); |
| | | for (DetlDto detail : orderDetails) { |
| | | DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(),detail.getBrand(),detail.getDevNo(),detail.getStandby2(),detail.getStandby3(),detail.getLineNumber(), |
| | | detail.getBoxType1(),detail.getBoxType2(),detail.getBoxType3(), detail.getAnfme()); |
| | | if (DetlDto.hasLineNumber(list, dto)) { |
| | | DetlDto detlDto = DetlDto.findLineNumber(list, dto.getMatnr(), dto.getBatch(),dto.getBrand(),dto.getDevNo(),dto.getStandby2(),dto.getStandby3(),dto.getLineNumber(), |
| | | dto.getBoxType1(),dto.getBoxType2(),dto.getBoxType3()); |
| | | assert detlDto != null; |
| | | detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme()); |
| | | } else { |
| | | list.add(dto); |
| | | } |
| | | } |
| | | |
| | | //下发的订单明细,查看库存是否有足够的库存,排除机台的库位 |
| | | for (DetlDto detail : list) { |
| | | List<LocDetl> matnr = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("matnr", detail.getMatnr())); |
| | | Double count=0.0; |
| | | for (LocDetl detl : matnr) { |
| | | LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", detl.getLocNo()).eq("loc_sts","F")); |
| | | if (locMast != null) { |
| | | LocAroundBind bLocNo = locAroundBindService.selectOne(new EntityWrapper<LocAroundBind>().eq("b_loc_no", locMast.getLocNo())); |
| | | if (Cools.isEmpty(bLocNo)) { |
| | | count=detl.getAnfme()+count; |
| | | } |
| | | } |
| | | } |
| | | if (count<detail.getAnfme()) { |
| | | return R.error("物料="+detail.getMatnr()+"库存不足无法生成订单"); |
| | | } |
| | | //机台有冻结时禁止呼叫物料 |
| | | BasDevice basDevice = basDeviceService.selectOne(new EntityWrapper<BasDevice>() |
| | | .eq("status", 1) |
| | | .eq("dev_no", detail.getStandby1())); |
| | | if (Objects.isNull(basDevice)) { |
| | | return R.error(detail.getStandby1()+"机台信息不存在或已禁用!!"); |
| | | } |
| | | List<LocAroundBind> binds = locAroundBindService.selectList(new EntityWrapper<LocAroundBind>() |
| | | .eq("dev_no", basDevice.getType()) |
| | | .eq("freeze", 1)); |
| | | if (!Cools.isEmpty(binds)) { |
| | | return R.error(detail.getStandby1()+"机台被冻结无法呼叫物料"); |
| | | } |
| | | } |
| | | // 单据主档 |
| | | order = new OrderPakout( |
| | | String.valueOf(snowflakeIdWorker.nextId()), // 编号[非空] |
| | |
| | | ); |
| | | if (!orderPakoutService.insert(order)) { |
| | | throw new CoolException("生成单据主档失败,请联系管理员"); |
| | | } |
| | | // 单据明细档 |
| | | List<DetlDto> list = new ArrayList<>(); |
| | | List<DetlDto> orderDetails = param.getMatList(); |
| | | for (DetlDto detail : orderDetails) { |
| | | DetlDto dto = new DetlDto(detail.getMatnr(), detail.getBatch(),detail.getBrand(),detail.getDevNo(),detail.getStandby2(),detail.getStandby3(),detail.getLineNumber(), |
| | | detail.getBoxType1(),detail.getBoxType2(),detail.getBoxType3(), detail.getAnfme()); |
| | | if (DetlDto.hasLineNumber(list, dto)) { |
| | | DetlDto detlDto = DetlDto.findLineNumber(list, dto.getMatnr(), dto.getBatch(),dto.getBrand(),dto.getDevNo(),dto.getStandby2(),dto.getStandby3(),dto.getLineNumber(), |
| | | dto.getBoxType1(),dto.getBoxType2(),dto.getBoxType3()); |
| | | assert detlDto != null; |
| | | detlDto.setAnfme(detlDto.getAnfme() + detail.getAnfme()); |
| | | } else { |
| | | list.add(dto); |
| | | } |
| | | } |
| | | //下发的订单明细,查看库存是否有足够的库存,排除机台的库位 |
| | | for (DetlDto detail : list) { |
| | | List<LocDetl> matnr = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("matnr", detail.getMatnr())); |
| | | Double count=0.0; |
| | | for (LocDetl detl : matnr) { |
| | | LocMast locMast = locMastService.selectOne(new EntityWrapper<LocMast>().eq("loc_no", detl.getLocNo()).eq("loc_sts","F")); |
| | | if (locMast != null) { |
| | | LocAroundBind bLocNo = locAroundBindService.selectOne(new EntityWrapper<LocAroundBind>().eq("b_loc_no", locMast.getLocNo())); |
| | | if (Cools.isEmpty(bLocNo)) { |
| | | count=detl.getAnfme()+count; |
| | | } |
| | | } |
| | | } |
| | | if (count<detail.getAnfme()) { |
| | | return R.error("物料="+detail.getMatnr()+"库存不足无法生成订单"); |
| | | } |
| | | } |
| | | for (DetlDto detlDto : list) { |
| | | Mat mat = matService.selectByMatnr(detlDto.getMatnr()); |
| | |
| | | }); |
| | | return R.ok(s+"成功 !!"); |
| | | } |
| | | |
| | | @Override |
| | | public R getDeviceFreezeStatus() { |
| | | List<BasDevice> devices = basDeviceService.selectList(new EntityWrapper<BasDevice>() |
| | | .eq("status", 1) |
| | | .orderBy("dev_no", true)); |
| | | if (Cools.isEmpty(devices)) { |
| | | return R.ok(Collections.emptyList()); |
| | | } |
| | | |
| | | List<LocAroundBind> binds = locAroundBindService.selectList(new EntityWrapper<LocAroundBind>() |
| | | .orderBy("dev_no", true) |
| | | .orderBy("order_no", true) |
| | | .orderBy("id", true)); |
| | | Map<String, List<LocAroundBind>> bindMap = new HashMap<>(); |
| | | if (!Cools.isEmpty(binds)) { |
| | | bindMap = binds.stream() |
| | | .filter(bind -> !Cools.isEmpty(bind.getDevNo())) |
| | | .collect(Collectors.groupingBy(LocAroundBind::getDevNo, LinkedHashMap::new, Collectors.toList())); |
| | | } |
| | | |
| | | List<HmesDeviceFreezeStatusDto> result = new ArrayList<>(); |
| | | for (BasDevice device : devices) { |
| | | List<LocAroundBind> deviceBinds = bindMap.getOrDefault(device.getType(), Collections.emptyList()); |
| | | List<HmesStationFreezeStatusDto> stationList = deviceBinds.stream() |
| | | .sorted(Comparator.comparing(LocAroundBind::getOrderNo, Comparator.nullsLast(Integer::compareTo)) |
| | | .thenComparing(LocAroundBind::getId, Comparator.nullsLast(Long::compareTo))) |
| | | .map(this::buildStationFreezeStatus) |
| | | .collect(Collectors.toList()); |
| | | boolean frozen = stationList.stream().anyMatch(station -> "Y".equals(station.getFreeze())); |
| | | result.add(new HmesDeviceFreezeStatusDto() |
| | | .setDevNo(device.getDevNo()) |
| | | .setDevType(device.getType()) |
| | | .setFreeze(toFreezeFlag(frozen ? 1 : 0)) |
| | | .setStationList(stationList)); |
| | | } |
| | | return R.ok(result); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R backLoc(HmesBackLocParams params) { |
| | | Integer backType = parseBackType(params.getBackType()); |
| | | if (backType == null) { |
| | | return R.error("回库类型[backType]仅支持 EMPTY/4/空桶/空板 或 SURPLUS/5/余料"); |
| | | } |
| | | |
| | | WrkMast mast = findBackLocWrkMast(params); |
| | | if (Objects.isNull(mast)) { |
| | | return R.error("未查询到可回库的任务档"); |
| | | } |
| | | if (!Objects.equals(mast.getWrkSts(), 15L)) { |
| | | return R.error("当前任务状态不能执行回库!"); |
| | | } |
| | | |
| | | workService.updateWrkIsSuplus(String.valueOf(mast.getWrkNo()), HMES_USER_ID, backType); |
| | | return workService.backLocOperation(String.valueOf(mast.getWrkNo()), HMES_USER_ID); |
| | | } |
| | | |
| | | private HmesStationFreezeStatusDto buildStationFreezeStatus(LocAroundBind bind) { |
| | | return new HmesStationFreezeStatusDto() |
| | | .setLocNo(bind.getBlocNo()) |
| | | .setOrderNo(bind.getOrderNo()) |
| | | .setLocType(bind.getLocType()) |
| | | .setFreeze(toFreezeFlag(bind.getFreeze())); |
| | | } |
| | | |
| | | private String toFreezeFlag(Integer freeze) { |
| | | return Objects.equals(freeze, 1) ? "Y" : "N"; |
| | | } |
| | | |
| | | private Integer parseBackType(String backType) { |
| | | if (Cools.isEmpty(backType)) { |
| | | return null; |
| | | } |
| | | String normalized = backType.trim().toUpperCase(Locale.ROOT); |
| | | if ("4".equals(normalized) || "EMPTY".equals(normalized) || "EMPTY_BUCKET".equals(normalized) |
| | | || "EMPTY_PLATE".equals(normalized) || "空桶".equals(backType.trim()) |
| | | || "空板".equals(backType.trim())) { |
| | | return 4; |
| | | } |
| | | if ("5".equals(normalized) || "SURPLUS".equals(normalized) || "SUPLUS".equals(normalized) |
| | | || "LEFTOVER".equals(normalized) || "余料".equals(backType.trim())) { |
| | | return 5; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private WrkMast findBackLocWrkMast(HmesBackLocParams params) { |
| | | if (!Cools.isEmpty(params.getLocNo())) { |
| | | List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>() |
| | | .eq("loc_no", params.getLocNo()) |
| | | .orderBy("appe_time", false) |
| | | .orderBy("wrk_no", false)); |
| | | WrkMast matched = pickLatestCompletedWrkMast(wrkMasts); |
| | | if (matched != null) { |
| | | return matched; |
| | | } |
| | | } |
| | | if (!Cools.isEmpty(params.getWorkNo())) { |
| | | return wrkMastService.selectById(params.getWorkNo()); |
| | | } |
| | | List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>() |
| | | .eq("barcode", params.getBarcode()) |
| | | .orderBy("appe_time", false) |
| | | .orderBy("wrk_no", false)); |
| | | return pickLatestCompletedWrkMast(wrkMasts); |
| | | } |
| | | |
| | | private WrkMast pickLatestCompletedWrkMast(List<WrkMast> wrkMasts) { |
| | | if (Cools.isEmpty(wrkMasts)) { |
| | | return null; |
| | | } |
| | | for (WrkMast wrkMast : wrkMasts) { |
| | | if (Objects.equals(wrkMast.getWrkSts(), 15L)) { |
| | | return wrkMast; |
| | | } |
| | | } |
| | | return wrkMasts.get(0); |
| | | } |
| | | } |