2022年9月14日 星期三

V20104 - 資料庫批次更新 - Transaction 處理 - V20301 - 批次新增 -transaction 處理

 目的: V20104 - 資料庫批次更新時,Transaction 處理 , 若批次處理有問題,則整批 Rollback

  處理說明: 1>產生 OracleTransaction
                         OracleTransaction trn = conn.BeginTransaction();
                         try
                        {
                            for (i=0;i<length;i++)
                                  cmd.ExecuteNonQuery(); //Loop 多筆更新
                            trn.Commit();
                        }
                        catch (Exception ex)
                       {
                           trn.Rollback();
                        }


1>*.cs

            try
            {
                List<string> SQLStringList_U = new List<string>();
                List<string> SQLStringList_A = new List<string>();

                    updSql = "update AMM_DEVICE set SNM = '" + Tmp_SNM + "', NOTE = '" + Tmp_NOTE + "'" + sWhr;
                SQLStringList_U.Add(updSql);
                     :
                    excuteSQLTran(SQLStringList_U);
                    excuteSQLTran(SQLStringList_A);
                }





2>BaseAPIController.cs excuteSQLTran 函式

public int excuteSQLTran(List<string> SQLStringList)
        {
            setActionName();
            int rows = 0;
OracleConnection conn = new OracleConnection(DBService.ConnectionString(DBLINK));
conn.Open();
conn.ClientInfo = User.Identity.Name;
conn.ModuleName = BaseSYS + "_" + BaseMODID;
conn.ActionName = ActionName;
OracleTransaction trn = conn.BeginTransaction();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.Transaction = trn;
try
{
excMsg = "";
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
cmd.CommandText = strsql;
rows += cmd.ExecuteNonQuery();
}
trn.Commit();
}
catch (Exception ex)
{
trn.Rollback();
rows = -1;
//Console.WriteLine("error : " + ex.Message);
excMsg = ex.Message;
throw ex;
}
finally
{
cmd.Cancel();
conn.Close();
}
return rows;
}


3>*.cs  V20301 - 批次新增 - transaction 處理

