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

using System.Data.OleDb;
using DataLinkMesWork;
//using SystemFramework;

namespace bizFacade
{
    /// <summary>
    /// 关键零部件追溯
    /// </summary>
    public partial class BaseDataSystemMES
    {
        /// <summary>
        /// 根据工位号，将计划下达到工位PC
        /// [MES_计划BOM_订单计划下达_执行顺序]
        /// [MES_计划BOM_发动机号] 
        /// [MES_计划BOM_发动机装配指南]
        /// </summary>
        /// <param name="opName"></param>
        //public static void GetOrderForm(string opName)
        //{
        //    //订单号
        //    string orderForm;
        //    //1：获得需要下达的订单列表
        //    DataTable dt_orderForm;
        //    OrderFormOpName_Select(opName, out dt_orderForm);
        //    if (dt_orderForm != null)
        //    {
        //        for (int i = 0; i < dt_orderForm.Rows.Count; i++)
        //        {
        //            try
        //            {
        //                orderForm = dt_orderForm.Rows[i]["订单号"].ToString();
        //                //2：根据订单号，工位号，获得[MES_计划BOM_订单计划下达_执行顺序]
        //                //[MES_计划BOM_发动机号] 
        //                //[MES_计划BOM_发动机装配指南]
        //                DataTable dt_OrderFormOrderBy;
        //                DataTable dt_EngineID;
        //                DataTable dt_OperationGuid;
        //                GetOrderPlanEngineNumber_OrderForm(orderForm, out dt_OrderFormOrderBy);
        //                GetOrderPlanEngineNumberToPcStation(orderForm, out dt_EngineID);
        //                GetOperationGuid(orderForm, opName, out dt_OperationGuid);

        //                //3将数据导入到工位PC

        //                if (!GetOrderPlanEngineNumberToPcStation_OrderForm_Delete(orderForm)) continue;
        //                if(!GetOrderPlanEngineNumberToPcStation_Delete(orderForm)) continue;
        //                if(!GetOperationGuidToPcStation_Delete(orderForm)) continue;

        //                if(!GetOrderPlanEngineNumberToPcStation_OrderForm(dt_OrderFormOrderBy)) continue;
        //                if(!GetOrderPlanEngineNumberToPcStation(dt_EngineID)) continue;
        //                if(!GetOperationGuidToPcStation(dt_OperationGuid)) continue;

        //                OrderFormOpName_Update(orderForm, opName);
        //            }
        //            catch (Exception err)
        //            {
        //                //ApplicationLog.WriteLog(err,err.Message);
        //            }

