using System;
using tcEAI_C;
using iMESCore.Settings;
using System.Collections;
using System.Collections.Generic;
using static iMESCore.Base.iMESConst;

namespace EAIws_C
{

    static class modERP
    {

        public static string strService = "ERP";
        public static string strDataBaseType;
        public static string strConnectionString;

        public static string funCreateOperation(string InXml)
        {
            string funCreateOperationRet = default(string);
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();    // 用以讀取Xml字串
            string OutXml = null;

            string strIdentity = string.Empty;    // Identity XML字串
            string strResult = "fail";            // Result XML字串
            List<ArrayList> strException = new List<ArrayList>();  // Exception XML字串
            List<ArrayList> strMessage = new List<ArrayList>();    // Message XML字串
            string strKeyValue = string.Empty;    // TransactionLot KeyValue
            string strMode = string.Empty;        // Mode

            try
            {
                try
                {
                    // 讀取InXml字串
                    xmlDoc.LoadXml(InXml);
                }
                catch (Exception ex)
                {
                    clsERP.combineException(ref strException, null, ex.Message, "[%InXmlError%]", ex.StackTrace);
                    goto exitFun;
                }

                // 組Identity字串
                try
                {
                    strIdentity = modWS.CombineXMLIdentity(xmlDoc);
                }
                catch (Exception ex)
                {
                    clsERP.combineException(ref strException, null, ex.Message, "[%IndentityError%]", ex.StackTrace);
                    goto exitFun;
                }


                // 2019/05/09 OwenLiu, Mantis:0057304 作業站整合若資料已存在，
                // 且ERP未傳值的狀況， 不更新OPType, OPClass, OPShortName, OPOrder
                string OPNo = string.Empty;
                string OPName = string.Empty;
                string OPType = defString;
                string PSNo = defString;
                int OPClass = defInteger;                      // Default 0 : General OP
                string OPShortName = defString;
                int OPOrder = defInteger;                        // Default 0
                string Description = string.Empty;
                string Creator = string.Empty;
                DateTime CreateDate = DateTime.Now;               // Default Today
                int PrintOutOnRunCard = 1;                // Default 1
                double StdUnitRunTime = 0;                    // Default 0
                long CountOpUnitQty = 1;                      // Default 1
                double StdQueueTime = 0;                      // Default 0
                decimal decStdUnitRunTime = defInteger;
                decimal decCountOpUnitQty = defInteger;
                decimal decStdQueueTime = defInteger;
                int OSOption = defInteger;
                int OSOptionTemp = defInteger;
                string ERPPDLineNo = defString;
                // Dim OldERPPDLineNo As String = defString '存放作業站修改前的群組資料

                // OPNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("opno").Count > 0 && xmlDoc.GetElementsByTagName("opno").Item(0).SelectNodes("value").Count > 0)
                    OPNo = modWS.CInput(xmlDoc.DocumentElement.GetElementsByTagName("opno").Item(0).SelectNodes("value").Item(0).InnerText);

                // OPName
                if (xmlDoc.DocumentElement.GetElementsByTagName("opname").Count > 0 && xmlDoc.GetElementsByTagName("opname").Item(0).SelectNodes("value").Count > 0)
                    OPName = modWS.CInput(xmlDoc.DocumentElement.GetElementsByTagName("opname").Item(0).SelectNodes("value").Item(0).InnerText);



                // 判斷成功與否
                if (strException.Count > 0)
                    goto exitFun;

                strResult = "success";

            exitFun:
                ;
            }

            catch (Exception ex)
            {
                clsERP.combineException(ref strException, null, ex.Message, "[%CreateOPFail%]", ex.StackTrace);
            }

            finally
            {

                // 將各部份之XML字串組起來並傳出
                OutXml = modWS.CombineXMLResponse(strIdentity, null, strException, strResult, strMessage);

                funCreateOperationRet = OutXml;
            }

            return funCreateOperationRet;
        }



    }
}