246 lines
10 KiB
C#
246 lines
10 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 SiteInformationService : ISiteInformationService
|
|
{
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public SiteInformationService(IUnitOfWork unitOfWork)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public IEnumerable<SiteInformationViewModel> GetSiteInformations()
|
|
{
|
|
var datas = _unitOfWork.SiteInformationRepository.GetSiteInformations();
|
|
var result = new List<SiteInformationViewModel>();
|
|
var eventTypes = _unitOfWork.EventTypeRepository.GetEventTypes();
|
|
var companys = _unitOfWork.CompanyInformationRepository.GetCompanyInformations();
|
|
foreach (var data in datas)
|
|
{
|
|
var eventType = eventTypes.Where(i => i.EventType == data.EventType);
|
|
var vm = new SiteInformationViewModel();
|
|
vm.InjectFrom(data);
|
|
vm.Id = data.Id;
|
|
vm.EventName = eventType.Any() ? eventType.FirstOrDefault().EventName : string.Empty;
|
|
var company = companys.Where(o => o.CompanyCode == vm.CompanyCode);
|
|
if (company.Any())
|
|
{
|
|
vm.CompanyName = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).CompanyName;
|
|
vm.CompanyEmail = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).Email;
|
|
}
|
|
result.Add(vm);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public IEnumerable<SiteInformationViewModel> GetSiteInformationsByUser(int userId)
|
|
{
|
|
var sites = GetSiteInformations();
|
|
var userSites = _unitOfWork.AccountRepository.GetAccountUserSite(userId);
|
|
|
|
var result = new List<SiteInformationViewModel>();
|
|
foreach (var item in userSites)
|
|
{
|
|
var site = sites.Where(i => i.Id == item.SiteId);
|
|
if (site.Any())
|
|
{
|
|
result.Add(site.FirstOrDefault());
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public IEnumerable<SiteInformationViewModel> GetSiteInformationsByEventTypes(List<string> eventTypes)
|
|
{
|
|
var datas = _unitOfWork.SiteInformationRepository.GetSiteInformations(eventTypes);
|
|
var result = new List<SiteInformationViewModel>();
|
|
var eventTypeList = _unitOfWork.EventTypeRepository.GetEventTypes();
|
|
var companys = _unitOfWork.CompanyInformationRepository.GetCompanyInformations();
|
|
foreach (var data in datas)
|
|
{
|
|
var eventType = eventTypeList.Where(i => i.EventType == data.EventType);
|
|
var vm = new SiteInformationViewModel();
|
|
vm.InjectFrom(data);
|
|
vm.Id = data.Id;
|
|
vm.EventName = eventType.Any() ? eventType.FirstOrDefault().EventName : string.Empty;
|
|
var company = companys.Where(o => o.CompanyCode == vm.CompanyCode);
|
|
if (company.Any())
|
|
{
|
|
vm.CompanyName = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).CompanyName;
|
|
vm.CompanyEmail = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).Email;
|
|
}
|
|
result.Add(vm);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public PageViewModel<IEnumerable<SiteInformationViewModel>> GetSiteInformations(SearchModel searchModel)
|
|
{
|
|
var datas = _unitOfWork.SiteInformationRepository.GetSiteInformations();
|
|
|
|
if (!string.IsNullOrEmpty(searchModel.Term))
|
|
{
|
|
datas = datas.Where(x => x.County.Contains(searchModel.Term) || x.SiteID.Contains(searchModel.Term));
|
|
}
|
|
|
|
int count = datas.Count();
|
|
int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize;
|
|
|
|
switch (searchModel.Order.ToLower())
|
|
{
|
|
case "id":
|
|
default:
|
|
datas = searchModel.IsAsc ? datas.OrderBy(x => x.Id) : datas.OrderByDescending(x => x.Id);
|
|
break;
|
|
case "county":
|
|
datas = searchModel.IsAsc ? datas.OrderBy(x => x.County).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.County).ThenByDescending(x => x.Id);
|
|
break;
|
|
case "area":
|
|
datas = searchModel.IsAsc ? datas.OrderBy(x => x.Area).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.Area).ThenByDescending(x => x.Id);
|
|
break;
|
|
case "siteid":
|
|
datas = searchModel.IsAsc ? datas.OrderBy(x => x.SiteID).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.SiteID).ThenByDescending(x => x.Id);
|
|
break;
|
|
case "sitename":
|
|
datas = searchModel.IsAsc ? datas.OrderBy(x => x.SiteName).ThenBy(x => x.Id) : datas.OrderByDescending(x => x.SiteName).ThenByDescending(x => x.Id);
|
|
break;
|
|
}
|
|
|
|
var eventTypes = _unitOfWork.EventTypeRepository.GetEventTypes();
|
|
|
|
datas = datas.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
|
|
var result = new List<SiteInformationViewModel>();
|
|
var companys = _unitOfWork.CompanyInformationRepository.GetCompanyInformations();
|
|
foreach (var data in datas)
|
|
{
|
|
var eventType = eventTypes.Where(i => i.EventType == data.EventType);
|
|
var vm = new SiteInformationViewModel();
|
|
vm.InjectFrom(data);
|
|
vm.Id = data.Id;
|
|
vm.EventName = eventType.Any() ? eventType.FirstOrDefault().EventName : string.Empty;
|
|
var company = companys.Where(o => o.CompanyCode == vm.CompanyCode);
|
|
if (company.Any())
|
|
{
|
|
vm.CompanyName = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).CompanyName;
|
|
vm.CompanyEmail = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).Email;
|
|
}
|
|
result.Add(vm);
|
|
}
|
|
|
|
return new PageViewModel<IEnumerable<SiteInformationViewModel>>
|
|
{
|
|
RowCount = count,
|
|
PageCount = pageCount,
|
|
CurrentPage = searchModel.Page,
|
|
Data = result,
|
|
};
|
|
}
|
|
|
|
public SiteInformationViewModel GetSiteInformationById(int id)
|
|
{
|
|
try
|
|
{
|
|
var data = _unitOfWork.SiteInformationRepository.GetSiteInformationById(id);
|
|
if (data != null)
|
|
{
|
|
var eventType = _unitOfWork.EventTypeRepository.GetEventTypeByEventType(data.EventType);
|
|
var result = new SiteInformationViewModel();
|
|
result.InjectFrom(data);
|
|
result.Id = data.Id;
|
|
result.EventName = eventType != null ? eventType.EventName : string.Empty;
|
|
var companys = _unitOfWork.CompanyInformationRepository.GetCompanyInformations();
|
|
var company = companys.Where(o => o.CompanyCode == data.CompanyCode);
|
|
if (company.Any())
|
|
{
|
|
result.CompanyName = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).CompanyName;
|
|
result.CompanyEmail = company.FirstOrDefault(i => i.CompanyCode == data.CompanyCode).Email;
|
|
}
|
|
return result;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public TradeResultModel InsertSiteInformation(SiteInformationViewModel viewModel)
|
|
{
|
|
try
|
|
{
|
|
var eventType = _unitOfWork.EventTypeRepository.GetEventTypeByEventType(viewModel.EventType);
|
|
if (eventType == null)
|
|
{
|
|
return new TradeResultModel { Success = false, Message = "查無此事件類型" };
|
|
}
|
|
|
|
var data = new SiteInformation();
|
|
data.InjectFrom(viewModel);
|
|
|
|
var result = _unitOfWork.SiteInformationRepository.InsertSiteInformation(data);
|
|
_unitOfWork.Complete();
|
|
|
|
return new TradeResultModel { Success = result, Message = result ? "新增成功" : "新增失敗" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_unitOfWork.Rollback();
|
|
return new TradeResultModel() { Success = false, Message = ex.ToString() };
|
|
}
|
|
}
|
|
|
|
public TradeResultModel UpdatSiteInformation(int id, SiteInformationViewModel viewModel)
|
|
{
|
|
try
|
|
{
|
|
var data = new SiteInformation();
|
|
data.InjectFrom(viewModel);
|
|
data.Id = id;
|
|
var result = _unitOfWork.SiteInformationRepository.UpdateSiteInformation(data);
|
|
_unitOfWork.Complete();
|
|
|
|
return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_unitOfWork.Rollback();
|
|
return new TradeResultModel() { Success = false, Message = ex.ToString() };
|
|
}
|
|
}
|
|
|
|
public TradeResultModel DeleteSiteInformation(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = _unitOfWork.SiteInformationRepository.DeleteSiteInformation(id);
|
|
_unitOfWork.Complete();
|
|
|
|
return new TradeResultModel { Success = result, Message = result ? "刪除成功" : "刪除失敗" };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_unitOfWork.Rollback();
|
|
return new TradeResultModel() { Success = false, Message = ex.ToString() };
|
|
}
|
|
}
|
|
}
|
|
}
|