using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.Data;
using System.Web.Services;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using static iMESCore.Base.iMESComXML;
using static iMESCore.Base.iMESConst;
using static iMESCore.Base.iMESComSubroutine;
namespace wsSXS
{
    public class ws_21685
    {
        private MEStc_SXS .clsENT_21685  objENT = new MEStc_SXS.clsENT_21685();
        private kcSYS.clsSYSUserLog objSYS = new kcSYS.clsSYSUserLog();   // 宣告Sys的物件
        //private kcBasis_UPD.clsBasis_UPD objBasis = new kcBasis_UPD.clsBasis_UPD();    //主檔優化
        //private kcBasis_Query.clsBasis_Query objQuery = new kcBasis_Query.clsBasis_Query();    //主檔優化
        private System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();       // 用以讀取Xml字串
        private DateTime ArriveTime;              // 送達時間
        private string strIdentity;               // Identity XML字串
        private string strReturnValue;            // ReturnValue XML字串
        private string strException;              // Exception XML字串
        private string strResult;                 // Result XML字串
        private string strMessage;                // Message XML字串
        private iMESCore.Settings.AppSettings objSetting = new iMESCore.Settings.AppSettings();
        private string strResourceDir = "Resources";

        public ws_21685()
        {
            try
            {
                strResourceDir = System.IO.Path.Combine("wsUpdateResource", objSetting["ResourceDir"].ToString());
            }
            catch (Exception)
            { }
        }

        #region --- tblStuBasis_21685 学生信息 ---

        [WebMethod(Description = "Load StuBasis Data")]
        public string LoadStuBasis(string InXml)
        {
            string LoadStuBasisRet = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            string StuBasisNo = defString;
            int IssueState = 2;

            ArriveTime = DateTime.Now;
            string AdditionalXml = "";
            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 判斷是否有傳入StuBasis,若有,表示要依StuBasis查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("StuBasisNo").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("StuBasisNo").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("StuBasisNo").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // 判斷是否有傳入IssueState,若有,表示要依IssueState查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                }

                // 呼叫副程式處理查詢條件與Additional格式
                AdditionalXml = CombineXMLQueryAdditional(xmlDoc);

                // 呼叫Dll執行
                strReturnValue = objENT.LoadStuBasis(StuBasisNo, IssueState, AdditionalXml);
                strException = "";
                strResult = "success";
            }

