/// 
///LOGIC CLASS FOR TABLE t_tmsLineDetail
///By wm with codesmith. 
///on 07/23/2017
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeiNiu.wms.Data.Model;
using System.Data;
using System.Transactions;
namespace DeiNiu.wms.Logical
{
    [Serializable]
    public class lTmsLineDetail : lbase
    {
        TmsLineDetail _obj;
        public lTmsLineDetail()
        {
            initialize();
        }
        public lTmsLineDetail(int operId)
            : base(operId)
        {
            initialize();
        }
        public TmsLineDetail getTmsLineDetail
        {
            get
            {
                return _obj;
            }
        }
        /// 
        /// 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 TmsLineDetail(id) : new TmsLineDetail();
        }
        /// 
        /// get a record by id 0
        /// 
        public void initialize()
        {
            initialize(0);
        }
        /// 
        /// get a record by id
        /// 
        public void initialize(DataRow dr)
        {
            _obj = new TmsLineDetail(dr);
        }
        protected override DeiNiu.Data.BaseObject.BaseModel getModel()
        {
            return _obj;
        }
        //begin cust db operation, query, excute sql etc.
        /// 
        /// update in a transaction scrop
        /// 
        public void update()
        {
            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();
            }
        }
        public DataTable getAllLines(string custName)
        {
            if (string.IsNullOrEmpty(custName))
            {
                return new Node().queryChildsByFlag(3001);
            }
            else
            {
                return _obj.getLinesByCust(custName);
            }
        }
        internal bool addLineCust(int lineNo, string custId, int order)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    if (order > 0)
                    {
                        int cnt = _obj.getLineDetailCount(lineNo);
                        if (order <= cnt)
                        {
                            updateLineCustOrder(lineNo,0, cnt+1, order);
                        }
                    }
                    order = order < 0 ? 1 : order;
                    TmsLineDetail td = new TmsLineDetail();
                    td.lineId = lineNo;
                    td.custId = custId;
                    td.lineOrder = order;
                    td.operater = operId;
                    td.Add();
                    _obj.Update();
                    scope.Complete();
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }
        internal bool updateLineCustOrder(int id, int order)
        {
            TmsLineDetail td = new TmsLineDetail(id);
            td.lineOrder = order;
            td.operater = operId;
            return td.Update() > 0;
        }
        internal bool updateLineCustOrder(int lineId, int detailId, int orderOld, int orderNew)
        {
            int cnt = _obj.getLineDetailCount(lineId);
            int start = 0;
            int end = 0;
            int value = 0;
            if (orderOld == -1) // new cust to line
            {
                return true;
            }
            if (orderNew > orderOld)  // all items plus -1 ,,, 50 ->60
            {
                start = orderOld + 1;
                end = orderNew;
                value = -1;
            }
            else if (orderNew < orderOld) //all items plus 1 ,,,60 -->50
            {
                start = orderNew;
                end = orderOld - 1;
                value = 1;
            }
            else if (orderNew == orderOld) //all items plus 1 ,, new one
            {
                start = orderNew - 1;
                end = orderOld + 1;
                value = 1;
            }
             
            try
            {
                using (TransactionScope scope = new TransactionScope())
                { 
                    _obj.updateCustOrders(lineId, start, end, value); 
                    if (detailId > 0)
                    {
                        updateLineCustOrder(detailId, orderNew);
                    }
                    scope.Complete();
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }
        internal bool removeCust(int lineDetailId)
        {
            _obj = new TmsLineDetail(lineDetailId);
            return _obj.Delete() > 0;
        }
        internal bool reOrderLine(int lineId)
        {
            DataTable dt = getTmsLineDetail.queryByLineID(lineId);  
            int i = 1;
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        _obj = new TmsLineDetail(dr);
                        _obj.lineOrder = i;
                        int k= _obj.Update(); 
                        i++;
                    }
                    scope.Complete();
                    return true;
                }
            }
            catch (Exception e)
            {
                return false;
            }
          
          
        }
        
    }
    
    
}