        //        }
        //    }
        //}
        /// <summary>
        /// "delete from [MES_计划BOM_订单计划下达_执行顺序] where 订单号='" + orderForm + "'";
        /// </summary>
        /// <param name="orderForm"></param>
        //private static bool GetOrderPlanEngineNumberToPcStation_OrderForm_Delete(string orderForm)
        //{
        //    string sql;
        //    sql = "delete from [MES_计划BOM_订单计划下达_执行顺序] where 订单号='" + orderForm + "'";
        //    return DataAccess.ExecuteSQL_OleDb_Access(sql);
        //}
        /// <summary>
        /// "delete from [MES_计划BOM_发动机号] where 订单号='" + orderForm + "'";
        /// </summary>
        /// <param name="orderForm"></param>
        //private static bool GetOrderPlanEngineNumberToPcStation_Delete(string orderForm)
        //{
        //    string sql;
        //    sql = "delete from [MES_计划BOM_发动机号] where 订单号='" + orderForm + "'";
        //    return DataAccess.ExecuteSQL_OleDb_Access(sql);
        //}
        /// <summary>
        /// "delete from [MES_计划BOM_订单计划下达_执行顺序] where 订单号='" + orderForm + "'";
        /// </summary>
        /// <param name="orderForm"></param>
        //private static bool GetOperationGuidToPcStation_Delete(string orderForm)
        //{
        //    string sql;
        //    sql = "delete from [MES_计划BOM_发动机装配指南] where 订单号='" + orderForm + "'";
        //    return DataAccess.ExecuteSQL_OleDb_Access(sql);
        //}
        /// <summary>
        /// exec MES_计划BOM_发动机订单计划_下达工位_修改
        /// set 同步状态=1,同步日期=getdate()
        /// </summary>
        /// <param name="orderForm"></param>
        /// <param name="opName"></param>
        public static void OrderFormOpName_Update(string orderForm,string opName)
        {
            string sql;
            sql = "exec MES_计划BOM_发动机订单计划_下达工位_修改 '"+orderForm+"','"+opName+"'";
            DataAccess.ExecuteSQL(sql);
        }
        /// <summary>
        /// 获得工位对应的订单号
        /// MES_计划BOM_发动机订单计划_下达工位
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="dt"></param>
        public static void OrderFormOpName_Select(string opName, out DataTable dt)
        {
            string sql;

            

                sql = "select 订单号 from  MES_计划BOM_发动机订单计划_下达工位 "
              + " where 同步状态 = 0 and 工位号='" + opName + "'";
                DataAccess.ExecuteDataTable(sql, out dt);

            

        }
        /// <summary>
        /// 下达装配BOM到工位Access文件 INSERT INTO [MES_计划BOM_发动机装配指南]  
        /// </summary>
        /// <param name="dt"></param>
        //static public bool GetOperationGuidToPcStation(DataTable dt)
        //{
        //    string sql;
        //    sql =
        //        "INSERT INTO [MES_计划BOM_发动机装配指南]" +
        //        "([订单号], [主配厂家],[工位号], [工位名称], [物料号], [零件图号], [零件名称]," +
        //        " [零件数量],[是否扫描],[扫描方式],[是否主物料],[厂家代码], [生产厂家],[操作指导] )" +
        //        "VALUES(@订单号,@主配厂家,@工位号,@工位名称,@物料号,@零件图号,@零件名称," +
        //        "@零件数量,@是否扫描,@扫描方式,@是否主物料,@厂家代码,@生产厂家,@操作指导)";
        //    OleDbParameter[] thisParms = new OleDbParameter[14];
        //    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, 255, "工位名称");
        //    thisParms[4] = new OleDbParameter("@物料号", OleDbType.VarChar, 50, "物料号");
        //    thisParms[5] = new OleDbParameter("@零件图号", OleDbType.VarChar, 50, "零件图号");
        //    thisParms[6] = new OleDbParameter("@零件名称", OleDbType.VarChar, 255, "零件名称");
        //    thisParms[7] = new OleDbParameter("@零件数量", OleDbType.Integer, 4, "零件数量");
        //    thisParms[8] = new OleDbParameter("@是否扫描", OleDbType.Integer, 4, "是否扫描");
        //    thisParms[9] = new OleDbParameter("@扫描方式", OleDbType.Integer, 4, "扫描方式");
        //    thisParms[10] = new OleDbParameter("@是否主物料", OleDbType.Integer, 4, "是否主物料");
        //    thisParms[11] = new OleDbParameter("@厂家代码", OleDbType.VarChar, 255, "厂家代码");
        //    thisParms[12] = new OleDbParameter("@生产厂家", OleDbType.VarChar, 255, "生产厂家");
        //    thisParms[13] = new OleDbParameter("@操作指导", OleDbType.VarChar, 255, "操作指导");

        //    return DataAccess.ExecuteDataTable_OleDb_Insert_Access(sql, dt, ref thisParms);

