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

//using SystemFramework;
using DataLinkMesWork;
namespace bizFacade
{
    public partial class BaseDataSystemMES
    {
        /// <summary>
        /// 保存[MES_装配数据_发动机号_追踪代码]
        /// 与SqlServer连接正常，保存到SqlServer数据库中；
        /// 与SqlServer连接不正常，保存到SqlServer数据库中；
        /// </summary>
        public static void TableTraceInsert_SaveData()
        {
            DataTable dt;
            dt = TraceCodeDataTableUpdate_Access();
            //if (!SQLCommon.SqlServerConnectionStatus)
            //{
            //    TableTraceInsert_Access(dt);
            //}
            //else
            {
                TableTraceInsert_SqlServer(dt);
            }
        }


        /// <summary>
        /// [MES_装配数据_发动机号_追踪代码]数据保存到本地PC的Access数据库中
        /// </summary>
        //private static bool TableTraceInsert_Access(DataTable dt)
        //{
        //    string sql = null;
        //    sql =
        //        "INSERT INTO [MES_装配数据_发动机号_追踪代码] " +
        //        "(订单号,订单计划发动机号,发动机号,追踪代码,主物料号,工位号,操作者工号,班次代码," +
        //        "[SAP物料号],[发动机型号],[发动机型号代码], [发动机机型])" +
        //        "VALUES(@订单号,@订单计划发动机号,@发动机号,@追踪代码,@主物料号,@工位号,@操作者工号,@班次代码," +
        //        "@SAP物料号,@发动机型号,@发动机型号代码,@发动机机型)";
        //    OleDbParameter[] thisParms = new OleDbParameter[12];
        //    thisParms[0] = new OleDbParameter("@订单号", OleDbType.VarChar, 50, "订单号");
        //    thisParms[1] = new OleDbParameter("@订单计划发动机号", OleDbType.VarChar, 50, "订单计划发动机号");
        //    thisParms[2] = new OleDbParameter("@发动机号", OleDbType.VarChar, 50, "发动机号");
        //    thisParms[3] = new OleDbParameter("@追踪代码", OleDbType.VarChar, 50, "追踪代码");
        //    thisParms[4] = new OleDbParameter("@主物料号", OleDbType.VarChar, 50, "主物料号");
        //    thisParms[5] = new OleDbParameter("@工位号", OleDbType.VarChar, 50, "工位号");
        //    thisParms[6] = new OleDbParameter("@操作者工号", OleDbType.VarChar, 50, "操作者工号");
        //    thisParms[7] = new OleDbParameter("@班次代码", OleDbType.VarChar, 50, "班次代码");
        //    thisParms[8] = new OleDbParameter("@SAP物料号", OleDbType.VarChar, 50, "SAP物料号");
        //    thisParms[9] = new OleDbParameter("@发动机型号", OleDbType.VarChar, 50, "发动机型号");
        //    thisParms[10] = new OleDbParameter("@发动机型号代码", OleDbType.Integer, 4, "发动机型号代码");
        //    thisParms[11] = new OleDbParameter("@发动机机型", OleDbType.VarChar, 50, "发动机机型");
        //    return DataAccess.ExecuteDataTable_OleDb_Insert_Access(sql, dt, ref thisParms);
        //}
        /// <summary>
        /// [MES_装配数据_发动机号_追踪代码]保存到SqlServer数据库服务器
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private static bool TableTraceInsert_SqlServer(DataTable dt)
        {
            string sql = null;
            sql =
                "INSERT INTO [MES_装配数据_发动机号_追踪代码] " +
                "(订单号,订单计划发动机号,发动机号,追踪代码,主物料号,工位号,操作者工号,班次代码," +
                "[SAP物料号],[发动机型号],[发动机型号代码], [发动机机型])" +
                "VALUES(@订单号,@订单计划发动机号,@发动机号,@追踪代码,@主物料号,@工位号,@操作者工号,@班次代码," +
                "@SAP物料号,@发动机型号,@发动机型号代码,@发动机机型)";
            SqlParameter[] thisParms = new SqlParameter[12];
            thisParms[0] = new SqlParameter("@订单号", SqlDbType.NVarChar, 50, "订单号");
            thisParms[1] = new SqlParameter("@订单计划发动机号", SqlDbType.NVarChar, 50, "订单计划发动机号");
            thisParms[2] = new SqlParameter("@发动机号", SqlDbType.NVarChar, 50, "发动机号");
            thisParms[3] = new SqlParameter("@追踪代码", SqlDbType.NVarChar, 50, "追踪代码");
            thisParms[4] = new SqlParameter("@主物料号", SqlDbType.NVarChar, 50, "主物料号");
            thisParms[5] = new SqlParameter("@工位号", SqlDbType.NVarChar, 50, "工位号");
            thisParms[6] = new SqlParameter("@操作者工号", SqlDbType.NVarChar, 50, "操作者工号");
            thisParms[7] = new SqlParameter("@班次代码", SqlDbType.NVarChar, 50, "班次代码");
            thisParms[8] = new SqlParameter("@SAP物料号", SqlDbType.NVarChar, 50, "SAP物料号");
            thisParms[9] = new SqlParameter("@发动机型号", SqlDbType.NVarChar, 50, "发动机型号");
            thisParms[10] = new SqlParameter("@发动机型号代码", SqlDbType.Int, 4, "发动机型号代码");
            thisParms[11] = new SqlParameter("@发动机机型", SqlDbType.NVarChar, 50, "发动机机型");
            return DataAccess.ExecuteSQL(sql, ref  thisParms, dt);
            //OleDbParameter[] thisParms = new OleDbParameter[12];
            //thisParms[0] = new OleDbParameter("@订单号", OleDbType.VarChar, 50, "订单号");
            //thisParms[1] = new OleDbParameter("@订单计划发动机号", OleDbType.VarChar, 50, "订单计划发动机号");
            //thisParms[2] = new OleDbParameter("@发动机号", OleDbType.VarChar, 50, "发动机号");
            //thisParms[3] = new OleDbParameter("@追踪代码", OleDbType.VarChar, 50, "追踪代码");
            //thisParms[4] = new OleDbParameter("@主物料号", OleDbType.VarChar, 50, "主物料号");
            //thisParms[5] = new OleDbParameter("@工位号", OleDbType.VarChar, 50, "工位号");
            //thisParms[6] = new OleDbParameter("@操作者工号", OleDbType.VarChar, 50, "操作者工号");
            //thisParms[7] = new OleDbParameter("@班次代码", OleDbType.VarChar, 50, "班次代码");
            //thisParms[8] = new OleDbParameter("@SAP物料号", OleDbType.VarChar, 50, "SAP物料号");
            //thisParms[9] = new OleDbParameter("@发动机型号", OleDbType.VarChar, 50, "发动机型号");
            //thisParms[10] = new OleDbParameter("@发动机型号代码", OleDbType.Integer, 4, "发动机型号代码");
            //thisParms[11] = new OleDbParameter("@发动机机型", OleDbType.VarChar, 50, "发动机机型");
            //return DataAccess.ExecuteDataTable_OleDb_Insert_SqlServer(sql, dt, ref thisParms);
        }
        /// <summary>
        /// 创建临时表[MES_装配数据_发动机号_追踪代码]
        /// </summary>
        /// <returns></returns>
        private static DataTable CreateEngineTraceCodeDataTable()
        {
            DataTable dt;
            dt = new DataTable();
            dt.Columns.Add("订单号", typeof(string));
            dt.Columns.Add("订单计划发动机号", typeof(string));
            dt.Columns.Add("发动机号", typeof(string));
            dt.Columns.Add("追踪代码", typeof(string));
            dt.Columns.Add("主物料号", typeof(string));
            dt.Columns.Add("工位号", typeof(string));
            dt.Columns.Add("操作者工号", typeof(string));
            dt.Columns.Add("班次代码", typeof(string));
            dt.Columns.Add("SAP物料号", typeof(string));
            dt.Columns.Add("发动机型号", typeof(string));
            dt.Columns.Add("发动机型号代码", typeof(int));
            dt.Columns.Add("发动机机型", typeof(string));
            return dt;
        }
        /// <summary>
        /// 上线工位的[MES_装配数据_发动机号_追踪代码]数据
        /// </summary>
        /// <returns></returns>
        private DataTable GetTraceCodeDataTable()
        {
            DataTable dt;
            dt = CreateEngineTraceCodeDataTable();
            DataRow dr;
            dr = dt.NewRow();
            dr["订单号"] = MaterialData.orderForm;
            dr["订单计划发动机号"] = MaterialData.engineNumber_OrderPlan;
            dr["发动机号"] = MaterialData.engineNumber;
            dr["追踪代码"] = MaterialData.engineTraceCode;
            dr["主物料号"] = MaterialData.mainMaterialNumber;
            dr["工位号"] = MaterialData.opName;
            dr["操作者工号"] = MaterialData.operatorCode;
            dr["班次代码"] = MaterialData.operatorShift;
            dr["SAP物料号"] = MaterialData.sAPMaterialNumber;
            dr["发动机型号"] = MaterialData.engineNumberType;
            dr["发动机型号代码"] = MaterialData.engineNumberCode;
            dr["发动机机型"] = MaterialData.engineNumberBasicType;
            dt.Rows.Add(dr);
            return dt;
        }
        /// <summary>
        /// 上线工位的[MES_装配数据_发动机号_追踪代码]数据
        /// 从本工位
        /// </summary>
        /// <returns></returns>
        //public static bool GetTraceCodeDataTableFromAccess(string opName, out DataTable dt)
        //{
        //    string sql;
        //    sql = "select 订单号,订单计划发动机号,发动机号,追踪代码,主物料号,工位号,操作者工号,班次代码,SAP物料号,发动机型号,发动机型号代码,发动机机型"
        //        + " from  MES_装配数据_发动机号_追踪代码 where IsOK=0 and 工位号='" + opName + "'";
        //    return DataAccess.ExecuteDataTable_OleDb_Access(sql, out dt);
        //}
        /// <summary>
        /// 更新IsOK = 1 [MES_装配数据_发动机号_追踪代码]数据
        /// </summary>
        /// <returns></returns>
        //public static bool GetTraceCodeDataTableFromAccess_Update(string opName)
        //{
        //    string sql;
        //    sql = "update MES_装配数据_发动机号_追踪代码 set IsOK = 1 where IsOK=0 and 工位号='" + opName + "'";
        //    return DataAccess.ExecuteSQL_OleDb_Access(sql);
        //}