            catch (iMESException.MESException ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Show StuBasis Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            catch (Exception ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(defWSErrCode, ex.Message, "Show StuBasis Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                LoadStuBasisRet = CombineXMLResponse(strIdentity, strReturnValue, strException, strResult, "");

            }

            return LoadStuBasisRet;

        }

        [WebMethod(Description = "Add StuBasis data")]
        public string AddStuBasis(string InXml)
        {
            string AddStuBasisRet = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            var StuBasisNo = default(string);
            string StuBasisName = defString;
            string Sex = defString;
            string Description = defString;
            string Creator = defString;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);

                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // StuBasis一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                }
                if (xmlDoc.DocumentElement.GetElementsByTagName("stubasisname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("stubasisname").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisName = xmlDoc.DocumentElement.GetElementsByTagName("stubasisname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisName Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisName Not Found!");
                }
                // Sex
                if (xmlDoc.DocumentElement.GetElementsByTagName("sex").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("sex").Item(0).SelectNodes("value").Count > 0)
                    {
                        Sex = xmlDoc.DocumentElement.GetElementsByTagName("sex").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "Sex Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "Sex Not Found!");
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Creator
                if (xmlDoc.DocumentElement.GetElementsByTagName("creator").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("creator").Item(0).SelectNodes("value").Count > 0)
                    {
                        Creator = xmlDoc.DocumentElement.GetElementsByTagName("creator").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // 呼叫Dll執行新增資料
                objENT.AddStuBasis(StuBasisNo, StuBasisName, Sex, Description, Creator);

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "AddStuBasis");
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Add StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Add StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                AddStuBasisRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return AddStuBasisRet;

        }

        [WebMethod(Description = "Edit Specified StuBasis Data")]
        public string EditStuBasis(string InXml)
        {
            string EditStuBasisRet = default(string);

            // 參數先給預設值,以判斷是否有傳入該參數
            var StuBasisNo = default(string);
            int IssueState = defInteger;
            string StuBasisName = defString;
            string Description = defString;
            string Sex = defString;
            int DataStamp = defInteger;
            string AdditionalXml = "";

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // StuBasis一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }
                // StuBasisName不可空白
                if (xmlDoc.DocumentElement.GetElementsByTagName("stubasisname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("stubasisname").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisName = xmlDoc.DocumentElement.GetElementsByTagName("stubasisname").Item(0).SelectNodes("value").Item(0).InnerText;
                        if (string.IsNullOrEmpty(StuBasisName))
                        {
                            throw new iMESException.MESException("0000-200001", "StuBasisName is empty!");
                        }
                    }
                }
                // Sex
                if (xmlDoc.DocumentElement.GetElementsByTagName("sex").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("sex").Item(0).SelectNodes("value").Count > 0)
                    {
                        Sex = xmlDoc.DocumentElement.GetElementsByTagName("sex").Item(0).SelectNodes("value").Item(0).InnerText;
                        if (string.IsNullOrEmpty(StuBasisName))
                        {
                            throw new iMESException.MESException("0000-200001", "Sex is empty!");
                        }
                    }
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // AdditionalXml
                GetXMLTagValue(ref AdditionalXml, xmlDoc, "additionalxml");

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                if (IssueState == 0)     // Unfrozen時才可修改
                {
                    // 有修改欄位值時
                    if ((StuBasisName ?? "") != defString | (Description ?? "") != defString)
                    {
                        // 呼叫Dll執行修改
                        objENT.EditStuBasis(StuBasisNo, StuBasisName,Sex, Description, AdditionalXml, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                    }
                }

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "EditStuBasis");
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Edit StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Edit StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                EditStuBasisRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");
            }

            return EditStuBasisRet;

        }

        [WebMethod(Description = "Delete StuBasis By Specified StuBasis")]
        public string DelStuBasis(string InXml)
        {
            string DelStuBasisRet = default(string);

            var StuBasisNo = default(string);
            int IssueState;
            string strDelState; // 寫入tblSysEventLog中的訊息,0:未簽核刪除,2:已簽核刪除,-1:不使用刪除
            int DataStamp = defInteger;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // StuBasis一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                switch (IssueState)
                {
                    case 0:
                        {
                            objENT.DelStuBasis(StuBasisNo);
                            strDelState = "未簽核刪除";
                            break;
                        }
                    case 2:
                        {
                            objENT.SetStuBasisIssueState(StuBasisNo, -1, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                            strDelState = "已簽核刪除";
                            break;
                        }
                    case -1:
                        {
                            objENT.DelStuBasis(StuBasisNo);
                            strDelState = "不使用刪除";
                            break;
                        }

                    default:
                        {
                            throw new iMESException.MESException("0000-200021", "[%IssueState%]");
                        }
                }

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "DelStuBasis:" + strDelState);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Delete StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Delete StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                DelStuBasisRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return DelStuBasisRet;

        }

        [WebMethod(Description = "Approve Specified StuBasis Data")]
        public string ApproveStuBasis(string InXml)
        {
            string ApproveStuBasisRet = default(string);

            string IssueNo = "SUBCONTRACTOR BASIS"; // 簽核編號:SUBCONTRACTOR BASIS.若找不到,表示直接將狀態改為Active.
            var StuBasisNo = default(string);
            int IssueState;
            var objIssue = new kcISSUE.clsIssue();
            DataRow IssueDR;
            int DataStamp = defInteger;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // StuBasis一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                }
                // IssueState一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Count > 0)
                    {
                        IssueState = Conversions.ToInteger(xmlDoc.DocumentElement.GetElementsByTagName("issuestate").Item(0).SelectNodes("value").Item(0).InnerText);
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "[%IssueState%]");
                }

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                if (IssueState == 0)     // 當狀態為Unfrozen時才作
                {
                    // 去IssueManagement中找核准的方式
                    IssueDR = objIssue.GetIssueJobMap(IssueNo);

                    // 找不到表示bypass直接核准
                    if (IssueDR["JobNo"] is DBNull)
                    {
                        // 直接核准
                        objENT.SetStuBasisIssueState(StuBasisNo, 2, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                        objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "ApproveStuBasis");
                    }

                    else
                    {
                        // 檢查JobNo是否有定義Detail檔,沒有Detail檔則直接核准
                        DataRow tmpDR;
                        tmpDR = objIssue.GetIssueJobDetail(Conversions.ToString(IssueDR["JobNo"]));
                        if (tmpDR["JobNo"] is DBNull)
                        {
                            objENT.SetStuBasisIssueState(StuBasisNo, 2, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                            objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "ApproveStuBasis");
                        }

                        else
                        {
                            // 將狀態改為Pending,且應以找到的JobNo來進入簽核
                            // 填入簽核現況檔之Initial
                            var ApproveSQL = new Collection();
                            var RejectSQL = new Collection();
                            string tmpSQL;
                            string IssueSubject;
                            string IssueMasterNo;

                            // 核准後應將狀態改為Active
                            tmpSQL = "Update tblENTStuBasis Set IssueState = 2 Where StuBasisNo = '" + StuBasisNo + "'";
                            ApproveSQL.Add(tmpSQL);
                            // 退回則應將狀態回復到Unfrozen
                            tmpSQL = "Update tblENTStuBasis Set IssueState = 0 Where StuBasisNo = '" + StuBasisNo + "'";
                            RejectSQL.Add(tmpSQL);

                            // Creator未傳入表示以目前的User
                            string Creator;
                            if (xmlDoc.DocumentElement.GetElementsByTagName("creator").Count > 0)
                            {
                                if (xmlDoc.GetElementsByTagName("creator").Item(0).SelectNodes("value").Count > 0)
                                {
                                    Creator = xmlDoc.DocumentElement.GetElementsByTagName("creator").Item(0).SelectNodes("value").Item(0).InnerText;
                                }
                                else
                                {
                                    Creator = GetXMLCurUserNo(xmlDoc);
                                }
                            }
                            else
                            {
                                Creator = GetXMLCurUserNo(xmlDoc);
                            }

                            IssueSubject = "StuBasisNo: " + StuBasisNo;
                            IssueMasterNo = objIssue.InitIssueMasterNo();
                            // 此筆資料進入Issue流程
                            objIssue.StartIssueProcess(IssueMasterNo, ApproveSQL, RejectSQL, IssueNo, IssueDR["JobNo"].ToString(), IssueSubject, Creator);
                            // 將狀態改為Pending
                            objENT.SetStuBasisIssueState(StuBasisNo, 1, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);

                            objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "ApproveStuBasis");

                            // 發Email給JobSerialNo=1之負責群組
                            objIssue.SendEmailToFirstGroup(IssueMasterNo, IssueSubject);
                        }
                    }
                }
                strException = "";
                strResult = "success";
            }


            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Approve StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Approve StuBasis failed!", ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                ApproveStuBasisRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");
            }

            return ApproveStuBasisRet;

        }

        // StuBasis Contactor
        [WebMethod(Description = "Load StuBasis Contactor Data")]
        public string LoadStuBasisCont(string InXml)
        {
            string LoadStuBasisContRet = default(string);

            // 先給預設值,以判斷是否有傳入該參數
            string StuBasisNo = defString;
            string TeacherName = defString;

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 判斷是否有傳入StuBasisNo,若有,表示要依StuBasisNo查詢
                if (xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                if (xmlDoc.DocumentElement.GetElementsByTagName("teachername").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("teachername").Item(0).SelectNodes("value").Count > 0)
                    {
                        TeacherName = xmlDoc.DocumentElement.GetElementsByTagName("teachername").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // 呼叫Dll執行
                strReturnValue = objENT.LoadStuBasisCont(StuBasisNo, TeacherName);
                strException = "";
                strResult = "success";
            }

            catch (iMESException.MESException ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Show StuBasis Contactor Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            catch (Exception ex)
            {
                strReturnValue = "";
                strException = CombineXMLException(defWSErrCode, ex.Message, "Show StuBasis Contactor Failed!!", ex.StackTrace);
                strResult = "fail";
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                LoadStuBasisContRet = CombineXMLResponse(strIdentity, strReturnValue, strException, strResult, "");

            }

            return LoadStuBasisContRet;

        }

        [WebMethod(Description = "Add StuBasis Contactor data")]
        public string AddStuBasisCont(string InXml)
        {
            string AddStuBasisContRet = default(string);
            // 先給預設值,以判斷是否有傳入該參數
            var StuBasisNo = default(string);
            var TeacherName = default(string);
            string TelNo = defString;
            string Description = defString;
            string AdditionalXml = "";

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);

                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // StuBasisNo一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("stubasisno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                }
                // ContactorName一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("teachername").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("teachername").Item(0).SelectNodes("value").Count > 0)
                    {
                        TeacherName = xmlDoc.DocumentElement.GetElementsByTagName("teachername").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "TeacherName Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "ContactorName Not Found!");
                }
                // TelNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("telno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("telno").Item(0).SelectNodes("value").Count > 0)
                    {
                        TelNo = xmlDoc.DocumentElement.GetElementsByTagName("telno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // AdditionalXml
                GetXMLTagValue(ref AdditionalXml, xmlDoc, "additionalxml");


                // 呼叫Dll執行新增資料
                objENT.AddStuBasisCont(StuBasisNo, TelNo, Description, AdditionalXml, GetXMLCurUserNo(xmlDoc), ArriveTime);

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "AddStuBasisContactor ContactorName:" + TeacherName);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Add StuBasisContactor Data failed, ContactorName:" + TeacherName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Add StuBasisContactor Data failed, ContactorName:" + TeacherName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                AddStuBasisContRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return AddStuBasisContRet;

        }

        [WebMethod(Description = "Edit Specified StuBasis Contactor Data")]
        public string EditStuBasisCont(string InXml)
        {
            string EditStuBasisContRet = default(string);
            // 先給預設值,以判斷是否有傳入該參數
            var StuBasisNo = default(string);
            var ContactorName = default(string);
            string TelNo = defString;
            string FaxNo = defString;
            string Title = defString;
            string Address = defString;
            string EMail = defString;
            string Description = defString;

            int DataStamp = defInteger;
            string AdditionalXml = "";

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // StuBasisNo一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                }
                // ContactorName一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        ContactorName = xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "ContactorName Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "ContactorName Not Found!");
                }
                // TelNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("telno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("telno").Item(0).SelectNodes("value").Count > 0)
                    {
                        TelNo = xmlDoc.DocumentElement.GetElementsByTagName("telno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // FaxNo
                if (xmlDoc.DocumentElement.GetElementsByTagName("faxno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("faxno").Item(0).SelectNodes("value").Count > 0)
                    {
                        FaxNo = xmlDoc.DocumentElement.GetElementsByTagName("faxno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Title
                if (xmlDoc.DocumentElement.GetElementsByTagName("title").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("title").Item(0).SelectNodes("value").Count > 0)
                    {
                        Title = xmlDoc.DocumentElement.GetElementsByTagName("title").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Address
                if (xmlDoc.DocumentElement.GetElementsByTagName("address").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("address").Item(0).SelectNodes("value").Count > 0)
                    {
                        Address = xmlDoc.DocumentElement.GetElementsByTagName("address").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // EMail
                if (xmlDoc.DocumentElement.GetElementsByTagName("email").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("email").Item(0).SelectNodes("value").Count > 0)
                    {
                        EMail = xmlDoc.DocumentElement.GetElementsByTagName("email").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }
                // Description
                if (xmlDoc.DocumentElement.GetElementsByTagName("description").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("description").Item(0).SelectNodes("value").Count > 0)
                    {
                        Description = xmlDoc.DocumentElement.GetElementsByTagName("description").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                // AdditionalXml
                GetXMLTagValue(ref AdditionalXml, xmlDoc, "additionalxml");

                // DataStamp
                GetXMLTagValue(ref DataStamp, xmlDoc, "datastamp", true);

                // If IssueState = 0 Then     'Unfrozen時才可修改
                // 有修改欄位值時
                if ((TelNo ?? "") != defString | (FaxNo ?? "") != defString | (Title ?? "") != defString | (Address ?? "") != defString | (EMail ?? "") != defString | (Description ?? "") != defString)
                {
                    // 呼叫Dll執行修改
                    objENT.EditStuBasisCont(StuBasisNo, ContactorName, TelNo, FaxNo, Title, Address, EMail, Description, AdditionalXml, DataStamp: DataStamp, Reviser: GetXMLCurUserNo(xmlDoc), ReviseDate: ArriveTime);
                }
                // End If

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "EditStuBasisContactor ContactorName:" + ContactorName);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Edit StuBasisContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Edit StuBasisContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                // 將各部份之XML字串組起來並傳出
                EditStuBasisContRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return EditStuBasisContRet;

        }

        [WebMethod(Description = "Delete StuBasis Contactor By Specified StuBasisNo and ContactorName")]
        public string DelStuBasisCont(string InXml)
        {
            string DelStuBasisContRet = default(string);

            var StuBasisNo = default(string);
            string ContactorName = defString; // 沒有傳入表示要刪除指定StuBasis的所有Contactor

            ArriveTime = DateTime.Now;

            try
            {
                // 讀取InXml字串
                xmlDoc.LoadXml(InXml);
                // 組Identity字串
                strIdentity = CombineXMLIdentity(Environment.MachineName, GetXMLCurUserNo(xmlDoc), Conversions.ToString(ArriveTime));

                // 開始解譯InXml字串
                // StuBasisNo一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Count > 0)
                    {
                        StuBasisNo = xmlDoc.DocumentElement.GetElementsByTagName("subcontractorno").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                    else
                    {
                        throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                    }
                }
                else
                {
                    throw new iMESException.MESException("0000-200002", "StuBasisNo Not Found!");
                }
                // ContactorName一定要傳入
                if (xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Count > 0)
                {
                    if (xmlDoc.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Count > 0)
                    {
                        ContactorName = xmlDoc.DocumentElement.GetElementsByTagName("contactorname").Item(0).SelectNodes("value").Item(0).InnerText;
                    }
                }

                objENT.DelStuBasisCont(StuBasisNo, ContactorName);

                strException = "";
                strResult = "success";

                objSYS.AddEventLog("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, "DelStuBasisContactor ContactorName:" + ContactorName);
            }

            catch (iMESException.MESException ex)
            {
                strException = CombineXMLException(Conversions.ToString(ex.ErrorCode), TranslateMsg(ex.Message, GetXMLLanguageMode(xmlDoc), strResourceDir), "Del StuBasisContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: Conversions.ToString(ex.ErrorCode));
            }

            catch (Exception ex)
            {
                strException = CombineXMLException(defWSErrCode, ex.Message, "Del StuBasisContactor Data failed, ContactorName:" + ContactorName, ex.StackTrace);

                strResult = "fail";

                objSYS.AddErrorLog_ErrorCode("wsENT", GetXMLCurUserNo(xmlDoc), "StuBasisNo", StuBasisNo, DateTime.Now, strException, ComputerName: GetXMLCurComputer(xmlDoc), ErrorCode: defWSErrCode);
            }

            finally
            {
                DelStuBasisContRet = CombineXMLResponse(strIdentity, "", strException, strResult, "");

            }

            return DelStuBasisContRet;

        }


        #endregion
    }
}