//[匯入] 鈕 , 匯入 XLS 檔案
        [HttpPost]
        public HttpResponseMessage uploadFileToDB()
        {

            HttpContext c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            string FName = nvc["FName"];
            Workbook wk = null;

            HttpRequest Request = HttpContext.Current.Request;
            var response = this.Request.CreateResponse();
            string file_Name = FName;
            string FILEDT = DateTime.Now.ToString("yyyy/MM/dd");
            string Tmp_RtnMsg = "";
            string Tmp_RtnMsg1 = "";
            string Tmp_Sql = "", Tmp_Str = "";
            int Tmp_cnt = 0;
            
            List<string> SQLStringList_A = new List<string>();

            foreach (string cur_FName in Request.Files)
 {
                string fileType = Request.Files[cur_FName].ContentType;
                Stream file_Strm = Request.Files[cur_FName].InputStream;
                file_Name = Path.GetFileName(Request.Files[cur_FName].FileName);
                int fileSize = Request.Files[cur_FName].ContentLength;
                byte[] fileRcrd = new byte[fileSize];
                //更新資料庫欄位值                
                int Tmp_curpos = 0;
                try
                {

                    wk = new Workbook();
                    wk.LoadFromStream(file_Strm);
                    Worksheet sheet1 = wk.Worksheets[0];//獲取第一個工作表
                    /*
                    //共 33 欄
                    //string[] outFieldArray = { 資料來源,拆檢提領編號,維修提領編號,拆檢交修單號,維修約交修單號,
                                                                    WBS,數量,中文名稱(SAP勞務內容),英文簡稱,料號,
                                                                    件號,序號,機種,接收批次,預計拆檢完工日,
                                                                    客戶拆檢需求日,實際工廠接收日期,SAP OSM,EO,情況碼,
                                                                    工單類型,發工人員,管制人員,維修人員,拆檢結束日期,
                                                                    QDRNO,AMM工單備註,承修工場,專案,群組,
                                                                    是否序號管制,保固或構改管制碼,專案需求日};
                    //int RowsCount = sheet1.Rows.Count();
                    */
                    int RowsCount = sheet1.LastRow;
                    string Tmp_FMNO;
                    string  Tmp_DTSOR, Tmp_OGONOAF, Tmp_NGONOAF, Tmp_RPNOAF, Tmp_MRPNOAF;
                    string Tmp_WBSNO, Tmp_RQTYAF,  Tmp_C_NM, Tmp_ES_NM, Tmp_NSN;
                    string  Tmp_PN, Tmp_SEQOAF, Tmp_EIAC_CUS,Tmp_RBTHAF,Tmp_PRNRAF;
                    string  Tmp_CRNRAF, Tmp_FREDTAF, Tmp_OSM, Tmp_EONO, Tmp_CDCODE;
                    string Tmp_FRTP, Tmp_DLEMPLYID, Tmp_CREMPLYID, Tmp_MTEMPLYID, Tmp_NRCLAF;
                    string Tmp_QDRNO, Tmp_REMARK, Tmp_FACWC, Tmp_NEMPID, Tmp_LGROUP;
                    string Tmp_ISSN, Tmp_MCODE, Tmp_PJDT,Tmp_STATFM,Tmp_BTDT,Tmp_MDDT;

                    //STEP1 檢核  XLS 欄位名稱資料是否正確                                            
                    Tmp_DTSOR = sheet1.Range[myfunc.GetExcelPos(0, 0)].Value;
                    Tmp_OGONOAF = sheet1.Range[myfunc.GetExcelPos(1, 0)].Value;
                    Tmp_NGONOAF = sheet1.Range[myfunc.GetExcelPos(2, 0)].Value;
                    Tmp_RPNOAF = sheet1.Range[myfunc.GetExcelPos(3, 0)].Value;
                    Tmp_MRPNOAF = sheet1.Range[myfunc.GetExcelPos(4, 0)].Value;

                    Tmp_WBSNO = sheet1.Range[myfunc.GetExcelPos(5, 0)].Value;
                    Tmp_RQTYAF = sheet1.Range[myfunc.GetExcelPos(6, 0)].Value;
                    Tmp_C_NM = sheet1.Range[myfunc.GetExcelPos(7, 0)].Value;
                    Tmp_ES_NM = sheet1.Range[myfunc.GetExcelPos(8, 0)].Value;
                    Tmp_NSN = sheet1.Range[myfunc.GetExcelPos(9, 0)].Value;
                    
                    Tmp_PN = sheet1.Range[myfunc.GetExcelPos(10, 0)].Value;
                    Tmp_SEQOAF = sheet1.Range[myfunc.GetExcelPos(11, 0)].Value;
                    Tmp_EIAC_CUS = sheet1.Range[myfunc.GetExcelPos(12, 0)].Value;
                    Tmp_RBTHAF = sheet1.Range[myfunc.GetExcelPos(13, 0)].Value;
                    Tmp_PRNRAF = sheet1.Range[myfunc.GetExcelPos(14, 0)].Value;

                    Tmp_CRNRAF = sheet1.Range[myfunc.GetExcelPos(15, 0)].Value;
                    Tmp_FREDTAF = sheet1.Range[myfunc.GetExcelPos(16, 0)].Value;
                    Tmp_OSM = sheet1.Range[myfunc.GetExcelPos(17, 0)].Value;
                    Tmp_EONO = sheet1.Range[myfunc.GetExcelPos(18, 0)].Value;
                    Tmp_CDCODE = sheet1.Range[myfunc.GetExcelPos(19, 0)].Value;
                    
                    Tmp_FRTP = sheet1.Range[myfunc.GetExcelPos(20, 0)].Value;
                    Tmp_DLEMPLYID = sheet1.Range[myfunc.GetExcelPos(21, 0)].Value;
                    Tmp_CREMPLYID = sheet1.Range[myfunc.GetExcelPos(22, 0)].Value;
                    Tmp_MTEMPLYID = sheet1.Range[myfunc.GetExcelPos(23, 0)].Value;
                    Tmp_NRCLAF = sheet1.Range[myfunc.GetExcelPos(24, 0)].Value;

                    Tmp_QDRNO = sheet1.Range[myfunc.GetExcelPos(25, 0)].Value;
                    Tmp_REMARK = sheet1.Range[myfunc.GetExcelPos(26, 0)].Value;
                    Tmp_FACWC = sheet1.Range[myfunc.GetExcelPos(27, 0)].Value;
                    Tmp_NEMPID = sheet1.Range[myfunc.GetExcelPos(28, 0)].Value;
                    Tmp_LGROUP = sheet1.Range[myfunc.GetExcelPos(29, 0)].Value;

                    Tmp_ISSN = sheet1.Range[myfunc.GetExcelPos(30, 0)].Value;
                    Tmp_MCODE = sheet1.Range[myfunc.GetExcelPos(31, 0)].Value;
                    Tmp_PJDT = sheet1.Range[myfunc.GetExcelPos(32, 0)].Value;

                    /*
                     string  Tmp_DTSOR, Tmp_OGONOAF, Tmp_NGONOAF, Tmp_RPNOAF, Tmp_MRPNOAF;
                    string Tmp_WBSNO, Tmp_SEQOAF, Tmp_C_NM, Tmp_ES_NM, Tmp_NSN;
                    string  Tmp_PN, Tmp_SEQOAF, Tmp_EIAC_CUS,Tmp_RBTHAF,Tmp_PRNRAF;
                    string  Tmp_CRNRAF, Tmp_FREDTAF, Tmp_OSM, Tmp_EONO,, Tmp_CDCODE;
                    string Tmp_FRTP, Tmp_DLEMPLYID, Tmp_CREMPLYID, Tmp_MTEMPLYID, Tmp_NRCLAF;
                    string Tmp_QDRNO, Tmp_REMARK, Tmp_FACWC, Tmp_NEMPID, Tmp_LGROUP;
                    string Tmp_ISSN, Tmp_MCODE, Tmp_PJDT;
                     */
                    //xls 欄位名稱有問題
                    if ((Tmp_DTSOR != "資料來源") || (Tmp_OGONOAF != "拆檢提領編號") || (Tmp_NGONOAF != "維修提領編號") || (Tmp_RPNOAF != "拆檢交修單號") || (Tmp_MRPNOAF != "維修約交修單號") ||
                          (Tmp_WBSNO != "WBS") || (Tmp_RQTYAF != "數量") || (!Tmp_C_NM.Contains("中文名稱")) || (Tmp_ES_NM != "英文簡稱") || (Tmp_NSN != "料號") ||
                          (Tmp_PN != "件號") || (Tmp_SEQOAF != "序號")|| (Tmp_EIAC_CUS != "機種") || (Tmp_RBTHAF != "接收批次") || (Tmp_PRNRAF != "預計拆檢完工日") ||
                          (Tmp_CRNRAF != "客戶拆檢需求日") || (Tmp_FREDTAF != "實際工廠接收日期")|| (!Tmp_OSM.Contains("OSM")) || (Tmp_EONO != "EO") || (Tmp_CDCODE != "情況碼") ||
                          (Tmp_FRTP != "工單類型") || (Tmp_DLEMPLYID != "發工人員")|| (Tmp_CREMPLYID != "管制人員") || (Tmp_MTEMPLYID != "維修人員") || (Tmp_NRCLAF != "拆檢結束日期") ||
                          (Tmp_QDRNO != "QDRNO") || (Tmp_REMARK != "AMM工單備註")|| (Tmp_FACWC != "承修工場") || (Tmp_NEMPID != "專案") || (Tmp_LGROUP != "群組") ||
                          (Tmp_ISSN != "是否序號管制") || (Tmp_MCODE != "保固或構改管制碼")|| (Tmp_PJDT != "專案需求日")
                          )
                    {
                        Tmp_RtnMsg = "匯入檔案欄位名稱必需如下:<br>";
                        //(Tmp_DTSOR != "資料來源") || (Tmp_OGONOAF != "拆檢提領編號") || (Tmp_NGONOAF != "維修提領編號") || (Tmp_RPNOAF != "拆檢交修單號") || (Tmp_MRPNOAF != "維修約交修單號") ||
                        if (Tmp_DTSOR != "資料來源")
                            Tmp_RtnMsg = Tmp_RtnMsg+"第1欄欄位名稱:  [資料來源];";
                        if (Tmp_OGONOAF != "拆檢提領編號")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第2欄欄位名稱:  [拆檢提領編號];";
                        if (Tmp_NGONOAF != "維修提領編號")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第3欄欄位名稱:  [維修提領編號];";
                        if (Tmp_RPNOAF != "拆檢交修單號")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第4欄欄位名稱:  [拆檢交修單號];";
                        if (Tmp_MRPNOAF != "維修約交修單號")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第5欄欄位名稱:  [維修約交修單號];";
                        
                        //(Tmp_WBSNO != "WBS") || (Tmp_RQTYAF != "數量") || (!Tmp_C_NM.Contains("中文名稱")) || (Tmp_ES_NM != "英文簡稱") || (Tmp_PRNRAF != "保管人") || (Tmp_NSN != "料號") ||
                        if (Tmp_WBSNO != "WBS")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第6欄欄位名稱:  [WBS];";
                        if (Tmp_RQTYAF != "數量")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第7欄欄位名稱:  [數量];";
                        if (!Tmp_C_NM.Contains("中文名稱"))
                            Tmp_RtnMsg = Tmp_RtnMsg + "第8欄欄位名稱:  [中文名稱];";
                        if (Tmp_ES_NM != "英文簡稱")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第9欄欄位名稱:  [英文簡稱];";
                        if (Tmp_NSN != "料號")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第10欄欄位名稱:  [料號];";

                        //(Tmp_PN != "件號") || (Tmp_SEQOAF != "序號")|| (Tmp_EIAC_CUS != "機種") || (Tmp_RBTHAF != "接收批次") || (Tmp_PRNRAF != "預計拆檢完工日") ||
                        if (Tmp_PN != "件號")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第11欄欄位名稱:  [件號];";
                        if (Tmp_SEQOAF != "序號")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第12欄欄位名稱:  [序號];";
                        if (Tmp_EIAC_CUS != "機種")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第13欄欄位名稱:  [機種];";
                        if (Tmp_RBTHAF != "接收批次")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第14欄欄位名稱:  [接收批次];";
                        if (Tmp_PRNRAF != "預計拆檢完工日")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第15欄欄位名稱:  [預計拆檢完工日];";



                        //(Tmp_CRNRAF != "客戶拆檢需求日") || (Tmp_FREDTAF != "實際工廠接收日期") || (!Tmp_OSM.Contains("OSM")) || (Tmp_EONO != "EO") || (Tmp_CDCODE != "情況碼") ||
                        if (Tmp_CRNRAF != "客戶拆檢需求日")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第16欄欄位名稱:  [客戶拆檢需求日];";
                        if (Tmp_FREDTAF != "實際工廠接收日期")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第17欄欄位名稱:  [實際工廠接收日期];";
                        if (!Tmp_OSM.Contains("OSM"))
                            Tmp_RtnMsg = Tmp_RtnMsg + "第18欄欄位名稱:  [OSM];";
                        if (Tmp_EONO != "EO")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第19欄欄位名稱:  [EO];";
                        if (Tmp_CDCODE != "情況碼")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第20欄欄位名稱:  [情況碼];";

                        //(Tmp_FRTP != "工單類型") || (Tmp_DLEMPLYID != "發工人員") || (Tmp_CREMPLYID != "管制人員") || (Tmp_MTEMPLYID != "維修人員") || (Tmp_NRCLAF != "拆檢結束日期") ||
                        if (Tmp_FRTP != "工單類型")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第21欄欄位名稱:  [工單類型];";
                        if (Tmp_DLEMPLYID != "發工人員")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第22欄欄位名稱:  [發工人員];";
                        if (Tmp_CREMPLYID != "管制人員")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第23欄欄位名稱:  [管制人員];";
                        if (Tmp_MTEMPLYID != "維修人員")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第24欄欄位名稱:  [維修人員];";
                        if (Tmp_NRCLAF != "拆檢結束日期")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第25欄欄位名稱:  [拆檢結束日期];";

                        //(Tmp_QDRNO != "QDRNO") || (Tmp_REMARK != "AMM工單備註")|| (Tmp_FACWC != "承修工場") || (Tmp_NEMPID != "專案") || (Tmp_LGROUP != "群組") ||
                        if (Tmp_QDRNO != "QDRNO")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第26欄欄位名稱:  [QDRNO];";
                        if (Tmp_REMARK != "AMM工單備註")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第27欄欄位名稱:  [AMM工單備註];";
                        if (Tmp_FACWC != "承修工場")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第28欄欄位名稱:  [承修工場];";
                        if (Tmp_NEMPID != "專案")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第29欄欄位名稱:  [專案];";
                        if (Tmp_LGROUP != "群組")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第30欄欄位名稱:  [群組];";

                        // (Tmp_ISSN != "是否序號管制") || (Tmp_MCODE != "保固或構改管制碼")|| (Tmp_PJDT != "專案需求日")    
                        if (Tmp_ISSN != "是否序號管制")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第31欄欄位名稱:  [是否序號管制];";
                        if (Tmp_MCODE != "保固或構改管制碼")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第32欄欄位名稱:  [保固或構改管制碼];";
                        if (Tmp_PJDT != "專案需求日")
                            Tmp_RtnMsg = Tmp_RtnMsg + "第33欄欄位名稱:  [專案需求日];";                        
                        //Tmp_RtnMsg = "匯入檔案欄位名稱必需如下:<br>"
                        //                         + "\"資料來源\",\"拆檢提領編號\",\"維修提領編號\",\"拆檢交修單號\",\"維修約交修單號\",<br>"
                        //                         + "\"WBS\",\"數量\",\"中文名稱(SAP勞務內容)\",\"英文簡稱\",\"料號\",<br>"
                        //                         + "\"件號\",\"序號\",\"機種\",\"接收批次\",\"預計拆檢完工日\",<br>"
                        //                         + "\"客戶拆檢需求日\",\"實際工廠接收日期\",\"SAP OSM\",\"EO\",\"情況碼\",<br>"
                        //                         + "\"工單類型\",\"發工人員\",\"管制人員\",\"維修人員\",\"拆檢結束日期\",<br>"
                        //                         + "\"QDRNO\",\"AMM工單備註\",\"承修工場\",\"專案\",\"群組\",<br>"
                        //                         + "\"是否序號管制\",\"保固或構改管制碼\",\"專案需求日\"<br>"
                        //                         + " 敬請檢核";
                        Tmp_Str = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
                        response.Content = new StringContent(Tmp_Str);    // 回應內容
                        return response;
                    };


                    int Tmp_index;
                    //STEP2 檢核 xls 欄位值
                    Tmp_RtnMsg = "";
                    for (int i = 0; i < RowsCount; i++)
                    {
                        Tmp_RtnMsg1 = "";
                        Tmp_curpos = i + 1;
                        Tmp_DTSOR = sheet1.Range[myfunc.GetExcelPos(0, i + 1)].Value;
                        if (myfunc.checkisnull(Tmp_DTSOR))
                        {
                            break;
                            //Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[資料來源]欄位值不可空白;";
                        }
                        Tmp_OGONOAF = sheet1.Range[myfunc.GetExcelPos(1, i + 1)].Value;
                        Tmp_NGONOAF = sheet1.Range[myfunc.GetExcelPos(2, i + 1)].Value;
                        Tmp_RPNOAF = sheet1.Range[myfunc.GetExcelPos(3, i + 1)].Value;
                        Tmp_MRPNOAF = sheet1.Range[myfunc.GetExcelPos(4, i + 1)].Value;

                        Tmp_WBSNO = sheet1.Range[myfunc.GetExcelPos(5, i + 1)].Value;
                        Tmp_RQTYAF = sheet1.Range[myfunc.GetExcelPos(6, i + 1)].Value;
                        Tmp_C_NM = sheet1.Range[myfunc.GetExcelPos(7, i + 1)].Value;
                        Tmp_ES_NM = sheet1.Range[myfunc.GetExcelPos(8, i + 1)].Value;
                        Tmp_NSN = sheet1.Range[myfunc.GetExcelPos(9, i + 1)].Value;

                        Tmp_PN = sheet1.Range[myfunc.GetExcelPos(10, i + 1)].Value;
                        Tmp_SEQOAF = sheet1.Range[myfunc.GetExcelPos(11, i + 1)].Value;
                        Tmp_EIAC_CUS = sheet1.Range[myfunc.GetExcelPos(12, i + 1)].Value;
                        Tmp_RBTHAF = sheet1.Range[myfunc.GetExcelPos(13, i + 1)].Value;
                        Tmp_PRNRAF = sheet1.Range[myfunc.GetExcelPos(14, i + 1)].Value;

                        Tmp_CRNRAF = sheet1.Range[myfunc.GetExcelPos(15, i + 1)].Value;
                        Tmp_FREDTAF = sheet1.Range[myfunc.GetExcelPos(16, i + 1)].Value;
                        Tmp_OSM = sheet1.Range[myfunc.GetExcelPos(17, i + 1)].Value;
                        Tmp_EONO = sheet1.Range[myfunc.GetExcelPos(18, i + 1)].Value;
                        Tmp_CDCODE = sheet1.Range[myfunc.GetExcelPos(19, i + 1)].Value;

                        Tmp_FRTP = sheet1.Range[myfunc.GetExcelPos(20, i + 1)].Value;
                        Tmp_DLEMPLYID = sheet1.Range[myfunc.GetExcelPos(21, i + 1)].Value;
                        Tmp_CREMPLYID = sheet1.Range[myfunc.GetExcelPos(22, i + 1)].Value;
                        Tmp_MTEMPLYID = sheet1.Range[myfunc.GetExcelPos(23, i + 1)].Value;
                        Tmp_NRCLAF = sheet1.Range[myfunc.GetExcelPos(24, i + 1)].Value;

                        Tmp_QDRNO = sheet1.Range[myfunc.GetExcelPos(25, i + 1)].Value;
                        Tmp_REMARK = sheet1.Range[myfunc.GetExcelPos(26, i + 1)].Value;
                        Tmp_FACWC = sheet1.Range[myfunc.GetExcelPos(27, i + 1)].Value;
                        Tmp_NEMPID = sheet1.Range[myfunc.GetExcelPos(28, i + 1)].Value;
                        Tmp_LGROUP = sheet1.Range[myfunc.GetExcelPos(29, i + 1)].Value;

                        Tmp_ISSN = sheet1.Range[myfunc.GetExcelPos(30, i + 1)].Value;
                        Tmp_MCODE = sheet1.Range[myfunc.GetExcelPos(31, i + 1)].Value;
                        Tmp_PJDT = sheet1.Range[myfunc.GetExcelPos(32, i + 1)].Value;


                        //if (Tmp_curpos == 290)
                        //{
                        //    Tmp_Str = " 第 290筆資料";
                        //};                                                
                        //將日期轉成  yyyy/MM/dd 格式, 若非日期格式,則傳回 ""
                        Tmp_PRNRAF = myfunc.DatetoStr(Tmp_PRNRAF);
                        Tmp_CRNRAF = myfunc.DatetoStr(Tmp_CRNRAF);
                        Tmp_NRCLAF = myfunc.DatetoStr(Tmp_NRCLAF);
                        Tmp_FREDTAF = myfunc.DatetoStr(Tmp_FREDTAF);

                        //檢核 1: 欄位值不可空白 - 資料來源、WBS、數量、序號、機種、工單類型、是否序號管制
                        if (myfunc.checkisnull(Tmp_WBSNO))
                        {
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[WBS]不可空白;";
                        }
                        if (myfunc.checkisnull(Tmp_RQTYAF))
                        {
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[數量]不可空白;";
                        }
                        if (myfunc.checkisnull(Tmp_SEQOAF))
                        {
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[序號]不可空白;";
                        }
                        if (myfunc.checkisnull(Tmp_EIAC_CUS))
                        {
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[機種]不可空白;";
                        }
                        if (myfunc.checkisnull(Tmp_FRTP))
                        {
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[工單類型]不可空白;";
                        }
                        if (myfunc.checkisnull(Tmp_ISSN))
                        {
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[是否序號管制]不可空白;";
                        }
                        if (Tmp_PN.Substring(0,2)=="R=")
                            if (Tmp_FRTP!="DT03")
                            {
                                Tmp_RtnMsg1 = Tmp_RtnMsg1 + "件號以[R=]開頭,則工單類別必需為[DT03];";
                            }
                        if (  (!myfunc.checkisnull(Tmp_EONO)) && (!myfunc.checkisnull(Tmp_OSM))  )                            
                                Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[EO]及 [SAP OSM] 不可均有值;";
                        if ( (!myfunc.checkisnull(Tmp_CDCODE)) && (!myfunc.checkisnull(Tmp_OSM)) )
                                Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[情況碼]及 [SAP OSM] 不可均有值;";                        
                        if (!myfunc.checkisnum(Tmp_DLEMPLYID))
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[發工人員] 必需為員工編號,非姓名;";
                        if (!myfunc.checkisnum(Tmp_CREMPLYID))
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[管制人員] 必需為員工編號,非姓名;";
                        if (!myfunc.checkisnum(Tmp_MTEMPLYID))
                            Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[維修人員] 必需為員工編號,非姓名;";
                        if (Tmp_MCODE.Substring(0, 1) == "C")
                            if (myfunc.checkisnull(Tmp_PJDT))
                             {
                                Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[保固或構改管制碼]='C',則[專案需求日]不可空白;";
                            }
                        if (Tmp_RtnMsg1.Length > 0)
                            Tmp_RtnMsg = Tmp_RtnMsg
                                                      +"第" + Tmp_curpos.ToString() + "行欄位值錯誤, 請檢核:<br>"
                                                     + Tmp_RtnMsg1
                                                     +"<br>";

                    }  //end for 
                    if (Tmp_RtnMsg.Length>0)
                    {
                        Tmp_RtnMsg = "批次匯入失敗!!<br>"                                             
                                                 + Tmp_RtnMsg + "<br>";
                        //+ Tmp_Sql ;
                        //Tmp_Str = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) +",Tmp_Sql:"+ myfunc.AA(Tmp_Sql) +"}";
                        Tmp_Str = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
                        response.Content = new StringContent(Tmp_Str);    // 回應內容                    
                        return response;
                    }
                    //STEP3 寫入資料庫
                    Tmp_RtnMsg = "";
                    var Tmp_pre_FMNO = "";
                    for (int i = 0; i < RowsCount; i++)
                    {
                        Tmp_curpos = i + 1;
                        Tmp_DTSOR = sheet1.Range[myfunc.GetExcelPos(0, i + 1)].Value;
                        if (myfunc.checkisnull(Tmp_DTSOR))
                        {
                            break;
                            //Tmp_RtnMsg = Tmp_RtnMsg + "[資料來源]欄位值不可空白";
                        }
                        Tmp_OGONOAF = sheet1.Range[myfunc.GetExcelPos(1, i + 1)].Value;
                        Tmp_NGONOAF = sheet1.Range[myfunc.GetExcelPos(2, i + 1)].Value;
                        Tmp_RPNOAF = sheet1.Range[myfunc.GetExcelPos(3, i + 1)].Value;
                        Tmp_MRPNOAF = sheet1.Range[myfunc.GetExcelPos(4, i + 1)].Value;

                        Tmp_WBSNO = sheet1.Range[myfunc.GetExcelPos(5, i + 1)].Value;
                        Tmp_RQTYAF = sheet1.Range[myfunc.GetExcelPos(6, i + 1)].Value;
                        Tmp_C_NM = sheet1.Range[myfunc.GetExcelPos(7, i + 1)].Value;
                        Tmp_ES_NM = sheet1.Range[myfunc.GetExcelPos(8, i + 1)].Value;
                        Tmp_NSN = sheet1.Range[myfunc.GetExcelPos(9, i + 1)].Value;

                        Tmp_PN = sheet1.Range[myfunc.GetExcelPos(10, i + 1)].Value;
                        Tmp_SEQOAF = sheet1.Range[myfunc.GetExcelPos(11, i + 1)].Value;
                        Tmp_EIAC_CUS = sheet1.Range[myfunc.GetExcelPos(12, i + 1)].Value;
                        Tmp_RBTHAF = sheet1.Range[myfunc.GetExcelPos(13, i + 1)].Value;
                        Tmp_PRNRAF = sheet1.Range[myfunc.GetExcelPos(14, i + 1)].Value;

                        Tmp_CRNRAF = sheet1.Range[myfunc.GetExcelPos(15, i + 1)].Value;
                        Tmp_FREDTAF = sheet1.Range[myfunc.GetExcelPos(16, i + 1)].Value;
                        Tmp_OSM = sheet1.Range[myfunc.GetExcelPos(17, i + 1)].Value;
                        Tmp_EONO = sheet1.Range[myfunc.GetExcelPos(18, i + 1)].Value;
                        Tmp_CDCODE = sheet1.Range[myfunc.GetExcelPos(19, i + 1)].Value;

                        Tmp_FRTP = sheet1.Range[myfunc.GetExcelPos(20, i + 1)].Value;
                        Tmp_DLEMPLYID = sheet1.Range[myfunc.GetExcelPos(21, i + 1)].Value;
                        Tmp_CREMPLYID = sheet1.Range[myfunc.GetExcelPos(22, i + 1)].Value;
                        Tmp_MTEMPLYID = sheet1.Range[myfunc.GetExcelPos(23, i + 1)].Value;
                        Tmp_NRCLAF = sheet1.Range[myfunc.GetExcelPos(24, i + 1)].Value;

                        Tmp_QDRNO = sheet1.Range[myfunc.GetExcelPos(25, i + 1)].Value;
                        Tmp_REMARK = sheet1.Range[myfunc.GetExcelPos(26, i + 1)].Value;
                        Tmp_FACWC = sheet1.Range[myfunc.GetExcelPos(27, i + 1)].Value;
                        Tmp_NEMPID = sheet1.Range[myfunc.GetExcelPos(28, i + 1)].Value;
                        Tmp_LGROUP = sheet1.Range[myfunc.GetExcelPos(29, i + 1)].Value;

                        Tmp_ISSN = sheet1.Range[myfunc.GetExcelPos(30, i + 1)].Value;
                        Tmp_MCODE = sheet1.Range[myfunc.GetExcelPos(31, i + 1)].Value;
                        Tmp_PJDT = sheet1.Range[myfunc.GetExcelPos(32, i + 1)].Value;


                        //if (Tmp_curpos == 290)
                        //{
                        //    Tmp_Str = " 第 290筆資料";
                        //};                                                
                        //將日期轉成  yyyy/MM/dd 格式, 若非日期格式,則傳回 ""
                        Tmp_PRNRAF = myfunc.DatetoStr(Tmp_PRNRAF);
                        Tmp_CRNRAF = myfunc.DatetoStr(Tmp_CRNRAF);
                        Tmp_NRCLAF = myfunc.DatetoStr(Tmp_NRCLAF);
                        Tmp_FREDTAF = myfunc.DatetoStr(Tmp_FREDTAF);
                        Tmp_PJDT = myfunc.DatetoStr(Tmp_PJDT);

                        Tmp_STATFM = "A";
                        Tmp_BTDT= DateTime.Now.ToString("yyyy/MM/dd");
                        Tmp_MDDT = DateTime.Now.ToString("yyyy/MM/dd");



                        Tmp_FMNO = get_NEXT_FMNO1(Tmp_pre_FMNO);
                        
                        Tmp_Sql = " INSERT  INTO  AMM_FM "
                                        + "  ( FMNO,"
                                        +     " DTSOR, OGONOAF, NGONOAF, RPNOAF, MRPNOAF,"
                                             + " WBSNO, RQTYAF, C_NM, ES_NM, NSN,  "
                                             + " PN, SEQOAF, EIAC_CUS,RBTHAF,PRNRAF, "
                                             + " CRNRAF, FREDTAF, OSM, EONO, CDCODE, "
                                             + " FRTP, DLEMPLYID, CREMPLYID, MTEMPLYID, NRCLAF, "
                                             + " QDRNO, REMARK, FACWC, NEMPID, LGROUP, "
                                             + " ISSN, MCODE, PJDT , STATFM, BTDT, "
                                             +"  MDDT"
                                        + ") "
                                        + "  VALUES "                                        
                                        + "(" + myfunc.AA(Tmp_FMNO) + ","
                                        + myfunc.AA(Tmp_DTSOR) + ","+ myfunc.AA(Tmp_OGONOAF) + "," + myfunc.AA(Tmp_NGONOAF) + "," + myfunc.AA(Tmp_RPNOAF) + "," + myfunc.AA(Tmp_MRPNOAF) + ",  "
                                        + myfunc.AA(Tmp_WBSNO) + "," + myfunc.AA(Tmp_RQTYAF) + "," + myfunc.AA(Tmp_C_NM) + "," + myfunc.AA(Tmp_ES_NM) + "," + myfunc.AA(Tmp_NSN) + ",  "
                                        + myfunc.AA(Tmp_PN) + "," + myfunc.AA(Tmp_SEQOAF) + "," + myfunc.AA(Tmp_EIAC_CUS) + "," + myfunc.AA(Tmp_RBTHAF) + "," + myfunc.AA(Tmp_PRNRAF) + ",  "
                                        + myfunc.AA(Tmp_CRNRAF) + "," + myfunc.AA(Tmp_FREDTAF) + "," + myfunc.AA(Tmp_OSM) + "," + myfunc.AA(Tmp_EONO) + "," + myfunc.AA(Tmp_CDCODE) + ",  "
                                        + myfunc.AA(Tmp_FRTP) + "," + myfunc.AA(Tmp_DLEMPLYID) + "," + myfunc.AA(Tmp_CREMPLYID) + "," + myfunc.AA(Tmp_MTEMPLYID) + "," + myfunc.AA(Tmp_NRCLAF) + ",  "
                                        + myfunc.AA(Tmp_QDRNO) + "," + myfunc.AA(Tmp_REMARK) + "," + myfunc.AA(Tmp_FACWC) + "," + myfunc.AA(Tmp_NEMPID) + "," + myfunc.AA(Tmp_LGROUP) + ",  "
                                        + myfunc.AA(Tmp_ISSN) + "," + myfunc.AA(Tmp_MCODE) + "," + myfunc.AA(Tmp_PJDT) +","+ myfunc.AA(Tmp_STATFM)+"," + myfunc.AA(Tmp_BTDT) +","
                                        + myfunc.AA(Tmp_MDDT)
                                        + ") ";
                        //myfunc.SqlExec(Tmp_Sql);
                        //因為批次執行,所以 Tmp_NEXT_FMNO 均相同, 多筆時,會重複
                        SQLStringList_A.Add(Tmp_Sql);
                        Tmp_pre_FMNO = Tmp_FMNO;
                    };
                    excuteSQLTran(SQLStringList_A);
                    Tmp_RtnMsg = "匯入成功!!";
                    Tmp_Str = "{success: true,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
                    response.Content = new StringContent(Tmp_Str);    // 回應內容
                    return response;
                }// try
                catch (Exception e)
                {                                                                                          //失敗
                    Tmp_RtnMsg = "批次匯入失敗!!<br>"
                                             + "第" + Tmp_curpos.ToString() + "筆資料<br>"
                                             + e.Message + "<br>";
                    //+ Tmp_Sql ;
                    //Tmp_Str = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) +",Tmp_Sql:"+ myfunc.AA(Tmp_Sql) +"}";
                    Tmp_Str = "{success: false,FName:" + myfunc.AA(FName) + ",Rtn_Msg:" + myfunc.AA(Tmp_RtnMsg) + "}";
                    response.Content = new StringContent(Tmp_Str);    // 回應內容                    
                    return response;
                }
            }  //end of foreach (string cur_FName in Request.Files)
            return response;
        }  //  end of  uploadFileToDB()



        //若 par_pre_FMNO空白,則由資料庫取 MAX(FMNO), 
        //若有值,則直接+1
        public string get_NEXT_FMNO1(string par_pre_FMNO)
        {
            var response = this.Request.CreateResponse();
            string Tmp_Sql, Tmp_Str;
            string Tmp_DStr = "FM" + DateTime.Now.ToString("yyyyMMdd"); ;
            string Tmp_MAX_FMNO, Tmp_NEXT_FMNO;
            if (myfunc.checkisnull(par_pre_FMNO))
            {
                Tmp_Sql = "  SELECT   MAX(FMNO) "
                                + "  FROM      AMM_FM "
                                + "  WHERE   FMNO LIKE  " + myfunc.AA(Tmp_DStr + '%');
                Tmp_MAX_FMNO = myfunc.SqlValue(Tmp_Sql);
            }
            else
                Tmp_MAX_FMNO = par_pre_FMNO;
            if (myfunc.checkisnull(Tmp_MAX_FMNO))
            {
                Tmp_NEXT_FMNO = Tmp_DStr + "001";
            }
            else
            {
                //取流水號(末3碼)+1  
                int Tmp_NextSerno = int.Parse(Tmp_MAX_FMNO.Substring(Tmp_MAX_FMNO.Length - 3, 3));
                Tmp_NextSerno = Tmp_NextSerno + 1;
                Tmp_NEXT_FMNO = Tmp_DStr + Tmp_NextSerno.ToString("000");
            }            
            return Tmp_NEXT_FMNO;
            //return Tmp_NEXT_FMNO;
        }

