/// 
///LOGIC CLASS FOR TABLE t_wmsImportSeeds
///By wm with codesmith. 
///on 07/21/2019
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeiNiu.wms.Data.Model;
using System.Data;
using System.Transactions;
using DeiNiu.wms.win;
using DeiNiu.Utils;
namespace DeiNiu.wms.Logical
{
    [Serializable]
    public class lWmsImportSeeds   :lbase
    {
        WmsImportSeeds _obj;
        public lWmsImportSeeds()
        {
             initialize();
        }
 
        public  WmsImportSeeds getWmsImportSeeds
        {
            get
            {
             if (_obj == null)
                {
                    _obj = new WmsImportSeeds();
                   
                } 
                _obj.operater = operId;
                return _obj;
            }
        }
        
       public lWmsImportSeeds(int operId)
            : base(operId)
        {
            initialize();
        }
        
  
		/// 
		/// get all data
		/// 
        public DataSet getAllData()
        {
           return _obj.Query();
        }
        /// 
        /// get all data
        /// 
        public DataSet getAllActiveData()
        {
            return _obj.QueryActived();
        }
		/// 
		/// get a record by id
		/// 
        public void initialize(int id)
		{
            _obj = id != 0 ? new WmsImportSeeds(id) : new WmsImportSeeds();
		}
        /// 
		/// get a record by id 0
		/// 
        public void initialize()
        {
            initialize(0);
        }
         /// 
        /// get a record by id
        /// 
        public void initialize(DataRow dr)
        {
            _obj =   new WmsImportSeeds(dr);
        }
        
        protected override DeiNiu.Data.BaseObject.BaseModel getModel()
        {
            return _obj;
        }
 
        //begin cust db operation, query, excute sql etc.
     
        internal int add(WmsImportSeeds obj)
        {
            return obj.Add();
        }
     
