/// 
///INTERFACE CLASS FOR TABLE t_tmsStock
///By wm  
///on 07/27/2017
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DeiNiu.Utils;
namespace DeiNiu.wms.Data.Model
{
    [Serializable]
    public class TmsStock : TmsStock_base
    {
        public TmsStock()
        {
        }
        public TmsStock(int id): base(id)
        {
        }
        public TmsStock(DataRow dr): base(dr)
        {
        }
      protected override void getImp()
        {
            model_imp = new TmsStock_Imp();  
        }
        //begin cust db operation, query, excute sql etc.
        public DataSet QueryByName(string name)
        {
            cmdParameters[0] = name;
            return  CustQuery(100);
        }
        public List getFreeLocation(int transLine,enumWhLocVol volType,  enumWhType whType, int count = 1)
        {
            List locations = new List(); 
            cmdParameters[0] = transLine;
            cmdParameters[1] =(int)volType;
            cmdParameters[2] = count; 
            cmdParameters[3] = (int)whType;
            DataSet ds = CustQuery(200);
            int freeCnt = 0;
            foreach (DataTable dt in ds.Tables)
            {
                freeCnt += dt.Rows.Count;
            }
            
            foreach (DataTable dt in ds.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    locations.Add(new Wmslocation(dr));
                    if (locations.Count == count)
                    {
                        return locations;
                    }
                }
            }
            if (locations.Count < count) //未找到足够空货位
            { 
                string error = string.Format("空货位不足,需要{0}个,找到{1}个。", count, locations.Count);
                //throw new DeiNiuNoFreeLocationkException(error);
            }
            return locations;
        }
        public string getTranLocation(string custId)
        {
            string locationId = "";
            cmdParameters[0] = custId;
          DataTable dt =  CustQuery(300).Tables[0];
          if (dt.Rows.Count > 0)
          {
              return dt.Rows[0][0].ToString();
          }
          
          dt = CustQuery(301).Tables[0];
          if (dt.Rows.Count > 0)
          {
              return dt.Rows[0][0].ToString();
          }
             
          return locationId;
        }
        /// 
        /// 取零货集货明细
        /// 
        /// 
        /// 
        public DataTable getTmsStockBulkDetail(string pickOrderNo)
        { 
            cmdParameters[0] = pickOrderNo;
            return CustQuery(400).Tables[0]; 
        }
    }
}