c#建立後端介面
阿新 • • 發佈:2020-12-22
c#建立後端介面的框架如下圖結構所示:
共分為四個步驟:
-
先寫好資料庫查詢語句
-
在控制器(Controllers)中建立後端介面函式
/// <summary> /// 獲取performance bySales頁面資料 /// </summary> /// <param name="model"></param> /// <returns></returns> [HttpPost] public IActionResult GetPerformanceBySales(QueryDescriptor model) { var list = service.GetPerformanceBySales(model);// return Ok(JsonConvert.SerializeObject(list)); return MstResult.Success(list); }
-
在 Interface 中為介面函式配置介面規範
object GetPerformanceBySales(QueryDescriptor model);
-
在服務(Service)中為介面函式配置服務:資料庫查詢語句、邏輯處理等皆在此檔案中進行
/// <summary> /// 獲取performance bySales頁面資料 /// </summary>/// <param name="model"></param> /// <returns></returns> public object GetPerformanceBySales(QueryDescriptor model) { //方法一:利用c#中的SqlSugar方法將資料庫的查詢語句轉換成該方法的函式形式 //table //sql語句: SELECT business.iotype AS TYPE,business.sales AS SalesName,business.customerid AS CustID,business.customername AS CustName, COUNT(business.id) AS Shipment, SUM(business.volume) Volume,SUM(business.receivable) AS SalesAmount, SUM(business.profit) AS GrossProfit department.name AS Company FROM business LEFT JOIN department ON business.departmentid = department.code GROUP BY TYPE ,SalesName,CustID,CustName, Companyvar listTable = dal.SqlSugarClient().Queryable<Business, Department> ((s1, s2) => new Object[] { JoinType.Left, s1.Departmentid == s2.Ccode }) .GroupBy((s1, s2) => new { s1.Iotype, s1.Sales, s1.Rccode, s1.Rccodename, s2.Name }) .Where( s1 => s1.BusinessOwner == "SZX" ) .Select((s1, s2) => new { Type = s1.Iotype, SalesName = s1.Sales, CustID = s1.Rccode, CustName = s1.Rccodename, Shipment = SqlFunc.AggregateCount(s1.Id), Volume = SqlFunc.AggregateSum(s1.Volume), SalesAmount = SqlFunc.AggregateSum(s1.Receivable), GrossProfit = SqlFunc.AggregateSum(s1.Profit) }) .MergeTable() .ToPageList(1, 50); //Type = s1.Iotype 等號前的變數(Type)為資料庫欄位的別名 //方法二:直接使用 $ 符號將資料庫查詢語句包含起來 //string strsql4 = $"SELECT rccodename,SUM(profit) AS grossprofit FROM business WHERE {sqlWhere} GROUP BY rccodename ORDER BY grossprofit DESC"; //DataTable listTable4 = ((SugarRepository)dal).DbContext.Ado.GetDataTable(strsql4); //var obj = new { shipmentTotal = list1, shipment = listTable1, volumeTotal = list2, volume = listTable2, salesAmountToatl = list3, salesAmount = listTable3, grossProfitTotal = list4, grossProfit = listTable4 }; //return obj; return listTable; }
補充:在頁面中傳入引數去後端查詢出指定的資料
/// <summary> /// 獲取performance bySales頁面資料 /// </summary> /// <param name="model"></param> /// <returns></returns> public object GetPerformanceBySales(QueryDescriptor model) { //在頁面中傳入引數去後端查詢出指定的資料 string BusinessType = string.Empty; //考慮到效能問題,所以初始化欄位時對它賦值為empty //遍歷model.Conditions中的資料,根據前端傳入的條件獲取相對應的資料 model.Conditions.ForEach(s => { if (s.Key == "business_owner") { BusinessType = s.Value.ToString(); } }); //table //SELECT business.iotype AS TYPE,business.sales AS SalesName,business.customerid AS CustID,business.customername AS CustName, COUNT(business.id) AS Shipment, SUM(business.volume) Volume,SUM(business.receivable) AS SalesAmount, SUM(business.profit) AS GrossProfit department.name AS Company FROM business LEFT JOIN department ON business.departmentid = department.code GROUP BY TYPE ,SalesName,CustID,CustName, Company var listTable = dal.SqlSugarClient().Queryable<Business, Department> ((s1, s2) => new Object[] { JoinType.Left, s1.Departmentid == s2.Ccode }) .GroupBy((s1, s2) => new { s1.Iotype, s1.Sales, s1.Rccode, s1.Rccodename, s2.Name }) .Where( s1 => s1.BusinessOwner == BusinessType ) //頁面中傳入引數去後端查詢出指定的資料:BusinessOwner為資料庫欄位 .Select((s1, s2) => new { Type = s1.Iotype, SalesName = s1.Sales, CustID = s1.Rccode, CustName = s1.Rccodename, Shipment = SqlFunc.AggregateCount(s1.Id), Volume = SqlFunc.AggregateSum(s1.Volume), SalesAmount = SqlFunc.AggregateSum(s1.Receivable), GrossProfit = SqlFunc.AggregateSum(s1.Profit) }) .MergeTable() .ToPageList(1, 50); return listTable; }
頁面中傳參形式為:
{ "pageSize": 0, //後端自定義的行數 "pageIndex": 0, //後端自定義的頁碼 //排序 "orderBys": [ { "sort": "string", "order": 0 } ], //條件 "conditions": [ { "key": "business_owner", //資料庫對應欄位 "value":"SZX", //資料庫對應欄位的值 "operator": 0, "character": 0 } ] }
c#中建立一個輸出結果為json的物件。
JObject data = new JObject { { "Alleen", 43024 }, { "Lydia", 38024 }, { "Lrina", 35224 }, { "Yanni", 34321 }, { "Sasaki", 32324 }, { "Morita", 31992 }, { "Kijyo", 29238 }, { "Kobayashi", 28324 }, { "William", 27345 }, { "Lina", 89313 } }; var obj = new { data }; return obj; //輸出結果: //{"data":{"Alleen":43024,"Lydia":38024,"Lrina":35224,"Yanni":34321,"Sasaki":32324,"Morita":31992,"Kijyo":29238,"Kobayashi":28324,"William":27345,"Lina":89313}}
建立物件並插入五條資料。
JObject item = new JObject { { "type","AEX"}, { "company","KWESZX" }, { "department","Sales" },{ "team","Div1." },{ "salesname","Allen"},{ "custid","302174961" },{ "custname","IFLIGHT TECHNOLGY CO,LTD" },{ "shipment","35" },{ "volume","72.36" },{ "salesamount","468962.23" },{"grossprofit","98005.6" } }; tableData.Add(item); tableData.Add(item); tableData.Add(item); tableData.Add(item); tableData.Add(item); var obj = new { tableData }; return obj;