From 04a22f3f691e76c1449969048b502f80a19a06ec Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期六, 06 一月 2024 10:03:01 +0800 Subject: [PATCH] 添加网格接口 --- JiangSu/JiangSu.csproj | 3 JiangSu/index.html | 42 +++++++ JiangSu/cs/GridDAL.cs | 71 ++++++++++++++ JiangSu/Models/Grid.cs | 16 +++ JiangSu/Controllers/GridController.cs | 132 ++++++++++++++++++++++++++ JiangSu/App_Data/js.db | 0 6 files changed, 261 insertions(+), 3 deletions(-) diff --git a/JiangSu/App_Data/js.db b/JiangSu/App_Data/js.db index 2590bb8..d7a6456 100644 --- a/JiangSu/App_Data/js.db +++ b/JiangSu/App_Data/js.db Binary files differ diff --git a/JiangSu/Controllers/GridController.cs b/JiangSu/Controllers/GridController.cs new file mode 100644 index 0000000..8a942b6 --- /dev/null +++ b/JiangSu/Controllers/GridController.cs @@ -0,0 +1,132 @@ +锘縰sing JiangSu.cs; +using JiangSu.Models; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; + +namespace JiangSu.Controllers +{ + public class GridController : ApiController + { + [HttpGet] + public object SelectAll() + { + try + { + List<Grid> grids = GridDAL.SelectAll(); + List<JObject> list = GetJObjects(grids); + + Dictionary<string, object> dict = GetDict(); + dict.Add("features", list); + + return dict; + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + return null; + } + } + + private Dictionary<string, object> GetDict() + { + string crs = "{ \"type\": \"name\", \"properties\": { \"name\": \"urn:ogc:def:crs:OGC:1.3:CRS84\" } }"; + JObject crsJObj = JsonConvert.DeserializeObject<JObject>(crs); + + Dictionary<string, object> dict = new Dictionary<string, object>(); + dict.Add("type", "FeatureCollection"); + dict.Add("name", "缃戞牸4549"); + dict.Add("crs", crsJObj); + + return dict; + } + + private List<JObject> GetJObjects(List<Grid> grids) + { + List<JObject> list = new List<JObject>(); + if (null != grids && grids.Count > 0) + { + foreach (Grid g in grids) + { + JObject obj = JsonConvert.DeserializeObject<JObject>(g.json); + list.Add(obj); + } + } + + return list; + } + + [HttpGet] + public object SelectById(long id) + { + try + { + Grid grid = GridDAL.SelectById(id); + List<JObject> list = GetJObjects(null == grid ? null : new List<Grid> { grid }); + + Dictionary<string, object> dict = GetDict(); + dict.Add("features", list); + + return dict; + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + return null; + } + } + + [HttpGet] + public int DeleteByIds([FromUri] List<int> ids) + { + try + { + if (null == ids || ids.Count == 0) return 0; + + return GridDAL.DeleteByIds(ids); + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + return 0; + } + } + + [HttpPost] + public int Insert([FromBody] Grid grid) + { + try + { + if (null == grid) return 0; + + return GridDAL.Insert(grid); + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + return 0; + } + } + + [HttpPost] + public int UpdateById([FromBody] Grid grid) + { + try + { + if (null == grid) return 0; + + return GridDAL.UpdateById(grid); + } + catch (Exception ex) + { + LogOut.Error(ex.Message + "\r\n" + ex.StackTrace); + return 0; + } + } + } +} diff --git a/JiangSu/JiangSu.csproj b/JiangSu/JiangSu.csproj index 2b4d5db..887672e 100644 --- a/JiangSu/JiangSu.csproj +++ b/JiangSu/JiangSu.csproj @@ -136,6 +136,8 @@ <Compile Include="Controllers\HomeController.cs" /> <Compile Include="Controllers\OpController.cs" /> <Compile Include="Controllers\ValuesController.cs" /> + <Compile Include="Controllers\GridController.cs" /> + <Compile Include="cs\GridDAL.cs" /> <Compile Include="cs\LogOut.cs" /> <Compile Include="cs\ModelDAL.cs" /> <Compile Include="cs\ModelHandler.cs" /> @@ -143,6 +145,7 @@ <Compile Include="Global.asax.cs"> <DependentUpon>Global.asax</DependentUpon> </Compile> + <Compile Include="Models\Grid.cs" /> <Compile Include="Models\Model.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> diff --git a/JiangSu/Models/Grid.cs b/JiangSu/Models/Grid.cs new file mode 100644 index 0000000..d5a8824 --- /dev/null +++ b/JiangSu/Models/Grid.cs @@ -0,0 +1,16 @@ +锘縰sing System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace JiangSu.Models +{ + public class Grid + { + public Grid() { } + + public long id { set; get; } + + public string json { set; get; } + } +} diff --git a/JiangSu/cs/GridDAL.cs b/JiangSu/cs/GridDAL.cs new file mode 100644 index 0000000..c28ec92 --- /dev/null +++ b/JiangSu/cs/GridDAL.cs @@ -0,0 +1,71 @@ +锘縰sing JiangSu.Models; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.SQLite; +using System.Linq; +using System.Web; + +namespace JiangSu.cs +{ + public class GridDAL + { + public static List<Grid> SelectAll() + { + string sql = "select * from grid"; + + DataTable dt = SQLiteHelper.GetDataTable(sql); + if (null == dt || dt.Rows.Count == 0) + { + return null; + } + + List<Grid> list = ModelHandler.FillModel<Grid>(dt); + + return list; + } + + public static Grid SelectById(long id) + { + string sql = "select * from grid where id = @id"; + + SQLiteParameter param = new SQLiteParameter("@id"); + param.Value = id; + + DataTable dt = SQLiteHelper.GetDataTable(sql, param); + if (null == dt || dt.Rows.Count == 0) + { + return null; + } + + List<Grid> list = ModelHandler.FillModel<Grid>(dt); + + return null == list || list.Count == 0 ? null : list[0]; + } + + public static int DeleteByIds(List<int> ids) + { + string str = string.Join(",", ids.ToArray()); + string sql = string.Format("delete from grid where id in ({0})", str); + + return SQLiteHelper.ExecuteNonQuery(sql); + } + + public static int Insert(Grid grid) + { + string sql = "insert into grid (json) values (@json);select last_insert_rowid();"; + SQLiteParameter[] sqlParams = ModelDAL.GetParams<Grid>(sql, grid); + + object obj = SQLiteHelper.ExecuteScalar(sql, sqlParams); + return null == obj ? 0 : Convert.ToInt32(obj); + } + + public static int UpdateById(Grid grid) + { + string sql = "update grid set json = @json where id = @id"; + SQLiteParameter[] sqlParams = ModelDAL.GetParams<Grid>(sql, grid); + + return SQLiteHelper.ExecuteNonQuery(sql, sqlParams); + } + } +} diff --git a/JiangSu/index.html b/JiangSu/index.html index 775e60f..f353162 100644 --- a/JiangSu/index.html +++ b/JiangSu/index.html @@ -42,6 +42,33 @@ return JSON.stringify(model); } + function InsertGrid() { + var data = getGrid(); + + ajax("Grid/Insert", "POST", data, null, null, function (rs) { + console.log(rs); + alert(rs); + }); + } + + function UpdateGridById() { + var data = getGrid(); + + ajax("Grid/UpdateById", "POST", data, null, null, function (rs) { + console.log(rs); + alert(rs); + }); + } + + function getGrid() { + var model = { + id: $("#gid").val(), + json: $("#gjson").val() + }; + + return JSON.stringify(model); + } + function ajax(url, type, data, dataType, contentType, fn) { $.ajax({ url: url, @@ -60,13 +87,13 @@ </script> </head> <body> - <a href="http://localhost/JiangSu/Values/Get">Test</a> <br /> + <a href="http://localhost/JiangSu/Values/Get">Test</a> <br /> <br /> <a href="http://localhost/JiangSu/Op/SelectByPage?pageSize=10&pageIndex=1&name=">SelectByPage?pageSize=10&pageIndex=1&name=</a> <br /> <a href="http://localhost/JiangSu/Op/SelectById?id=1">SelectById?id=1</a> <br /> - <a href="http://localhost/JiangSu/Op/DeleteByIds?ids=13&ids=14">DeleteByIds?ids=13&ids=14</a> <br /> + <a href="http://localhost/JiangSu/Op/DeleteByIds?ids=47&ids=14">DeleteByIds?ids=47&ids=14</a> <br /> <br /> <form> id: <input id="id" name="id" value="3" /> <br /> @@ -74,7 +101,16 @@ json: <input id="json" name="json" value="{}" /> <br /> <input type="button" value="Insert" onclick="Insert();" /> - <input type="button" value="UpdateById" onclick="UpdateById();" /><br /> + <input type="button" value="UpdateById" onclick="UpdateById();" /><br /> <br /> + + 缃戞牸锛�<a href="http://localhost/JiangSu/Grid/SelectAll">SelectAll</a> + <a href="http://localhost/JiangSu/Grid/SelectById?id=1">SelectById?id=1</a> + <a href="http://localhost/JiangSu/Grid/DeleteByIds?ids=12&ids=13">DeleteByIds?ids=12&ids=13</a> <br /> + + id: <input id="gid" value="12" /> <br /> + json: <textarea id="gjson" style="width: 1024px;height: 70px;">{ "type": "Feature", "properties": { "ID": "12", "NAME": "Area8", "REMARK": "" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 120.24179180542139, 32.005615085853492, 13.770 ], [ 120.24175240823769, 32.005610442630996, 13.770 ], [ 120.24173428512847, 32.005722106674966, 13.770 ], [ 120.24177368235908, 32.005726749902777, 13.770 ], [ 120.24179180542139, 32.005615085853492, 13.770 ] ] ] } }</textarea> <br /> + <input type="button" value="InsertGrid" onclick="InsertGrid();" /> + <input type="button" value="UpdateGridById" onclick="UpdateGridById();" /><br /> <br /> </form> </body> </html> -- Gitblit v1.9.3