PDA库位商品出库适配算法

时间:2023-12-12 11:44:32

  1. 功能描述
  2. 代码实现
  3. 算法需求

功能描述

  PDA库位商品出库适配算法
  在PDA上面,通过扫描订单号,查询该商品的款号,尺码,订单数;同时适配仓库库位商品【SKU】,显示该商品所在库位,库存数量,适配数。
  因为PDA界面小,所以对表格拆分显示为俩个,代码也相应复杂一些。

代码实现

  
        /// <summary>
        /// 读头数据处理
        /// </summary>
        private void HandleData(Symbol.Barcode.ReaderData TheReaderData)
        {
              //定单适配
            )
            {
                txtWmsNo.Text = TheReaderData.Text;

                BindOrderDetailGrid(txtWmsNo.Text.Trim());
                btnAdaptor_Click(null, null);
            }
        }    
     private void BindOrderDetailGrid(string wmsID)
        {
            lv_Order.Items.Clear();

            try
            {
                _allOrderDetail = _repository.GetOrderDetail(wmsID);
                if (_allOrderDetail.Any())
                {
                    foreach (OrderDetail detail in _allOrderDetail)
                    {
                        ListViewItem item = new ListViewItem();
                        item.SubItems[].Text = detail.StyleNo;
                        item.SubItems.Add(detail.Size);
                        item.SubItems.Add(detail.OrderNum.ToString());//订单数
                        lv_Order.Items.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询出错:" + ex.Message);
                btnExit_Click(null, null);
            }
        }
     //批量适配
        private void btnAdaptor_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtWmsNo.Text.Trim()) || txtWmsNo.Text == "扫描WMS单号…")
            {
                MessageBox.Show("请先扫描wmsID。", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }
            if (_repository.ExitsWmsID(txtWmsNo.Text.Trim()))
            {
                DialogResult result = MessageBox.Show("该单号已经适配过了,是否继续?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                if (result != DialogResult.OK)
                {
                    lv_Adaptor.Items.Clear();
                    return;
                }
            }

            _differenceQty = ;
            _list.Clear();

            try{
                _goodList = _repository.GetGoodsList(string.Join(",",_allOrderDetail.Select(c=>"'"+c.StyleNo+"'").ToArray()));
             }
            catch (Exception ex)
            {
                MessageBox.Show("查询出错:" + ex.Message);
                btnExit_Click(null, null);
            }

            if (_goodList.Any())
            {
                _allOrderDetail.ForEach(order => BindAdaptorGrid(_goodList.Where(c => c.StyleNo == order.StyleNo).ToList(), order.Size, order.OrderNum, false));
            }

            if (_list.Any())
            {
                _list.ForEach(o => lv_Adaptor.Items.Add(o));

                btnSave.Enabled = true;
                lbl_difNum.Text = "总差异数:" + _differenceQty;
                lblMsg.Text = "适配成功,请保存!";
            }
            else
            {
                btnSave.Enabled = false;
                lblMsg.Text = "适配失败,请先维护库存!";
            }
        }
        /// <summary>
        /// 绑定适配表
        /// 如果数量相等,则按照库位匹配
        /// </summary>
        /// <param name="orderNum">订单数</param>
        /// <param name="isSingle">是否单款</param>
        private void BindAdaptorGrid(List<PDA_TGoods> goodsByStyleNo,string size,int orderNum,bool isSingle)
        {
            lv_Adaptor.Items.Clear();
            lblMsg.Text = string.Empty;

            _orderNum = orderNum;

            List<GoodsSummary> summary_goods = GetGoodsSummary(goodsByStyleNo);

            if (summary_goods.Any())
            {
                int stockSumNum = goodsByStyleNo.Count();        //库存数总和
                bool isBreak = false;

                //差异数算法:[订单数>=库存数总和-->差异数=订单数-库存数;订单数<库存数总和-->差异数=0]
                );
                _differenceQty += singleDifNum;
                if (isSingle)
                    lbl_difNum.Text = "单款差异:" + singleDifNum;

                foreach (var g in summary_goods)
                {
                    ListViewItem item = new ListViewItem();
                    item.SubItems[].Text = g.StyleNo;      //库位
                    item.SubItems.Add(g.StockNo.ToString()); //库存数
                    item.SubItems.Add(g.SkuNum.ToString()); //库存数   

                    //适配数算法:
                    //(1)订单数>=库存数总和-->适配数=库存数;
                    //(2)订单数<库存数总和-->适配数=任意库位的数量的分配(必须=订单数)
                    ;
                    if (_orderNum >= stockSumNum)
                        adaptorNum = g.SkuNum;
                    else
                        adaptorNum = GetAdaptorNum(_orderNum, g.SkuNum, out isBreak);  

                    item.SubItems.Add(adaptorNum.ToString());
                    //item.SubItems.Add(g.Sku);         //条码[不可见]
                    item.SubItems.Add(size);          //尺码[不可见]

                    if (isSingle)
                       lv_Adaptor.Items.Add(item);
                    else
                        _list.Add(item);

                    if (isBreak) break;//适配到,则不再进行其他库位的适配
                }
            }
        }

        //计算适配数
        private int GetAdaptorNum(int orderNum, int stockNum,out bool isBreak)
        {
            ;
            if (orderNum >= stockNum)
            {
                adptorNum = stockNum;
                isBreak = false;
            }
            else
            {
                adptorNum = _orderNum;
                isBreak = true;
            }

            _orderNum = _orderNum - stockNum;

            )
                isBreak = true;

            return adptorNum;
        }
     /// <summary>
        /// 按款汇总商品总数,并做排序
        /// </summary>
        /// <param name="PDA_TGoods"></param>
        /// <returns></returns>
        private static List<GoodsSummary> GetGoodsSummary(List<PDA_TGoods> goods)
        {
            List<GoodsSummary> summary_goods = new List<GoodsSummary>();
            foreach (var g in goods.GroupBy(o => new { o.StockNo, o.StyleNo }))
            {
                GoodsSummary gt = new GoodsSummary();
                gt.SkuNum = g.Count();
                gt.StockNo = g.First().StockNo;
                gt.StyleNo = g.First().StyleNo;
                summary_goods.Add(gt);
            }

            //库存量大优先;同等数量,库位优先策略
            return summary_goods.OrderByDescending(o => o.SkuNum).ThenBy(o => o.StockNo).ToList();
        }

算法需求

    (1)订单数>=库存数总和 则 适配数 = 库存数;
         (2)订单数<库存数总和  则 适配数 = 任意库位的数量的分配(必须=订单数)

        (3)库存量大的库位优先适配;同等数量的库位,库位编号小的优先适配策略