顯示具有 自訂SQL 標籤的文章。 顯示所有文章
顯示具有 自訂SQL 標籤的文章。 顯示所有文章

2023年8月22日 星期二

V120401A -myfunc.getDataTable1() -T1C - 自訂 SQL 取得 Grid資料 - 不含RNK , 自行處理分頁

 目的: V120401A -myfunc.getDataTable1() - 自訂 SQL 取得 Grid資料 - 不含RNK , 自行處理分頁 

處理說明: 1> 自訂 SQL 取得 Grid資料, 不含 RNK  , 不用 count_Sql                       
                        --> 呼叫  ds = myfunc.getDataTable1(par_Sql,par_page);
                  2> 順序依自訂 SQL 的  order  by xxxx; 排序 
                  3>新增 DataTable("T1C")  的 [TOTAL]欄位值=目前筆數
                      DataSet ds = new DataSet();
                      DataTable T1C = new DataTable();
                      T1C.TableName = "T1C";
                      DataRow row = T1C.NewRow();
                      T1C.Columns.Add(new DataColumn("TOTAL", typeof(Int32)));
                       row["TOTAL"] = dt.Rows.Count;
                      T1C.Rows.Add(row);
                      ds.Tables.Add(T1C);
                  4>取得 dt 的目前頁次資料 傳回 ds.T1
                      OracleDataAdapter da = new OracleDataAdapter();
                      da.SelectCommand = cmd;
                      //取 cmd 結果的 page資料,存入 ds.Tables["T1"]
                      da.Fill(ds, start, limit, "T1");   


1>myfunc.cs - get_DataTable1(string par_Sql, bool  par_page)
        //函式名稱: getDataTable1
           傳入參數  :  par_Sql  :  目前 SQL
                                par_page: true/false  : 分頁,不分頁 , 預設值: true 
          傳回值 : ds   - 已分頁的資料 , 依 page,start,limit 分頁
        */
        public static dynamic getDataTable1(string par_Sql, bool par_page=true)
        {            
            var c = HttpContext.Current;
            NameValueCollection nvc = c.Request.Form;
            DataSet ds = new DataSet();
            OracleConnection conn = new OracleConnection(DBService.ConnectionString("AMMEU"));
            try
            {            
            int page = 1;
            int start = 1;
            int limit = 30;

            if (nvc.Count > 0 && nvc["page"] != null)
            {
                page = int.Parse(nvc["page"]);
                start = int.Parse(nvc["start"]);
                limit = int.Parse(nvc["limit"]);                
            }

            //產生 par_SQL  的 DataTable , 全部資料全取
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = conn;
            cmd.CommandText = par_Sql;                
            conn.Open();
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());

            //加入 T1C 
            DataTable T1C = new DataTable();
            T1C.TableName = "T1C";
            DataRow row = T1C.NewRow();
            T1C.Columns.Add(new DataColumn("TOTAL", typeof(Int32)));
            row["TOTAL"] = dt.Rows.Count;
            T1C.Rows.Add(row);
            ds.Tables.Add(T1C);

            //加入 T1  -將 par_Sql 的 分頁資料填入 ds.T1 
            OracleDataAdapter da = new OracleDataAdapter();
            da.SelectCommand = cmd;
            if (par_page)
               da.Fill(ds, start, limit, "T1");
            else
               da.Fill(ds, "T1");
             return ds;
            }  // end try 
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {                
            }         
            return ds;
        }

2>*.js
 {
                                    xtype: 'button',
                                    id: 'btn_sub_Show',
                                    flex: 2,
                                    border: 1,
                                    text: '資料顯示',  //sub_btn_Add
                                    iconCls: 'icon-search',                                    
                                    handler: function () {
                                        //清空 - 勾選註記
                                        CHK_PK_OBJ1.PK_LIST = [];
                                        CHK_PK_OBJ1.ALL_LIST = [];
                                        //--> 需改為將 TMNO 的[AMM_TMDWG]資料加入 store 
                                        var np = {};
                                        np["sub_DOC_NUM"] = Ext.getCmp("sub_DOC_NUM").getValue();                                        
                                        np["sub_STAT"] = Ext.getCmp("sub_STAT").getValue();
                                        var is_Ok = true;
                                                                                
                                        var Tmp_store = Ext.getCmp('sub_Grid').store;
                                        console.log("0 Tmp_store: ", Tmp_store);
                                        Tmp_store.getProxy().url = '../../api/V120401AAPI/get_sub_Data1';
                                        Tmp_store.getProxy().extraParams = np; //分頁OK,篩選條件OK                  
                                        Tmp_store.pageSize = 30;
                                        Tmp_store.load();
                                        console.log("1 Tmp_store: ", Tmp_store);
                                    } // end of handler of sub_btn_Add
                                },// end of  button  sub_btn_Show  , 資料顯示