2022年9月12日 星期一

V80C03 - 單檔 - 編輯頁 & 編輯按鈕 不顯示(設為 false)

目的: 編輯頁 & 編輯按鈕 設為 false

處理說明:  //將 [編輯]頁 & 編輯按鈕設為 false

    1>Ext.getCmp('TMSingleViewEdit').setTabVisible(0, false);

    2>Ext.getCmp('TMSingleViewEdit').setTemplateBtnVisible(false);



1> *.js
    //將 [編輯]頁 & 編輯按鈕設為 false
    Ext.getCmp('TMSingleViewEdit').setTabVisible(0, false);         
    Ext.getCmp('TMSingleViewEdit').setTemplateBtnVisible(false);



V80C03 - 顯示資料 - 分頁不正確

 目的: 顯示資料的分頁處理(RNK), RNK 必需為唯一, 即 order by 必需為 PK

處理說明:  1>*.cs 顯示資料的 SQL  , RNK 必需為唯一 , 即 order by 必需為 PK 



1>*.cs
[HttpPost]
        public dynamic getGridData_M(bool par_paging= true)
        {
        
Sql = " SELECT  RNK,     "
                          + " ACNO,SAPNO,AMMNO,ITM,PN,MOSM, PLANNINGCARD,AREA, TEAM,CRITCL,BSDT,BEDT,STAFF,HRS,STDHRS,DURAION,STARTDAY,ENDDAY,INSDT, CLS,  "
                            + "  GRP,FOREMAN1,TEAM,LDMAN1,EMPLYID,DT,CLSRM,PATCHMAN,ASHR,REMARK,PTY,CREWBOARDSDT "
                            + "  FROM  ( "
+"select  dense_rank()  over(order by  nvl(d.STARTVAL,d.ACNO),d.SAPNO,a.AMMNO,a.ITM,b.EMPLYID,b.DT) as RNK,"
//+ "select  dense_rank()  over(order by  nvl(d.STARTVAL,d.ACNO),d.SAPNO) as RNK,"   //非唯一,分頁會有問題
                                                   + "   nvl(d.STARTVAL,d.ACNO) as ACNO, d.SAPNO,d.PTY,d.CREWBOARDSDT, "
                                                   + "  a.AMMNO,a.ITM,a.PN,a.MOSM,a.PLANNINGCARD,a.AREA, a.TEAM,a.CRITCL,a.BSDT,a.BEDT,a.STAFF,a.HRS,a.STDHRS,a.DURAION,a.STARTDAY,a.ENDDAY,a.INSDT, a.CLS, "
                                                   + "  b.GRP,b.FOREMAN1,b.TEAM as bTEAM,b.LDMAN1,b.EMPLYID,b.DT,b.CLSRM,b.PATCHMAN,b.ASHR,b.REMARK "
                                       + "  from AMM_CREW a, AMM_SRO d,  "
                                                   + " (select    a.AMMNO,a.ITM,a.GRP,b.FOREMAN1,a.TEAM,c.LDMAN1,a.EMPLYID,a.DT,a.CLSRM,a.PATCHMAN,a.ASHR,a.REMARK  "
                                                   + "  from      AMM_CREWD a, AMM_CREWGRP b,AMM_CREWTEAM c "
                                                   + "   where   a.GRP = b.GRP and a.TEAM = c.TEAM) b  "
                                      + "   where  a.AMMNO = b.AMMNO(+)  "
                                      + "   and       a.ITM = b.ITM(+)   "
                                      + "   and      a.AMMNO=d.AMMNO  ";                                    
            //AMM單號
            if (!myfunc.checkisnull(Tmp_AMMNO))
            {
                Tmp_Sql = Tmp_Sql + " AND   A.AMMNO=" + myfunc.AA(Tmp_AMMNO);
            };
            //分工單計畫開始日   起~迄
            if ((!myfunc.checkisnull(Tmp_BEDT1)) || (!myfunc.checkisnull(Tmp_BEDT2)))
            {
                if (!myfunc.checkisnull(Tmp_BEDT1))
                {
                    Tmp_Sql = Tmp_Sql + " AND   A.BEDT  >=" + myfunc.AA(Tmp_BEDT1);
                }
                if (!myfunc.checkisnull(Tmp_BEDT2))
                {
                    Tmp_Sql = Tmp_Sql + " AND   A.BEDT  <=" + myfunc.AA(Tmp_BEDT2);
                }
            };
            //施工日期   起~迄
            if ((!myfunc.checkisnull(Tmp_DT1)) || (!myfunc.checkisnull(Tmp_DT2)))
            {
                if (!myfunc.checkisnull(Tmp_DT1))
                {
                    Tmp_Sql = Tmp_Sql + " AND   B.DT  >=" + myfunc.AA(Tmp_DT1);
                }
                if (!myfunc.checkisnull(Tmp_DT2))
                {
                    Tmp_Sql = Tmp_Sql + " AND   B.DT  <=" + myfunc.AA(Tmp_DT2);
                }
            };
            //區域
            if (!myfunc.checkisnull(Tmp_AREA))
            {
                Tmp_Sql = Tmp_Sql + " AND   A.AREA=" + myfunc.AA(Tmp_AREA);
            };
            //發工件號
            if (!myfunc.checkisnull(Tmp_PN))
            {
                Tmp_Sql = Tmp_Sql + " AND   A.PN=" + myfunc.AA(Tmp_PN);
            };
            //CREWBOARD起始天   起~迄
            if ((!myfunc.checkisnull(Tmp_PLANDAY1)) || (!myfunc.checkisnull(Tmp_PLANDAY2)))
            {
                Tmp_Sql = Tmp_Sql + " AND   A.PN  IN  (SELECT  DISTINCT PLANNINGCARD FROM   AMM_PLANCARD  WHERE  1=1 ";
                if (!myfunc.checkisnull(Tmp_PLANDAY1))
                {
                    Tmp_Sql = Tmp_Sql + " AND   PLANDAY  >=" + myfunc.AA(Tmp_PLANDAY1);
                }
                if (!myfunc.checkisnull(Tmp_PLANDAY2))
                {
                    Tmp_Sql = Tmp_Sql + " AND   PLANDAY  <=" + myfunc.AA(Tmp_PLANDAY2);
                }
                Tmp_Sql = Tmp_Sql + " ) ";
            };
            //TEAM
            if (!myfunc.checkisnull(Tmp_TEAM))
            {
                Tmp_Sql = Tmp_Sql + " AND   A.TEAM=" + myfunc.AA(Tmp_TEAM);
            };
            //領班 FOREMAN1 of   AMM_CREWGRP.GRP
            if (!myfunc.checkisnull(Tmp_FOREMAN1))
            {
                Tmp_Sql = Tmp_Sql + " AND   B.GRP  IN (SELECT GRP FROM  AMM_CREWGRP  WHERE  FOREMAN1=" + myfunc.AA(Tmp_FOREMAN1) + ")";
            };
            //小組長 LDMAN1 of  AMM_CREWTEAM.TEAM
            if (!myfunc.checkisnull(Tmp_LDMAN1))
            {
                Tmp_Sql = Tmp_Sql + " AND   B.TEAM  IN (SELECT TEAM FROM  AMM_CREWTEAM  WHERE  LDMAN1=" + myfunc.AA(Tmp_LDMAN1) + ")";
            };
            // AMM_SRO的過濾條件
            //機號 & 工號
            if ((!myfunc.checkisnull(Tmp_ACNO)) || (!myfunc.checkisnull(Tmp_SAPNO)) ||
                  (!myfunc.checkisnull(Tmp_PTY1)) || (!myfunc.checkisnull(Tmp_PTY2)) ||
                  (!myfunc.checkisnull(Tmp_CREWBOARDSDT1)) || (!myfunc.checkisnull(Tmp_CREWBOARDSDT2)))
            {
                Tmp_Sql = Tmp_Sql + " AND   A.AMMNO  IN (SELECT AMMNO FROM  AMM_SRO WHERE  1=1 ";
                if (!myfunc.checkisnull(Tmp_ACNO))
                {
                    Tmp_Sql = Tmp_Sql + " AND   nvl(STARTVAL,ACNO)=" + myfunc.AA(Tmp_ACNO);
                };
                if (!myfunc.checkisnull(Tmp_SAPNO))
                {
                    Tmp_Sql = Tmp_Sql + " AND   SAPNO=" + myfunc.AA(Tmp_SAPNO);
                };
                if (!myfunc.checkisnull(Tmp_PTY1))
                {
                    Tmp_Sql = Tmp_Sql + " AND   PTY>=" + Tmp_PTY1;
                };
                if (!myfunc.checkisnull(Tmp_PTY2))
                {
                    Tmp_Sql = Tmp_Sql + " AND   PTY<=" + Tmp_PTY2;
                };
                if (!myfunc.checkisnull(Tmp_CREWBOARDSDT1))
                {
                    Tmp_CREWBOARDSDT1 = string.Format("{0:yyyy/MM/dd}", DateTime.Parse(Tmp_CREWBOARDSDT1));
                    Tmp_Sql = Tmp_Sql + " AND   CREWBOARDSDT>=" + myfunc.AA(Tmp_CREWBOARDSDT1);
                };
                if (!myfunc.checkisnull(Tmp_CREWBOARDSDT2))
                {
                    Tmp_CREWBOARDSDT2 = string.Format("{0:yyyy/MM/dd}", DateTime.Parse(Tmp_CREWBOARDSDT2));
                    Tmp_Sql = Tmp_Sql + " AND   CREWBOARDSDT<=" + myfunc.AA(Tmp_CREWBOARDSDT2);
                };
                Tmp_Sql = Tmp_Sql + " )";
            }

            Tmp_Sql = Tmp_Sql + "   )   ";                                                     
            cmd.CommandText = Tmp_Sql;
            string countSql = " SELECT COUNT(*) as total FROM (" + Tmp_Sql + ")";
            //匯出時 ,取全部資料, par_paging =true
            DataSet ds = getDataTable(cmd, countSql, par_paging);


2022年9月7日 星期三

V20306 - 傳送檔案至前端 - 若有錯誤(Exception),則顯示錯誤訊息

 目的: 傳送檔案至前端 - 若有錯誤(Exception),則顯示錯誤訊息

處理說明:  1>*.cs  , 
            將錯誤訊息加入 Cookie & HttpContext.Current.ApplicationInstance.CompleteRequest();

             catch (Exception e)
            {
                var Tmp_ErrMsg = e.Message;
                //包裝標籤檔產生完成
                Tmp_Str = "產生包裝匯出檔案失敗<br>"                                
                                + Tmp_ErrMsg;
                MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));                
                HttpContext.Current.Response.Cookies.Add(MyCookie);
                HttpContext.Current.ApplicationInstance.CompleteRequest();

                return;
            }




