using System; using System.Collections.Generic; using GpsModels; using log4net; using MongoDB.Driver; namespace Toogps.Mongo.Repository { public class GpsRealTimeRepository { /// /// MongoDB中Gps实时分析数据表的表名(MongoDB不区分生产与测试服务器),GpsRealTime为生产表名,GpsRealTime_Test为测试表名 /// public static string GpsRealTimeTableName = System.Configuration.ConfigurationManager.AppSettings["GpsRealTimeTableName"]; /// /// MongoDB中Acc/作业持续时长数据表的表名(MongoDB不区分生产与测试服务器),GpsCarStatusDuration为生产表名,GpsCarStatusDuration_Test为测试表名 /// public static string GpsCarStatusDurationTableName = System.Configuration.ConfigurationManager.AppSettings["GpsCarStatusDurationTableName"]; public static ILog Log = LogManager.GetLogger(""); /// /// 获取离线并且开启了Acc或者Work的车辆 /// /// public static List GetOffLineGpsRealTime() { try { var datetime = DateTime.Now.AddMinutes(-15); var filter = Builders.Filter.Lte("GpsTime", datetime); var list = RepositoryContext.Server.Find(filter, GpsRealTimeTableName); return list.Count > 0 ? list : new List(); } catch (Exception ex) { return new List(); } } /// /// 更新Gps实时数据 /// /// /// public static bool UpdateGpsRealTime(GpsModel gpsModel) { try { var companyId = gpsModel.CompanyId; var filter = Builders.Filter.Eq("CompanyId", companyId) & Builders.Filter.Eq("VehicleId", gpsModel.VehicleId); var update = Builders.Update .Set(s => s.GpsTime, gpsModel.GpsTime) .Set(s => s.RecTime, gpsModel.RecTime) .Set(s => s.Longitude, gpsModel.Longitude) .Set(s => s.Latitude, gpsModel.Latitude) .Set(s => s.Address, gpsModel.Address) .Set(s => s.Direct, gpsModel.Direct) .Set(s => s.Speed, gpsModel.Speed) .Set(s => s.Acc, gpsModel.Acc) .Set(s => s.GpsAlert, gpsModel.GpsAlert) .Set(s => s.Work, gpsModel.Work) .Set(s => s.Useing, gpsModel.Useing) .Set(s => s.Address, gpsModel.Address ?? "") .Set(s => s.ModifiedTime, DateTime.Now) .Set(s => s.GpsMode, gpsModel.GpsMode) .Set(s => s.SimNo, gpsModel.SimNo); var isSuccess = RepositoryContext.Server.UpdateOne(filter, update, GpsRealTimeTableName); if (!isSuccess) Log.InfoFormat( "UpdateGpsRealTime失败:VehicleId={0},GpsTime={1},RecTime={2},Longitude={3},Latitude={4},Direct={5},Speed={6},Acc={7},ModifiedTime={8},{9}", gpsModel.VehicleId, gpsModel.GpsTime, gpsModel.RecTime, gpsModel.Longitude, gpsModel.Latitude, gpsModel.Direct, gpsModel.Speed, gpsModel.Acc, DateTime.Now, gpsModel.CompanyId); return isSuccess; } catch (Exception ex) { Log.InfoFormat(ex.StackTrace + ex.Message); } return true; } /// /// 更新车辆gps状态: /// /// /// /// /// /// /// public static bool UpdateGpsRealTimeGpsState(VehicleModel vehicle, int gpsState, string durationTime, string AccDurationTime, string workDurationTime) { // var companyId = vehicle.CompanyId ; var filter = Builders.Filter.Eq("CompanyId", vehicle.CompanyId) & Builders.Filter.Eq("VehicleId", vehicle.Id); var update = Builders.Update .Set(s => s.GpsState, gpsState) .Set(s => s.DurationTime, durationTime) .Set(s => s.AccDurationTime, AccDurationTime) .Set(s => s.ModifiedTime, DateTime.Now) .Set(s => s.WorkDurationTime, workDurationTime); var isSuccess = RepositoryContext.Server.UpdateOne(filter, update, GpsRealTimeTableName); if (!isSuccess) Log.InfoFormat( "UpdateGpsRealTimeGpsState失败:VehicleId={0},GpsState={1},DurationTime={2},AccDurationTime={3}", vehicle.Id, gpsState, durationTime, AccDurationTime); return isSuccess; } /// /// 更新车辆gps状态,使用场景,1、沃瑞特在线车辆心跳时进行更新,2、车辆未定位时数据更新。 /// /// /// public static bool UpdateGpsRealTimeGpsStateWrt(GpsModel gpsModel) { var now = DateTime.Now; var companyId = gpsModel.CompanyId ; var filter = Builders.Filter.Eq("CompanyId", companyId) & Builders.Filter.Eq("VehicleId", gpsModel.VehicleId); var update = Builders.Update .Set(s => s.GpsState, gpsModel.GpsState) .Set(s => s.DurationTime, gpsModel.DurationTime ?? "") .Set(s => s.AccDurationTime, gpsModel.AccDurationTime ?? "") .Set(s => s.WorkDurationTime, gpsModel.WorkDurationTime ?? "") .Set(s => s.Address, gpsModel.Address) .Set(s => s.GpsMode, gpsModel.GpsMode) .Set(s => s.ModifiedTime, now); if (gpsModel.Acc != -1) //车辆未定位时更新Acc状态 update = update.Set(s => s.Acc, gpsModel.Acc); if (gpsModel.Work != -1) //车辆未定位时更新作业状态 update = update.Set(s => s.Work, gpsModel.Work); if (gpsModel.GpsTime.Year > 2000) update = update.Set(s => s.GpsTime, now).Set(s => s.RecTime, now); var isSuccess = RepositoryContext.Server.UpdateOne(filter, update, GpsRealTimeTableName); if (!isSuccess) Log.InfoFormat( "UpdateGpsRealTimeGpsStateWrt失败:VehicleId={0},GpsState={1},GpsTime、RecTime、ModifiedTime={2:yyyy-MM-dd HH:mm:ss},DurationTime={3},AccDurationTime={4}", gpsModel.VehicleId, gpsModel.GpsState, now, gpsModel.DurationTime, gpsModel.AccDurationTime); return isSuccess; } /// /// 新增Acc持续时长记录 /// public static bool AddGpsCarStatusDuration(GpsCarStatusDurationModel model) { try { return RepositoryContext.Server.InsertOne(model, GpsCarStatusDurationTableName); } catch (Exception ex) { Log.ErrorFormat( "AddGpsCarStatusDuration=报错=,ex.Message={0},ex.InnerException={1},ex.StackTrace={2}", ex.Message, ex.InnerException, ex.StackTrace); return false; } } /// /// 新增Acc持续时长记录 /// public static bool AddGpsTime(GpsRealTimeModel model) { try { return RepositoryContext.Server.InsertOne(model, GpsRealTimeTableName); } catch (Exception ex) { Log.ErrorFormat( "AddGpsTime=报错=,ex.Message={0},ex.InnerException={1},ex.StackTrace={2}", ex.Message, ex.InnerException, ex.StackTrace); return false; } } } }