/// 
///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;
                     }
             }
         }
    }
    
    
}