1>*.js
//查詢  - 將目前所選的資料,匯出成報表
    function LookBtn_click() {
        var Tmp_Grid = Ext.getCmp('grid_Single');
        var cur_recs,cur_rec;
        var Tmp_SAPNO, Tmp_PN, Tmp_QTY, Tmp_EOCND, Tmp_RMK;
        var Tmp_data_JSON = "";  //將store資料轉成 JSON 的字串
        var Tmp_sub_np = {};
        let Tmp_DataAry1 = [];
        var Tmp_Str = "";
        cur_recs = Tmp_Grid.getView().selModel.getSelection();
        //console.log("cur_recs:", cur_recs);
        if (cur_recs.length == 0) {
            mywarnalert("請先選擇要查詢的資料");
            return;
        }
        for (var i = 0; i < cur_recs.length; i++) {
            cur_rec = cur_recs[i];
            //console.log(Tmp_Str + "cur_rec:", cur_rec);
            Tmp_DataAry1.push(cur_rec.data);
        }
        //console.log("Tmp_DataAry1:", Tmp_DataAry1);
        // 將目前子畫面的資料(Table)轉成 JSON字串
        let Tmp_DataString1 = JSON.stringify(Tmp_DataAry1);
        //console.log("Tmp_DataString1:", Tmp_DataString1);        
        var np = {};
        np = {
            DataString1: Tmp_DataString1,            
        };

        Ext.getCmp('s_form').submit({
        //Ext.Ajax.request({
            url: '../../api/V20306API/DoPRNLIST',
            method: 'POST',
            async: false,
            standardSubmit: true, //若要傳送檔案至前端, standardSubmit必需設為 true  
            params: np,
        });
        //顯示結果訊息..

        var mask = new Ext.LoadMask(Ext.getBody(), {
            msg: '處理中,請稍待...'
        });

        mask.show();//使用 mask 需手動呼叫show() 方法下
        var timer = setInterval(function () {
            clearInterval(timer);
            mask.hide();
            timer = null;
            var r = r_cookies('EX_DFile');
            //console.log("r_cookies=", r);
            if (!checkisnull(r)) {
                mysuccessalert(r);
            }
        }, 5000);  //3000ms = 3sec
    }  // end of  function LookBtn_click() {


