tycg_carviolation_BE/Traffic.Service/Implements/RepairService.cs

237 lines
8.3 KiB
C#
Raw Normal View History

using Mail.ViewModels;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
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 RepairService : IRepairService
{
private readonly IConfiguration _configuration;
private readonly IUnitOfWork _unitOfWork;
private readonly ILogger _logger;
private readonly IMailService _mailService;
public RepairService(IConfiguration configuration, IUnitOfWork unitOfWork, ILogger<RepairService> logger, IMailService mailService)
{
_configuration = configuration;
_unitOfWork = unitOfWork;
_logger = logger;
_mailService = mailService;
}
public IEnumerable<RepairViewModel> GetRepairs()
{
try
{
_logger.LogInformation($"GetRepairs Init");
var datas = _unitOfWork.RepairRepository.GetRepairs();
var result = new List<RepairViewModel>();
foreach (var item in datas)
{
var vm = new RepairViewModel();
vm.InjectFrom(item);
result.Add(vm);
}
return result;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return null;
}
finally
{
_logger.LogInformation($"GetRepairs Finish");
}
}
public PageViewModel<IEnumerable<RepairViewModel>> GetRepairs(SearchModel searchModel)
{
try
{
_logger.LogInformation($"GetRepairs Init");
var data = _unitOfWork.RepairRepository.GetRepairs();
if (!string.IsNullOrEmpty(searchModel.Term))
{
_logger.LogInformation($"GetRepairs searchModel.Term ={searchModel.Term}");
data = data.Where(x => x.Title.Contains(searchModel.Term));
}
int count = data.Count();
int pageCount = count % searchModel.PageSize != 0 ? (count / searchModel.PageSize) + 1 : count / searchModel.PageSize;
switch (searchModel.Order.ToLower())
{
case "id":
default:
data = searchModel.IsAsc ? data.OrderBy(x => x.Id) : data.OrderByDescending(x => x.Id);
break;
case "sendto":
data = searchModel.IsAsc ? data.OrderBy(x => x.SendTo).ThenBy(x => x.Id) : data.OrderByDescending(x => x.SendTo).ThenByDescending(x => x.Id);
break;
case "title":
data = searchModel.IsAsc ? data.OrderBy(x => x.Title).ThenBy(x => x.Id) : data.OrderByDescending(x => x.Title).ThenByDescending(x => x.Id);
break;
case "describe":
data = searchModel.IsAsc ? data.OrderBy(x => x.Content).ThenBy(x => x.Id) : data.OrderByDescending(x => x.Content).ThenByDescending(x => x.Id);
break;
}
var eventTypes = _unitOfWork.EventTypeRepository.GetEventTypes();
data = data.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
var result = new List<RepairViewModel>();
foreach (var item in data)
{
var vm = new RepairViewModel();
vm.InjectFrom(item);
result.Add(vm);
}
return new PageViewModel<IEnumerable<RepairViewModel>>
{
RowCount = count,
PageCount = pageCount,
CurrentPage = searchModel.Page,
Data = result,
};
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return null;
}
finally
{
_logger.LogInformation($"GetRepairs Finish");
}
}
public RepairViewModel GetRepairById(int id)
{
try
{
_logger.LogDebug($"GetRepairById Init");
_logger.LogDebug($"GetRepairById id = {id}");
var data = _unitOfWork.RepairRepository.GetRepairById(id);
if (data == null)
{
_logger.LogWarning($"GetRepairById id = {id} is Null");
return null;
}
var result = new RepairViewModel();
result.InjectFrom(data);
return result;
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
return null;
}
finally
{
_logger.LogDebug($"GetRepairById Finish");
}
}
public TradeResultModel InsertRepair(RepairViewModel viewModel)
{
try
{
var data = new Repair();
data.InjectFrom(viewModel);
if (viewModel.QuestionType == 0)
{
data.SendTo = _configuration["Repair:Email"];
}
var result = _unitOfWork.RepairRepository.InsertRepair(data);
_unitOfWork.Complete();
if (_configuration["MailServer:IsTest"] != "true")
{
var mail = new MailViewModel
{
From = "tfc017@mail.tycg.gov.tw",
To = data.SendTo.Split(','),
Subject = data.Title,
Body = data.Content,
};
_mailService.SendMail(mail);
}
return new TradeResultModel { Success = result, Message = result ? "新增成功" : "新增失敗" };
}
catch (Exception ex)
{
_unitOfWork.Rollback();
_logger.LogError(ex.Message);
return new TradeResultModel() { Success = false, Message = ex.ToString() };
}
}
public TradeResultModel UpdatRepair(int id, RepairViewModel viewModel)
{
try
{
var data = new Repair();
data.InjectFrom(viewModel);
data.Id = id;
var result = _unitOfWork.RepairRepository.UpdateRepair(data);
_unitOfWork.Complete();
return new TradeResultModel { Success = result, Message = result ? "修改成功" : "修改失敗" };
}
catch (Exception ex)
{
_unitOfWork.Rollback();
_logger.LogError(ex.Message);
return new TradeResultModel() { Success = false, Message = ex.ToString() };
}
}
public TradeResultModel DeleteRepair(int id)
{
try
{
_logger.LogDebug($"DeleteRepair Init");
var data = _unitOfWork.RepairRepository.GetRepairById(id);
if (data == null)
{
_logger.LogWarning($"UpdatRepair id = {id} 無此不開單原因");
return new TradeResultModel { Success = false, Message = "查無此不開單原因" };
}
var result = _unitOfWork.RepairRepository.DeleteRepair(id);
_unitOfWork.Complete();
_logger.LogDebug($"DeleteRepair result is {result}");
_logger.LogDebug($"DeleteRepair Finish");
return new TradeResultModel { Success = result, Message = result ? "刪除成功" : "刪除失敗" };
}
catch (Exception ex)
{
_unitOfWork.Rollback();
_logger.LogError(ex.Message);
return new TradeResultModel() { Success = false, Message = ex.ToString() };
}
}
}
}