/// <summary> /// 开始模拟数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, EventArgs e) { int interval = 30; if (string.IsNullOrWhiteSpace(txtInterval.Text.Trim())) { PrintError("请输入数据生成时间间隔!"); return; } else { if (!int.TryParse(txtInterval.Text.Trim(), out interval)) { PrintError("时间间隔数字格式错误!"); return; } else if (interval < 3) { PrintError("时间间隔最少3秒!"); return; } } List <ShowEntity> list = new List <ShowEntity>(); for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value)) { var entity = dataGridView1.Rows[i].DataBoundItem as ShowEntity; list.Add(entity); } } if (list.Count <= 0) { PrintError("请至少选择一条数据进行生成!"); return; } isInsertData = true; taskSimpleScheduler.StartNewTask("生成实时数据", () => { if (isInsertData) { foreach (var item in list) { if (string.IsNullOrWhiteSpace(item.ROUTEPOINTS)) { this.rTxtOutputer.Output(string.Format("车号:{0},运输记录ID:{1}对应的发车未设置线路!", item.CARNUMBER, item.ID), eOutputType.Error); continue; } string[] lnglats = item.ROUTEPOINTS.Trim('|').Split('|'); DataTable dt = Dbers.GetInstance().SelfDber.ExecuteDataTable("select count(1) from CMCSTBLONGITUDEANDLATITUDE t where t.transportrecordid='" + item.ID + "'"); int index = 0; if (dt != null && dt.Rows.Count > 0) { index = int.Parse(dt.Rows[0][0].ToString()); } if (index >= lnglats.Length) { continue; } string insertSql = string.Format(@"insert into CMCSTBLONGITUDEANDLATITUDE (ID, CREATIONTIME, CREATORUSERID, ISDELETED, TRANSPORTRECORDID, LONGITUDE, LATITUDE) values ('{0}', to_date('{1}','yyyy-mm-dd hh24:mi:ss'), 1, 0, '{2}', {3}, {4})", Guid.NewGuid().ToString(), item.DEPARTTIME.AddSeconds(index * 10).ToString("yyyy-MM-dd HH:mm:ss"), item.ID, lnglats[index].Split(',')[0], lnglats[index].Split(',')[1]); if (Dbers.GetInstance().SelfDber.Execute(insertSql) > 0) { this.rTxtOutputer.Output(string.Format("车号:{0}插入历史记录!", item.CARNUMBER)); string updateSql = string.Format(@" update cmcstboutnettransport a set a.longitude='{0}', a.latitude='{1}',a.locationtime=to_date('{2}','yyyy-mm-dd hh24:mi:ss') where a.id='{3}'", lnglats[index].Split(',')[0], lnglats[index].Split(',')[1], DateTime.Now, item.ID); if (Dbers.GetInstance().SelfDber.Execute(updateSql) > 0) { this.rTxtOutputer.Output(string.Format("车号:{0}更新实时经纬度!", item.CARNUMBER)); //根据经纬度查询位置 string result = GaodeHelper.GetLocation(lnglats[index]); if (!String.IsNullOrWhiteSpace(result)) { updateSql = string.Format(@" update cmcstboutnettransport a set a.currentlocation='{0}' where a.id='{1}'", result, item.ID); Dbers.GetInstance().SelfDber.Execute(updateSql); this.rTxtOutputer.Output(string.Format("车号:{0}根据接口查询更新实时位置!", item.CARNUMBER)); } } } } } }, interval * 1000, OutputError); }
/// <summary> /// 生成路线偏离数据 /// </summary> /// <param name="output"></param> /// <returns></returns> public void SaveToCarDeciationRoute(Action <string, eOutputType> output) { DeviateWarning warning = SelfDber.Entity <DeviateWarning>("where IsDeleted=0"); if (warning == null) { output("未设置路线偏离预警距离!", eOutputType.Error); return; } if (warning.Distance <= 0) { output("设置路线偏离预警距离小于等于0,请正确设置!", eOutputType.Error); return; } //查询所有在途的车辆,然后根据运输记录中的实时位置去判断路线的偏离 List <ShowEntity> list = GetInRouteCars(); if (list == null || list.Count <= 0) { output("暂无在途车辆!", eOutputType.Warn); return; } foreach (var item in list) { if (string.IsNullOrWhiteSpace(item.ROUTEPOINTS)) { output(string.Format("车号:{0},运输记录ID:{1}对应的发车未设置线路!", item.CARNUMBER, item.ID), eOutputType.Error); continue; } //如果车辆已报修并且未处理就不管了偏不偏移了 DataTable dtIsRepair = SelfDber.ExecuteDataTable(String.Format("select count(1) from CMCSTBCARREPAIRINFO t where t.isdeleted=0 and t.repairstatus=0 and t.carid='{0}'", item.CarId)); if (dtIsRepair != null && dtIsRepair.Rows.Count > 0 && dtIsRepair.Rows[0][0].ToString() != "0")//车辆去修理了就不算路线偏移 { continue; } double sslng = 0; double sslat = 0; double.TryParse(item.LONGITUDE, out sslng); double.TryParse(item.LATITUDE, out sslat); if (sslng <= 0 || sslat <= 0) { output(string.Format("车号:{0},运输记录ID:{1}暂无实时位置!", item.CARNUMBER, item.ID), eOutputType.Error); continue; } string[] lnglats = item.ROUTEPOINTS.Trim('|').Split('|'); //循环点一个一个的计算 List <double> minDistance = new List <double>(); foreach (var lnglat in lnglats) { minDistance.Add(GaodeHelper.GetTwoPointDistance(sslat, sslng, double.Parse(lnglat.Split(',')[1]), double.Parse(lnglat.Split(',')[0]))); } //最近的距离偏离了就算偏离 decimal minDis = (decimal)Math.Round(minDistance.Min(), 2, MidpointRounding.AwayFromZero); DeviateErrorInfo entity = SelfDber.Entity <DeviateErrorInfo>(string.Format(" where TransportRecordId='{0}' and StartTime is not null and EndTime is null order by StartTime desc", item.ID)); if (minDis >= warning.Distance) { //偏离了 //首先判断存不存在有开始时间但是没有结束时间的数据,有就不管,没有则新增一条结束时间为空,开始时间为当前时间的数据,偏离距离取最小值 if (entity == null) { entity = new DeviateErrorInfo(); entity.TransportRecordId = item.ID; entity.Distance = minDis; entity.StartTime = DateTime.Now; entity.Remark = string.Format("货车:{0},在{1}偏离计划路线,偏离距离{2}米", item.CARNUMBER, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), entity.Distance); if (SelfDber.Insert(entity) > 0) { output(string.Format("车号:{0},运输记录ID:{1}发现路线偏离并记录,偏离距离:{2}!", item.CARNUMBER, item.ID, entity.Distance), eOutputType.Normal); string updateSql = string.Format("update cmcstbbuyfueltransport t set t.ISDEVIATEEERR=1 where t.id='{0}'", item.ID); SelfDber.Execute(updateSql); } } } else { //没有偏离 //首先判断存不存在有开始时间但是没有结束时间的数据,有就将结束时间改为当前时间,没有就不管 if (entity != null) { entity.Distance = entity.Distance > minDis ? entity.Distance : minDis; entity.EndTime = DateTime.Now; entity.Remark = string.Format("货车:{0},在{1}偏离计划路线,于{2}回归计划路线,最大偏离距离{3}米", item.CARNUMBER, entity.StartTime.ToString("yyyy-MM-dd HH:mm:ss"), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), entity.Distance); if (SelfDber.Update(entity) > 0) { output(string.Format("车号:{0},运输记录ID:{1}回归计划路线!", item.CARNUMBER, item.ID), eOutputType.Normal); string updateSql = string.Format("update cmcstbbuyfueltransport t set t.ISDEVIATEEERR=1 where t.id='{0}'", item.ID); SelfDber.Execute(updateSql); } } } } }
/// <summary> /// 生成车辆异常停留位置数据 /// </summary> /// <param name="output"></param> /// <returns></returns> public void SaveToCarStopTime(Action <string, eOutputType> output, AbnormalStayWarning warning) { //查询所有在途的车辆,然后判断该车辆最近的两次经纬度是否在同一点 List <ShowEntity> list = GetInRouteCars(); if (list == null || list.Count <= 0) { output("暂无在途车辆!", eOutputType.Warn); return; } foreach (var item in list) { item.isStop = false; //如果车辆已报修并且未处理就不管了 DataTable dtIsRepair = SelfDber.ExecuteDataTable(String.Format("select count(1) from CMCSTBCARREPAIRINFO t where t.isdeleted=0 and t.repairstatus=0 and t.carid='{0}'", item.CarId)); if (dtIsRepair != null && dtIsRepair.Rows.Count > 0 && dtIsRepair.Rows[0][0].ToString() != "0")//车辆去修理了就不管 { continue; } //List<CarLongLatHistory> historyList = SelfDber.Entities<CarLongLatHistory>(string.Format(" where TransportRecordId='{0}' and CreationTime>=to_date('{1}','yyyy-mm-dd hh24:mi:ss') order by CreationTime desc", item.ID, DateTime.Now.AddMinutes(-(double)warning.StopTime))); List <CarLongLatHistory> historyList = SelfDber.Entities <CarLongLatHistory>(string.Format(" where TransportRecordId='{0}' and CreationTime>=to_date('{1}','yyyy-mm-dd hh24:mi:ss') order by CreationTime desc", item.ID, DateTime.Parse("2020-04-10 11:05:58").AddMinutes(-(double)warning.StopTime))); if (historyList.Count <= 1) { continue; } historyList = historyList.OrderByDescending(a => a.CreationTime).ToList(); var distance = GaodeHelper.GetTwoPointDistance((double)historyList.First().Latitude, (double)historyList.First().Longitude, (double)historyList.Last().Latitude, (double)historyList.Last().Longitude); if (distance <= 5)//距离在5米内默认没移动 { item.StopMintes = (historyList.First().CreationTime - historyList.Last().CreationTime).TotalMinutes; item.LONGITUDE = historyList.First().Longitude.ToString(); item.LATITUDE = historyList.First().Latitude.ToString(); item.isStop = true; } } foreach (var item in list) { StopErrorInfo entity = SelfDber.Entity <StopErrorInfo>(string.Format(" where TransportRecordId='{0}' and StartTime is not null and EndTime is null order by StartTime desc", item.ID)); //同一批次其他的车没停,百分之几的车停了那停的车就算异常 bool isPercent = (list.Count(a => a.MineSendId == item.MineSendId && a.isStop) < (list.Count(a => a.MineSendId == item.MineSendId) * warning.StopNumber) / 100m); if (item.isStop && entity == null && isPercent) { entity = new StopErrorInfo(); entity.TransportRecordId = item.ID; entity.StopPoint = item.LONGITUDE + "," + item.LATITUDE; string place = GaodeHelper.GetLocation(entity.StopPoint); if (string.IsNullOrWhiteSpace(place)) { continue; } entity.StopPlace = place; entity.StopTime = decimal.Parse(item.StopMintes.ToString("F0")); entity.StopTime = entity.StopTime > warning.StopTime ? entity.StopTime : warning.StopTime; entity.StartTime = DateTime.Now; entity.StopNum = Math.Round((list.Count(a => a.MineSendId == item.MineSendId && a.isStop) / list.Count(a => a.MineSendId == item.MineSendId)) * 100m, 2, MidpointRounding.AwayFromZero); entity.Remark = string.Format("货车:{0},于{1}开始在{2}异常停留时间超过{3}分钟!", item.CARNUMBER, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), entity.StopPlace, entity.StopTime); if (SelfDber.Insert(entity) > 0) { output(entity.Remark, eOutputType.Normal); string updateSql = string.Format("update cmcstbbuyfueltransport t set t.isstoperr=1 where t.id='{0}'", item.ID); SelfDber.Execute(updateSql); } } else if (entity != null)//如果没有异常停留需要将异常停留信息结束 { entity.EndTime = DateTime.Now; entity.StopTime = decimal.Parse((entity.StartTime - entity.EndTime).TotalMinutes.ToString("F0")); entity.Remark = string.Format("货车:{0},于{1}至{2}在{3}异常停留{4}分钟!", item.CARNUMBER, entity.StartTime.ToString("yyyy-MM-dd HH:mm:ss"), entity.EndTime.ToString("yyyy-MM-dd HH:mm:ss"), entity.StopPlace, entity.StopTime); if (SelfDber.Update(entity) > 0) { output(entity.Remark, eOutputType.Normal); string updateSql = string.Format("update cmcstbbuyfueltransport t set t.isstoperr=1 where t.id='{0}'", item.ID); SelfDber.Execute(updateSql); } } } }