You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Service/Fee/FeeRecordService.cs

131 lines
4.7 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.EventBus;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Service.Fee.Dto;
using Myshipping.Core;
using Myshipping.Core.Service;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Myshipping.Application
{
/// <summary>
/// 费用录入
/// </summary>
[ApiDescriptionSettings("Application", Name = "FeeRecord", Order = 1)]
public class FeeRecordService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<FeeRecord> _repRecord;
private readonly ILogger<FeeRecordService> _logger;
private readonly ISysCacheService _cache;
private readonly ISysUserRoleService _sysUserRoleService;
private readonly IEventPublisher _publisher;
public FeeRecordService(ILogger<FeeRecordService> logger,
ISysCacheService cache,
IEventPublisher publisher,
SqlSugarRepository<FeeRecord> repRecord,
ISysUserRoleService sysUserRoleService)
{
_logger = logger;
_cache = cache;
_sysUserRoleService = sysUserRoleService;
_publisher = publisher;
_repRecord = repRecord;
}
/// <summary>
/// 费用记录查询
/// </summary>
[HttpGet("/FeeRecord/Query")]
public async Task<List<FeeRecordDto>> Query([FromQuery] FeeRecordInputDto input)
{
List<FeeRecord> feeRecords = await _repRecord.AsQueryable()
.Where(r => r.BussId == input.BussId)
.OrderBy(r => new { r.FeeType, r.Sort, r.CreatedTime })
.ToListAsync();
// 如果费用中含有机密费用,再判断是否有查询的权限
if (feeRecords.Any(r => !r.IsOpen))
{
bool canReviewSecretFee = await _sysUserRoleService.IsUserInRole(UserManager.UserId, CommonConst.SECRET_FEE_REVIEW);
if (!canReviewSecretFee)
{
feeRecords.RemoveAll(r => !r.IsOpen && r.CreatedUserId != UserManager.UserId);
}
}
var result = feeRecords.Adapt<List<FeeRecordDto>>();
return result;
}
/// <summary>
/// 费用录入保存
/// </summary>
[HttpPost("/FeeRecord/Save")]
[SqlSugarUnitOfWork]
public async Task Save(FeeRecordSaveDto input)
{
input.FeeRecordList ??= new List<FeeRecordDto>();
var waitDeleteList = input.FeeRecordList.Where(r => r.MarkDelete && r.Id != 0).ToList();
var waitInsertList = input.FeeRecordList.Where(r => !r.MarkDelete && r.Id == 0).ToList();
var waitUpdateList = input.FeeRecordList.Where(r => !r.MarkDelete && r.MarkModify && r.Id != 0).ToList();
#region 校验
foreach (var item in waitInsertList.Concat(waitUpdateList))
{
// 人民币汇率必须为1其它外币汇率不能为空
if (item.ExchangeRate == 0) throw Oops.Bah("汇率不能为空或0请调整");
if (string.IsNullOrEmpty(item.Currency)) throw Oops.Bah("币别不能为空");
if (item.Currency?.ToUpper() == "RMB")
{
if (item.ExchangeRate != 1) throw Oops.Bah("币别为人民币时汇率需为1请调整");
}
}
#endregion
// 删除
if (waitDeleteList.Any())
{
var waitDeleteListTemp = waitDeleteList.Select(r => r.Id).ToList();
await _repRecord.UpdateAsync(r => waitDeleteListTemp.Contains(r.Id), r => new FeeRecord() { IsDeleted = true });
}
// 新增
if (waitInsertList.Any())
{
var waitInsertListTemp = waitInsertList.Adapt<List<FeeRecord>>();
await _repRecord.InsertAsync(waitInsertListTemp);
}
// 更新
if (waitUpdateList.Any())
{
await _repRecord.AsUpdateable(waitUpdateList).IgnoreColumns(r => new
{
r.Reason,
r.TenantId,
r.TenantName
}).ExecuteCommandAsync();
}
}
}
}