using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using DataLinkMesWork;
using System.Collections;

//using SystemFramework;

namespace bizFacade
{
    public partial class BaseDataSystemMES
    {

        /// <summary>
        /// 
        /// [MES_基本变量_厂家代码]
        /// </summary>
        /// <returns></returns>
        //public static Hashtable GetFatoryCodeHashtable()
        //{
        //    //厂家代码
        //    string fatoryCode;
        //    //三位码
        //    string threeCode;
        //    Hashtable fatoryCodeHashtable;
        //    fatoryCodeHashtable = new Hashtable ();
        //    DataTable dt;
        //    string sql;
        //    sql = "SELECT * FROM MES_基本变量_厂家代码 ";
        //    DataAccess.ExecuteDataTable_OleDb_Access(sql, out  dt);
        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {

        //        if (Convert.IsDBNull(dt.Rows[i]["厂家代码"]))
        //        {
        //            fatoryCode = "";
        //        }
        //        else
        //        {
        //            fatoryCode = dt.Rows[i]["厂家代码"].ToString();
        //        }

        //        if (Convert.IsDBNull(dt.Rows[i]["三位码"]))
        //        {
        //            threeCode = "";
        //        }
        //        else
        //        {
        //            threeCode = dt.Rows[i]["三位码"].ToString();
        //        }

        //        fatoryCodeHashtable.Add(fatoryCode, threeCode);

        //    }
        //    return fatoryCodeHashtable;
        //}
        /// <summary>
        /// 根据订单号和工位获得主物料号
        /// [MES_计划BOM_发动机装配指南]
        /// </summary>
        /// <param name="orderForm">订单号</param>
        /// <param name="OpName">工位号</param>
        /// <returns></returns>
        public static string GetMainMaterialCode(string orderForm, string OpName)
        {
            string mainMaterialNumber="";
            DataTable dt;
            string sql;
            sql = "SELECT * FROM MES_计划BOM_发动机装配指南 " +
                " WHERE 订单号 = '" + orderForm + "'" +
                " AND 工位号 = '" + OpName + "'" +
                " AND  是否主物料= 1";
            //if (!SQLCommon.SqlServerConnectionStatus)
            //{
            //    DataAccess.ExecuteDataTable_OleDb_Access(sql, out  dt);
            //}
            //else
            {
                DataAccess.ExecuteDataTable(sql, out  dt);
            }
            if (dt == null)
            {
                mainMaterialNumber = "";
            }
            if (dt.Rows.Count > 0)
            {
                mainMaterialNumber = dt.Rows[0]["物料号"].ToString().Trim();
            }
            return mainMaterialNumber;
        }

        /// <summary>
        /// 根据订单号和工位获得装配指南信息
        /// [MES_计划BOM_订单计划下达]
        /// 同步通讯，从SqlServer数据库获得
        /// 异步通讯，从Access数据库获得
        /// </summary>
        /// <param name="orderForm">订单号</param>
        /// <param name="OpName">工位号</param>
        /// <param name="isVerify">(true 是验证物料)(false 是全部物料)</param>
        /// <returns></returns>
        public static DataTable GetMaterialList(string orderForm, string OpName, bool isVerify)
        {
            string errorMessage;
            DataTable dt;
            string sql;
            if (isVerify)
            {
                sql = "SELECT [ID], [订单号], " +
               "[主配厂家],[工位号], [工位名称], [物料号], [零件图号], [零件名称], [零件数量],[是否扫描],d.扫描方式代码,d.扫描方式,[是否主物料],[厂家代码], [生产厂家], " +
               "[操作指导],'' as 发动机号,'' as 追踪代码,'' as 主物料号,0 as 物料号表索引号,0 as 零件数量验证,"
            + "'' as 物料条码,'' as 物料条码物料号,'' as 物料条码批次,0 as 验证结果,"
            + "'' as 操作者工号,'' as 操作者姓名,'' as 班次代码,"
            + "b.物料来源代码, b.物料来源, a.是否禁用代码, a.是否禁用,被强制验证物料号,更改代码,更改原因 "
                + " FROM MES_计划BOM_订单计划下达_扫描方式 AS d INNER JOIN (MES_计划BOM_订单计划下达_是否禁用 AS a INNER JOIN (MES_计划BOM_订单计划下达_物料来源 AS b INNER JOIN MES_计划BOM_发动机装配指南 AS c ON b.物料来源代码 = c.物料来源代码) ON a.是否禁用代码 = c.是否禁用代码) ON c.扫描方式=d.扫描方式代码 "
                + "where 订单号='" + orderForm + "'"
                + "and 工位号='" + OpName + "' and a.是否禁用代码 = 0 and 是否扫描 <> 0 "
                + " ORDER BY 物料号 ";

                sql = "SELECT [ID],[订单号],"
                    + "[主配厂家],[工位号], [工位名称], [物料号], [零件图号], [零件名称],[零件数量],"
                    + "[是否扫描],扫描方式 as 扫描方式代码,'' as 扫描方式,是否主物料,[厂家代码], [生产厂家], "
                    + "'' as 操作指导,'' as 发动机号,'' as 追踪代码,'' as 主物料号,0 as 物料号表索引号,0 as 零件数量验证,"
                    + "'' as 物料条码,'' as 物料条码物料号,'' as 物料条码批次,0 as 验证结果,'' as 操作者工号,'' as 操作者姓名,'' as 班次代码,"
                    + " 物料来源代码,'' as 物料来源,是否禁用代码,'' as 是否禁用, 被强制验证物料号,更改代码,更改原因"
                    + " from MES_计划BOM_发动机装配指南" 
                    + " where 是否扫描 <> 0"
                    + " and 订单号='" + orderForm + "'"
                    +"and 工位号='" + OpName + "'";

                //                + "([订单号],"
                //+ "[主配厂家],[工位号], [工位名称],[物料号], [零件图号], [零件名称], [零件数量],"
                //+ "[是否扫描],[扫描方式代码], [扫描方式],[是否主物料], [厂家代码],[生产厂家],  "
                //+ "[操作指导],[发动机号],[追踪代码],[主物料号],[物料号表索引号],[零件数量验证],"
                //+ "[物料条码], [物料条码物料号], [物料条码批次],[验证结果], [操作者工号], [操作者姓名], [班次代码], "
                //+ "物料来源代码,物料来源,是否禁用代码,是否禁用,被强制验证物料号,更改代码,更改原因)"
//                SELECT [ID],[订单号],[主配厂家],[工位号], [工位名称], [物料号], [零件图号], [零件名称],[零件数量],[是否扫描],
//是否主物料,[厂家代码], [生产厂家], 0 as 零件数量验证,'' as 物料条码,'' 
//as 物料条码物料号,'' as 物料条码批次,0 as 验证结果,被强制验证物料号,更改代码,更改原因,是否禁用代码,物料来源代码,扫描方式
//from MES_计划BOM_发动机装配指南
//where 订单号='100080211'and 工位号='OP3120'
            }
            else
            {
                sql = "SELECT [ID],[订单号]," +
                   "[主配厂家],[工位号], [工位名称], [物料号], [零件图号], [零件名称], [零件数量],[是否扫描],d.扫描方式代码,d.扫描方式,是否主物料,[厂家代码], [生产厂家], " +
                   "[操作指导],'' as 发动机号,'' as 主物料号,0 as 物料号表索引号,0 as 零件数量验证,"
                + "'' as 物料条码,'' as 物料条码物料号,'' as 物料条码批次,0 as 验证结果,"
                + "'' as 操作者工号,'' as 操作者姓名,'' as 班次代码,"
                + "b.物料来源代码, b.物料来源, a.是否禁用代码, a.是否禁用,被强制验证物料号,更改代码,更改原因 "
                    + " FROM MES_计划BOM_订单计划下达_扫描方式 AS d INNER JOIN (MES_计划BOM_订单计划下达_是否禁用 AS a INNER JOIN (MES_计划BOM_订单计划下达_物料来源 AS b INNER JOIN MES_计划BOM_发动机装配指南 AS c ON b.物料来源代码 = c.物料来源代码) ON a.是否禁用代码 = c.是否禁用代码) ON c.扫描方式=d.扫描方式代码 "
                    + "where 订单号='" + orderForm + "'"
                    + "and 工位号='" + OpName + "' and a.是否禁用代码 = 0 "
                    + " ORDER BY 物料号 ";
                sql = "SELECT [ID],[订单号],"
                    + "[主配厂家],[工位号], [工位名称], [物料号], [零件图号], [零件名称],[零件数量],"
                    + "[是否扫描],扫描方式 as 扫描方式代码,'' as 扫描方式,是否主物料,[厂家代码], [生产厂家], "
                    + "'' as 操作指导,'' as 发动机号,'' as 追踪代码,'' as 主物料号,0 as 物料号表索引号,0 as 零件数量验证,"
                    + "'' as 物料条码,'' as 物料条码物料号,'' as 物料条码批次,0 as 验证结果,'' as 操作者工号,'' as 操作者姓名,'' as 班次代码,"
                    + " 物料来源代码,'' as 物料来源,是否禁用代码,'' as 是否禁用, 被强制验证物料号,更改代码,更改原因"
                    + " from MES_计划BOM_发动机装配指南" 
           + " where 订单号='" + orderForm + "'"
           + "and 工位号='" + OpName + "'";
            }
            //if (!SQLCommon.SqlServerConnectionStatus)
            //{
            //    SQLCommon.ExecuteDataTable_OleDb(sql, ApplicationConfig.ConnectionString_Access, out dt, out errorMessage);
            //}
            //else
            {
                DataAccess.ExecuteDataTable(sql, out dt);
            }
            return dt;
        }
        /// <summary>
        /// MES_装配数据_零部件追溯_临时状态_增加
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        //public static void VerifyMaterialTempTableInsert(DataTable dt)
        //{
        //    string sql;
        //    sql = "INSERT INTO [MES_装配数据_零部件追溯_临时状态]"
        //        + "([订单号],"
        //        + "[主配厂家],[工位号], [工位名称],[物料号], [零件图号], [零件名称], [零件数量],"
        //        + "[是否扫描],[扫描方式代码], [扫描方式],[是否主物料], [厂家代码],[生产厂家],  "
        //        + "[操作指导],[发动机号],[追踪代码],[主物料号],[物料号表索引号],[零件数量验证],"
        //        + "[物料条码], [物料条码物料号], [物料条码批次],[验证结果], [操作者工号], [操作者姓名], [班次代码], "
        //        + "物料来源代码,物料来源,是否禁用代码,是否禁用,被强制验证物料号,更改代码,更改原因)"

        //        + "VALUES(@订单号,"
        //        + "@主配厂家,@工位号,@工位名称,@物料号,@零件图号,@零件名称,@零件数量,"
        //        + " @是否扫描,@扫描方式代码,@扫描方式,@是否主物料,@厂家代码,@生产厂家,"
        //        + "@操作指导,@发动机号,@追踪代码,@主物料号,@物料号表索引号,@零件数量验证,"
        //        + "@物料条码,@物料条码物料号,@物料条码批次,@验证结果,@操作者工号,@操作者姓名,@班次代码,"
        //        + "@物料来源代码,@物料来源,@是否禁用代码,@是否禁用,@被强制验证物料号,@更改代码,@更改原因)";


        //    OleDbParameter[] thisParms1 = new OleDbParameter[34];
        //    //    + "([订单号] "
        //    thisParms1[0] = new OleDbParameter("@订单号", OleDbType.VarChar, 50, "订单号");

        //    //    + "@主配厂家,@工位号,@工位名称,@物料号,@零件图号,@零件名称,@零件数量,"

        //    thisParms1[1] = new OleDbParameter("@主配厂家", OleDbType.VarChar, 50, "主配厂家");
        //    thisParms1[2] = new OleDbParameter("@工位号", OleDbType.VarChar, 50, "工位号");
        //    thisParms1[3] = new OleDbParameter("@工位名称", OleDbType.VarChar, 50, "工位名称");
        //    thisParms1[4] = new OleDbParameter("@物料号", OleDbType.VarChar, 50, "物料号");
        //    thisParms1[5] = new OleDbParameter("@零件图号", OleDbType.VarChar, 50, "零件图号");
        //    thisParms1[6] = new OleDbParameter("@零件名称", OleDbType.VarChar, 50, "零件名称");
        //    thisParms1[7] = new OleDbParameter("@零件数量", OleDbType.Integer, 4, "零件数量");

        //    //    +"[是否扫描],扫描方式代码, [扫描方式], [厂家代码],[生产厂家],  "

        //    thisParms1[8] = new OleDbParameter("@是否扫描", OleDbType.Integer, 4, "是否扫描");
        //    thisParms1[9] = new OleDbParameter("@扫描方式代码", OleDbType.Integer, 4, "扫描方式代码");
        //    thisParms1[10] = new OleDbParameter("@扫描方式", OleDbType.VarChar, 50, "扫描方式");
        //    thisParms1[11] = new OleDbParameter("@是否主物料", OleDbType.Integer, 4, "是否主物料");
        //    thisParms1[12] = new OleDbParameter("@厂家代码", OleDbType.VarChar, 50, "厂家代码");
        //    thisParms1[13] = new OleDbParameter("@生产厂家", OleDbType.VarChar, 50, "生产厂家");

        //    //+ "@操作指导,@发动机号,@物料号表索引号,@零件数量验证,"

        //    thisParms1[14] = new OleDbParameter("@操作指导", OleDbType.VarChar, 255, "操作指导");
        //    thisParms1[15] = new OleDbParameter("@发动机号", OleDbType.VarChar, 50, "发动机号");
        //    thisParms1[16] = new OleDbParameter("@追踪代码", OleDbType.VarChar, 50, "追踪代码");
        //    thisParms1[17] = new OleDbParameter("@主物料号", OleDbType.VarChar, 50, "主物料号");
        //    thisParms1[18] = new OleDbParameter("@物料号表索引号", OleDbType.Integer, 4, "物料号表索引号");
        //    thisParms1[19] = new OleDbParameter("@零件数量验证", OleDbType.Integer, 4, "零件数量验证");

        //    //     +"[物料条码], [物料条码物料号], [物料条码批次],@验证结果, [操作者工号], [操作者姓名], [班次代码], "
        //    thisParms1[20] = new OleDbParameter("@物料条码", OleDbType.VarChar, 50, "物料条码");
        //    thisParms1[21] = new OleDbParameter("@物料条码物料号", OleDbType.VarChar, 50, "物料条码物料号");
        //    thisParms1[22] = new OleDbParameter("@物料条码批次", OleDbType.VarChar, 50, "物料条码批次");
        //    thisParms1[23] = new OleDbParameter("@验证结果", OleDbType.Integer, 4, "验证结果");
        //    thisParms1[24] = new OleDbParameter("@操作者工号", OleDbType.VarChar, 50, "操作者工号");
        //    thisParms1[25] = new OleDbParameter("@操作者姓名", OleDbType.VarChar, 50, "操作者姓名");
        //    thisParms1[26] = new OleDbParameter("@班次代码", OleDbType.VarChar, 50, "班次代码");

        //    //    +"物料来源代码,物料来源,是否禁用代码,是否禁用,被强制验证物料号,更改代码,更改原因)"

        //    thisParms1[27] = new OleDbParameter("@物料来源代码", OleDbType.Integer, 4, "物料来源代码");
        //    thisParms1[28] = new OleDbParameter("@物料来源", OleDbType.VarChar, 50, "物料来源");
        //    thisParms1[29] = new OleDbParameter("@是否禁用代码", OleDbType.Integer, 4, "是否禁用代码");
        //    thisParms1[30] = new OleDbParameter("@是否禁用", OleDbType.VarChar, 50, "是否禁用");
        //    thisParms1[31] = new OleDbParameter("@被强制验证物料号", OleDbType.VarChar, 50, "被强制验证物料号");
        //    thisParms1[32] = new OleDbParameter("@更改代码", OleDbType.VarChar, 50, "更改代码");
        //    thisParms1[33] = new OleDbParameter("@更改原因", OleDbType.VarChar, 255, "更改原因");

        //    DataAccess.ExecuteDataTable_OleDb_Insert_Access(sql, dt, ref  thisParms1);
        //}



        /// <summary>
        /// 查询订单号对应的订单数据
        /// [MES_计划BOM_发动机号]
        /// </summary>
        /// <param name="orderForm"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static bool EngineSelectByOrderForm(string orderForm, out DataTable dt)
        {
            string sql;
            sql = "SELECT TOP 1 * FROM MES_计划BOM_发动机号 WHERE 订单号='" + orderForm+"'";
            //if (!SQLCommon.SqlServerConnectionStatus)
            //{
            //    return DataAccess.ExecuteDataTable_OleDb_Access(sql, out  dt);
            //}
            //else
            {
                return DataAccess.ExecuteDataTable(sql, out  dt);

            }
        }


        /// <summary>
        /// 根据追踪代码查询订单号对应的订单数据
        /// [MES_装配数据_发动机号_追踪代码]
        /// </summary>
        /// <param name="engineTraceCode"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        //public static bool GetOrderFormByEngineTraceCode(string engineTraceCode, out DataTable dt)
        //{
        //    string sql;
        //    sql = "SELECT TOP 1 * FROM MES_装配数据_发动机号_追踪代码 WHERE 追踪代码='" + engineTraceCode + "'";
        //    return DataAccess.ExecuteDataTable_OleDb_Access(sql, out  dt);
        //}



    }
}
