using bizFacade;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SyUpLoad
{
    public partial class PartLoad
    {
        /// <summary>
        /// 获取自动或手动，并以对应的方式创建总成号
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="engineType">产品型号</param>
        /// <param name="engineID">产品编号</param>
        private void CreateEngineID(string OpName, out string engineType, out int engineTypeID, out string engineID, out string orderForm)
        {
            //string orderForm = "";//订单号
            string repairCode = "";//返修代码
            string engineID2 = "";
            string palletCode = "";//托盘编号

            int palletCode_PLC = 0;
            int isRepair = 0;
            int engineNumber = 0;
            int IsAutoOrManul = 0;//0为自动方式，1为手动方式

            DataTable dt_EngineType = null;
            DataTable dt_temp_isAutoOrManul;//查询创建总成编号的table

            engineType = "";
            engineID = "";

            //获取订单号，发动机型号，发动机型号代码(在线首工位将选择的订单，机型等信息保存到监控表中)
            BaseDataSystemMES.OpName_MIS_Moby_Select(OpName, out orderForm, out engineTypeID, out engineType, out engineID2, out palletCode, out isRepair, out repairCode);
            //MIS_BASE.Read_PalletCode(OpName, out palletCode_PLC);//获取托盘编号



            //以自动或手动方式创建发动机号
            CreateEngineID(OpName, IsAutoOrManul, orderForm, engineType, engineTypeID, palletCode_PLC.ToString(), engineNumber, out dt_EngineType, out engineID);

        }


        /// <summary>
        /// 以自动或手动方式创建工件编号
        /// </summary>
        /// <param name="opName">工位号</param>
        /// <param name="IsAutoOrManul">自动手动</param>
        /// <param name="orderForm">订单号</param>
        /// <param name="engineType">产品型号</param>
        /// <param name="engineTypeID">产品型号代码</param>
        /// <param name="palletCode">托盘编号</param>
        /// <param name="engineYear">手动创建_年</param>
        /// <param name="engineMoon">手动创建_月</param>
        /// <param name="engineDay">手动创建_日</param>
        /// <param name="engineNumber">流水号</param>
        /// <param name="dt_EngineType">返回信息</param>
        /// <param name="engineID">产品编号</param>
        /// <param name="printBarCode1_MES"></param>
        /// <param name="printBarCode2_MES"></param>
        private void CreateEngineID(string opName, int IsAutoOrManul, string orderForm, string engineType, int engineTypeID, string palletCode, int engineNumber, out DataTable dt_EngineType, out string engineID)
        {
            string engineYear = "";
            string engineMoon = "";
            string engineDay = "";

            DataTable dt_MaualPrint;

            dt_EngineType = null;
            engineID = "";

                //自动方式创建工件编号
            CreateEngineID_Auto(opName, orderForm, engineType, engineTypeID, palletCode, out dt_EngineType, out engineID);


            if (!GetEngineID_Save(opName, IsAutoOrManul, orderForm, engineType, engineTypeID, engineID, palletCode.ToString(), dt_EngineType, engineYear, engineMoon, engineDay))
            {
                engineID = "";
                engineTypeID = -1;
                engineType = "";
                //AsyncSendMessage_Notice(opName, "保存工件信息出错...");
                return;
            }
        }

        /// <summary>
        /// 自动方式创建工件编号
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="orderForm"></param>
        /// <param name="engineType"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="palletCode">托盘号</param>
        /// <param name="dt_EngineType"></param>
        /// <param name="engineID">发动机号</param>
        private void CreateEngineID_Auto(string opName, string orderForm, string engineType, int engineTypeID, string palletCode, out DataTable dt_EngineType, out string engineID)
        {
            dt_EngineType = null;
            engineID = "";
            BaseDataSystemMES.GetEngineID_Auto_branch(opName, engineType, palletCode, out dt_EngineType);

            if (dt_EngineType != null && dt_EngineType.Rows.Count > 0)
            {
                engineID = dt_EngineType.Rows[0]["发动机号"].ToString();

            }
        }

        /// <summary>
        /// 手动方式创建总成号
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="orderForm"></param>
        /// <param name="engineType"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="palletCode"></param>
        /// <param name="engineYear"></param>
        /// <param name="engineMoon"></param>
        /// <param name="engineDay"></param>
        /// <param name="engineNumber"></param>
        /// <param name="dt_EngineType"></param>
        /// <param name="engineID"></param>
        private void CreateEngineID_Manul(string opName, string orderForm, string engineType, int engineTypeID, string palletCode,
            string engineYear, string engineMoon, string engineDay, int engineNumber, out DataTable dt_EngineType, out string engineID)
        {
            dt_EngineType = null;
            engineID = "";

            //手动方式创建
            BaseDataSystemMES.GetEngineID_Manual_branch(opName, engineType, engineYear, engineMoon, engineDay, engineNumber, true, out dt_EngineType);
            if (dt_EngineType != null)
            {
                if (dt_EngineType.Rows.Count > 0)
                {
                    engineID = dt_EngineType.Rows[0]["发动机号"].ToString();
                }
            }
        }


        /// <summary>
        /// 电子看板_发动机号_线首工位_增加
        /// </summary>
        /// <param name="opName"></param>
        /// <param name="IsAutoOrManul"></param>
        /// <param name="orderForm"></param>
        /// <param name="engineType"></param>
        /// <param name="engineTypeID"></param>
        /// <param name="engineID"></param>
        /// <param name="partPalletCode"></param>
        /// <param name="dt_EngineType"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        private bool GetEngineID_Save(string opName, int IsAutoOrManul,
            string orderForm, string engineType, int engineTypeID, string engineID, string partPalletCode, DataTable dt_EngineType,
            string year, string month, string day
            )
        {
            if (IsAutoOrManul == 1)
            {
                string EngineID_DateTime = year + "-" + month + "-" + day + " 00:00:00";
                return BaseDataSystemMES.GetEngineID_ManulEngine_Save(opName, orderForm, partPalletCode, engineType, EngineID_DateTime, dt_EngineType);
            }
            else
            {

                return BaseDataSystemMES.GetEngineID_Save(opName, orderForm, partPalletCode, engineType, dt_EngineType);


            }
        }
    }
}
