protected override void ChangeDatabase(IServiceProvider serviceProvider, string tableName) { using (DensityContext context = serviceProvider.GetRequiredService <DensityContext>()) { try { context.ChangeDatabase(tableName); _logger.LogInformation((int)LogEvent.分支切换, $"切换密度数据表成功 {tableName}"); } catch (MySqlException ex) { _logger.LogError((int)LogEvent.分支切换, ex, "切换密度数据表失败"); } } }
public static Dictionary <TrafficRegion, int> CreateData(IServiceProvider serviceProvider, List <DensityDevice> devices, List <DataCreateMode> modes, List <DateTime> startTimes, List <DateTime> endTimes, bool initDatabase = false) { if (initDatabase) { ResetDatabase(serviceProvider); } Dictionary <TrafficRegion, int> regions = new Dictionary <TrafficRegion, int>(); for (int i = 0; i < startTimes.Count; ++i) { DateTime minTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, startTimes[i]); DateTime maxTime = TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, minTime); DensityBranchBlock branch = new DensityBranchBlock(serviceProvider); branch.Open(devices, minTime, maxTime); Random random = new Random(); foreach (DensityDevice device in devices) { foreach (var relation in device.DensityDevice_DensityChannels) { foreach (TrafficRegion region in relation.Channel.Regions) { int randomValue = random.Next(1, 1000); if (modes[i] == DataCreateMode.Random) { regions[region] = randomValue; } int value = 1; for (int m = 0; m < 1440; ++m) { DateTime dataTime = startTimes[i].AddMinutes(m); if (dataTime > DateTime.Now) { break; } if (dataTime >= startTimes[i] && dataTime < endTimes[i]) { TrafficDensity density; if (modes[i] == DataCreateMode.Fixed) { density = new TrafficDensity { MatchId = $"{device.Ip}_{relation.Channel.ChannelIndex}_{region.RegionIndex}", DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0), Value = 1 }; } else if (modes[i] == DataCreateMode.Sequence) { density = new TrafficDensity { MatchId = $"{device.Ip}_{relation.Channel.ChannelIndex}_{region.RegionIndex}", DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0), Value = value }; } else { density = new TrafficDensity { MatchId = $"{device.Ip}_{relation.Channel.ChannelIndex}_{region.RegionIndex}", DateTime = new DateTime(dataTime.Year, dataTime.Month, dataTime.Day, dataTime.Hour, dataTime.Minute, 0), Value = randomValue }; } branch.Post(density); } ++value; } } } } branch.Close(); if (i == startTimes.Count - 1) { DateTime currentTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel); if (minTime != currentTime) { using (DensityContext context = serviceProvider.GetRequiredService <DensityContext>()) { context.ChangeDatabase(BranchDbConvert.GetTableName(minTime)); } branch.SwitchBranch(maxTime, TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, maxTime)); } } else { DateTime nextItem = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, startTimes[i + 1]); if (minTime != nextItem) { using (DensityContext context = serviceProvider.GetRequiredService <DensityContext>()) { context.ChangeDatabase(BranchDbConvert.GetTableName(minTime)); } branch.SwitchBranch(maxTime, TimePointConvert.NextTimePoint(BranchDbConvert.DateLevel, maxTime)); } } } return(regions); }
/// <summary> /// 初始化数据库 /// </summary> private async void InitDb() { _logger.LogInformation((int)LogEvent.系统, "初始化数据库"); DateTime minTime = TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel); using (IServiceScope serviceScope = _serviceProvider.CreateScope()) { using (DensityContext context = serviceScope.ServiceProvider.GetRequiredService <DensityContext>()) { if (context.Database.EnsureCreated()) { #region 用户 _logger.LogInformation((int)LogEvent.系统, "创建管理员用户"); UserManager <IdentityUser> userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <IdentityUser> >(); await userManager.CreateAsync(new IdentityUser("admin"), "123456"); #endregion #region 权限 _logger.LogInformation((int)LogEvent.系统, "创建权限"); IdentityUser adminUser = await userManager.FindByNameAsync("admin"); List <YumekoJabami.Models.Claim> claims = new List <YumekoJabami.Models.Claim> { new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03000000", Descirption = "智慧高点视频检测系统" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010000", Descirption = "设备管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010100", Descirption = "设备信息维护" }, //new YumekoJabami.Models.Claim{ Type = ClaimTypes.Webpage, Value = "03010200", Descirption = "设备位置维护"}, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010300", Descirption = "国标网关设置" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010400", Descirption = "设备运行状态" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03010500", Descirption = "视频信息维护" }, //new YumekoJabami.Models.Claim{ Type = ClaimTypes.Webpage, Value = "03010600", Descirption = "视频位置维护"}, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020000", Descirption = "数据分析" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020100", Descirption = "交通密度查询" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020200", Descirption = "交通密度分析" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020300", Descirption = "拥堵事件统计" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020400", Descirption = "拥堵事件排名" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03020500", Descirption = "拥堵高发时段" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030000", Descirption = "系统设置" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030100", Descirption = "路口维护" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030300", Descirption = "用户管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030400", Descirption = "角色管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030600", Descirption = "字典管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030700", Descirption = "参数管理" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030800", Descirption = "日志查询" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03030900", Descirption = "系统监控" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03040000", Descirption = "状况监测" }, new YumekoJabami.Models.Claim { Type = ClaimTypes.Webpage, Value = "03040100", Descirption = "应用检测" }, }; context.TrafficClaims.AddRange(claims); foreach (YumekoJabami.Models.Claim claim in claims) { await userManager.AddClaimAsync(adminUser, new Claim(claim.Type, claim.Value)); } #endregion #region 字典 _logger.LogInformation((int)LogEvent.系统, "创建字典"); List <Code> densityCodes = new List <Code> { new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.FiveMinutes, Description = "五分钟密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.FifteenMinutes, Description = "十五分钟密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.Hour, Description = "一小时密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.Day, Description = "一天密度" }, new Code { Key = "DensityDateLevel", Value = (int)DateTimeLevel.Month, Description = "一月密度" } }; densityCodes.AddRange(Enum.GetValues(typeof(ChannelType)) .Cast <ChannelType>() .Select(e => new Code { Key = typeof(ChannelType).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(DeviceModel)) .Cast <DeviceModel>() .Select(e => new Code { Key = typeof(DeviceModel).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(DeviceStatus)) .Cast <DeviceStatus>() .Select(e => new Code { Key = typeof(DeviceStatus).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(RtspProtocol)) .Cast <RtspProtocol>() .Select(e => new Code { Key = typeof(RtspProtocol).Name, Value = (int)e, Description = e.ToString() })); densityCodes.AddRange(Enum.GetValues(typeof(LogEvent)) .Cast <LogEvent>() .Select(e => new Code { Key = typeof(LogEvent).Name, Value = (int)e, Description = e.ToString() })); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Debug, Description = "调试" }); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Information, Description = "消息" }); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Warning, Description = "警告" }); densityCodes.Add(new Code { Key = "LogLevel", Value = (int)LogLevel.Error, Description = "错误" }); context.Codes.AddRange(densityCodes); #endregion context.Version.Add(new TrafficVersion { Version = Assembly.GetExecutingAssembly().GetName().Version.ToString() }); context.SaveChanges(); } else { context.SaveChanges(); try { TrafficDensity_One density = context.Densities_One.OrderByDescending(d => d.Id).FirstOrDefault(); if (density != null && TimePointConvert.CurrentTimePoint(BranchDbConvert.DateLevel, density.DateTime) != minTime) { context.ChangeDatabase(BranchDbConvert.GetTableName(density.DateTime)); } } catch (MySqlException) { } } } } }