|  |  |  | 
|---|
|  |  |  | package com.zy.asrs.controller; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
|---|
|  |  |  | import com.core.annotations.ManagerAuth; | 
|---|
|  |  |  | import com.core.common.BaseRes; | 
|---|
|  |  |  | import com.core.common.Cools; | 
|---|
|  |  |  | import com.core.common.R; | 
|---|
|  |  |  | import com.zy.asrs.entity.BasDevp; | 
|---|
|  |  |  | import com.zy.asrs.entity.LocDetl; | 
|---|
|  |  |  | import com.zy.asrs.entity.OrderDetl; | 
|---|
|  |  |  | import com.zy.asrs.entity.result.StoPreTab; | 
|---|
|  |  |  | import com.zy.asrs.entity.*; | 
|---|
|  |  |  | import com.zy.asrs.service.*; | 
|---|
|  |  |  | import com.zy.common.model.DetlDto; | 
|---|
|  |  |  | import com.zy.common.model.LocDto; | 
|---|
|  |  |  | import com.zy.common.model.TaskDto; | 
|---|
|  |  |  | import com.zy.common.web.BaseController; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestBody; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestParam; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RestController; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Created by vincent on 2022/3/26 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocDetlService locDetlService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocMastService locMastService; | 
|---|
|  |  |  | private PakoutService pakoutService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private StaDescService staDescService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private WorkService workService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private BasDevpService basDevpService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ManPakOutService manPakOutService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ManLocDetlService manLocDetlService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    @PostMapping("/out/pakout/preview/auth") | 
|---|
|  |  |  | //    public R pakoutPreview(@RequestBody List<Long> ids) { | 
|---|
|  |  |  | //        if (Cools.isEmpty(ids)) { | 
|---|
|  |  |  | //            return R.parse(BaseRes.PARAM); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        List<OrderDetl> orderDetls = orderDetlService.selectBatchIds(ids); | 
|---|
|  |  |  | //        Set<DetlDto> detlDtos = new HashSet<>(); | 
|---|
|  |  |  | //        for (OrderDetl orderDetl : orderDetls) { | 
|---|
|  |  |  | //            if (DetlDto.hasList(detlDtos, orderDetl)) { | 
|---|
|  |  |  | //                DetlDto detlDto = DetlDto.find(detlDtos, orderDetl.getMatnr(), orderDetl.getBatch()); | 
|---|
|  |  |  | //                assert detlDto != null; | 
|---|
|  |  |  | //                detlDto.setAnfme(detlDto.getAnfme() + orderDetl.getAnfme()); | 
|---|
|  |  |  | //            } else { | 
|---|
|  |  |  | //                detlDtos.add(new DetlDto(orderDetl.getMatnr(), orderDetl.getBatch(), orderDetl.getAnfme())); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        List<LocDto> locDtos = new ArrayList<>(); | 
|---|
|  |  |  | //        for (DetlDto detlDto : detlDtos) { | 
|---|
|  |  |  | //            double issued = Optional.ofNullable(detlDto.getAnfme()).orElse(0.0D) ; | 
|---|
|  |  |  | //            List<LocDetl> locDetls = locDetlService.queryStock(detlDto.getMatnr(), detlDto.getBatch(), null); | 
|---|
|  |  |  | //            for (LocDetl locDetl : locDetls) { | 
|---|
|  |  |  | //                if (issued > 0) { | 
|---|
|  |  |  | //                    locDtos.add(new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getBatch(), issued>=locDetl.getAnfme()?locDetl.getAnfme():issued)); | 
|---|
|  |  |  | //                    // 剩余待出数量递减 | 
|---|
|  |  |  | //                    issued = issued - locDetl.getAnfme(); | 
|---|
|  |  |  | //                } else { | 
|---|
|  |  |  | //                    break; | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        return R.ok().add(locDtos); | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  | @PostMapping("/out/pakout/orderDetlIds/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R pakoutOrderDetlIds(@RequestParam Long orderId) throws InterruptedException { | 
|---|
|  |  |  | Thread.sleep(200); | 
|---|
|  |  |  | return R.ok().add(orderDetlService.selectByOrderId(orderId).stream().map(OrderDetl::getId).distinct().collect(Collectors.toList())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/out/pakout/preview/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<OrderDetl> orderDetls = orderDetlService.selectBatchIds(ids); | 
|---|
|  |  |  | List<LocDto> locDtos = new ArrayList<>(); | 
|---|
|  |  |  | for (OrderDetl o: orderDetls) { | 
|---|
|  |  |  | if (o.getAnfme().equals(o.getWorkQty())){ | 
|---|
|  |  |  | return R.error("出库的明细中有已出库物料"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Set<String> exist = new HashSet<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (OrderDetl orderDetl : orderDetls) { | 
|---|
|  |  |  | double issued = Optional.ofNullable(orderDetl.getAnfme()).orElse(0.0D); | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStock(orderDetl.getMatnr(), orderDetl.getBatch(), null); | 
|---|
|  |  |  | for (LocDetl locDetl : locDetls) { | 
|---|
|  |  |  | BigDecimal a1 = new BigDecimal(orderDetl.getAnfme()).setScale(2,BigDecimal.ROUND_HALF_UP); | 
|---|
|  |  |  | BigDecimal b1 = new BigDecimal(orderDetl.getQty()).setScale(2,BigDecimal.ROUND_HALF_UP); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | double issued = Optional.of(orderDetl.getAnfme() - orderDetl.getWorkQty()).orElse(0.0D); | 
|---|
|  |  |  | if (issued <= 0.0D) { continue; } | 
|---|
|  |  |  | List<ManLocDetl> manLocDetls = manLocDetlService.queryStock(orderDetl.getMatnr(),orderDetl.getBatch()); | 
|---|
|  |  |  | for (ManLocDetl locDetl : manLocDetls) { | 
|---|
|  |  |  | if (issued > 0) { | 
|---|
|  |  |  | LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(), | 
|---|
|  |  |  | issued >= locDetl.getAnfme() ? locDetl.getAnfme() : issued); | 
|---|
|  |  |  | List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(), issued >= locDetl.getAnfme() ? 101 : 103); | 
|---|
|  |  |  | boolean sign = false; | 
|---|
|  |  |  | if (issued > locDetl.getAnfme() || locDetl.getAnfme().equals(issued)){ | 
|---|
|  |  |  | sign=true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LocDto locDto = new LocDto(locDetl.getHostId(),locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(), | 
|---|
|  |  |  | sign ? locDetl.getAnfme() : issued,"平库"); | 
|---|
|  |  |  | List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(),  sign? 101 : 103); | 
|---|
|  |  |  | //                    LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(), | 
|---|
|  |  |  | //                            locDetl.getAnfme(), issued < locDetl.getAnfme()? issued : locDetl.getAnfme()); | 
|---|
|  |  |  | //                    //速腾只有全板,所以只能101全板出库 | 
|---|
|  |  |  | //                    List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locDetl.getLocNo(), 101); | 
|---|
|  |  |  | locDto.setStaNos(staNos); | 
|---|
|  |  |  | locDtos.add(locDto); | 
|---|
|  |  |  | exist.add(locDetl.getLocNo()); | 
|---|
|  |  |  | BigDecimal iss=new BigDecimal(Double.toString(issued)).setScale(2,BigDecimal.ROUND_HALF_UP); | 
|---|
|  |  |  | BigDecimal an=new BigDecimal(Double.toString(locDetl.getAnfme())).setScale(2,BigDecimal.ROUND_HALF_UP); | 
|---|
|  |  |  | // 剩余待出数量递减 | 
|---|
|  |  |  | issued = issued - locDetl.getAnfme(); | 
|---|
|  |  |  | issued=iss.subtract(an).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (issued > 0) { | 
|---|
|  |  |  | LocDto locDto = new LocDto(null, orderDetl.getMatnr(), orderDetl.getMaktx(), orderDetl.getBatch(), orderDetl.getOrderNo(), issued); | 
|---|
|  |  |  | LocDto locDto = new LocDto(null,null, orderDetl.getMatnr(), orderDetl.getMaktx(), orderDetl.getBatch(), orderDetl.getOrderNo(), issued,"平库"); | 
|---|
|  |  |  | //                LocDto locDto = new LocDto(null, orderDetl.getMatnr(), orderDetl.getMaktx(), orderDetl.getBatch(), orderDetl.getOrderNo(), 0.0,issued); | 
|---|
|  |  |  | locDto.setLack(Boolean.TRUE); | 
|---|
|  |  |  | locDtos.add(locDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/out/pakout/auth") | 
|---|
|  |  |  | @ManagerAuth(memo = "订单出库") | 
|---|
|  |  |  | public synchronized R pakout(@RequestBody List<LocDto> locDtos) { | 
|---|
|  |  |  | if (Cools.isEmpty(locDtos)) { | 
|---|
|  |  |  | return R.parse(BaseRes.PARAM); | 
|---|
|  |  |  | public synchronized R pakout(@RequestBody List<LocDto> locDtos) throws InterruptedException { | 
|---|
|  |  |  | int[][] is=new int[locDtos.size()][2]; | 
|---|
|  |  |  | for (int i=0;i<locDtos.size();i++){ | 
|---|
|  |  |  | LocDto locDto=locDtos.get(locDtos.size()-1-i); | 
|---|
|  |  |  | if (locDto.getLocNo()==null || locDto.getManu().equals("平库")){ | 
|---|
|  |  |  | is[i][0]=1; | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | is[i][0]=0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | is[i][1]=locDtos.size()-1-i; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (int[] i:is){ | 
|---|
|  |  |  | if (i[0]==1){ | 
|---|
|  |  |  | locDtos.remove(i[1]); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.isEmpty(locDtos)) { | 
|---|
|  |  |  | return R.ok("立库参数为空,请生成拣货单,平库拣货出库"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | boolean lack = true; | 
|---|
|  |  |  | for (LocDto locDto : locDtos) { | 
|---|
|  |  |  | if (!locDto.isLack()) { | 
|---|
|  |  |  | lack = false; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (lack) { | 
|---|
|  |  |  | return R.error("库存不足"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Thread.sleep(1000L); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<TaskDto> taskDtos = new ArrayList<>(); | 
|---|
|  |  |  | // 根据 (库位 & 出库站) 分组; 理想状态:一组为一次出库任务 | 
|---|
|  |  |  | for (LocDto locDto : locDtos) { | 
|---|
|  |  |  | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/out/pakout2/auth") | 
|---|
|  |  |  | @ManagerAuth(memo = "生成拣货单") | 
|---|
|  |  |  | public synchronized R pakout2(@RequestBody List<LocDto> locDtos) throws InterruptedException { | 
|---|
|  |  |  | int[][] is = new int[locDtos.size()][2]; | 
|---|
|  |  |  | List<String> matnrs=new ArrayList<>(); | 
|---|
|  |  |  | for (int i = 0; i < locDtos.size(); i++) { | 
|---|
|  |  |  | LocDto locDto = locDtos.get(locDtos.size() - 1 - i); | 
|---|
|  |  |  | if (locDto.getLocNo() == null) { | 
|---|
|  |  |  | is[i][0] = 1; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | is[i][0] = 0; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | is[i][1] = locDtos.size() - 1 - i; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<LocDto> locDtos1=new ArrayList<>(); | 
|---|
|  |  |  | for (int[] i : is) { | 
|---|
|  |  |  | if (i[0] == 2) { | 
|---|
|  |  |  | if (!matnrs.contains(locDtos.get(i[1]).getMatnr())){ | 
|---|
|  |  |  | matnrs.add(locDtos.get(i[1]).getMatnr()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | locDtos1.add(locDtos.get(i[1])); | 
|---|
|  |  |  | locDtos.remove(i[1]); | 
|---|
|  |  |  | }else if (i[0]==1){ | 
|---|
|  |  |  | LocDto locDto = locDtos.get(i[1]); | 
|---|
|  |  |  | locDto.setLocNo("库存不足"); | 
|---|
|  |  |  | locDtos.remove(i[1]); | 
|---|
|  |  |  | locDtos.add(locDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (String matnr:matnrs){ | 
|---|
|  |  |  | Double a = 0.0; | 
|---|
|  |  |  | int b = 0; | 
|---|
|  |  |  | for (int i = 0; i < locDtos1.size(); i++){ | 
|---|
|  |  |  | if (locDtos1.get(i).getMatnr().equals(matnr)){ | 
|---|
|  |  |  | if (a.equals(0.0)){ | 
|---|
|  |  |  | b=i; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | a=a+locDtos1.get(i).getAnfme(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | LocDto locDto = locDtos1.get(b); | 
|---|
|  |  |  | locDto.setLocNo("立库"); | 
|---|
|  |  |  | locDto.setAnfme(a); | 
|---|
|  |  |  | locDtos.add(locDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (Cools.isEmpty(locDtos)) { | 
|---|
|  |  |  | return R.error("平库参数为空,没有生成拣货单"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String uuid = String.valueOf(System.currentTimeMillis()); | 
|---|
|  |  |  | boolean lack = true; | 
|---|
|  |  |  | for (LocDto locDto : locDtos) { | 
|---|
|  |  |  | if (!locDto.isLack()) { | 
|---|
|  |  |  | lack = false; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (lack) { | 
|---|
|  |  |  | return R.error("库存不足"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (LocDto locDto:locDtos){ | 
|---|
|  |  |  | ManLocDetl manLocDetl = manLocDetlService.selectItem(locDto.getLocNo(), locDto.getMatnr(), locDto.getBatch()); | 
|---|
|  |  |  | if (!Cools.isEmpty(manLocDetl) && !(locDto.getAnfme()>manLocDetl.getAnfme())) { | 
|---|
|  |  |  | manLocDetl.setStatus(0); | 
|---|
|  |  |  | if (manLocDetlService.update(manLocDetl, new EntityWrapper<ManLocDetl>().eq("loc_no", manLocDetl.getLocNo()).eq("matnr",manLocDetl.getMatnr()))) { | 
|---|
|  |  |  | if (addPakOUT(locDto,uuid).equals(R.ok())) { | 
|---|
|  |  |  | Order order = orderService.selectByNo(locDto.getOrderNo()); | 
|---|
|  |  |  | if (Cools.isEmpty(order)){ | 
|---|
|  |  |  | return R.error("查询订单失败,请联系管理员"+locDto.getOrderNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (order.getSettle()==1){ | 
|---|
|  |  |  | if (!orderService.updateSettle(order.getId(),2L,null)){ | 
|---|
|  |  |  | return R.error("修改订单状态失败,请联系管理员"+locDto.getOrderNo()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | OrderDetl orderDetl = orderDetlService.selectOne(new EntityWrapper<OrderDetl>().eq("order_no", locDto.getOrderNo()).eq("matnr", locDto.getMatnr())); | 
|---|
|  |  |  | if (Cools.isEmpty(orderDetl)){ | 
|---|
|  |  |  | return R.error("查询订单明细失败,请联系管理员"+locDto.getOrderNo()+locDto.getMatnr()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | orderDetl.setWorkQty(orderDetl.getWorkQty()+locDto.getAnfme()); | 
|---|
|  |  |  | if (!orderDetlService.update(orderDetl,new EntityWrapper<OrderDetl>().eq("order_no", locDto.getOrderNo()).eq("matnr", locDto.getMatnr()))){ | 
|---|
|  |  |  | return R.error("修改订单明细失败,请联系管理员"+locDto.getOrderNo()+locDto.getMatnr()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return R.error("添加拣货明细失败,请联系管理员"+locDto.getOrderNo()+locDto.getMatnr()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else if (locDto.getLocNo().equals("立库")){ | 
|---|
|  |  |  | addPakOUT(locDto,uuid).equals(R.ok()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (LocDto locDto:locDtos){ | 
|---|
|  |  |  | if (locDto.getLocNo().equals("库存不足")){ | 
|---|
|  |  |  | addPakOUT(locDto,uuid).equals(R.ok()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private R addPakOUT(LocDto locDto,String uuid){ | 
|---|
|  |  |  | ManPakOut manPakOut=new ManPakOut(); | 
|---|
|  |  |  | manPakOut.setDocId(locDto.getHostId()); | 
|---|
|  |  |  | manPakOut.setWrkNo(locDto.getOrderNo()+"-"+System.currentTimeMillis()); | 
|---|
|  |  |  | manPakOut.setWrkSts((long)1); | 
|---|
|  |  |  | manPakOut.setAnfme(locDto.getAnfme()); | 
|---|
|  |  |  | manPakOut.setLocNo(locDto.getLocNo()); | 
|---|
|  |  |  | manPakOut.setMatnr(locDto.getMatnr()); | 
|---|
|  |  |  | manPakOut.setMaktx(locDto.getMaktx()); | 
|---|
|  |  |  | manPakOut.setDocNum(locDto.getOrderNo()); | 
|---|
|  |  |  | manPakOut.setCount(0.0); | 
|---|
|  |  |  | manPakOut.setStatus(0); | 
|---|
|  |  |  | manPakOut.setCreateTime(new Date()); | 
|---|
|  |  |  | manPakOut.setBatch(locDto.getBatch()); | 
|---|
|  |  |  | manPakOut.setUpdateTime(new Date()); | 
|---|
|  |  |  | manPakOut.setUuid(uuid); | 
|---|
|  |  |  | if (Cools.isEmpty(manPakOutService.selectOne(new EntityWrapper<ManPakOut>().eq("doc_num",locDto.getOrderNo())))){ | 
|---|
|  |  |  | manPakOut.setName(locDto.getMaktx()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | if (!manPakOutService.insert(manPakOut)){ | 
|---|
|  |  |  | return R.error("添加拣货明细失败,请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | return R.error("添加拣货明细失败,请联系管理员"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|