tycg_carviolation_BE/Traffic.Service/Implements/ReportService.cs

456 lines
21 KiB
C#
Raw Permalink Normal View History

using Omu.ValueInjecter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traffic.Data.Enums;
using Traffic.Data.ViewModels;
using Traffic.Repository.Infrastructures;
using Traffic.Service.Interfaces;
namespace Traffic.Service.Implements
{
public class ReportService : IReportService
{
private readonly IUnitOfWork _unitOfWork;
public ReportService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public ReportSiteEventTypesViewModel GetReportSiteEventTypes(List<string> sites, List<string> eventTypes, string startTime, string endTime)
{
try
{
var result = new ReportSiteEventTypesViewModel();
var events = _unitOfWork.EventRepository.GetEventBySitesAndEventType(sites, eventTypes, startTime, endTime);
var siteGroup = events.GroupBy(i => i.SiteId);
var eachSiteList = new List<EachSiteEventTypesViewModel>();
foreach (var s in sites)
{
var data = siteGroup.Where(i => i.Key == s);
var siteName = _unitOfWork.SiteInformationRepository.GetSiteInformationBySiteId(s).SiteName;
if (data.Any())
{
var vm = new EachSiteEventTypesViewModel
{
SiteId = s,
SiteName = siteName,
NoStatusCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Punished) == 0 && Convert.ToInt16(i.CheckPunish) == 0 && Convert.ToInt16(i.Rejected) == 0 && string.IsNullOrWhiteSpace(i.Ps)),
CheckPunishCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.CheckPunish) == 1),
RejectedCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Rejected) == 1),
PunishCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Punished) == 1),
NoPunishCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Punished) == 0 && Convert.ToInt16(i.CheckPunish) == 0 && Convert.ToInt16(i.Rejected) == 0 && !string.IsNullOrWhiteSpace(i.Ps)),
EventTotalCount = data.FirstOrDefault().Count()
};
eachSiteList.Add(vm);
}
else
{
var vm = new EachSiteEventTypesViewModel
{
SiteId = s,
SiteName = siteName,
NoStatusCount = 0,
CheckPunishCount = 0,
RejectedCount = 0,
PunishCount = 0,
NoPunishCount = 0,
EventTotalCount = 0,
};
eachSiteList.Add(vm);
}
}
var eventGroup = events.GroupBy(i => i.EventType);
var eachEventList = new List<EachEventTypesSiteViewModel>();
foreach (var e in eventTypes)
{
var data = eventGroup.Where(i => i.Key == e);
var eventName = _unitOfWork.EventTypeRepository.GetEventTypeByEventType(e).EventName;
if (data.Any())
{
var vm = new EachEventTypesSiteViewModel
{
EventName = eventName,
NoStatusCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Punished) == 0 && Convert.ToInt16(i.CheckPunish) == 0 && Convert.ToInt16(i.Rejected) == 0 && string.IsNullOrWhiteSpace(i.Ps)),
CheckPunishCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.CheckPunish) == 1),
RejectedCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Rejected) == 1),
PunishCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Punished) == 1),
NoPunishCount = data.FirstOrDefault().Count(i => Convert.ToInt16(i.Punished) == 0 && Convert.ToInt16(i.CheckPunish) == 0 && Convert.ToInt16(i.Rejected) == 0 && !string.IsNullOrWhiteSpace(i.Ps)),
EventTotalCount = data.FirstOrDefault().Count()
};
eachEventList.Add(vm);
}
else
{
var vm = new EachEventTypesSiteViewModel
{
EventName = eventName,
NoStatusCount = 0,
CheckPunishCount = 0,
RejectedCount = 0,
PunishCount = 0,
NoPunishCount = 0,
EventTotalCount = 0,
};
eachEventList.Add(vm);
}
}
result.EachSiteEventTypesViewModel = eachSiteList.OrderBy(i => i.SiteId).ToList();
result.EachEventTypesSiteViewModel = eachEventList;
return result;
}
catch (Exception)
{
return new ReportSiteEventTypesViewModel();
}
}
public IEnumerable<TycgTableNameViewModel> GetTycgTableName()
{
try
{
var datas = _unitOfWork.TycgRepository.GetTycgsTableName();
var result = new List<TycgTableNameViewModel>();
foreach (var data in datas)
{
var vm = new TycgTableNameViewModel();
vm.InjectFrom(data);
vm.Id = data.Id;
if (string.IsNullOrWhiteSpace(data.APIName))
{
vm.APIName = data.LogName;
}
result.Add(vm);
}
return result;
}
catch (Exception ex)
{
return new List<TycgTableNameViewModel>();
}
}
public IEnumerable<TycgViewModel> GetTycgLogAll(string tycgTableName)
{
try
{
if (string.IsNullOrWhiteSpace(tycgTableName))
{
return new List<TycgViewModel>();
}
var datas = _unitOfWork.TycgRepository.GetTycgs(tycgTableName);
var result = new List<TycgViewModel>();
foreach (var data in datas)
{
var value = data.Value.Split(';');
var vm = new TycgViewModel();
vm.InjectFrom(data);
vm.Id = data.Id;
vm.Time = JavaTimeStampToDateTime(data.Timestamp);
vm.ip = value.Length > 2 ? value[0] : "";
vm.api = value.Length > 2 ? value[2] : "";
result.Add(vm);
}
return result.OrderByDescending(i => i.Time);
}
catch (Exception ex)
{
return new List<TycgViewModel>();
}
}
public PageViewModel<IEnumerable<TycgViewModel>> GetTycgLog(string tycgTableName, SearchModel searchModel)
{
try
{
if (string.IsNullOrWhiteSpace(tycgTableName))
{
return new PageViewModel<IEnumerable<TycgViewModel>>();
}
var datas = _unitOfWork.TycgRepository.GetTycgs(tycgTableName);
int count = datas.Count();
int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize;
datas = datas.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
var result = new List<TycgViewModel>();
foreach (var data in datas)
{
var value = data.Value.Split(';');
var vm = new TycgViewModel();
vm.InjectFrom(data);
vm.Id = data.Id;
vm.Time = JavaTimeStampToDateTime(data.Timestamp);
vm.ip = value.Length > 2 ? value[0] : "";
vm.api = value.Length > 2 ? value[2] : "";
result.Add(vm);
}
return new PageViewModel<IEnumerable<TycgViewModel>>
{
RowCount = count,
PageCount = pageCount,
CurrentPage = searchModel.Page,
Data = result,
};
}
catch (Exception ex)
{
return new PageViewModel<IEnumerable<TycgViewModel>>();
}
}
private static string JavaTimeStampToDateTime(double javaTimeStamp)
{
// Java timestamp is milliseconds past epoch
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
var strdateTime = dateTime.AddMilliseconds(javaTimeStamp).ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
return strdateTime;
}
public List<ReportEventType1ViewModel> GetEventType1(ReportRequestViewModel viewModel)
{
try
{
var start = Convert.ToDateTime(viewModel.StartDate).ToString("yyyy-MM-dd 00:00:00");
var end = Convert.ToDateTime(viewModel.EndDate).ToString("yyyy-MM-dd 23:59:59");
var data = _unitOfWork.EventRepository.GetEventBySitesAndEventType(viewModel.Sites, new List<string> { "1" }, start, end);
data = data.Where(i => Convert.ToDateTime(i.InTime).Hour >= viewModel.StartTime && Convert.ToDateTime(i.InTime).Hour <= viewModel.EndTime);
var result = new List<ReportEventType1ViewModel>();
var vmdata = data.Where(i => (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).Minutes > 1 && (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).Minutes <= 2);
var vm1 = new ReportEventType1ViewModel
{
DateTime = "一至兩分鐘",
CarCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Car).ToString()),
BusCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Coach).ToString()),
TaxiCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Taxi).ToString()),
MotorcycleCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Motorcycle).ToString()),
HasNumber = vmdata.Count(i => !string.IsNullOrWhiteSpace(i.Number)),
HasNoNumber = vmdata.Count(i => string.IsNullOrWhiteSpace(i.Number))
};
result.Add(vm1);
vmdata = data.Where(i => (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).Minutes > 2 && (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).Minutes <= 3);
var vm2 = new ReportEventType1ViewModel
{
DateTime = "兩至三分鐘",
CarCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Car).ToString()),
BusCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Coach).ToString()),
TaxiCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Taxi).ToString()),
MotorcycleCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Motorcycle).ToString()),
HasNumber = vmdata.Count(i => !string.IsNullOrWhiteSpace(i.Number)),
HasNoNumber = vmdata.Count(i => string.IsNullOrWhiteSpace(i.Number))
};
result.Add(vm2);
vmdata = data.Where(i => (Convert.ToDateTime(i.OutTime) - Convert.ToDateTime(i.InTime)).Minutes > 3);
var vm3 = new ReportEventType1ViewModel
{
DateTime = "兩至三分鐘",
CarCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Car).ToString()),
BusCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Coach).ToString()),
TaxiCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Taxi).ToString()),
MotorcycleCount = vmdata.Count(i => i.VehicleType == ((int)EnumVehicleType.Motorcycle).ToString()),
HasNumber = vmdata.Count(i => !string.IsNullOrWhiteSpace(i.Number)),
HasNoNumber = vmdata.Count(i => string.IsNullOrWhiteSpace(i.Number))
};
result.Add(vm3);
var vm = new ReportEventType1ViewModel
{
DateTime = "總數",
CarCount = vm1.CarCount + vm2.CarCount + vm3.CarCount,
BusCount = vm1.BusCount + vm2.BusCount + vm3.BusCount,
TaxiCount = vm1.TaxiCount + vm2.TaxiCount + vm3.TaxiCount,
MotorcycleCount = vm1.MotorcycleCount + vm2.MotorcycleCount + vm3.MotorcycleCount,
HasNumber = vm1.HasNumber + vm2.HasNumber + vm3.HasNumber,
HasNoNumber = vm1.HasNoNumber + vm2.HasNoNumber + vm3.HasNoNumber,
};
result.Add(vm);
return result;
}
catch (Exception ex)
{
return new List<ReportEventType1ViewModel>();
}
}
public List<ReportEventType2ViewModel> GetEventType2(ReportRequestViewModel viewModel)
{
try
{
var start = Convert.ToDateTime(viewModel.StartDate).ToString("yyyy-MM-dd 00:00:00");
var end = Convert.ToDateTime(viewModel.EndDate).ToString("yyyy-MM-dd 23:59:59");
var data = _unitOfWork.EventRepository.GetEventBySitesAndEventType(viewModel.Sites, new List<string> { "2" }, start, end);
data = data.Where(i => Convert.ToDateTime(i.InTime).Hour >= viewModel.StartTime && Convert.ToDateTime(i.InTime).Hour <= viewModel.EndTime);
var result = new List<ReportEventType2ViewModel>();
var vm = new ReportEventType2ViewModel
{
Speed = "~80 km/h",
Count = data.Count(i => Convert.ToDouble(i.Speed) <= 80)
};
result.Add(vm);
vm = new ReportEventType2ViewModel
{
Speed = "80~90 km/h",
Count = data.Count(i => Convert.ToDouble(i.Speed) > 80 && Convert.ToDouble(i.Speed) <= 90)
};
result.Add(vm);
vm = new ReportEventType2ViewModel
{
Speed = "90~100 km/h",
Count = data.Count(i => Convert.ToDouble(i.Speed) > 90 && Convert.ToDouble(i.Speed) <= 100)
};
result.Add(vm);
vm = new ReportEventType2ViewModel
{
Speed = "100~ km/h",
Count = data.Count(i => Convert.ToDouble(i.Speed) > 100)
};
result.Add(vm);
return result;
}
catch (Exception)
{
return new List<ReportEventType2ViewModel>();
}
}
public List<ReportEventType34ViewModel> GetEventType34(ReportRequestViewModel viewModel)
{
try
{
var start = Convert.ToDateTime(viewModel.StartDate).ToString("yyyy-MM-dd 00:00:00");
var end = Convert.ToDateTime(viewModel.EndDate).ToString("yyyy-MM-dd 23:59:59");
var data = _unitOfWork.EventRepository.GetEventBySitesAndEventType(viewModel.Sites, new List<string> { "3", "4" }, start, end);
data = data.Where(i => Convert.ToDateTime(i.InTime).Hour >= viewModel.StartTime && Convert.ToDateTime(i.InTime).Hour <= viewModel.EndTime);
var result = new List<ReportEventType34ViewModel>();
var vm = new ReportEventType34ViewModel
{
CarCount = data.Count(i => i.VehicleType == ((int)EnumVehicleType.Car).ToString()),
CoachCount = data.Count(i => i.VehicleType == ((int)EnumVehicleType.Coach).ToString()),
TruckCount = data.Count(i => i.VehicleType == ((int)EnumVehicleType.Truck).ToString()),
};
result.Add(vm);
return result;
}
catch (Exception ex)
{
return new List<ReportEventType34ViewModel>();
}
}
public List<ReportEventType6ViewModel> GetEventType6(ReportRequestViewModel viewModel)
{
try
{
var start = Convert.ToDateTime(viewModel.StartDate).ToString("yyyy-MM-dd 00:00:00");
var end = Convert.ToDateTime(viewModel.EndDate).ToString("yyyy-MM-dd 23:59:59");
var data = _unitOfWork.EventRepository.GetEventBySitesAndEventType(viewModel.Sites, new List<string> { "6" }, start, end);
data = data.Where(i => Convert.ToDateTime(i.InTime).Hour >= viewModel.StartTime && Convert.ToDateTime(i.InTime).Hour <= viewModel.EndTime);
var result = new List<ReportEventType6ViewModel>();
var vm = new ReportEventType6ViewModel
{
Ton = "0~15噸",
Count = data.Count(i => Convert.ToDouble(i.Ton) <= 15)
};
result.Add(vm);
vm = new ReportEventType6ViewModel
{
Ton = "15~20噸",
Count = data.Count(i => Convert.ToDouble(i.Ton) > 15 && Convert.ToDouble(i.Ton) <= 20)
};
result.Add(vm);
vm = new ReportEventType6ViewModel
{
Ton = "20~25噸",
Count = data.Count(i => Convert.ToDouble(i.Ton) > 20 && Convert.ToDouble(i.Ton) <= 25)
};
result.Add(vm);
vm = new ReportEventType6ViewModel
{
Ton = "25~30噸",
Count = data.Count(i => Convert.ToDouble(i.Ton) > 25 && Convert.ToDouble(i.Ton) <= 30)
};
result.Add(vm);
vm = new ReportEventType6ViewModel
{
Ton = "30噸以上",
Count = data.Count(i => Convert.ToDouble(i.Ton) > 30)
};
result.Add(vm);
return result;
}
catch (Exception ex)
{
return new List<ReportEventType6ViewModel>();
}
}
public List<ReportMalfunctionViewModel> GetMalfunction(ReportRequestViewModel viewModel)
{
try
{
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區
var start = ((DateTimeOffset)Convert.ToDateTime(viewModel.StartDate)).ToUnixTimeSeconds().ToString();
var end = ((DateTimeOffset)Convert.ToDateTime(viewModel.EndDate).AddDays(1)).ToUnixTimeSeconds().ToString();
var data = _unitOfWork.MalfunctionRepository.GetMalfunctions(viewModel.Sites, start, end);
var result = new List<ReportMalfunctionViewModel>();
foreach (var item in data)
{
DateTime dt = startTime.AddSeconds(Convert.ToInt32(item.Timestamp));
var vm = new ReportMalfunctionViewModel
{
Time = dt.ToString("yyyy-MM-dd HH:mm:ss"),
SiteId = item.Siteid,
SiteName = item.SiteName,
CompanyCode = item.CompanyCode,
DeviceID = item.DeviceID,
DeviceType = item.DeviceType,
Message = item.Message,
};
result.Add(vm);
}
result = result.Where(i => Convert.ToDateTime(i.Time).Hour >= viewModel.StartTime && Convert.ToDateTime(i.Time).Hour < viewModel.EndTime + 1).ToList();
result = result.OrderBy(i => i.SiteId).ToList();
return result;
}
catch (Exception ex)
{
return new List<ReportMalfunctionViewModel>();
}
}
}
}