|  |  |  | 
|---|
|  |  |  | import com.zy.common.model.LocDto; | 
|---|
|  |  |  | import com.zy.common.model.TaskDto; | 
|---|
|  |  |  | import com.zy.common.web.BaseController; | 
|---|
|  |  |  | import com.zy.system.entity.User; | 
|---|
|  |  |  | import org.slf4j.Logger; | 
|---|
|  |  |  | import org.slf4j.LoggerFactory; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestBody; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.annotation.Resource; | 
|---|
|  |  |  | import java.math.BigDecimal; | 
|---|
|  |  |  | import java.text.SimpleDateFormat; | 
|---|
|  |  |  | import java.time.LocalDate; | 
|---|
|  |  |  | import java.time.ZoneId; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | public class OutController extends BaseController { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static final Logger log = LoggerFactory.getLogger(OutController.class); | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private OrderService orderService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PostMapping("/out/pakout/preview/auth") | 
|---|
|  |  |  | @ManagerAuth | 
|---|
|  |  |  | public R pakoutPreview(@RequestBody List<Long> ids) { | 
|---|
|  |  |  | public R pakoutPreview(@RequestBody HashMap<String,Object> param) { | 
|---|
|  |  |  | List<Long> ids = (List<Long>) param.get("ids"); | 
|---|
|  |  |  | if (Cools.isEmpty(ids)) { | 
|---|
|  |  |  | return R.parse(BaseRes.PARAM); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String cstateid = null; | 
|---|
|  |  |  | Object cstateidObj = param.get("cstateid"); | 
|---|
|  |  |  | if (!Cools.isEmpty(cstateidObj)) { | 
|---|
|  |  |  | cstateid = String.valueOf(cstateidObj); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<OrderDetlPakout> orderDetlPakouts = orderDetlPakoutService.selectBatchIds(ids); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (orderDetlPakouts.size() == 0) { | 
|---|
|  |  |  | return R.parse("订单明细为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<String> disableCstateidList = new ArrayList<>(); | 
|---|
|  |  |  | disableCstateidList.add("1001A1100000000H0KJP");//不合格 | 
|---|
|  |  |  | disableCstateidList.add("1001A1100000000LFERA");//待处理 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<LocDto> locDtos = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | if (issued <= 0.0D) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStockAll(null, exist,orderDetl.getMatnr(), orderDetl.getBatch(), | 
|---|
|  |  |  | orderDetl.getBrand(),orderDetl.getStandby1(),orderDetl.getStandby2(),orderDetl.getStandby3(),orderDetl.getBoxType1(),orderDetl.getBoxType2(),orderDetl.getBoxType3()); | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStockAll(null, exist, orderDetl.getMatnr(), orderDetl.getBatch(), | 
|---|
|  |  |  | orderDetl.getBrand(), orderDetl.getStandby1(), orderDetl.getStandby2(), orderDetl.getStandby3(), orderDetl.getBoxType1(), orderDetl.getBoxType2(), orderDetl.getBoxType3(), cstateid); | 
|---|
|  |  |  | for (LocDetl locDetl : locDetls) { | 
|---|
|  |  |  | if (issued > 0) { | 
|---|
|  |  |  | LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(), | 
|---|
|  |  |  | 
|---|
|  |  |  | if (issued <= 0.0D) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStockAll(null, exist,orderDetl.getMatnr(), orderDetl.getBatch(), | 
|---|
|  |  |  | orderDetl.getBrand(),orderDetl.getStandby1(),orderDetl.getStandby2(),orderDetl.getStandby3(),orderDetl.getBoxType1(),orderDetl.getBoxType2(),orderDetl.getBoxType3()); | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.queryStockAll(null, exist, orderDetl.getMatnr(), orderDetl.getBatch(), | 
|---|
|  |  |  | orderDetl.getBrand(), orderDetl.getStandby1(), orderDetl.getStandby2(), orderDetl.getStandby3(), orderDetl.getBoxType1(), orderDetl.getBoxType2(), orderDetl.getBoxType3(), cstateid); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | HashMap<String, Double> locAnfmeMap = new HashMap<>(); | 
|---|
|  |  |  | HashMap<String, Date> locTimeMap = new HashMap<>(); | 
|---|
|  |  |  | HashMap<String, List<LocDetl>> map = new HashMap<>(); | 
|---|
|  |  |  | for (LocDetl locDetl : locDetls) { | 
|---|
|  |  |  | if (disableCstateidList.contains(locDetl.getCstateid())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (map.containsKey(locDetl.getLocNo())) { | 
|---|
|  |  |  | List<LocDetl> list = map.get(locDetl.getLocNo()); | 
|---|
|  |  |  | list.add(locDetl); | 
|---|
|  |  |  | map.put(locDetl.getLocNo(), list); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Double originAnfme = locAnfmeMap.get(locDetl.getLocNo()); | 
|---|
|  |  |  | originAnfme += locDetl.getAnfme(); | 
|---|
|  |  |  | locAnfmeMap.put(locDetl.getLocNo(), originAnfme); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | List<LocDetl> list = new ArrayList<>(); | 
|---|
|  |  |  | list.add(locDetl); | 
|---|
|  |  |  | map.put(locDetl.getLocNo(), list); | 
|---|
|  |  |  | locAnfmeMap.put(locDetl.getLocNo(), locDetl.getAnfme()); | 
|---|
|  |  |  | locTimeMap.put(locDetl.getLocNo(), locDetl.getAppeTime()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); | 
|---|
|  |  |  | // 使用 TreeMap(自动按 key 升序排序) | 
|---|
|  |  |  | Map<String, List<Map.Entry<String, Date>>> sortedByDayAsc = locTimeMap.entrySet() | 
|---|
|  |  |  | .stream() | 
|---|
|  |  |  | .collect(Collectors.groupingBy( | 
|---|
|  |  |  | entry -> sdf.format(entry.getValue()), | 
|---|
|  |  |  | TreeMap::new, // 自动按键(日期)升序排序 | 
|---|
|  |  |  | Collectors.toList() | 
|---|
|  |  |  | )); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //未匹配上整托库存 | 
|---|
|  |  |  | HashMap<String, Double> lessMap = new HashMap<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<LocDetl> allLoc = new ArrayList<>(); | 
|---|
|  |  |  | List<LocDetl> lessLoc = new ArrayList<>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //                //优先找整托 | 
|---|
|  |  |  | //                for (Map.Entry<String, List<Map.Entry<String, Date>>> listEntry : sortedByDayAsc.entrySet()) { | 
|---|
|  |  |  | //                    for (Map.Entry<String, Date> dateEntry : listEntry.getValue()) { | 
|---|
|  |  |  | //                        if (issued <= 0) { | 
|---|
|  |  |  | //                            break; | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                        String locNo = dateEntry.getKey(); | 
|---|
|  |  |  | //                        Double anfme = locAnfmeMap.get(locNo); | 
|---|
|  |  |  | //                        if(anfme > orderDetl.getAnfme()) { | 
|---|
|  |  |  | //                            lessMap.put(locNo, anfme); | 
|---|
|  |  |  | //                            continue; | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                        List<LocDetl> list = map.get(locNo); | 
|---|
|  |  |  | //                        allLoc.addAll(list); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        // 剩余待出数量递减 | 
|---|
|  |  |  | //                        issuedBigDecimal = issuedBigDecimal.subtract(BigDecimal.valueOf(anfme)); | 
|---|
|  |  |  | //                        issued = issuedBigDecimal.doubleValue(); | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                if(issued > 0) { | 
|---|
|  |  |  | //                    //零散搜索 | 
|---|
|  |  |  | //                    for (Map.Entry<String, Double> entry : lessMap.entrySet()) { | 
|---|
|  |  |  | //                        if (issued <= 0) { | 
|---|
|  |  |  | //                            break; | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                        String locNo = entry.getKey(); | 
|---|
|  |  |  | //                        Double anfme = entry.getValue(); | 
|---|
|  |  |  | //                        if (anfme >= issued) { | 
|---|
|  |  |  | //                            List<LocDetl> list = map.get(locNo); | 
|---|
|  |  |  | //                            lessLoc.addAll(list); | 
|---|
|  |  |  | //                            // 剩余待出数量递减 | 
|---|
|  |  |  | //                            issuedBigDecimal = issuedBigDecimal.subtract(BigDecimal.valueOf(anfme)); | 
|---|
|  |  |  | //                            issued = issuedBigDecimal.doubleValue(); | 
|---|
|  |  |  | //                        } | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //                allLoc.addAll(lessLoc); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //必须把当天库存用完 | 
|---|
|  |  |  | for (Map.Entry<String, List<Map.Entry<String, Date>>> listEntry : sortedByDayAsc.entrySet()) { | 
|---|
|  |  |  | for (Map.Entry<String, Date> dateEntry : listEntry.getValue()) { | 
|---|
|  |  |  | if (issued <= 0) { | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | String locNo = dateEntry.getKey(); | 
|---|
|  |  |  | Double anfme = locAnfmeMap.get(locNo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<LocDetl> list = map.get(locNo); | 
|---|
|  |  |  | allLoc.addAll(list); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 剩余待出数量递减 | 
|---|
|  |  |  | issuedBigDecimal = issuedBigDecimal.subtract(BigDecimal.valueOf(anfme)); | 
|---|
|  |  |  | issued = issuedBigDecimal.doubleValue(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | issuedBigDecimal = Optional.of(BigDecimal.valueOf(orderDetl.getAnfme()).subtract(BigDecimal.valueOf(orderDetl.getWorkQty()))).orElse(new BigDecimal(0)); | 
|---|
|  |  |  | issued = issuedBigDecimal.doubleValue(); | 
|---|
|  |  |  | for (LocDetl locDetl : allLoc) { | 
|---|
|  |  |  | if (disableCstateidList.contains(locDetl.getCstateid())) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (issued > 0) { | 
|---|
|  |  |  | LocDto locDto = new LocDto(locDetl.getLocNo(), locDetl.getMatnr(), locDetl.getMaktx(), locDetl.getBatch(), orderDetl.getOrderNo(), | 
|---|
|  |  |  | issued >= locDetl.getAnfme() ? locDetl.getAnfme() : issued); | 
|---|
|  |  |  | 
|---|
|  |  |  | locDto.setBoxType2(orderDetl.getBoxType2()); | 
|---|
|  |  |  | locDto.setBoxType3(orderDetl.getBoxType3()); | 
|---|
|  |  |  | locDto.setUnit(orderDetl.getUnit()); | 
|---|
|  |  |  | locDto.setInitialAnfme(locDetl.getAnfme());//原托盘数量 | 
|---|
|  |  |  | locDto.setInitialWeight(locDetl.getWeight());//原托盘辅数量 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //计算辅单位 | 
|---|
|  |  |  | Double weight = MatUtils.calcWeight(locDto.getMatnr(), locDto.getAnfme()); | 
|---|
|  |  |  | 
|---|
|  |  |  | locDtos.add(locDto); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 重新计算出库站点,可能存在一个托盘多条明细,多条明细统计下来可全板出库,但实际给出的站点只能拣选出库 | 
|---|
|  |  |  | HashMap<String, Double> locAnfmeMap = new HashMap<>(); | 
|---|
|  |  |  | for (LocDto locDto : locDtos) { | 
|---|
|  |  |  | if(locAnfmeMap.containsKey(locDto.getLocNo())) { | 
|---|
|  |  |  | Double anfme = locAnfmeMap.get(locDto.getLocNo()); | 
|---|
|  |  |  | BigDecimal decimal = BigDecimal.valueOf(locDto.getAnfme()).add(BigDecimal.valueOf(anfme)); | 
|---|
|  |  |  | locAnfmeMap.put(locDto.getLocNo(), decimal.doubleValue()); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | locAnfmeMap.put(locDto.getLocNo(), locDto.getAnfme()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | HashMap<String, List<Integer>> locStaNosMap = new HashMap<>(); | 
|---|
|  |  |  | for (Map.Entry<String, Double> entry : locAnfmeMap.entrySet()) { | 
|---|
|  |  |  | String locNo = entry.getKey(); | 
|---|
|  |  |  | Double realAnfme = entry.getValue(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | double stockAnfme = 0D; | 
|---|
|  |  |  | List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("loc_no", locNo)); | 
|---|
|  |  |  | for (LocDetl locDetl : locDetls) { | 
|---|
|  |  |  | BigDecimal decimal = BigDecimal.valueOf(stockAnfme).add(BigDecimal.valueOf(locDetl.getAnfme())); | 
|---|
|  |  |  | stockAnfme = decimal.doubleValue(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int ioType = 103; | 
|---|
|  |  |  | if(stockAnfme == realAnfme) { | 
|---|
|  |  |  | ioType = 101; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Integer> staNos = staDescService.queryOutStaNosByLocNo(locNo, ioType); | 
|---|
|  |  |  | User user = null; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | user = getUser(); | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(user != null) { | 
|---|
|  |  |  | Integer defaultOutSta = user.getDefaultOutSta(); | 
|---|
|  |  |  | if(defaultOutSta != null) { | 
|---|
|  |  |  | if (staNos.contains(defaultOutSta)) { | 
|---|
|  |  |  | staNos.remove(defaultOutSta); | 
|---|
|  |  |  | staNos.add(0, defaultOutSta); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | locStaNosMap.put(locNo, staNos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (LocDto locDto : locDtos) { | 
|---|
|  |  |  | List<Integer> staNos = locStaNosMap.get(locDto.getLocNo()); | 
|---|
|  |  |  | locDto.setStaNos(staNos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return R.ok().add(locDtos); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|