tycg_carviolation_BE/Traffic.Service/Implements/MalfunctionService.cs

155 lines
5.4 KiB
C#

using Omu.ValueInjecter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traffic.Data.Models;
using Traffic.Data.ViewModels;
using Traffic.Repository.Infrastructures;
using Traffic.Service.Interfaces;
namespace Traffic.Service.Implements
{
public class MalfunctionService : IMalfunctionService
{
private readonly IUnitOfWork _unitOfWork;
public MalfunctionService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IEnumerable<SiteMalfunctionViewModel> GetMalfunctionsOld(int userid)
{
var userSites = _unitOfWork.AccountRepository.GetAccountUserSite(userid);
var sites = _unitOfWork.SiteInformationRepository.GetSiteInformations();
var result = new List<SiteMalfunctionViewModel>();
var datas = _unitOfWork.MalfunctionRepository.GetMalfunctions(userid);
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區
var mList = new List<MalfunctionViewModel>();
foreach (var data in datas)
{
var vm = new MalfunctionViewModel();
vm.InjectFrom(data);
DateTime dt = startTime.AddSeconds(Convert.ToInt32(data.Timestamp));
vm.Time = dt.ToString("yyyy-MM-dd HH:mm:ss");
if (string.IsNullOrWhiteSpace(vm.Message))
{
vm.Status = "未連線";
}
else if (vm.Message.ToLower() == "error")
{
vm.Status = "異常";
}
else if (vm.Message.ToLower() == "normal")
{
vm.Status = "正常";
}
else
{
vm.Status = "未連線";
}
mList.Add(vm);
}
foreach (var item in userSites)
{
var site = sites.Where(i => i.Id == item.SiteId).FirstOrDefault();
var li = mList.Where(i => i.Siteid == site.SiteID);
var vm = new SiteMalfunctionViewModel
{
SiteName = site.SiteName,
EventType = site.EventType,
Area = site.Area,
County = site.County,
Siteid = site.SiteID,
SiteStatus = li.Any(i => i.Status == "異常") ? "1" : "0",
UserId = userid,
Malfunctions = li.ToList()
};
if (li.All(i => i.Status == "未連線"))
{
continue;
}
result.Add(vm);
}
return result;
}
public IEnumerable<SiteMalfunctionViewModel> GetMalfunctions(int userid)
{
var datas = _unitOfWork.MalfunctionRepository.GetMalfunctions();
var userSites = _unitOfWork.AccountRepository.GetAccountUserSite(userid);
var sites = _unitOfWork.SiteInformationRepository.GetSiteInformations();
var result = new List<SiteMalfunctionViewModel>();
DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 當地時區
var mList = new List<MalfunctionViewModel>();
foreach (var data in datas)
{
DateTime dt = startTime.AddSeconds(Convert.ToInt32(data.Timestamp));
var vm = new MalfunctionViewModel
{
CompanyCode = data.CompanyCode,
Siteid = data.Siteid,
Message = data.Message,
DeviceID = data.DeviceID,
DeviceType = data.DeviceType,
Time = dt.ToString("yyyy-MM-dd HH:mm:ss"),
UserId = userid,
};
if (string.IsNullOrWhiteSpace(vm.Message))
{
vm.Status = "未連線";
}
else if (vm.Message.ToLower() == "error")
{
vm.Status = "異常";
}
else if (vm.Message.ToLower() == "normal")
{
vm.Status = "正常";
}
else
{
vm.Status = "未連線";
}
mList.Add(vm);
}
foreach (var item in userSites)
{
var site = sites.Where(i => i.Id == item.SiteId).FirstOrDefault();
var li = mList.Where(i => i.Siteid == site.SiteID);
var vm = new SiteMalfunctionViewModel
{
SiteName = site.SiteName,
EventType = site.EventType,
Area = site.Area,
County = site.County,
Siteid = site.SiteID,
SiteStatus = li.Any(i => i.Status == "異常") ? "1" : "0",
UserId = userid,
Malfunctions = li.ToList()
};
if (li.All(i => i.Status == "未連線"))
{
continue;
}
result.Add(vm);
}
return result;
}
}
}