Skip to content

Commit

Permalink
修改Mongo Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
1764564459 committed Apr 15, 2019
1 parent a98340e commit a7f856a
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 29 deletions.
2 changes: 2 additions & 0 deletions EntityFrameWork.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Reference Include="MongoDB.Driver.Core, Version=2.8.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\MongoDB.Driver.Core.2.8.0\lib\net452\MongoDB.Driver.Core.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
Expand Down Expand Up @@ -97,6 +98,7 @@
<Compile Include="Entity\Auth\AppUserClaim.cs" />
<Compile Include="Entity\Auth\AppUserLogin.cs" />
<Compile Include="Entity\Auth\AppUserRole.cs" />
<Compile Include="Helper\ExcelAndWordHelper.cs" />
<Compile Include="Helper\GPS_To_Baidu.cs" />
<Compile Include="Helper\MongoDbHelper.cs" />
<Compile Include="Helper\SQLiteHelper.cs" />
Expand Down
13 changes: 13 additions & 0 deletions Helper/ExcelAndWordHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EntityFrameWork.Server.Helper
{
public class ExcelAndWordHelper
{

}
}
30 changes: 30 additions & 0 deletions Helper/GPS_To_Baidu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public static double[] wgs2bd(double lat, double lon)
return dgcj2bd;
}

/// <summary>
/// 火星坐标转百度坐标
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
public static double[] gcj2bd(double lat, double lon)
{
double x = lon, y = lat;
Expand All @@ -115,6 +121,12 @@ public static double[] gcj2bd(double lat, double lon)
return new double[] { bd_lat, bd_lon };
}

/// <summary>
/// 百度坐标转火星坐标
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
public static double[] bd2gcj(double lat, double lon)
{
double x = lon - 0.0065, y = lat - 0.006;
Expand All @@ -125,6 +137,12 @@ public static double[] bd2gcj(double lat, double lon)
return new double[] { gg_lat, gg_lon };
}

/// <summary>
/// GPS 转 火星坐标
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
public static double[] wgs2gcj(double lat, double lon)
{
double dLat = transformLat(lon - 105.0, lat - 35.0);
Expand All @@ -141,6 +159,12 @@ public static double[] wgs2gcj(double lat, double lon)
return loc;
}

/// <summary>
/// 转纬度
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
private static double transformLat(double lat, double lon)
{
double ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon + 0.2 * Math.Sqrt(Math.Abs(lat));
Expand All @@ -150,6 +174,12 @@ private static double transformLat(double lat, double lon)
return ret;
}

