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.

128 lines
3.8 KiB

using Entity.DbModel.System.App;
using Entity.Dto.Req;
using HybirdFrameworkCore.Autofac.Attribute;
using HybirdFrameworkCore.Entity;
using Repository.Station;
using SqlSugar;
namespace Service.Station;
[Scope]
public class CarTeamService : BaseServices<CarTeam>
{
private readonly CarTeamRepository _carTeamRepository;
public CarTeamService(
CarTeamRepository carTeamRepository
)
{
_carTeamRepository = carTeamRepository;
}
/// <summary>
/// 获取车队分页
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PageResult<CarTeam>> Page(PageCarTeamReq input)
{
RefAsync<int> total = 0;
var items = await _carTeamRepository.QueryPageAsync(
entity => true,
!string.IsNullOrWhiteSpace(input.TeamCode), u => u.TeamCode.Equals(input.TeamCode.Trim()),
!string.IsNullOrWhiteSpace(input.TeamName), u => u.TeamName.Equals(input.TeamName.Trim()),
!string.IsNullOrWhiteSpace(input.Principal), u => u.Principal.Equals(input.Principal.Trim()),
u => u.CreatedTime, OrderByType.Desc, input.PageNum, input.PageSize, total);
return new PageResult<CarTeam>()
{
PageNum = input.PageNum,
PageSize = input.PageSize,
ToTal = total,
Rows = items,
};
}
/// <summary>
/// 获取所有车队
/// </summary>
/// <returns></returns>
public async Task<List<CarTeam>> GetTeamList()
{
return await _carTeamRepository.QueryAsync();
}
/// <summary>
/// 新增车队
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<string> AddCarTeamReq(AddCarTeamReq input)
{
var existingUser =
await _carTeamRepository.QueryByClauseAsync(u =>
u.TeamCode == input.TeamCode || u.TeamName == input.TeamName);
if (existingUser?.TeamCode == input.TeamCode)
{
return "车队编码已存在";
}
if (existingUser?.TeamName == input.TeamName)
{
return "车队名称已存在";
}
// 插入新用户
await _carTeamRepository.InsertAsync(input);
// 查询新ID
CarTeam carTeam = await _carTeamRepository.QueryByClauseAsync(u => u.TeamCode == input.TeamCode);
return "新增车队:" + carTeam.TeamName;
}
/// <summary>
/// 车队修改
/// </summary>
/// <param name="team"></param>
/// <returns></returns>
public virtual async Task<Result<string>> UpdateTeam(UpdateCarTeamReq team)
{
var existingTeam =
await _carTeamRepository.QueryByClauseAsync(u =>
u.TeamCode == team.TeamCode || u.TeamName == team.TeamName && u.Id != team.Id);
if (existingTeam?.TeamCode == team.TeamCode)
{
return Result<string>.Fail("修改失败,车队编码已存在");
}
if (existingTeam?.TeamName == team.TeamName)
{
return Result<string>.Fail("修改失败,车队名称已存在");
}
var updateAsync = await _carTeamRepository.UpdateAsync(team);
if (updateAsync)
{
return Result<string>.Success("修改成功");
}
return Result<string>.Fail("修改失败");
}
/// <summary>
/// 车队删除
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public virtual async Task<bool> DeleteTeam(DeleteCarTeamReq input)
{
var user = await _carTeamRepository.QueryByClauseAsync(u => u.Id == input.Id);
if (user == null)
throw new ArgumentException($"车队不存在");
return await _carTeamRepository.DeleteAsync(user);
}
}