using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DBFactory;
using ICommLayer;
using CommLayerFactory;
namespace ControlSystem
{
    /// <summary>
    /// 码垛位的条形码扫描、组盘、码垛结束的申请管理任务；
    /// 输送机上的扫描器条形码申请管理任务。
    /// </summary>
    public class CScanBarcode
    {
        DBOperator dbo = new DBOperator();
        DBOperator dboM = new DBOperator("ManConnString", "ManDBFactory");
        public void BarCodeApply()
        {
            IGetDeviceState gds;
            int[] bc;
            string sql = "SELECT F_DeviceIndex, F_BarCodeUse, F_BindingDevice, F_BarCode FROM T_Base_ScanBarcode order by F_DeviceIndex asc";
            DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
            for (int i = 0; i < dv.Count; i++)
            {//扫描到的条形码的用途：1申请任务；2条码比对
                switch (dv[i]["F_BarCodeUse"].ToString())
                {
                    case "1":
                        #region 1申请任务
                        //扫到的条码（全部非零）与记录的条形码不相等就认为是新条码
                        gds = CommModeCreate.CreateGetDeviceState(Convert.ToInt32(dv[i]["F_DeviceIndex"]));
                        bc= gds.GetDeviceState(Convert.ToInt32(dv[i]["F_DeviceIndex"]), 64777);
                        int bcz=0;
                        string barcode = string.Empty;
                        for (int j = 2; j < 12; j++)
                        {
                            bcz = bcz + bc[j];
                            barcode = barcode + bc[j].ToString();
                        }
                        if (bcz == 0)
                        {
                            continue;
                        }
                        DataView dvbc = dbo.ExceSQL("SELECT F_TxtParam FROM T_Monitor_Task WHERE (F_TxtParam = '"+barcode +"')").Tables[0].DefaultView;
                        if (dvbc.Count > 0)
                        {
                            continue;
                        }
                        //向管理申请任务

                        #endregion
                        break;
                    
                    default:
                        break;
                }
                
            }
        }
    }
}