2>*.cs  -  HttpContext.Current.ApplicationInstance.CompleteRequest();

 HttpCookie MyCookie;
 try
 {

}
 catch (Exception e) {
               var Tmp_ErrMsg = e.Message;
                //包裝標籤檔產生完成
                Tmp_Str = "產生包裝匯出檔案失敗<br>"                                
                                + Tmp_ErrMsg;
                MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));                
                HttpContext.Current.Response.Cookies.Add(MyCookie);  
                HttpContext.Current.ApplicationInstance.CompleteRequest();
                return;
}
 finally
            {                
                wk.Dispose();
                conn.Close();
            }

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode((string)FileName, System.Text.Encoding.UTF8) + "\"");
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            HttpContext.Current.Response.BinaryWrite(mstream.ToArray());
            //包裝標籤檔產生完成
            Tmp_Str = "產生包裝匯出檔案成功<br>"
                            + "(" + FileName + ")<br>";
            MyCookie = new HttpCookie("EX_DFile", HttpUtility.UrlEncode(Tmp_Str));
            HttpContext.Current.Response.Cookies.Add(MyCookie);
            HttpContext.Current.Response.End();

2022年9月6日 星期二

Array 處理 - Array宣告 - 判斷元素是否在 Array內 - c# 判斷字串是否在長字串內

 目的: Array 的宣告 & 判斷 元素是否在 Array內

