204 lines
7.4 KiB
C#
204 lines
7.4 KiB
C#
|
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 CompanyInformationService : ICompanyInformationService
|
|||
|
{
|
|||
|
private readonly IUnitOfWork _unitOfWork;
|
|||
|
private readonly ILogger _logger;
|
|||
|
|
|||
|
public CompanyInformationService(IUnitOfWork unitOfWork, ILogger<CompanyInformationService> logger)
|
|||
|
{
|
|||
|
_unitOfWork = unitOfWork;
|
|||
|
_logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerable<CompanyInformationViewModel> GetCompanyInformations()
|
|||
|
{
|
|||
|
var datas = _unitOfWork.CompanyInformationRepository.GetCompanyInformations();
|
|||
|
var result = new List<CompanyInformationViewModel>();
|
|||
|
foreach (var data in datas)
|
|||
|
{
|
|||
|
var vm = new CompanyInformationViewModel();
|
|||
|
vm.InjectFrom(data);
|
|||
|
vm.Id = data.Id;
|
|||
|
result.Add(vm);
|
|||
|
}
|
|||
|
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public IEnumerable<CompanyInformationViewModel> GetCompanyInformationsEmailNotNull()
|
|||
|
{
|
|||
|
var datas = _unitOfWork.CompanyInformationRepository.GetCompanyInformations();
|
|||
|
var result = new List<CompanyInformationViewModel>();
|
|||
|
foreach (var data in datas)
|
|||
|
{
|
|||
|
var vm = new CompanyInformationViewModel();
|
|||
|
vm.InjectFrom(data);
|
|||
|
vm.Id = data.Id;
|
|||
|
result.Add(vm);
|
|||
|
}
|
|||
|
|
|||
|
return result.Where(i => !string.IsNullOrWhiteSpace(i.Email));
|
|||
|
}
|
|||
|
|
|||
|
public PageViewModel<IEnumerable<CompanyInformation>> GetCompanyInformations(SearchModel searchModel)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
var company = _unitOfWork.CompanyInformationRepository.GetCompanyInformations();
|
|||
|
|
|||
|
if (!string.IsNullOrEmpty(searchModel.Term))
|
|||
|
{
|
|||
|
company = company.Where(x => x.CompanyCode.Contains(searchModel.Term) || x.CompanyName.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 "companycode":
|
|||
|
company = searchModel.IsAsc ? company.OrderBy(x => x.CompanyCode).ThenBy(x => x.Id) : company.OrderByDescending(x => x.CompanyCode).ThenByDescending(x => x.Id);
|
|||
|
break;
|
|||
|
case "companyname":
|
|||
|
company = searchModel.IsAsc ? company.OrderBy(x => x.CompanyName).ThenBy(x => x.Id) : company.OrderByDescending(x => x.CompanyName).ThenByDescending(x => x.Id);
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
company = company.Skip((searchModel.Page - 1) * searchModel.PageSize).Take(searchModel.PageSize);
|
|||
|
var data = company.ToList().Select(x => new CompanyInformation
|
|||
|
{
|
|||
|
Id = x.Id,
|
|||
|
CompanyCode = x.CompanyCode,
|
|||
|
CompanyName = x.CompanyName,
|
|||
|
Email = x.Email
|
|||
|
});
|
|||
|
|
|||
|
return new PageViewModel<IEnumerable<CompanyInformation>>
|
|||
|
{
|
|||
|
RowCount = count,
|
|||
|
PageCount = pageCount,
|
|||
|
CurrentPage = searchModel.Page,
|
|||
|
Data = data,
|
|||
|
};
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex.Message);
|
|||
|
return new PageViewModel<IEnumerable<CompanyInformation>>();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public CompanyInformationViewModel GetCompanyInformationById(int id)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var company = _unitOfWork.CompanyInformationRepository.GetCompanyInformationById(id);
|
|||
|
if (company != null)
|
|||
|
{
|
|||
|
return new CompanyInformationViewModel
|
|||
|
{
|
|||
|
Id = company.Id,
|
|||
|
CompanyCode = company.CompanyCode,
|
|||
|
CompanyName = company.CompanyName,
|
|||
|
Email = company.Email,
|
|||
|
};
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex.Message);
|
|||
|
return null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public TradeResultModel InsertCompanyInformation(CompanyInformationViewModel CompanyInformation)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var company = new CompanyInformation
|
|||
|
{
|
|||
|
CompanyName = CompanyInformation.CompanyName,
|
|||
|
CompanyCode = CompanyInformation.CompanyCode,
|
|||
|
Email = CompanyInformation.Email,
|
|||
|
};
|
|||
|
|
|||
|
var result = _unitOfWork.CompanyInformationRepository.InsertCompanyInformation(company);
|
|||
|
_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 UpdatCompanyInformation(int id, CompanyInformationViewModel CompanyInformation)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var company = new CompanyInformation
|
|||
|
{
|
|||
|
Id = id,
|
|||
|
CompanyName = CompanyInformation.CompanyName,
|
|||
|
CompanyCode = CompanyInformation.CompanyCode,
|
|||
|
Email = CompanyInformation.Email,
|
|||
|
};
|
|||
|
|
|||
|
var result = _unitOfWork.CompanyInformationRepository.UpdateCompanyInformation(company);
|
|||
|
_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 DeleteCompanyInformation(int id)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var result = _unitOfWork.CompanyInformationRepository.DeleteCompanyInformation(id);
|
|||
|
_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() };
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|