2022年7月29日 星期五

V80C02 - 自訂SQL 取得 CaluField 欄位值

 目的: 自訂SQL 取得 CaluField 欄位值 ,  不用 getKeyCode

 處理說明: 由於 CaluField 欄位不是由單一欄位值取得, CaluField 欄位

                   必需自行組合 SQL , 取得欄位值



1>*.cs 
public dynamic getGridData_M()
{
            
            cmd.CommandText = Tmp_Sql;
            string countSql = " SELECT COUNT(*) as total FROM (" + Tmp_Sql + ")";
            DataSet ds = getDataTable(cmd, countSql);

            //STEP2: ds.Tables["T1"].rows  新增 column欄位值
            //自訂  CaluField 欄位  - [報工工時(ALSHR_)]
            //取得報工工時  -- JCN+DT+EMPLYID+MOSM , 工號+施工日期+ 施工人員+主步序            
            ds.Tables["T1"].Columns.Add("ALSHR_", typeof(object));           //報工工時 ALSHR_
            
            string Tmp_Val = "";  //欄位值 以 ; 分隔
            string Tmp_SAPNO = "",Tmp_DT,Tmp_EMPLYID,Tmp_MOSM;
            string Tmp_ALSHR_;

            foreach(DataRow row in ds.Tables["T1"].Rows)
            {
                Tmp_SAPNO = row["SAPNO"].ToString();
                Tmp_DT = myfunc.DatetoStr(DateTime.Parse(row["DT"].ToString()));
                Tmp_EMPLYID = row["EMPLYID"].ToString();
                Tmp_MOSM = row["MOSM"].ToString();
                //若為一般工單(非 QDR('F') 或  ILIAS工單('V') ), 則 SAP工號+'0000"              \
                if (Tmp_SAPNO.Length > 0)
                {
                    Tmp_SAPNO = myfunc.get_SAPNO(Tmp_SAPNO);                        
                    //自訂 SQL , 取得 CaluField 欄位值 
                    Tmp_Sql = " SELECT  JCN, WORK_ID, round((sum(HOUR_WORK)+sum(HOUR_OT))/60,2)  WCHR  "
                                    + " FROM  "
                                    + "  (  SELECT  JCN, ACT, WC, HOUR_WORK, WORK_ID, HOUR_OT "
                                         + " FROM   HOUR.ALS_HOUR_COMP@ALS_HOUR  "
                                         + " WHERE  JCN = " + myfunc.AA(Tmp_SAPNO)
                                         + " AND       substr(ACT, 1, 1) = " + myfunc.AA(Tmp_MOSM)
                                         + " AND        WORK_ID = " + myfunc.AA(Tmp_EMPLYID)
                                         + " AND        DATA_DATE =" + myfunc.AA(Tmp_DT)
                                         + " UNION  ALL "
                                         + " SELECT  JCN, ACT, WC, HOUR_WORK, WORK_ID, HOUR_OT  "
                                         + "  FROM   HOUR.ALS_HOUR_COMP_HISTORY@ALS_HOUR "
                                         + " WHERE  JCN = " + myfunc.AA(Tmp_SAPNO)
                                         + " AND       substr(ACT, 1, 1) = " + myfunc.AA(Tmp_MOSM)
                                         + " AND        WORK_ID = " + myfunc.AA(Tmp_EMPLYID)
                                         + " AND        DATA_DATE =" + myfunc.AA(Tmp_DT)
                                  + " ) "
                                  + "  GROUP BY  JCN, WORK_ID ";
                    Tmp_Val = myfunc.SqlValue(Tmp_Sql);
                    if (Tmp_Val.Length > 1)
                    {
                        row["ALSHR_"] = myfunc.StrExtract(Tmp_Val, 3);    //報工工時
                    }
                }  // end of   if (Tmp_SAPNO.Length > 0)
            }  // end of  foreach (DataRow row in ds.Tables["T1"].Rows)

            return ds;

}