You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

151 lines
6.1 KiB

using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Znyc.Dispatching.Core;
using Znyc.Dispatching.Core.Entitys;
using Znyc.Dispatching.Core.Extension;
using Znyc.Dispatching.Core.Helpers;
using ZNYC.Recruitment.Service.Dictionary;
namespace Znyc.Dispatching.Application.Services
{
/// <summary>
/// 施工单位
/// </summary>
[ApiDescriptionSettings(Name = "construction", Order = 259)]
public class ConstructionService : IConstructionService, IDynamicApiController, ITransient
{
private readonly ICacheService _cacheService;
private readonly IRepository<Construction> _repository;
private readonly IRepository<Project> _projectRepository;
private readonly IUserManager _userManager;
private string[] INDEX_STRINGS = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
"W", "X", "Y", "Z", "#"};
public ConstructionService(
IRepository<Construction> repository,
ICacheService cacheService,
IUserManager userManager,
IRepository<Project> projectRepository)
{
_repository = repository;
_cacheService = cacheService;
_userManager = userManager;
_projectRepository = projectRepository;
}
/// <summary>
/// InsertAsync
/// </summary>
/// <param name="constructionAddInput"></param>
/// <returns></returns>
[HttpPost]
[Route("api/v1/construction")]
public async Task<bool> InsertAsync(ConstructionAddInput constructionAddInput)
{
var result = false;
if (constructionAddInput.IsNotNull())
{
var isRepeatConstructionName = await _repository.AnyAsync(x => x.ConstructionName == constructionAddInput.ConstructionName && x.CompanyId == _userManager.CompanyId);
if (isRepeatConstructionName)
{
throw Oops.Bah($"施工单位重名,请修改");
}
var construction = constructionAddInput.Adapt<Construction>();
construction.CompanyId = _userManager.CompanyId;
long id = (await _repository.InsertNowAsync(construction)).Entity.Id;
if (id > 0)
{
result = true;
}
}
return result;
}
/// <summary>
/// UpdateAsync
/// </summary>
/// <param name="constructionUpdateInput"></param>
/// <returns></returns>
[HttpPut]
[Route("api/v1/construction")]
public async Task<bool> UpdateAsync(ConstructionUpdateInput constructionUpdateInput)
{
var result = false;
if (constructionUpdateInput.IsNotNull())
{
var construction = await _repository.FindAsync(constructionUpdateInput.Id);
if (construction.IsNotNull())
{
var isRepeatConstructionName = await _repository.AnyAsync(x => x.ConstructionName == constructionUpdateInput.ConstructionName && x.Id != constructionUpdateInput.Id && x.CompanyId == _userManager.CompanyId);
if (isRepeatConstructionName)
{
throw Oops.Bah($"施工单位重名,请修改");
}
construction.ConstructionName = constructionUpdateInput.ConstructionName;
construction.ContactName = constructionUpdateInput.ContactName;
construction.ContactPhone = constructionUpdateInput.ContactPhone;
construction.Status = constructionUpdateInput.Status;
long id = (await _repository.UpdateNowAsync(construction)).Entity.Id;
if (id > 0)
{
//同步工程列表中施工单位名称
var projects = _projectRepository.Where(x => x.ConstructionId == id).ToList();
if (projects.Count > 0)
{
foreach (var project in projects)
{
project.ConstructionName = construction.ConstructionName;
await _projectRepository.UpdateNowAsync(project);
await _cacheService.RemoveProjectAsync(project.Id);
}
}
await _cacheService.RemoveProjectListAsync(_userManager.CompanyId);
result = true;
}
}
}
return result;
}
/// <summary>
/// ListAsync
/// </summary>
/// <returns></returns>
[HttpGet]
[Route("api/v1/constructions/search")]
public async Task<ConstructionBaiscList> ListAsync(string key, int status)
{
var constructions = _repository.Where(x => x.CompanyId == _userManager.CompanyId)
.Where(key.IsNotEmptyOrNull(), x => x.ConstructionName.Contains(key))
.Where(status > 0, x => x.Status)
.ToList().Adapt<List<ConstructionOutput>>();
var constructionOutputs = new List<ConstructionLetterOutput>();
foreach (var INDEX_STRING in INDEX_STRINGS)
{
constructionOutputs.Add(new ConstructionLetterOutput()
{
Char = INDEX_STRING,
List = constructions.Where(x => StringHelper.GetStringFirstSpell(x.ConstructionName) == INDEX_STRING).ToList()
});
};
var data = new ConstructionBaiscList()
{
ConstructionLetterOutputs = constructionOutputs,
Total = constructions.Count
};
return data;
}
}
}