using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using MESClassLibrary.BLL.Log;
using MESClassLibrary.DAL;
using MESClassLibrary.DAL.BasicInfo;
using MESClassLibrary.DAL.Injection;
using MESClassLibrary.Model;

namespace MESClassLibrary.BLL.BasicInfo
{
    public class BarCodeBLL
    {
        private BarCodeDAl db = new BarCodeDAl();
        private WmsDAL wmsDal = new WmsDAL();
        private BadInjectionDAL badDb = new BadInjectionDAL();
        public DataTable SearchInfoByStock(string StockNo)
        {
            try
            {
                return db.SearchInfoByStock(StockNo);
            }
            catch (Exception ex)
            {
                
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                throw ex;
                //return null;
            }
        }

        public DataTable SearchInfoByPartNo(string partNo)
        {
            try
            {
                return db.SearchInfoByPartNo(partNo);
            }
            catch (Exception ex)
            {
                
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                throw ex;
            }
        }

        public DataTable SearchInfoByPartNo1(string partNo)
        {
            try
            {
                return db.SearchInfoByPartNo1(partNo);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                throw ex;
                //return null;
            }
        }

        public DataTable searchBarCode(string barcode, int flag)
        {
            try
            {
                return db.searchBarCode(barcode, flag);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public bool Add_Info(BarCodeModel md)
        {
            try
            {
                return db.AddInfo(md);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public bool Add_Info_New(BarCodeModel md,string tableName)
        {
            try
            {
                return db.AddInfoNew(md,tableName);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public DataTable SearchInfo(string stationNo)
        {
            try
            {
                return db.SearchInfo(stationNo);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchInfoAll()
        {
            try
            {
                return db.SearchInfoAll();
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchBarCode()
        {
            try
            {
                return db.SearchBarCode();
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchBarCodeByOne(string OneBarCode)
        {
            try
            {
                return db.SearchBarCodeByOne(OneBarCode);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchBarCodeByTwo(string BarCode)
        {
            try
            {
                return db.SearchBarCodeByTwo(BarCode);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchSerialNoByBarCode(string Code)
        {
            try
            {
                return db.SearchSerialNoByBarCode(Code);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchSerialNoByBarCodeNew(string Code,string tableName)
        {
            try
            {
                return db.SearchSerialNoByBarCodeNew(Code, tableName);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());

                throw ex;
                //return null;
            }
        }

        public DataTable SearchSerialNoByBarCodeNew1(string Code)
        {
            try
            {
                return db.SearchSerialNoByBarCodeNew1(Code);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchInfoByBarCode(string BarCode)
        {
            try
            {
                return db.SearchInfoByBarCode(BarCode);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchInfoByBarCodeNew(string BarCode,string tableName)
        {
            try
            {
                return db.SearchInfoByBarCodeNew(BarCode, tableName);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public bool Update_Info(BarCodeModel md)
        {
            try
            {
                return db.UpdateInfo(md);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public bool DelBarCode(BarCodeModel md)
        {
            try
            {
                return db.DelBarCode(md);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public bool DelBarCode_New(BarCodeModel md,string tableName)
        {
            try
            {
                return db.DelBarCode_New(md,tableName);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public DataTable SearchBoxIsNull(string partNo, string StationID, string tableName, string ProductNature)
        {
            try
            {
                return db.SearchBoxIsNull(partNo, StationID, tableName, ProductNature);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable IsHave(string partNo, string StationID, string tableName)
        {
            try
            {
                return db.IsHave(partNo, StationID, tableName);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
        /// <summary>
        /// 箱码中单个条码替换
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="boxCode"></param>
        /// <param name="oldID"></param>
        /// <param name="newID"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool BarCodeReplace(string oldtableName, string newtableName, string boxCode,string oldID,string newID,string newOneBarCode,string newBarCode, BadInjectionModel model)
        {
            LocalDBService localDB = new LocalDBService(SqlHelper.GetConnSting());
            try
            {
                localDB.BeginTrans();
                db.UpdateBoxNoByID(localDB, newtableName, boxCode, newID);
                db.UpdateBoxNoNull(localDB, oldtableName, oldID);
                badDb.AddInfo(localDB, model);
                wmsDal.DeleteRecord(localDB, boxCode, model.OneBarCode, model.BarCode);
                wmsDal.AddRecord(localDB, boxCode, newOneBarCode, newBarCode);
                localDB.Commit();
                return true;
            }
            catch (Exception ex)
            {
                if(localDB != null)
                {
                    localDB.Rollback();
                }
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                
                return false;
            }
            finally
            {
                if (localDB != null)
                {
                    localDB.EndTrans();
                }
            }
        }

        public bool UpdateBoxNo(string tableName, string boxNo, string partNo)
        {
            try
            {
                return db.UpdateBoxNo(tableName, boxNo, partNo);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }
      
        public DataTable SearchCountByBox(string boxNo, string tableName)
        {
            try
            {
                return db.SearchCountByBox(boxNo, tableName);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public bool UpdateIsBad(string tableName, string onebarcode, string barcode)
        {
            try
            {
                return db.UpdateIsBad(tableName, onebarcode, barcode);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public DataTable SearchBarCode(string boxNo, string tableName, string row)
        {
            try
            {
                return db.SearchBarCode(boxNo, tableName, row);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
        public DataTable SearchBarCodeByWhere(string whereStr, string tableName, string row)
        {
            try
            {
                return db.SearchBarCodeByWhere(whereStr, tableName, row);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
        
        public bool updateProductNature(string tableName, string barcode)
        {
            try
            {
                return db.updateProductNature(tableName, barcode);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }
        }

        public DataTable SeachInfoByOne(string barCode, string tableName,int flag)
        {
            try
            {
                return db.SeachInfoByOne(barCode, tableName, flag);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public string LastBarCode(string tableName)
        {
            try
            {
                return db.LastBarCode(tableName);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return "";
            }
        }

        public bool IsExist(string barCode)
        {
            try
            {
                return db.IsExist(barCode);
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(),MethodBase.GetCurrentMethod());
                return false;
            }
        }
    }
}