package com.zy.common.web; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.core.common.Cools; import com.core.common.R; import com.core.exception.CoolException; import com.zy.asrs.entity.*; import com.zy.asrs.entity.param.EmptyPlateOutParam; import com.zy.asrs.entity.param.StatusParam; import com.zy.asrs.service.*; import com.zy.common.CodeRes; import com.zy.common.model.LocTypeDto; import com.zy.common.model.StartupDto; import com.zy.common.service.CommonService; import com.zy.common.utils.HttpHandler; import com.zy.common.web.param.SearchLocParam; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; /** * Created by vincent on 2020/10/30 */ @Slf4j @RestController @RequestMapping("/rpc") public class WcsController { @Autowired private WrkMastService wrkMastService; @Autowired private BasDevpService basDevpService; @Autowired private CommonService commonService; @Autowired private WrkDetlService wrkDetlService; @Autowired private LocMastService locMastService; @Autowired private WaitPakinService waitPakinService; @Autowired private LocDetlService locDetlService; @Autowired private RowLastnoService rowLastnoService; @Autowired private WorkService workService; @Autowired private ApiLogService apiLogService; @Value("${wcs-slave.url}") private String url; @Value("${wcs-slave.loc}") private String loc; @PostMapping("/fromwcs/inware-task") @ResponseBody public synchronized Re getLocNo(@RequestBody SearchLocParam param) { log.info("收到WCS入库接口请求====>>入参:{}", param); if (Cools.isEmpty(param.getCargoHeight())) { return Re.error("入出库类型不能为空"); } if (Cools.isEmpty(param.getFromPort())) { return Re.error("源站编号不能为空"); } List waitPakins = null; if (param.getFromPort().equals("100")) { //有空板出库时禁止入库 int wrkCount = wrkMastService.selectCount(new EntityWrapper().eq("io_type",110).eq("sta_no",100).notIn("wrk_sts",7)); if(wrkCount > 0){ return Re.error("有空板出库到100站时时禁止入库"); } if (Cools.isEmpty(param.getBarCode())) { return Re.error("条码不能为空"); } waitPakins = waitPakinService.selectList(new EntityWrapper().eq("zpallet", param.getBarCode())); if (Cools.isEmpty(waitPakins)) { WrkMast wrkMast = wrkMastService.selectByBarcode(param.getBarCode()); if (wrkMast != null && wrkMast.getIoType() == 103) { return Re.parse(CodeRes.PICK_600); } return Re.parse(CodeRes.NO_COMB_700); } int countLoc = locDetlService.selectCount(new EntityWrapper().eq("zpallet", param.getBarCode())); int countWrk = wrkDetlService.selectCount(new EntityWrapper().eq("zpallet", param.getBarCode())); if (countLoc > 0 || countWrk > 0) { return Re.error(CodeRes.EXIST_500); } }else { //有满板出库时禁止空板入库 int wrkCount = wrkMastService.selectCount(new EntityWrapper().eq("sta_no",200).andNew().eq("io_type",101).or().eq("io_type",110)); if(wrkCount > 0){ return Re.error("有满板出库时禁止空板入库"); } int countLoc = wrkMastService.selectCount(new EntityWrapper().eq("io_type", 10)); if (countLoc > 0 ) { return Re.error("已有空板入库任务禁止在入空板"); } } if (Cools.isEmpty(param.getCargoHeight())) { return Re.error("高低检测信号不能为空"); } // 源站点状态检测 BasDevp sourceStaNo = basDevpService.checkSiteStatus(Integer.valueOf(param.getFromPort()), true); sourceStaNo.setLocType1(param.getCargoHeight().shortValue()); LocTypeDto locTypeDto = new LocTypeDto(sourceStaNo); StartupDto dto = null; switch (param.getFromPort()) { case "100"://满托盘入库 assert waitPakins != null; dto = startupFullPutStore(Integer.valueOf(param.getFromPort()), param.getBarCode(), locTypeDto, waitPakins); break; case "200"://空托盘入库 dto = emptyPlateIn(Integer.valueOf(param.getFromPort()), locTypeDto, param.getBarCode()); break; default: break; } log.info("WCS入库接口返参:{},托盘码:{}", dto, param.getBarCode()); return Re.ok(); } @PostMapping("/auto/emptyIn/v1") @ResponseBody public R autoEmptyIn(@RequestBody LocTypeDto locTypeDto) { // 源站点状态检测 BasDevp sourceStaNo = basDevpService.checkSiteStatus(12, true); // 检索库位 StartupDto dto = commonService.getLocNo(10, 12, null, null, null, locTypeDto); Date now = new Date(); // 生成工作档 WrkMast wrkMast = new WrkMast(); wrkMast.setWrkNo(dto.getWorkNo()); wrkMast.setIoTime(now); wrkMast.setWrkSts(1L); wrkMast.setIoPri(13D); wrkMast.setIoType(10); wrkMast.setCrnNo(dto.getCrnNo()); wrkMast.setSourceStaNo(dto.getSourceStaNo()); wrkMast.setStaNo(dto.getStaNo()); wrkMast.setLocNo(dto.getLocNo()); wrkMast.setFullPlt("N"); // 满板:N wrkMast.setPicking("N"); // 拣料 wrkMast.setExitMk("N"); // 退出 wrkMast.setEmptyMk("Y"); // 空板 wrkMast.setLinkMis("Y"); wrkMast.setCtnType(sourceStaNo.getCtnType()); // 容器类型 // 操作人员数据 wrkMast.setAppeTime(now); wrkMast.setModiTime(now); wrkMast.setMemo("自动空托入库"); return wrkMastService.insert(wrkMast) ? R.ok("自动空托入库成功,工作号:" + wrkMast.getWrkNo()) : R.error("生成自动空托入库失败"); } @PostMapping("auto/emptyOut/v1") @ResponseBody public R autoEmptyOut() { LocMast locMast = locMastService.selectOne(new EntityWrapper() .eq("loc_sts", "D")); if (Cools.isEmpty(locMast)) { return R.error("库存没有空板"); } EmptyPlateOutParam emptyPlateOutParam = new EmptyPlateOutParam(); emptyPlateOutParam.setOutSite(12); emptyPlateOutParam.setLocNos(new ArrayList() {{ add(locMast.getLocNo() + ""); }}); WrkMast wrkMast = workService.emptyPlateOut(emptyPlateOutParam); return R.ok(!Cools.isEmpty(wrkMast) ? R.ok("自动空托出库成功,工作号:" + wrkMast.getWrkNo()) : R.error("生成自动空托出库失败")); } /** * 全板入库 */ @Transactional public StartupDto startupFullPutStore(Integer devpNo, String barcode, LocTypeDto locTypeDto, List waitPakins) { // 源站点状态检测 BasDevp sourceStaNo = basDevpService.checkSiteStatus(devpNo, true); // 检索库位 List matnrs = waitPakins.stream().map(WaitPakin::getMatnr).distinct().collect(Collectors.toList()); List batchs = waitPakins.stream().map(WaitPakin::getBatch).distinct().collect(Collectors.toList()); StartupDto dto = commonService.getLocNo(1, devpNo, matnrs.get(0), batchs.get(0), null, locTypeDto); int workNo = dto.getWorkNo(); Date now = new Date(); // 生成工作档 WrkMast wrkMast = new WrkMast(); wrkMast.setWrkNo(workNo); wrkMast.setIoTime(new Date()); wrkMast.setWrkSts(0L); // 工作状态:0.待接收 wrkMast.setIoType(1); // 入出库状态:1.入库 wrkMast.setIoPri(13D); // 优先级 wrkMast.setCrnNo(dto.getCrnNo()); wrkMast.setSourceStaNo(dto.getSourceStaNo()); wrkMast.setStaNo(dto.getStaNo()); wrkMast.setLocNo(dto.getLocNo()); wrkMast.setBarcode(barcode); // 托盘码 wrkMast.setFullPlt("Y"); // 满板:Y wrkMast.setPicking("N"); // 拣料 wrkMast.setExitMk("N"); // 退出 wrkMast.setEmptyMk("N"); // 空板 wrkMast.setLinkMis("Y"); wrkMast.setCtnType(sourceStaNo.getCtnType()); // 容器类型 wrkMast.setMk("N");//是否完成上报wcs wrkMast.setFullPlt("N");//出库空板消除标记 // 操作人员数据 wrkMast.setAppeTime(now); wrkMast.setModiTime(now); boolean res = wrkMastService.insert(wrkMast); if (!res) { throw new CoolException("保存工作档失败"); } // 生成工作档明细 waitPakins.forEach(waitPakin -> { WrkDetl wrkDetl = new WrkDetl(); wrkDetl.sync(waitPakin); wrkDetl.setWrkNo(wrkMast.getWrkNo()); wrkDetl.setIoTime(wrkMast.getIoTime()); wrkDetl.setAppeTime(now); wrkDetl.setModiTime(now); if (!wrkDetlService.insert(wrkDetl)) { throw new CoolException("保存工作明细失败"); } }); // 更新入库通知档 ioStatus ===>> Y Wrapper wrapper = new EntityWrapper() .eq("zpallet", barcode); WaitPakin setParam = new WaitPakin(); setParam.setLocNo(dto.getLocNo()); setParam.setIoStatus("Y"); setParam.setModiTime(now); if (!waitPakinService.update(setParam, wrapper)) { throw new CoolException("更新通知档失败"); } // 更新源站点信息 sourceStaNo.setWrkNo(workNo); sourceStaNo.setModiTime(now); if (!basDevpService.updateById(sourceStaNo)) { throw new CoolException("更新源站失败"); } // 更新目标库位状态 LocMast locMast = locMastService.selectById(dto.getLocNo()); if (locMast.getLocSts().equals("O")) { locMast.setLocSts("S"); // S.入库预约 locMast.setModiTime(now); if (!locMastService.updateById(locMast)) { throw new CoolException("改变库位状态失败"); } } else { throw new CoolException(dto.getLocNo() + "目标库位已被占用"); } return dto; } @Transactional public StartupDto emptyPlateIn(Integer devpNo, LocTypeDto locTypeDto, String barcode) { // 源站点状态检测 BasDevp sourceStaNo = basDevpService.checkSiteStatus(devpNo, true); // 检索库位 StartupDto dto = commonService.getLocNo(10, devpNo, null, null, null, locTypeDto); int workNo = dto.getWorkNo(); // 生成工作档 WrkMast wrkMast = new WrkMast(); wrkMast.setWrkNo(workNo); wrkMast.setIoTime(new Date()); wrkMast.setWrkSts(0L); // 工作状态:0.待接收 wrkMast.setIoType(10); // 入出库状态:10.空板入库 wrkMast.setIoPri(13D); // 优先级 wrkMast.setCrnNo(dto.getCrnNo()); wrkMast.setSourceStaNo(dto.getSourceStaNo()); wrkMast.setStaNo(dto.getStaNo()); wrkMast.setLocNo(dto.getLocNo()); wrkMast.setFullPlt("N"); // 满板 wrkMast.setPicking("N"); // 拣料 wrkMast.setExitMk("N"); // 退出 wrkMast.setEmptyMk("Y"); // 空板 wrkMast.setLinkMis("Y"); wrkMast.setBarcode(barcode); wrkMast.setCtnType(sourceStaNo.getCtnType()); // 容器类型 // 操作人员数据 wrkMast.setAppeTime(new Date()); wrkMast.setModiTime(new Date()); boolean res = wrkMastService.insert(wrkMast); if (!res) { throw new CoolException("保存工作档失败"); } // 更新源站点信息 sourceStaNo.setWrkNo(workNo); sourceStaNo.setModiTime(new Date()); if (!basDevpService.updateById(sourceStaNo)) { throw new CoolException("更新源站失败"); } // 更新目标库位状态 LocMast locMast = locMastService.selectById(dto.getLocNo()); if (locMast.getLocSts().equals("O")) { locMast.setLocSts("S"); // S.入库预约 locMast.setModiTime(new Date()); if (!locMastService.updateById(locMast)) { throw new CoolException("改变库位状态失败"); } } else { throw new CoolException(dto.getLocNo() + "目标库位已被占用"); } return dto; } @PostMapping("/fromwcs/task") @ResponseBody public Re status(@RequestBody StatusParam statusParam) { String[] wrkNo=statusParam.getTaskId().split("-",2); WrkMast wrkMast = wrkMastService.selectOne(new EntityWrapper().eq("wrk_no", Integer.parseInt(wrkNo[0]))); if(Cools.isEmpty(wrkMast)&&statusParam.getTaskStatus()!=8){ return Re.error("没有找到:"+statusParam.getTaskId()+"这条任务"); }else if(Cools.isEmpty(wrkMast)&&statusParam.getTaskStatus()==8){ return Re.ok(); }else if(!Cools.isEmpty(wrkMast)&&statusParam.getTaskStatus()==8&&wrkMast.getIoType()==110){ return Re.ok(); } /** * 任务状态 * 执行过程中 * 的状态。 0- 已 接 * 收 , 1- 任 务 开 * 始,2-取货完成,3- * 任务中断,4-放货 * 完成,8-任务结束 */ if (statusParam.getTaskStatus()==0){ wrkMast.setWrkSts(1L);//1.已接收 }else if (statusParam.getTaskStatus()==1){ wrkMast.setWrkSts(2L);//2.任务开始 }else if (statusParam.getTaskStatus()==2){ wrkMast.setWrkSts(3L);//3.取货完成 }else if (statusParam.getTaskStatus()==3){ wrkMast.setWrkSts(6L);//6.任务中断 } else if (statusParam.getTaskStatus()==4 || statusParam.getTaskStatus()==8){ // wrkMast.setWrkSts(7L);//6.放货完成 if(wrkMast.getIoType()==1 || wrkMast.getIoType() == 10 || wrkMast.getIoType() == 11){ wrkMast.setWrkSts(4L);//4.入库完成 }else if (wrkMast.getIoType()==101 || wrkMast.getIoType()==110){ wrkMast.setWrkSts(14L);//14.已出库未确认 } } wrkMastService.updateById(wrkMast); return Re.ok(); } @GetMapping("loc") public void loc(){ List locMasts=locMastService.selectList(new EntityWrapper()); List> mapList=new ArrayList<>(); for (LocMast locMast : locMasts) { if (locMast.getLev1()!=4){ HashMap map=new HashMap<>(); map.put("warehouse","wzzy"); map.put("posiX",locMast.getRow1()); map.put("posiY",locMast.getBay1()); map.put("posiZ",locMast.getLev1()); map.put("cargoNo",locMast.getLocNo()); map.put("type","0"); mapList.add(map); } } if(mapList.size()>0){ String response = ""; boolean success = false; try { response = new HttpHandler.Builder() .setUri(url) .setPath(loc) .setJson(JSON.toJSONString(mapList)) .build() .doPost(); JSONObject jsonObject = JSON.parseObject(response); if (jsonObject.getInteger("returnStatus").equals(0)) { success = true; } else { log.error("wms同步wcs库位失败!!!url:{};request:{};response:{}", url+"/"+loc, JSON.toJSONString(mapList), response); throw new CoolException("wms下发任务给wcs失败"); } } catch (Exception e) { log.error("fail", e); // TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); throw new CoolException("wms下发任务给wcs失败"); } finally { try { // 保存接口日志 apiLogService.save( "wms同步wcs库位失败", url+"/"+loc, null, "127.0.0.1", JSON.toJSONString(mapList), response, success ); } catch (Exception e) { log.error("", e); } } } } }