處理說明: 1>宣告 Array  :    
                        Opts1> let  Tmp_Ary=["A","B","C"];   //宣告 Tmp_Ary 共 3元素("A","B","C")
                        Opts2> let  Tmp_Ary= new Array(3);  //宣告 Tmp_Ary 共 3元素
                  2>判斷元素("A")是否在 Array內 , 利用 Array.indexOf("A") , 傳回 index 
                       if (Tmp_Ary.indexOf("A")== -1)
                  3>c# 字串判斷是否存在 , 若 Tmp_IRESAK 為"", 則傳回 0 , 非 -1
                        if ("1;2;3;4;5;6;7;8;".IndexOf(Tmp_IRESAK) == -1)
                            {
                                Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[檢測結果代碼]欄位值不符!!"
                                                          +"(" +Tmp_IRESAK + ")<br>";
                            }
                        --> 所以必需先判斷 Tmp_IRESAK 是否為空字串
                               if (myfunc.checkisnull(Tmp_IRESAK.Trim()))
                                {
                                  Tmp_RtnMsg1 = Tmp_RtnMsg1 + "[檢測結果代碼]欄位值不可空白!!<br>";
                                  }

 PS: *.js  利用 "A" in Tmp_Ary 不準,  不用之   , 

        --> 但 *.cs   key in Keys 可


