tycg_carviolation_BE/Traffic.Service/Implements/PoliceStationService.cs

163 lines
5.6 KiB
C#
Raw Normal View History

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 PoliceStationService : IPoliceStationService
{
private readonly IUnitOfWork _unitOfWork;
public PoliceStationService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public PageViewModel<IEnumerable<PoliceStationViewModel>> GetPoliceStations(SearchModel searchModel)
{
var company = _unitOfWork.PoliceStationRepository.GetPoliceStations();
if (!string.IsNullOrEmpty(searchModel.Term))
{
company = company.Where(x => x.Area.Contains(searchModel.Term) || x.StationName.Contains(searchModel.Term));
}
int count = company.Count();
int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize;
switch (searchModel.Order.ToLower())
{
case "id":
default:
company = searchModel.IsAsc? company.OrderBy(x => x.Id) : company.OrderByDescending(x => x.Id);
break;
case "area":
company = searchModel.IsAsc ? company.OrderBy(x => x.Area).ThenBy(x => x.Id) : company.OrderByDescending(x => x.Area).ThenByDescending(x => x.Id);
break;
case "stationname":
company = searchModel.IsAsc? company.OrderBy(x => x.StationName).ThenBy(x => x.Id) : company.OrderByDescending(x => x.StationName).ThenByDescending(x => x.Id);
break;
}
company = company.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
var data = company.ToList().Select(x => new PoliceStationViewModel
{
Id = x.Id,
Area = x.Area,
StationName = x.StationName
});
return new PageViewModel<IEnumerable<PoliceStationViewModel>>
{
RowCount = count,
PageCount = pageCount,
CurrentPage = searchModel.Page,
Data = data,
};
}
public PoliceStationViewModel GetPoliceStationById(int id)
{
try
{
var station = _unitOfWork.PoliceStationRepository.GetPoliceStationById(id);
if (station != null)
{
return new PoliceStationViewModel
{
Id = station.Id,
Area = station.Area,
StationName = station.StationName,
};
}
else
{
return null;
}
}
catch (Exception ex)
{
return null;
}
}
public IEnumerable<PoliceStationViewModel> GetGroupNameList()
{
var data = _unitOfWork.PoliceStationRepository.GetPoliceStations().Select(x => new PoliceStationViewModel
{
Id = x.Id,
Area = x.Area,
StationName = x.StationName
});
return data;
}
public TradeResultModel InsertPoliceStation(PoliceStationViewModel PoliceStation)
{
try
{
var station = new PoliceStation
{
Area = PoliceStation.Area,
StationName = PoliceStation.StationName,
};
var result = _unitOfWork.PoliceStationRepository.InsertPoliceStation(station);
_unitOfWork.Complete();
return new TradeResultModel { Success = result, Message = result ? "新增成功" : "新增失敗" };
}
catch (Exception ex)
{
_unitOfWork.Rollback();
return new TradeResultModel() { Success = false, Message = ex.ToString() };
}
}
public TradeResultModel UpdatPoliceStation(PoliceStationViewModel PoliceStation)
{
try
{
var station = new PoliceStation
{
Id = PoliceStation.Id,
Area = PoliceStation.Area,
StationName = PoliceStation.StationName,
};
var result = _unitOfWork.PoliceStationRepository.UpdatePoliceStation(station);
_unitOfWork.Complete();
return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" };
}
catch (Exception ex)
{
_unitOfWork.Rollback();
return new TradeResultModel() { Success = false, Message = ex.ToString() };
}
}
public TradeResultModel DeletePoliceStation(int id)
{
try
{
var result = _unitOfWork.PoliceStationRepository.DeletePoliceStation(id);
_unitOfWork.Complete();
return new TradeResultModel { Success = result, Message = result ? "刪除成功" : "刪除失敗" };
}
catch (Exception ex)
{
_unitOfWork.Rollback();
return new TradeResultModel() { Success = false, Message = ex.ToString() };
}
}
}
}