        /// <summary>
        /// 数据库同步，应用于分装线线首工位
        /// [数据MES_装配数据_发动机号_追踪代码_下达工位]
        /// 从到Access中获得[数据MES_装配数据_发动机号_追踪代码_下达工位]数据，
        /// 保存SqlServer数据库服务器，同时更新[是否同步]标志。
        /// </summary>
        /// <param name="opName">当前工位</param>
        /// <returns></returns>
        //public static void GetTraceCodeDataTableFromAccess_OpName_InsertIntoSqlServer(string opName)
        //{
        //    DataTable dt;
        //    if (GetTraceCodeDataTableFromAccess(opName, out dt))
        //    {
        //        if (TableTraceInsert_SqlServer(dt))
        //        {
        //            GetTraceCodeDataTableFromAccess_Update(opName);
        //        }
        //    }
        //}
        /// <summary>
        /// 上线工位的[MES_装配数据_发动机号_追踪代码]
        /// 对于分线上线工位，要将IsOK=0的行上传到分装部件的总线上线工位；上传后，该值置1。
        /// </summary>
        /// <returns></returns>
        private static DataTable TraceCodeDataTableUpdate_Access()
        {

            DataTable dt;
            dt = CreateEngineTraceCodeDataTable();
            DataRow dr;
            dr = dt.NewRow();
            dr["订单号"] = MaterialData.orderForm;
            dr["订单计划发动机号"] = MaterialData.engineNumber_OrderPlan;
            dr["发动机号"] = MaterialData.engineNumber;
            dr["追踪代码"] = MaterialData.engineTraceCode;
            dr["主物料号"] = MaterialData.mainMaterialNumber;
            dr["工位号"] = MaterialData.opName;
            dr["操作者工号"] = MaterialData.operatorCode;
            dr["班次代码"] = MaterialData.operatorShift;
            dr["SAP物料号"] = MaterialData.sAPMaterialNumber;
            dr["发动机型号"] = MaterialData.engineNumberType;
            dr["发动机型号代码"] = MaterialData.engineNumberCode;
            dr["发动机机型"] = MaterialData.engineNumberBasicType;
            dt.Rows.Add(dr);
            return dt;
        }
    }
}