1>*.js  var  Tmp_Ary=["A","B","C"];

  let Tmp_Ary = ["A", "B", "C", "D"];

            console.log("1 par_WTLV in [A,B, C,D] :", ("A" in Tmp_Ary));            

            console.log("3 Tmp_Ary.indexOf(par_WTLV) :", Tmp_Ary.indexOf(par_WTLV));

            //用  Arrary 用  in  不準, 改用 indexOf

            if (Tmp_Ary.indexOf(par_WTLV)==-1)

                return;


2>*.cs  string[] Tmp_StrAry= new string[3] {"A","B","C"}

             string[]  Tmp_StrAry=new string[3] {"MITM","WITM","DDITM"};

             int[]   Tmp_intAry= new int[3]{21,22,25};

            Tmp_intAry[2]=23;

            int Tmp_int2=Tmp_intAry[2];


             Tmp_StrAry[0].Contains("A");

             Tmp_StrAry.Contains(obj.Index);


3>Array 也是 Object 的型態之一

    Ex: var Tmp_Ary=[];

           Tmp_Ary.push(cur_rec);   //method

           Tmp_Ary.length=1;          //property  

           Tmp_Ary.newSet=-1;   //宣告新的 property





V20306 - 欄位值2Checkbox - 將資料庫欄位值顯示 在 checkbox

 目的: 將資料庫欄位值轉成 checkbox  &  將畫面checkbox值轉成資料庫欄位值