        /// 
        /// update in a transaction scrop
        /// 
        public void update()
        {
            if (valid())
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    //Node tmp = new Node();
                    //tmp.parentid = 1;
                    //tmp.name = "test trans" + DateTime.Now;
                    //tmp.description = "this is for transTest";
                    //tmp.Add();
                    _obj.Update();
                    scope.Complete();
                }
            }
        }
        
         private bool valid()
        {
            return true;
        }
         internal int updateCount(int count, int id, int status,String desc)
         {
             if (status == (int)enumSeedsState.待处理)
             {
                 WmsImportSeeds wis = new WmsImportSeeds(id);
                 if (wis.ID == id)
                 {
                     wis.countOut = count;
                     wis.description += desc;
                     return wis.Update();
                 }
             }
             else if (status == (int)enumSeedsState.处理中 || status == (int)enumSeedsState.已亮灯)
             {
                 WmsImportSeeds_tmp wis = new WmsImportSeeds_tmp(id);
                 if (wis.ID == id)
                 {
                     wis.countOut = count;
                     wis.description += desc;
                     return wis.Update();
                 }
             }
             else if (status == (int)enumSeedsState.已按灭)
             { 
                 WmsImportSeeds_history wis = new WmsImportSeeds_history(id);
                 if (wis.ID == id)
                 {
                     wis.countOut = count;
                     wis.description += desc;
                    return wis.Update();
                 } 
             }
             return 0;
         }
         internal int loadNewSeeds(int deskId, string seedsNo)
         {
             string isAutoBind = System.Configuration.ConfigurationManager.AppSettings["IsAutoBind"];
             if (!String.IsNullOrEmpty(isAutoBind) && Convert.ToInt16(isAutoBind) >0) //自动绑定标签ID
             {
               DataTable dt =  getWmsImportSeeds.QueryBySeeds(seedsNo).Tables[0];
               string lastCust = "";
               int elId =0;
               foreach (DataRow dr in dt.Rows)
               {
                   WmsImportSeeds wis = new WmsImportSeeds(dr); 
                   
                   if (string.IsNullOrEmpty(lastCust) || !lastCust.Equals(wis.custName))
                   {
                       lastCust = wis.custName;
                       elId++; 
                   }
                   wis.elabId = elId;
                   wis.Update(); 
               } 
             }
             return getWmsImportSeeds.loadNewSeeds(deskId, seedsNo);
         }
         internal DataTable getSumCount(string seedsNo, string barcode)
         {
             return getWmsImportSeeds.getSumCount(seedsNo, barcode);
         }
         internal List startNewSeed(int deskId, string seedsNo, string barcode, int operater, int color,bool isValid )
         {
             return getWmsImportSeeds.startNewSeed(deskId, seedsNo, barcode, operater, color,  isValid);
         }
         internal int deleteOrder(string seedsNo)
         {
             return getWmsImportSeeds.deleteOrder(seedsNo);
         }
 
        String seedsNo ="seedsNo123";       
        String barcode ="1099";
         internal bool validLimit(string input)
         {
             WmsOutSeeds wos = new WmsOutSeeds();
             DataTable dt = wos.getValidSeed(1000, seedsNo, barcode);
             if (dt.Rows.Count > 0)
             {
                 wos = new WmsOutSeeds(dt.Rows[0]);
                 return input.Equals(wos.description);
             }
             return false;
         }
         internal int isExceedLimitCnt(bool isFix, int limitCount)
         {
            
             int notExceed = -1;
             int exceed = 0;
             WmsOutSeeds wos = new WmsOutSeeds();
             DataTable dt = wos.getValidSeed(1000, seedsNo, barcode);
             if (dt.Rows.Count == 0)
             {
                 wos.seedsNo = seedsNo;
                 wos.barcode = barcode;
                 wos.deskId = 1000;
                 wos.count = 1;
                 wos.description ="D0-43-D4-A4-A9-EF-C8-72-AD-8E-FD-C3-81-07-BF-84";
                 wos.custOrder = "001";
                 wos.Add();
                 return notExceed;
             }
             else
             {
                 wos = new WmsOutSeeds(dt.Rows[0]);
                 if (isFix)
                 {
                     wos.count = -1;
                     wos.Update();
                     return notExceed;
                 }
                 int count = (int)wos.count;
                 if (count == -1)
                 {
                     return notExceed;
                 }
                 else
                     if (count < limitCount)
                     {
                         wos.count++;
                         wos.Update(); 
                         return limitCount -(int)wos.count;
                     }
                     else
                     {
                         return exceed;
                     } 
             } 
         }
        public ELight getLight(int deskId,string barcode)
         {
             
             ELight el = new ELight();
             DataTable dt = getWmsImportSeeds.getLightInfo(barcode, deskId);
             foreach (DataRow dr in dt.Rows)
             {
                 WmsImportSeeds_tmp it = new WmsImportSeeds_tmp(dr);
                 el.id = it.ID;
                 el.lightId = it.elabId;
                 el.which =Convert.ToByte( it.elabAddress-1);
                 el.port = it.port;
                 return el;
             }
             return el;
         }
        public ELight getSeedLight( string barcode)
        {
            ELight el = new ELight();
            DataTable dt = getWmsImportSeeds.getLightInfo(barcode);
            foreach (DataRow dr in dt.Rows)
            {
                WmsImportSeeds_tmp it = new WmsImportSeeds_tmp(dr); 
                Wmslocation loc = new Wmslocation().getLocsByOwner(it.custName); //一次分播的分播位
             
                el.lightId = loc.elabId;
                el.which = Convert.ToByte(loc.elabAddress - 1);  
                el.port = loc.port;
                return el;
            }
            return el;
        }
        internal List getLightsByDesk(int deskId)
        {
            DataTable dt = getWmsImportSeeds.getLightInfo( deskId);
            List els = new List(); 
            foreach (DataRow dr in dt.Rows)
            {
                ELight el = new ELight();
                WmsImportSeeds_tmp it = new WmsImportSeeds_tmp(dr);
                el.lightId = it.elabId;
                el.which = Convert.ToByte(it.elabAddress);
                el.port = it.port;
                els.Add(el);
            }
            return els;
        }
        internal void logLight(int ID,   int empId)
        {
            WmsImportSeeds_tmp wit = new WmsImportSeeds_tmp(ID); 
            wit.empId = empId;
            wit.state =(int) enumSeedsState.已亮灯;
            wit.picktime = wit.getDateTime();
            wit.Update();
        }
        internal void reOrderLabelID()
        {
          DataTable dt=  getWmsImportSeeds.getLocations();
          List locs = new List();
          Dictionary> dic= new  Dictionary>();
          foreach (DataRow dr in dt.Rows)
          {
              Wmslocation loc = new Wmslocation(dr);
              if (dic.ContainsKey(loc.channel))
              {
                  dic[loc.channel].Add(loc);
              }
              else
              {
                  dic.Add(loc.channel, new List());
                  dic[loc.channel].Add(loc);
              }
          }
          int labelId = 0;
          foreach (int channel in dic.Keys)
          {
              int lastLayer = 0;
              foreach (Wmslocation loc in dic[channel])
              {
                  if (loc.col <= 5)
                  {
                      if (lastLayer == 0 || lastLayer != loc.layer)
                      {
                          lastLayer = loc.layer;
                          labelId++;
                      }
                      loc.elabId = labelId;
                      loc.Update();
                  }
              }
              lastLayer = 0;
              foreach (Wmslocation loc in dic[channel])
              {
                  if (loc.col > 5)
                  {
                      if (lastLayer == 0 || lastLayer != loc.layer)
                      {
                          lastLayer = loc.layer;
                          labelId++;
                      }
                      loc.elabId = labelId;
                      loc.Update();
                  }
              }
          }
             
        }
        public void clearSeeds()
        {
            getWmsImportSeeds.clearSeeds();
          //  getWmsImportSeeds.startNewSeed();
        }
    }
    
    
}