java 判断一个物料是否有下级 并去重累计数量

 /**
     * 通过审核 cy  审核通过后生成sku 如果是成品 要得到需要多少半成品和多少原料 组成
     * 生成采购建议单   和 采 购建议明细单
     */
    @RequestMapping(value = "approved", method = RequestMethod.POST)
    public synchronized String approved(@RequestParam String saleOrderId) {
        ShiroDbRealm.ShiroUser shiroUser = (ShiroDbRealm.ShiroUser) SecurityUtils.getSubject().getPrincipal();
        String fid = shiroUser.getFid();
        Factory factory = factoryService.getFactory(fid);//
        SaleOrder saleOrder = saleOrderService.getSaleOrder(saleOrderId);

        Map searchParams = new HashMap<>();
        searchParams.put("NEQ_status", "99");
        searchParams.put("EQ_saleOrder.id", saleOrderId);
        List saleOrderItemList = saleOrderService.getAllSaleOrderItem(searchParams, 1, Integer.MAX_VALUE, "auto").getContent();
        PurchasePlanOrder purchasePlanOrder = new PurchasePlanOrder();//采购建议主单
        purchasePlanOrder.setOrderNo(getPurchasePlanOrderOrderNo());//单号
        purchasePlanOrder.setCreateTime(new Date());//创建时间
        purchasePlanOrder.setFactory(factory);//工厂
        purchasePlanOrder.setRemark("pcCreate:");
        purchasePlanOrder.setSaleOrder(saleOrder);//关联销售主单
        purchasePlanOrder.setStatus("0");//默认状态 0  , 99 删除
        purchasePlanOrderService.savePurchasePlanOrder(purchasePlanOrder);
        nextMaterielSkuArray.clear();
        for (int i = 0; i < saleOrderItemList.size(); i++) {//销售单的明细单
            SaleOrderItem saleOrderItem = saleOrderItemList.get(i);
            Long qty = saleOrderItem.getQty();
            MaterielSku materielSku = saleOrderItem.getMaterielSku();//得到销售单明细中的物料
            selectNextMaterielSku(materielSku.getBarcode(), qty.toString());//递归取查找下级
        }
        JSONArray jsonArray = delRepeatIndexid(nextMaterielSkuArray);//去重后的物料
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject nextSku = (JSONObject) jsonArray.get(i);
            String barcode = nextSku.get("barcode").toString();
            String num = nextSku.get("num").toString();
            MaterielSku byBarcode = materielSkuService.findByBarcode(barcode);
            PurchasePlanOrderItem purchasePlanOrderItem = new PurchasePlanOrderItem();//新建一个计划单
            purchasePlanOrderItem.setMaterielSku(byBarcode);//保存物料
            purchasePlanOrderItem.setActualNum(0L);//实际采购量
            purchasePlanOrderItem.setDemandNum(Long.valueOf(num));//订单需求量
            purchasePlanOrderItem.setCreateTime(new Date());//订单生成时间
            purchasePlanOrderItem.setPurchasePlanOrder(purchasePlanOrder);//保存主单
            purchasePlanOrderService.savePurchasePlanOrderItem(purchasePlanOrderItem);//保存明细
        }
        return "redirect:/order/saleOrder/list";
    }

//声明一个数组保存递归
 private JSONArray nextMaterielSkuArray = new JSONArray();
    
        //递归  查找这个物料是否有下级  把物料的最低级物料  组成一个list
        private  void selectNextMaterielSku(String skuBarcode, String skuNum) {
            MaterielSku materielSku = materielSkuService.findByBarcode(skuBarcode);
            String nextSkuList = materielSku.getNextSkuList();
            JSONArray nextSkuArray = JSONArray.parseArray(nextSkuList);//将下级的物料转成数组
            if (nextSkuArray != null && nextSkuArray.size() > 0) {//如果下级不是空的
                for (int k = 0; k < nextSkuArray.size(); k++) {//遍历这个物料的下级
                    JSONObject nextSku = (JSONObject) nextSkuArray.get(k);//得到下级 的 barcode 和 num.
                    //如果这个物料编码是空的  说明没有对应的下级   这里保存本级物料
                    if (!"".equals(nextSku.get("barcode").toString().trim()) && !" ".equals(nextSku.get("barcode").toString().trim())) {//这里判断 如果不是空的  继续查询
                        String barcode = (String) nextSku.get("barcode");//物料编码
                        String num = (String) nextSku.get("num");//数量
                        MaterielSku nextMaterielSku = materielSkuService.findByBarcode(barcode);//查到下级的物料
                        if (nextMaterielSku != null) {//如果查到的下级物料不是空的
                            Long sum = Long.valueOf(num) * Long.valueOf(skuNum);
                            selectNextMaterielSku(barcode,sum.toString() );//再次查询 下级物料是否还有下级   这里要乘以上级的数量
                        } else {//如果查到的物料是空的
                            JSONObject newObject = new JSONObject();
                            newObject.put("barcode", skuBarcode);
                            newObject.put("num", skuNum);
                            nextMaterielSkuArray.add(newObject);
                        }
                    } else {//如果是空的
                        JSONObject newObject = new JSONObject();
                        newObject.put("barcode", skuBarcode);
                        newObject.put("num", skuNum);
                        nextMaterielSkuArray.add(newObject);
                    }
                }
            } else {
                System.out.println("这个物料没有下级");
                JSONObject newObject = new JSONObject();
                newObject.put("barcode", skuBarcode);
                newObject.put("num", skuNum);
                nextMaterielSkuArray.add(newObject);
            }
        }
    
    
        /**
         * 去重复 
         */
        public  JSONArray delRepeatIndexid(JSONArray array) {
            JSONArray arrayTemp = new JSONArray();
            int num = 0;
            for (int i = 0; i < array.size(); i++) {
                if (num == 0) {
                    arrayTemp.add(array.get(i));
                } else {
                    int numJ = 0;
                    for (int j = 0; j < arrayTemp.size(); j++) {
                        JSONObject newJsonObjectI = (JSONObject) array.get(i);
                        JSONObject newJsonObjectJ = (JSONObject) arrayTemp.get(j);
                        String barcode1 = newJsonObjectI.get("barcode").toString();
                        String num1 = newJsonObjectI.get("num").toString();
                        String barcode2 = newJsonObjectJ.get("barcode").toString();
                        String num2 = newJsonObjectJ.get("num").toString();
                        if (barcode1.equals(barcode2)) {
                            int newValue = Integer.parseInt(num1) + Integer.parseInt(num2);
                            arrayTemp.remove(j);
                            JSONObject newObject = new JSONObject();
                            newObject.put("barcode", barcode1);
                            newObject.put("num", newValue);
                            arrayTemp.add(newObject);
                            break;
                        }
                        numJ++;
                    }
                    if (numJ - 1 == arrayTemp.size() - 1) {
                        arrayTemp.add(array.get(i));
                    }
                }
                num++;
            }
            return arrayTemp;
        }

你可能感兴趣的:(java)