/// <summary>
/// 转精度
/// </summary>
/// <param name="lat"></param>
/// <param name="lon"></param>
/// <returns></returns>
private static double transformLon(double lat, double lon)
{
double ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * Math.Sqrt(Math.Abs(lat));
Expand Down
86 changes: 60 additions & 26 deletions Helper/MongoDbHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using MongoDB.Bson;
using EntityFrameWork.Server.Entity.Auth;
using MongoDB.Bson;
using MongoDB.Driver;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -12,10 +14,10 @@ namespace EntityFrameWork.Server.Helper
public class MongoDbHelper//<T> where T : class
{
private IMongoDatabase db = null;



private string data_base = "Vehicle";
private string data_collect = "Real";
private string data_base = "MongoApp";
private string data_collect = "AppUser";
private string data_address = "mongodb://localhost:27017";
private static readonly object lockHelper = new object();
MongoClient client;
Expand All @@ -41,8 +43,8 @@ public MongoDbHelper()
/// <returns></returns>
public async Task<T> Insert<T>(T entity)
{
var flag = ObjectId.GenerateNewId();
entity.GetType().GetProperty("Name").GetValue(entity);//获取Name值
//var flag = ObjectId.GenerateNewId();
//entity.GetType().GetProperty("Name").GetValue(entity);//获取Name值
await db.GetCollection<T>(typeof(T).Name).InsertOneAsync(entity);
//await collection.InsertOneAsync(entity);
return entity;
Expand All @@ -53,19 +55,19 @@ public async Task<T> Insert<T>(T entity)
/// <param name="id"></param>
/// <param name="field"></param>
/// <param name="value"></param>
public void Modify<T>(string id, string field, string value)
public async Task Modify<T>(string id, string field, string value)
{
var filter = Builders<T>.Filter.Eq("Id", ObjectId.Parse(id));
var updated = Builders<T>.Update.Set(field, value);
//UpdateResult result = collection.UpdateOneAsync(filter, updated).Result;
UpdateResult result = db.GetCollection<T>(typeof(T).Name).UpdateOneAsync(filter, updated).Result;
//result.
UpdateResult result = await db.GetCollection<T>(typeof(T).Name).UpdateOneAsync(filter, updated);
}

/// <summary>
/// 更新
/// </summary>
/// <param name="entity"></param>
public void Update<T>(T entity)
public async Task Update<T>(T entity)
{
try
{
Expand Down Expand Up @@ -93,12 +95,12 @@ public void Update<T>(T entity)
//old.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

filter = Builders<T>.Filter.Eq("Id", id);//entity.Id);
ReplaceOneResult result = db.GetCollection<T>(typeof(T).Name).ReplaceOneAsync(filter, old).Result; //collection.ReplaceOneAsync(filter, old).Result;
ReplaceOneResult result = await db.GetCollection<T>(typeof(T).Name).ReplaceOneAsync(filter, old); //collection.ReplaceOneAsync(filter, old).Result;
}
catch (Exception ex)
{
var aaa = ex.Message + ex.StackTrace;
throw;
//var aaa = ex.Message + ex.StackTrace;
throw ex;
}
}
/// <summary>
Expand All @@ -107,8 +109,8 @@ public void Update<T>(T entity)
/// <param name="entity"></param>
public void Delete<T>(T entity)
{
var id_val = entity.GetType().GetProperties().Where(p => p.Name.ToLower().Equals("_id")).FirstOrDefault().GetValue(entity);
var filter = Builders<T>.Filter.Eq("Id", id_val);
var _Id = entity.GetType().GetProperties().Where(p => p.Name.ToLower().Equals("_id")).FirstOrDefault().GetValue(entity);
var filter = Builders<T>.Filter.Eq("Id", _Id);
db.GetCollection<T>(typeof(T).Name).DeleteOneAsync(filter);
}
/// <summary>
Expand All @@ -118,18 +120,40 @@ public void Delete<T>(T entity)
/// <returns></returns>
public T QueryOne<T>(string id)
{
var filter = Builders<T>.Filter.Eq("", "");
var filter = Builders<T>.Filter.Eq("Id",ObjectId.Parse(id));
return db.GetCollection<T>(typeof(T).Name).Find(filter).FirstOrDefault();
//return collection.Find(a => a.Id == ObjectId.Parse(id)).ToList().FirstOrDefault();
}
/// <summary>
/// 查询所有数据
/// </summary>
/// <returns></returns>
public List<T> QueryAll<T>()
public IEnumerable<T> QueryAll<T>()
{
var filter = Builders<T>.Filter.Empty;//.Ne("Name", "");
return db.GetCollection<T>(typeof(T).Name).Find(filter).ToEnumerable();
}

/// <summary>
/// 分页查询
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="page_index">页码</param>
/// <param name="page_size">每页数量</param>
/// <param name="express">条件</param>
/// <param name="sort_field">排序字段</param>
/// <param name="asc_desc">升序降序</param>
/// <returns></returns>
public IEnumerable<T> GetList<T>(int page_index,int page_size, Expression<Func<T, bool>> express, Expression<Func<T, dynamic>> sort_field,string asc_desc)
{
var filter = Builders<T>.Filter.Ne("Name", "");
return db.GetCollection<T>(typeof(T).Name).Find(filter).ToList();
var filter = Builders<T>.Filter.And(express);
//条件查询所有
var entity = db.GetCollection<T>(typeof(T).Name).Find(filter).Skip(page_index-1).Limit(page_size);
if (asc_desc.ToLower().Contains("asc"))
entity = entity.SortBy(sort_field);
else
entity = entity.SortByDescending(sort_field);
return entity.ToEnumerable();
}
/// <summary>
/// 根据条件查询一条数据
Expand All @@ -138,33 +162,43 @@ public List<T> QueryAll<T>()
/// <returns></returns>
public T QueryByFirst<T>(Expression<Func<T, bool>> express)
{
return db.GetCollection<T>(typeof(T).Name).Find(express).ToList().FirstOrDefault();
return db.GetCollection<T>(typeof(T).Name).Find(express).FirstOrDefault();
}
/// <summary>
/// 批量添加
/// </summary>
/// <param name="list"></param>
public void InsertBatch<T>(List<T> list)
public async Task InsertBatch<T>(IEnumerable<T> list)
{
db.GetCollection<T>(typeof(T).Name).InsertManyAsync(list);
await db.GetCollection<T>(typeof(T).Name).InsertManyAsync(list);
}
/// <summary>
/// 根据Id批量删除
/// </summary>
public void DeleteBatch<T>(List<ObjectId> list)
public async Task DeleteBatch<T>(IEnumerable<ObjectId> list)
{
var filter = Builders<T>.Filter.In("Id", list);
db.GetCollection<T>(typeof(T).Name).DeleteManyAsync(filter);
await db.GetCollection<T>(typeof(T).Name).DeleteManyAsync(filter);
}

/// <summary>
/// 未添加到索引的数据
/// </summary>
/// <returns></returns>
public List<T> QueryToLucene<T>()
public IEnumerable<T> QueryToLucene<T>()
{
//return new List<T>();
return db.GetCollection<T>(typeof(T).Name).Find(null).ToList();
return db.GetCollection<T>(typeof(T).Name).Find(null).ToEnumerable();
}
}
[Serializable]
public class MyApp
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime? Time { get; set; }
[JsonIgnore]
public IEnumerable<AppUser> users { get; set; }
}
}
2 changes: 1 addition & 1 deletion OAuthServer/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
}
var sigin = new SignInManager<AppUser, Guid>(UserManage, HttpContext.GetOwinContext().Authentication);
var Result = await sigin.PasswordSignInAsync(users.UserName, model.Password, false, false);
return View(model);
return Redirect(Url.Action("Index", "Home"));
}
//
// GET: /Account/Register
Expand Down
30 changes: 28 additions & 2 deletions OAuthServer/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
using EntityFrameWork.Server.Entity.Auth;
using EntityFrameWork.Server.Helper;
using EntityFrameWork.Server.UnitOfWork.complex;
using Newtonsoft.Json;
using OAuthServer.App_;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace OAuthServer.Controllers
{

public class HomeController : BaseController
{
public ActionResult Index()
public async Task<ActionResult> Index()
{
return View();
MongoDbHelper mongo = new MongoDbHelper();
Random rand = new Random(10);
int rand_num= rand.Next(0, 1000);
//List< MyApp> user = new List<MyApp>()
// {
// new MyApp (){
// Id = Guid.NewGuid(),
// Name = rand_num.ToString() ,
// Age = 12,
// Time = DateTime.UtcNow
// }
// };
// var result = await mongo.Insert(user[0]);
//var result = mongo.GetList<MyApp>(1, 10, p => p.Time > DateTime.Parse("2019-4-13 15:00:00") && p.Time < DateTime.Parse("2019-4-13 15:27:00"), p => p.Time, "asc");
//string json_data = JsonConvert.SerializeObject(result);
//JsonConvert.PopulateObject(json_data, user);
//user.ForEach(item =>
//{
// item.Time =item.Time.ToString();
//});
//var result= mongo.QueryOne<MyApp>();
//mongo.Delete<MyApp>(result);
return View(); // Json(result,JsonRequestBehavior.AllowGet);
}

public ActionResult About()
Expand Down

0 comments on commit a7f856a

Please sign in to comment.