處理說明: 1>顯示資料 -  將資料庫欄位值轉成 checkbox (tab1.onShow event )

                  2>儲存資料 -  將畫面checkbox值轉成資料庫欄位值(按[存檔]鈕)




1>*.js 顯示資料 - 將資料庫欄位值轉成 checkbox (tab1.onShow event )

Ext.getCmp("tab1").on("show", function (me, eOpts) {
        //編輯畫面顯示,將欄位值反應至編輯畫面的 checkbox 選項             
        console.log("*** tab1 show event trigger");
        var sel_recs = Ext.getCmp('grid_Single').getView().getSelectionModel().getSelection();
        if (sel_recs.length > 0) {
            var sel_rec = sel_recs[0];
            console.log("sel_rec:" , sel_rec);
            if (checkisnull(sel_rec)) {
                return true;
            }
            var Tmp_WTLV = sel_rec.data["WTLV"].toString();
            var Tmp_ITPR = sel_rec.data["ITPR"].toString();
            setcheckbox_WTLV(Tmp_WTLV);
            setcheckbox_ITPR(Tmp_ITPR);            
        }

        function setcheckbox_WTLV(par_WTLV) {            
            let Tmp_Ary = ["A", "B", "C", "D"];
            //用  Arrary 用  in  不準, 改用 indexOf
            if (Tmp_Ary.indexOf(par_WTLV)==-1)
                return;


            var WTLV_id = "WTLV_" + par_WTLV;
            Ext.getCmp(WTLV_id).setValue(true);
        }

        //欄位值 par_ITPR: A/B/C/D/E/F/G:其他1
        function setcheckbox_ITPR(par_ITPR) {
            let Tmp_Ary = ["A", "B", "C", "D", "E", "F", "G"];
            let Tmp_ITPR1 = par_ITPR.substr(0, 1);
            if (Tmp_Ary.indexOf(Tmp_ITPR1) == -1)
                return;

            var par_ITPR_Ary = new Array;
            par_ITPR_Ary = par_ITPR.split(":");         
            //設定其他備註欄位  
            if (par_ITPR_Ary[0] == "G") {
                Ext.getCmp("ITPR1").setValue(par_ITPR_Ary[1]);
            }
            var ITPR_id = "ITPR_" + par_ITPR_Ary[0];                        

            //若為 G:其他1 , 則需特別處理
            var Tmp_ITPR_id = Ext.getCmp(ITPR_id).getValue();            
            Ext.getCmp(ITPR_id).setValue(true);            
        }
    });


     //修改的存檔, 重新顯示
    Ext.getCmp('btn_save').beforeEdit = function () {
        //執行後端 Upate 程式
        isCheck = S_DB.doSave('Update');
        //後端   Insert 後, store 重取
        if (isCheck)
           Ext.getCmp("btn_Show").fireHandler();
        return isCheck;
    };


2>*.cs 儲存資料 -  將畫面checkbox值轉成資料庫欄位值(按[存檔]鈕)

[HttpPost]
        public void Update()
        {
            var c = System.Web.HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            string[] arrCondition = getPK();
            NameValueCollection nvc1 = new NameValueCollection();
            nvc1=Proc_ITPR(nvc1, nvc);            
            excuteUpdate(nvc1, DBTable, arrCondition);
        }

public NameValueCollection Proc_ITPR(NameValueCollection par_nvc1, NameValueCollection par_nvc)
        {
            //nvc 移除 radio開頭的 key
            var Tmp_key = "";
            foreach (string key in par_nvc.Keys)
            {
                if (!key.Contains("radio"))
                    par_nvc1[key] = par_nvc[key];
            }

            //若為[ITPR]若為'G', 其他則 nvc1[ITPR]=nvc1[ITPR]+":"+nvc1[ITPR1];
            //刪除 nvc1[ITRP1]
            if (par_nvc1["ITPR"] == "G")
                par_nvc1["ITPR"] = par_nvc1["ITPR"] + ":" + par_nvc1["ITPR1"];
            par_nvc1.Remove("ITPR1");
            return par_nvc1;
        }

 public void Insert()
        {
            var c = System.Web.HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            NameValueCollection nvc1= new NameValueCollection();            
            nvc1 = Proc_ITPR(nvc1, nvc);        
            excuteInsert(nvc1, DBTable);
        }


3>V20306_JSON.js  - 畫面欄位設計  - checkbox


{
  xtype: 'radiogroup', fieldLabel: '重量等級', labelWidth: 160, layout: 'vbox',
id: 'WTLV',
items: [
{
boxLabel: '極重(木箱)',
name: 'WTLV', 
id: 'WTLV_A', 
inputValue: 'A',
border: 1,
width:300
}, {
boxLabel: '很重(七層紙箱)',
name: 'WTLV', 
id: 'WTLV_B', 
inputValue: 'B',
border: 1,
}, {
boxLabel: '重(五層紙箱)',
name: 'WTLV', 
id: 'WTLV_C', 
inputValue: 'C',
checked: true,
},
{
boxLabel: '一般(三層紙箱)',
name: 'WTLV', 
id: 'WTLV_D', 
inputValue: 'D',
}
]
}, // end of  WTLV重量等級
{
                xtype: 'radiogroup', fieldLabel: '內層包裝需求', labelWidth: 160, layout: 'vbox',
id: 'ITPR',
items: [
{
boxLabel: '旭化層',
name: 'ITPR', 
id: 'ITPR_A', 
inputValue: 'A',
}, {
boxLabel: '大氣泡布',
name: 'ITPR', 
id: 'ITPR_B', 
inputValue: 'B',
}, {
boxLabel: '小氣泡布',
name: 'ITPR', 
id: 'ITPR_C', 
inputValue: 'C',
checked: true,
},
{
boxLabel: '舒美布',
name: 'ITPR', 
id: 'ITPR_D', 
inputValue: 'D',
},
{
boxLabel: '牛皮紙',
name: 'ITPR', 
id: 'ITPR_E', 
inputValue: 'E',
},
{
boxLabel: '紙版',
name: 'ITPR', 
id: 'ITPR_F', 
inputValue: 'F',
},
                                         //用 panel - 包含 checkbox & TextField  
{
xtype: 'panel', layout: { type: 'hbox' }, border: 0,
items: [
{
xtype: 'radio', boxLabel: '其他', name: 'ITPR',
id: 'ITPR_G', 
inputValue: 'G',
},
{ xtype: 'textfield', id: 'ITPR1', width: 150, padding: "0 5 0 5" },
]
},
]
}, // end of  ITPR