---
# System prepended metadata

title: 後端Controller（基本）
tags: [國興實習]

---

---
tags: 國興實習
---
# 後端Controller（基本）

:::info
**⚠️下拉命名：QL=查詢之下拉、DL=編輯or新增之下拉**
:::

### 初始方法
```C#=
[System.Web.Http.HttpGet]
public IHttpActionResult Init()
{
    Searching += l_Master_Searching; //搜尋事件
    Initialing += l_Master_Initialing; //加入下拉元件
    return doInitResult();
}
```

### 新增、刪除、編輯方法
```C#=
[System.Web.Http.HttpPost]
public IHttpActionResult Insert(DeltaMD a_dr)
{
    this.Posting += l_Master_Posting; //存檔前事件
    RefreshMasterData += l_Master_RefreshMasterData;
    return doPostResult(enStatus.Insert, a_dr);
}

[System.Web.Http.HttpPost]
public IHttpActionResult Delete(DeltaMD a_dr)
{
    return doPostResult(enStatus.Delete, a_dr);
}

[System.Web.Http.HttpPost]
public IHttpActionResult Edit(DeltaMD a_dr)
{
    RefreshMasterData += l_Master_RefreshMasterData;
    return doPostResult(enStatus.Edit, a_dr);
}
```

### 後端getMaster→SQL語法連結
```C#=
public override MasterClass getMaster()
{
    //SQL必須加入1=1條件，以便if判斷式參數取代
    StringBuilder l_SQL = new StringBuilder();
    l_SQL.Append(@" Select M.* From FNAPRJNEO M Where 1=1 order by m.APRJNEO_ID_NEO "); //前端Grid顯示依據
    return new MasterClass()
    {
        tableName = "FNAPRJNEO",
        isNoDataFirstLoad = false, //程式第一次載入時是否有資料
        sQL = l_SQL.ToString().ToUpper(),
        keyField = new string[1] { "APRJNEO_ID_NEO" },
        notNullField = new string[2] { "APRJNEO_NM_NEO", "APRJNEO_RM" },//不可空白欄位
        noEditButInsertField = new string[0] { },//不可編輯,但可新增欄位
        noEditField = new string[1] { "APRJNEO_ID_NEO" }, //不可編輯欄位
        searchData = new SearchClass() { APRJNEO_NM_NEO = "", APRJNEO_RM = "" }//查詢條件預設值
    };
}
```
### SearchClass變數宣告
```C#=
public class SearchClass : ISearchClass
{
    public string TYPE_DEPT { get; set; } //定義變數及設定取值與賦值之保護層級

    public string TYPE_NO { get; set; }

    public string TYPE_NM { get; set; }
}
```

### 搜尋方法
```C#=
[System.Web.Http.HttpPost]
public IHttpActionResult Search(SearchClass a_Search)
{
    Searching += l_Master_Searching;
    this.Initialing += l_Master_Initialing; //加入下拉元件
    return doSearchResult(a_Search);
}
void l_Master_Searching(object sender, SearchingEventArgs e)
{
    SearchClass l_Search = (SearchClass)e.SearchData;
    string l_SQL = "";

    if (!string.IsNullOrEmpty(l_Search.TYPE_DEPT))
    {
        l_SQL = l_SQL + " AND (IsNull(M.TYPE_DEPT,'') = @TYPE_DEPT )";
        e.ht.Add("@TYPE_DEPT", new StructureSQLParameter(l_Search.TYPE_DEPT, SqlDbType.NVarChar));
    }

    if (!string.IsNullOrEmpty(l_Search.TYPE_NO))
    {
        l_SQL = l_SQL + " AND (IsNull(M.TYPE_NO,'') = @TYPE_NO )";
        e.ht.Add("@TYPE_NO", new StructureSQLParameter(l_Search.TYPE_NO, SqlDbType.NVarChar));
    }


    if (!string.IsNullOrEmpty(l_Search.TYPE_NM))
    {
        l_SQL = l_SQL + " AND (IsNull(M.TYPE_NM,'') like '%' + @TYPE_NM + '%' )";
        e.ht.Add("@TYPE_NM", new StructureSQLParameter(l_Search.TYPE_NM, SqlDbType.NVarChar));
    }

    e.MasterSQL = e.MasterSQL.Replace("1=1", "1=1 " + l_SQL);
}
```
### 刷新資料方法
當資料有所變動時要刷新，所以才會在新增、編輯呼叫到
```C#=
#region l_Master_RefreshMasterData方法
void l_Master_RefreshMasterData(object sender, RefreshMasterDataEventArgs e) //刷新主鍵
{
    string l_SQL = @" AND TYPE_NO = @TYPE_NO ";
    e.ht.Add("@TYPE_NO", new StructureSQLParameter(e.newRow["TYPE_NO"].ToString(), SqlDbType.NVarChar));
    e.MasterSQL = e.MasterSQL.Replace("1=1", "1=1 " + l_SQL);
}
```
:::info
**⚠️若在getMaster()中的Append有join多個表，記得加上資料庫別名來參照該表主鍵**
:::
### 存檔前事件-以流水號為例
```C#=
void l_Master_Posting(object sender, CancelMsgEventArgs e){
    //e.newRow["TYPE_NO"] = ShareModel.GET_SEQ(e.newRow["TYPE_DEPT"].ToString()); //引用Model建立流水號
}
```
```C#=
public IHttpActionResult Insert(DeltaMD a_dr)
{
    this.Posting += l_Master_Posting; //按下新增鈕且在存檔之前
    RefreshMasterData += l_Master_RefreshMasterData;
    return doPostResult(enStatus.Insert, a_dr);
}
```
:::info
**newRow[] = 前端沒有的欄位。Ex:在SQL建立流水號（如新增、編輯）**
**oldRow[] = 前端有的值。Ex:下拉選單連動**
:::