/// <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="entity"></param> /// <param name="thirdDber"></param> /// <param name="output"></param> private void DownLoadData(TableOrView entity, OracleDapperDber thirdDber, Action <string, eOutputType> output) { //同步集团数据 string sbSearch = "select "; foreach (var item in entity.PropertySetDetails) { sbSearch += string.Format("{0},", item.Source); } sbSearch = sbSearch.Trim(','); sbSearch += string.Format(" from {0} ", entity.Source); if (!string.IsNullOrWhiteSpace(entity.TimeIntervalProperty)) { int intervalValue = 7; string configValue = commonDAO.GetAppletConfigString("数据同步智仁接口", "获取集团数据时间间隔(天)"); if (!string.IsNullOrWhiteSpace(configValue)) { Int32.TryParse(configValue, out intervalValue); } DateTime startTime = DateTime.Now.AddDays(-intervalValue); sbSearch += string.Format(" where {0}>=to_date('{1}','yyyy-MM-dd hh24:mi:ss')", entity.TimeIntervalProperty, startTime.ToString("yyyy-MM-dd HH:mm:ss")); } DataTable dt = thirdDber.ExecuteDataTable(sbSearch); if (dt == null || dt.Rows.Count <= 0) { return; } Boolean ishavepk = entity.PropertySetDetails.Count(a => a.DesPrimaryKey != null && a.DesPrimaryKey.ToLower() == "true") > 0; foreach (DataRow item in dt.Rows) { //只有更新和新增操作 string strChaXun = string.Format("select * from {0} where 1=1 ", entity.Destination); if (ishavepk) { foreach (var pk in entity.PropertySetDetails.Where(a => a.DesPrimaryKey != null && a.DesPrimaryKey.ToLower() == "true")) { strChaXun += string.Format("and {0}='{1}' ", pk.Destination, item[pk.Source] == null ? "" : item[pk.Source].ToString()); } } else { foreach (var pk in entity.PropertySetDetails) { strChaXun += string.Format("and {0}='{1}' ", pk.Destination, item[pk.Source] == null ? "" : item[pk.Source].ToString()); } } DataTable dtHaveData = SelfDber.ExecuteDataTable(strChaXun); if (dtHaveData == null || dtHaveData.Rows.Count <= 0) { //新增 string insertSql = string.Format(@"insert into {0} (", entity.Destination); string names = "ID, CREATIONTIME, CREATORUSERID,LASTMODIFICATIONTIME,"; string values = string.Format("'{0}', sysdate,1,sysdate,", Guid.NewGuid().ToString()); if (entity.Description == "矿点同步") { string code = commonDAO.GetMineNewChildCode("000"); if (!string.IsNullOrEmpty(code)) { names += "Code,"; values += "'" + code + "',"; } names += "Sort,"; values += commonDAO.GetMineSort() + ","; } else if (entity.Description == "煤种同步") { string code = commonDAO.GetFuelKindNewChildCode("000"); if (!string.IsNullOrEmpty(code)) { names += "Code,"; values += "'" + code + "',"; } names += "Sort,"; values += commonDAO.GetFuelKindSort() + ","; } if (!string.IsNullOrWhiteSpace(entity.IsSoftDelete) && entity.IsSoftDelete.ToLower() == "true") { names += "ISDELETED,"; values += "0,"; } if (!string.IsNullOrWhiteSpace(entity.TreeParentId)) { names += "parentid,"; values += string.Format("'{0}',", entity.TreeParentId); } foreach (var detail in entity.PropertySetDetails) { names += string.Format("{0},", detail.Destination); values += string.Format("'{0}',", item[detail.Source] == null ? "" : item[detail.Source].ToString()); } if (!string.IsNullOrWhiteSpace(entity.IsHaveSyncTime) && entity.IsHaveSyncTime.ToLower() == "true") { names += "SYNCTIME,"; values += "sysdate,"; } insertSql += names.Trim(',') + ") values (" + values.Trim(',') + ")"; if (SelfDber.Execute(insertSql) > 0) { output(string.Format("接口取数【{0}】已同步,操作:新增", entity.Description), eOutputType.Normal); } } else { //更新 string updateSql = string.Format("update {0} set ", entity.Destination); foreach (var detail in entity.PropertySetDetails) { updateSql += string.Format("{0}='{1}',", detail.Destination, item[detail.Source] == null ? "" : item[detail.Source].ToString()); } if (!string.IsNullOrWhiteSpace(entity.IsHaveSyncTime) && entity.IsHaveSyncTime.ToLower() == "true") { updateSql += "SYNCTIME=sysdate,"; } updateSql = updateSql.Trim(',') + string.Format(" where id='{0}'", dtHaveData.Rows[0]["ID"].ToString()); if (SelfDber.Execute(updateSql) > 0) { output(string.Format("接口取数【{0}】已同步,操作:更新", entity.Description), eOutputType.Normal); } } } }
/// <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); } } } }