        //}
        /// <summary>
        /// 下达装配BOM的发动机号到工位Access文件 INSERT INTO [MES_计划BOM_发动机号]  
        /// </summary>
        /// <param name="dt"></param>
        //static public bool GetOrderPlanEngineNumberToPcStation(DataTable dt)
        //{
        //    string sql;
        //    sql =
        //        "INSERT INTO [MES_计划BOM_发动机号]" +
        //        "([发动机号], [订单号], [SAP物料号], [发动机型号],  [发动机型号代码], [发动机机型]," +
        //        "[发动机数量], [厂牌编号起始号], [客户名称], [投料日期], [文件编号], [集团名称], [通知号], " +
        //        "[功率], [指南文件编号], [装机状态], [适用状态], [物料描述],[计划下达日期])" +
        //        "VALUES(@发动机号,@订单号,@SAP物料号,@发动机型号,@发动机型号代码,@发动机机型," +
        //        "@发动机数量,@厂牌编号起始号,@客户名称,@投料日期,@文件编号,@集团名称,@通知号," +
        //        "@功率,@指南文件编号,@装机状态,@适用状态,@物料描述,@计划下达日期)";
        //    OleDbParameter[] thisParms = new OleDbParameter[19];
        //    thisParms[0] = new OleDbParameter("@发动机号", OleDbType.VarChar, 50, "发动机号");
        //    thisParms[1] = new OleDbParameter("@订单号", OleDbType.VarChar, 50, "订单号");
        //    thisParms[2] = new OleDbParameter("@SAP物料号", OleDbType.VarChar, 50, "SAP物料号");
        //    thisParms[3] = new OleDbParameter("@发动机型号", OleDbType.VarChar, 50, "发动机型号");
        //    thisParms[4] = new OleDbParameter("@发动机型号代码", OleDbType.Integer, 4, "发动机型号代码");
        //    thisParms[5] = new OleDbParameter("@发动机机型", OleDbType.VarChar, 50, "发动机机型");

        //    thisParms[6] = new OleDbParameter("@发动机数量", OleDbType.Integer, 4, "发动机数量");
        //    thisParms[7] = new OleDbParameter("@厂牌编号起始号", OleDbType.VarChar, 50, "厂牌编号起始号");
        //    thisParms[8] = new OleDbParameter("@客户名称", OleDbType.VarChar, 255, "客户名称");
        //    thisParms[9] = new OleDbParameter("@投料日期", OleDbType.DBDate, 8, "投料日期");
        //    thisParms[10] = new OleDbParameter("@文件编号", OleDbType.VarChar, 50, "文件编号");
        //    thisParms[11] = new OleDbParameter("@集团名称", OleDbType.VarChar, 255, "集团名称");
        //    thisParms[12] = new OleDbParameter("@通知号", OleDbType.VarChar, 50, "通知号");

        //    thisParms[13] = new OleDbParameter("@功率", OleDbType.VarChar, 50, "功率");
        //    thisParms[14] = new OleDbParameter("@指南文件编号", OleDbType.VarChar, 50, "指南文件编号");
        //    thisParms[15] = new OleDbParameter("@装机状态", OleDbType.VarChar, 50, "装机状态");
        //    thisParms[16] = new OleDbParameter("@适用状态", OleDbType.VarChar, 50, "适用状态");
        //    thisParms[17] = new OleDbParameter("@物料描述", OleDbType.VarChar, 255, "物料描述");
        //    thisParms[18] = new OleDbParameter("@计划下达日期", OleDbType.DBDate, 8, "计划下达日期");

        //    return DataAccess.ExecuteDataTable_OleDb_Insert_Access(sql, dt, ref thisParms);

        //}
        /// <summary>
        /// 下达装配BOM的发动机号到工位Access文件 INSERT INTO [MES_计划BOM_订单计划下达_执行顺序]  
        /// </summary>
        /// <param name="dt"></param>
        //static public bool GetOrderPlanEngineNumberToPcStation_OrderForm(DataTable dt)
        //{
        //    string sql;
        //    sql =
        //        "INSERT INTO [MES_计划BOM_订单计划下达_执行顺序]" +
        //        "([订单号],[执行顺序],[SAP物料号], [发动机型号],  [发动机型号代码], [发动机机型]," +
        //        "[发动机数量], [厂牌编号起始号], [客户名称], [投料日期], [文件编号], [集团名称], [通知号], " +
        //        "[功率], [指南文件编号], [装机状态], [适用状态], [物料描述],[计划下达日期])" +
        //        "VALUES(@订单号,@执行顺序,@SAP物料号,@发动机型号,@发动机型号代码,@发动机机型," +
        //        "@发动机数量,@厂牌编号起始号,@客户名称,@投料日期,@文件编号,@集团名称,@通知号," +
        //        "@功率,@指南文件编号,@装机状态,@适用状态,@物料描述,@计划下达日期)";
        //    OleDbParameter[] thisParms = new OleDbParameter[19];
        //    thisParms[0] = new OleDbParameter("@订单号", OleDbType.VarChar, 50, "订单号");
        //    thisParms[1] = new OleDbParameter("@执行顺序", OleDbType.Integer, 4, "执行顺序");
        //    thisParms[2] = new OleDbParameter("@SAP物料号", OleDbType.VarChar, 50, "SAP物料号");
        //    thisParms[3] = new OleDbParameter("@发动机型号", OleDbType.VarChar, 50, "发动机型号");
        //    thisParms[4] = new OleDbParameter("@发动机型号代码", OleDbType.Integer, 4, "发动机型号代码");
        //    thisParms[5] = new OleDbParameter("@发动机机型", OleDbType.VarChar, 50, "发动机机型");

        //    thisParms[6] = new OleDbParameter("@发动机数量", OleDbType.Integer, 4, "发动机数量");
        //    thisParms[7] = new OleDbParameter("@厂牌编号起始号", OleDbType.VarChar, 50, "厂牌编号起始号");
        //    thisParms[8] = new OleDbParameter("@客户名称", OleDbType.VarChar, 255, "客户名称");
        //    thisParms[9] = new OleDbParameter("@投料日期", OleDbType.DBDate, 8, "投料日期");
        //    thisParms[10] = new OleDbParameter("@文件编号", OleDbType.VarChar, 50, "文件编号");
        //    thisParms[11] = new OleDbParameter("@集团名称", OleDbType.VarChar, 255, "集团名称");
        //    thisParms[12] = new OleDbParameter("@通知号", OleDbType.VarChar, 50, "通知号");

        //    thisParms[13] = new OleDbParameter("@功率", OleDbType.VarChar, 50, "功率");
        //    thisParms[14] = new OleDbParameter("@指南文件编号", OleDbType.VarChar, 50, "指南文件编号");
        //    thisParms[15] = new OleDbParameter("@装机状态", OleDbType.VarChar, 50, "装机状态");
        //    thisParms[16] = new OleDbParameter("@适用状态", OleDbType.VarChar, 50, "适用状态");
        //    thisParms[17] = new OleDbParameter("@物料描述", OleDbType.VarChar, 255, "物料描述");
        //    thisParms[18] = new OleDbParameter("@计划下达日期", OleDbType.DBDate, 8, "计划下达日期");

        //    return DataAccess.ExecuteDataTable_OleDb_Insert_Access(sql, dt, ref thisParms);

        //}
        /// <summary>
        /// 根据订单号，工位号获得[MES_计划BOM_发动机装配指南]  
        /// </summary>
        /// <param name="orderForm">订单号</param>
        /// <param name="opName">工位号</param>
        /// <param name="dt">MES_计划BOM_发动机装配指南</param>
        /// <returns></returns>
        static public bool GetOperationGuid(string orderForm,string opName,out DataTable dt)
        {
            string sql;
            sql = "exec MES_计划BOM_发动机装配指南_查询_下达到工位  '" + orderForm + "','" + opName + "'";
             return DataAccess.ExecuteDataTable(sql, out dt);
        }
        /// <summary>
        /// 根据订单号 [MES_计划BOM_订单计划下达_执行顺序]  
        /// </summary>
        /// <param name="orderForm">订单号</param>
        /// <param name="dt">[MES_计划BOM_订单计划下达_执行顺序] </param>
        static public bool GetOrderPlanEngineNumber_OrderForm(string orderForm, out DataTable dt)
        {
            string sql;
            sql = "exec MES_计划BOM_发动机订单计划_查询_下达到工位  '" + orderForm + "'";
            return DataAccess.ExecuteDataTable(sql, out dt);

        }
        /// <summary>
        /// 根据订单号 [MES_计划BOM_发动机号]  
        /// </summary>
        /// <param name="orderForm">订单号</param>
        /// <param name="dt">[MES_计划BOM_发动机号]</param>
        static public bool GetOrderPlanEngineNumberToPcStation(string orderForm, out DataTable dt)
        {
            string sql;
            sql = "exec MES_计划BOM_发动机号_查询_下达到工位  '" + orderForm + "'";
            return DataAccess.ExecuteDataTable(sql, out dt);
        }
    }
}
